|
|
View previous topic :: View next topic |
Author |
Message |
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
Setting Baud rate for Pic18f4550 |
Posted: Sun Oct 19, 2014 4:56 am |
|
|
Hello guys....
Today i m having a new Problem.....
Here is my code::
Code: |
#include <18f4550.h>
#use delay(clock=16000000)
#fuses HS, PLL4, CPUDIV1, USBDIV, NOWDT, NOPROTECT, BROWNOUT, NOLVP, PUT, MCLR,PUT
#use rs232(baud=9600,xmit=PIN_C6,parity=N,rcv=PIN_C7,bits=8,ERRORS)
#include <flex_lcd216.c>
void main()
{
delay_ms(500);
lcd_init();
lcd_setcursor_vb(1,1);
printf(lcd_putc,"\f ");
lcd_gotoxy(1,1);
delay_ms(10);
printf(lcd_putc,"WELCOME");
delay_ms(10);
int Val=0x55;
for(;;)
{
delay_ms(200);
printf("%x",Val);
delay_ms(1700);
output_toggle(PIN_E1);
delay_ms(100);
}
}
|
I want to transmit 0x55 serially to my pc...i m using teal term in my pc..
But instead of getting binary 01010101, i m getting output like this::
I m using a Crystal of 16 Mhz....i also set the fuses in such a way that controller has a 16 Mhz clock...
I dont know what is my problem...plz help....
Thank You... _________________ Krazzy7434 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun Oct 19, 2014 5:17 am |
|
|
obvious question
.... do you have a 'MAX232' interface chip between the PIC and the PC if using a real RS232 comport ?
Proper 3 wire connections from MAX232 to PC.DE-2 is PC rcv, DE-3 is xmt,DE-5 is ground.
You should tell us what hardware you're using.
Need a bit more info here.
I do use Real Term and 4550s so I know is will work....
Jay |
|
|
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
|
Posted: Sun Oct 19, 2014 6:05 am |
|
|
temtronic wrote: | obvious question
.... do you have a 'MAX232' interface chip between the PIC and the PC if using a real RS232 comport ?
Proper 3 wire connections from MAX232 to PC.DE-2 is PC rcv, DE-3 is xmt,DE-5 is ground.
You should tell us what hardware you're using.
Need a bit more info here.
I do use Real Term and 4550s so I know is will work....
Jay |
Hello Temtronic..
Sorry for missing that...i dont use max232 chip....instead i use a transistor based rs232 for communication...
Thank You... _________________ Krazzy7434
Last edited by Krazzy7434 on Sun Oct 19, 2014 6:09 am; edited 1 time in total |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Oct 19, 2014 6:08 am |
|
|
Another comment, the Fuses line should always be above the #use delay so it should look like this:
Code: | #include <18f4550.h>
#fuses HS, PLL4, CPUDIV1, USBDIV, NOWDT, NOPROTECT, BROWNOUT, NOLVP, PUT, MCLR,PUT
#use delay(clock=16000000)
#use rs232(baud=9600,xmit=PIN_C6,parity=N,rcv=PIN_C7,bits=8,ERRORS) |
You are setting your use delay value before you establish your clock source and multiplier. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
|
Posted: Sun Oct 19, 2014 6:18 am |
|
|
dyeatman wrote: | Another comment, the Fuses line should always be above the #use delay so it should look like this:
Code: |
include <18f4550.h>
#fuses HS, PLL4, CPUDIV1, USBDIV, NOWDT, NOPROTECT, BROWNOUT, NOLVP, PUT, MCLR,PUT
#use delay(clock=16000000)
#use rs232(baud=9600,xmit=PIN_C6,parity=N,rcv=PIN_C7,bits=8,ERRORS) |
You are setting your use delay value before you establish your clock source and multiplier. |
That was careless mistake by me...i now corrected my code:
Code: |
#include <18f4550.h>
#fuses HS, PLL4, CPUDIV1,NOWDT, NOPROTECT, BROWNOUT, NOLVP, PUT, MCLR,PUT
#use delay(clock=16000000)
#use rs232(baud=9600,xmit=PIN_C6,parity=N,rcv=PIN_C7,bits=8,ERRORS)
#include <flex_lcd216.c>
void main()
{
delay_ms(500);
lcd_init();
lcd_setcursor_vb(1,1);
printf(lcd_putc,"\f ");
lcd_gotoxy(1,1);
delay_ms(10);
printf(lcd_putc,"WELCOME");
delay_ms(10);
int Val=0x55;
for(;;)
{
delay_ms(200);
printf("%x",Val);
delay_ms(1700);
output_toggle(PIN_E1);
delay_ms(100);
}
}
|
but still no change output...also i have random delay between each byte reception ...some times long...sometimes short.... _________________ Krazzy7434 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sun Oct 19, 2014 6:23 am |
|
|
Hi,
Have you actually read the 'oscillator configuration! Section of the PIC 18F4550 datasheet?
Repeat after me "always do a 1Hz LED flasher test anytime you suspect your PIC is not running at the correct speedy"
Do that, and come back and tell us your results! Engage your brain, and then we'll help you try to solve this issue.
BTW, there are many threads on the forum covering this exact issue! Did you spend any time looking?
John |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Oct 19, 2014 9:09 am |
|
|
He has two separate issues:
1) The printf specifier is wrong for what he wants to send. %c needed not %x.
2) Clock rate. As ezflyr says, always test your actual clock rate _first_.
I'd actually suspect you may have a problem with the crystal not starting, so the chip is intermittently running off the internal oscillator.
Separately, stick to the C standard of declaring variables at the start of code sections. Though CCS 'allows' mid code declarations, it has the habit of causing problems... |
|
|
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
|
Posted: Sun Oct 19, 2014 11:50 am |
|
|
Ttelmah wrote: | He has two separate issues:
1) The printf specifier is wrong for what he wants to send. %c needed not %x.
2) Clock rate. As ezflyr says, always test your actual clock rate _first_.
I'd actually suspect you may have a problem with the crystal not starting, so the chip is intermittently running off the internal oscillator.
Separately, stick to the C standard of declaring variables at the start of code sections. Though CCS 'allows' mid code declarations, it has the habit of causing problems... |
Hi Ttelmah..
I change %x to %c....
I didn't get what do you mean by 1 Hz led flasher....
Are you saying that i should flash an led for 1 sec and turn it off...and measure the time...if so should i set my clock using trial and error method..???
Thank You.... _________________ Krazzy7434 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Oct 19, 2014 11:56 am |
|
|
The flash an LED test, is the most basic way of checking that your oscillator s running at the speed you think it is. The xx50 chips are more critical than their older brethren on crystals being the right type to actually work. Often a parallel resistor is needed to make them start properly.
Key is that when you have a timing problem, always start by checking the chip is running at the speed you expect, before wasting time on other things. |
|
|
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
|
Posted: Sun Oct 19, 2014 12:32 pm |
|
|
Ttelmah wrote: | The flash an LED test, is the most basic way of checking that your oscillator s running at the speed you think it is. The xx50 chips are more critical than their older brethren on crystals being the right type to actually work. Often a parallel resistor is needed to make them start properly.
Key is that when you have a timing problem, always start by checking the chip is running at the speed you expect, before wasting time on other things. |
hi Ttelmah..
I got it. But i dont know what is this led flasher..
Are you saying than i should turn on and off an led with 1000ms delay with different clock frequency.....
Also my board comes with an inbuilt 16mhz crystal...i bought it from a local store...so i don't have it version also...
Are you saying that i should edit the board...???
Thank You... _________________ Krazzy7434 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Oct 19, 2014 1:46 pm |
|
|
The flash an LED test, has been mentioned here literally thousands of times. It is a _test_. If you put 'delay_ms(1000)' in your code, that delay is dependant on the actual clock settings and crystal. It is a way of checking that the chip is running at the speed you expect. If it doesn't, the actual result, will often tell you what is wrong. If (for instance) it gives a 2 second delay, then you are looking at finding 'how' the chip can get 8MHz, not the 16 you expect.
Do a search. You'll find it mentioned here many times. It is so common and important, that the CCS 'wizard' even has an option to generate the code.... |
|
|
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
|
Posted: Tue Oct 21, 2014 7:34 am |
|
|
Hello friends....
Sorry for the late reply...
I tried Led Flash test....here is the code i used for led test..I hope it correct...make me corrected if wrong
Code: |
#include <18F4550.h>
#fuses HS, PLL4, CPUDIV1, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=32000000)
//============================
void main()
{
int i;
for(i=0;i<=9;i++)
{
output_high(PIN_E1);
delay_ms(500);
output_low(PIN_E1);
delay_ms(500);
}
}
|
for 16Mhz clock it takes 10.01 seconds from i=0 to i=9 ....,
i used a stopwatch to measure the time.....
now what should i do to calculate the exact baud rate plzz help....
Thank You... _________________ Krazzy7434 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Oct 21, 2014 7:44 am |
|
|
Er. CLOCK=32MHz, is an impossible value for the HS oscillator.
Maximum this supports is 25MHz.
If this is giving the correct times, it implies your chip is not running as you think it is. This is not a software fix, but says something is wrong with your hardware.
We did not want you to fiddle around changing the clock value, but just report what the routine gives.
However, you speak about your crystal being fitted by a local supplier, Does the board you have, have a part number, or a data sheet somewhere?.
If not, a good photograph of the board?.
The behaviour you have is classic for the crystal being too high gain, and overdriven by the PIC. It can be fixed by adding a serial resistor to the crystal to reduce the drive, but if this is a supplied board, you should not be having to do this. |
|
|
Krazzy7434
Joined: 21 Aug 2014 Posts: 39 Location: India
|
|
Posted: Tue Oct 21, 2014 8:52 am |
|
|
Ttelmah wrote: | Er. CLOCK=32MHz, is an impossible value for the HS oscillator.
Maximum this supports is 25MHz.
If this is giving the correct times, it implies your chip is not running as you think it is. This is not a software fix, but says something is wrong with your hardware.
We did not want you to fiddle around changing the clock value, but just report what the routine gives.
However, you speak about your crystal being fitted by a local supplier, Does the board you have, have a part number, or a data sheet somewhere?.
If not, a good photograph of the board?.
The behaviour you have is classic for the crystal being too high gain, and overdriven by the PIC. It can be fixed by adding a serial resistor to the crystal to reduce the drive, but if this is a supplied board, you should not be having to do this. |
Very sorry Ttelmah...actually that was a careless mistake by me...
actually its works very well on 16Mhz clock..i accidently post the wrong code...
This is my actual code::
Code: |
#include <18F4550.h>
#fuses HS, CPUDIV1, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=16000000)
//============================
void main()
{
int i;
for(i=0;i<=9;i++)
{
output_high(PIN_E1);
delay_ms(500);
output_low(PIN_E1);
delay_ms(500);
}
}
|
i was checking with each clock frequency..and accidently posted wrong code..sorry for that.... _________________ Krazzy7434 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Oct 21, 2014 9:24 am |
|
|
In which case it should work. With %c, instead of %x, it should send the 55 byte as expected. If it doesn't you need to be looking at the serial connection, not at the PIC clock. |
|
|
|
|
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
|