View previous topic :: View next topic |
Author |
Message |
camih03
Joined: 17 Nov 2018 Posts: 2
|
UART and pic18F4550 |
Posted: Sat Nov 17, 2018 6:26 pm |
|
|
Hi everyone. I've been working on this project for a while but i can't figure out how to make it work.
(Edit): I'm using PIC18F4550, PIC 5.015, Hercules (to see the COM), PL2303 (TTL to USB module).
Need to do an ADC 0-5V and display it on a LCD and show the same value through uart, using Proteus and Hercules/hyper terminal everything works fine but when i try to do it with pl2303 and my breadboard the data read through the rs232 isn't the same just display random symbols in the terminal.
This is the simulation with Proteus, as you can see, the value in the lcd is the same in the COM
[img]https://imgur.com/a/iISww4t[/img]
but when i check the physical COM this is what i see:
[img]https://imgur.com/nnQmrfT[/img]
And this is the code that I'm using:
Code: | //LCD module connections
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections
#include <18F4550.h>
#device ADC=10
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
#include <lcd.c>
#include <string.h>
#include <stdlib.h>
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_c7,parity=N,bits=8,stop=1)
unsigned int16 i;
void main(){
setup_oscillator(OSC_8MHZ);
lcd_init(); // Initialize LCD module
setup_adc(ADC_CLOCK_INTERNAL); // Set ADC conversion time to 8Tosc
setup_adc_ports(AN0); // Configure RA0 (AN0) as analog
set_adc_channel(0); // Select channel 0 input
delay_ms(100); // Wait 100ms
lcd_gotoxy(3, 1); // Go to column 3 row 1
lcd_putc("ADC reading:");
while(TRUE){
i = read_adc();
char str[20];
sprintf(str, "%lu", i);
printf(str);
lcd_gotoxy(7, 2); // Go to column 7 row 2
printf(lcd_putc,"%Lu",i); // Write i with 4 numbers max
delay_ms(1000); // Wait 10ms
}
} |
Honestly i don't know what to do. i've been tried eveything from the forums. Hope anyone of you could help me. Thanks in advance. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Sun Nov 18, 2018 3:44 am |
|
|
and, this is why we repeatedly say the _first_ thing you must always do, when
working with a chip, it perform a test to verify that your clock is running
at the speed to think it is. The simple 'flash an LED' test (using a stopwatch),
or output a higher speed pulse and test with a scope, should be the very first
thing you do. It is vital. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Sun Nov 18, 2018 6:06 am |
|
|
also...
please have a re-read of the ADC section of the datasheet.
this...
setup_adc(ADC_CLOCK_INTERNAL); // Set ADC conversion time to 8Tosc
.. is probably not correct for that PIC. I seem to recall '..internal' is for very slow, sleepy PICs. There will be a chart of validADC configs vs speed.
and
when I was using that PIC, I printed off the 'clock diagram', then highlighted the desired clock signal 'flow'. It was a great aid in seeing what bits had to setup in which registers for te PIC to function 100%.Turns out a 4MHz xtal was perfect and I have a jar full of them from the PIC16C84 days...
Jay |
|
|
elcrcp
Joined: 11 Mar 2016 Posts: 62 Location: izmir / Turkey
|
|
Posted: Sun Nov 18, 2018 1:52 pm |
|
|
As PCM Programmer mentioned, you need to do fuse settings for oscillator, do some more fuse settings while you are at it. It is better to define known settings than leaving them default.
Secondly, probably he didn't see it but if Ttelmah were to saw it he would say, "add errors to #use rs232" _________________ There is nothing you can't do if you try |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Sun Nov 18, 2018 2:53 pm |
|
|
Yes.
I didn't actually 'worry' about it, since he is only transmitting.
It is the receive part of the UART that gets hung if data is received, and not
handled in time, so a 'transmit only' program doesn't potentially have this
issue. However always better to have ERRORS... |
|
|
camih03
Joined: 17 Nov 2018 Posts: 2
|
|
Posted: Mon Nov 19, 2018 9:55 am |
|
|
Thanks to all for your replies.
Maybe its all about frequency and the pic's clock.
But i solved the problem using this:
Code: | #fuses NOMCLR INTRC_IO, HSPLL, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, NOCPD, NOVREGEN |
Honestly i don't know how those fuses solved the problem but finally it's working. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Mon Nov 19, 2018 1:13 pm |
|
|
You set the HSPLL fuse, which means you selected HS oscillator with PLL.
Chapter 25 of the PIC18F4550 datasheet lists all the fuses that you can set. |
|
|
|