CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

UART and pic18F4550

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
camih03



Joined: 17 Nov 2018
Posts: 2

View user's profile Send private message

UART and pic18F4550
PostPosted: Sat Nov 17, 2018 6:26 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Nov 17, 2018 8:40 pm     Reply with quote

Read this thread about the 18F4550 and CPUDIV1:
http://www.ccsinfo.com/forum/viewtopic.php?t=53089
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Nov 18, 2018 3:44 am     Reply with quote

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: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Nov 18, 2018 6:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Nov 18, 2018 1:52 pm     Reply with quote

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" Very Happy
_________________
There is nothing you can't do if you try
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Nov 18, 2018 2:53 pm     Reply with quote

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... Smile
camih03



Joined: 17 Nov 2018
Posts: 2

View user's profile Send private message

PostPosted: Mon Nov 19, 2018 9:55 am     Reply with quote

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. Smile
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Mon Nov 19, 2018 1:13 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group