|
|
View previous topic :: View next topic |
Author |
Message |
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
RS232 communication PIC18f4550 -- putc sending corrupt data |
Posted: Mon Sep 06, 2010 4:04 am |
|
|
I am a newbie to microcontroller programming I am using ccsc compiler and written a program to communicate to PC thru RS232 interface
It uses users232 directive.just that ..and after that i wrote putc functions.If you say i can post code also .
But its not sending the character i mentioned in putc but any random character is coming on hyperterminal ....
please help
Things i have already tried
1)enable interrupts global and INT_RDA
2)FUSES -- not sure just copied from somewhere
3)disassembled program ...disassembled program seems okay except one thing that
disassembled program dint seem to check for TMRT bit of TXSTA ...
Kindly help |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Mon Sep 06, 2010 5:23 am |
|
|
You don't say but you need to have a MAX232 or similar TTL to RS232 convertor chip between the PIC and the PC.
Either check with the CCS C manual or 'online' for more info.
Do help us debug your program it must be a small with fuse,and compiler version (3.xxx,4.yyy).
JAy |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Sep 06, 2010 7:31 am |
|
|
Also, the 'classic' thing would be for the clock statement, to not match the real crystal being used, or the fuses are not right for the oscillator hardware involved. Async serial, is _time critical_, so everything to do with timing on the chip, must be right for it to work.....
Best Wishes |
|
|
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
|
Posted: Mon Sep 06, 2010 10:00 am |
|
|
Code: |
#include <18F4550.h>
#fuses HS,NOWDT
#use delay(clock=20MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,BRGH1OK,STOP=1)
void main()
{
char c;
set_tris_c(0xBF);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
while(TRUE)
{
c=getc(); //if getc() timeouts 0 is returned to c
delay_ms(200);
putc(c);
}
}
|
This is my program which gives this following o/p on hyper terminal, in echo mode in hyperterminal.
dD ddddddffffffaybzaybzddeettyyzzayayaybzbzbzc{
Where in most characters are correctly transmitted and received like dd ee ff etc but definite characters like a, b, c which are converted to definite wrong characters like
a is transmitted but received is y similarly b----> z c----> { everytime
point to be noted is
a=97 0110 0001 in ascii
y=121 0111 1001
b = 98 0110 0010
z=122 0111 1010
similar for c------>{
Please reply, I have tried for hours but not able to come out why there is problem with definite few characters.
They only come wrong everytime. It doesn't seem to be a framing error as a,b,c might be transmitted at any time they have to come out wrong only that too definite wrong output. pl help. |
|
|
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
My specification |
Posted: Mon Sep 06, 2010 10:06 am |
|
|
I have pic18F4550 rhydolabs kit, MPLAB 8.53, CCSC Compiler, HARDWARE UART with MAX232 on board. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Sep 06, 2010 10:17 am |
|
|
Several big problems.
1) You must _never_ enable an interrupt without an interrupt handler being present. Get rid of the lines doing this. I'm surprised it runs at all with these present.
2) The code talks about 'getc timeout', but you have not got this enabled in the USE RS232 line.
3) Get rid of BRG10K. This is only for chips that have the BRG problem.
4) _Is_ your crystal 20MHz?.
5) Don't delay after getting the character. If another two characters arrive in this time, the UART _will_ overrun. This will hang the UART with the code as written (add 'ERRORS' to the USE RS232 line - this should always be used with the hardware UART, unless _you_ are handling error conditions yourself.
Best Wishes |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Sep 06, 2010 2:37 pm |
|
|
Ttelmah is right about the delay. RS232 is asynchronous and its often very smart to assume it is entirely incompatible with delays of any sort. That includes not only delay_ms but any time consuming thing like a printf. If you can't avoid using any delays then the solution is a circular buffer fed from an interrupt. The inbound int_rda is almost always needed but sometimes an output isr with a circular buffer is needed to avoid the output via rs232 blocking other processes. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 06, 2010 9:48 pm |
|
|
How about trying this:
Code: |
#include <18F4550.h>
#fuses HS,NOWDT
#use delay(clock=20MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,STOP=1)
void main() {
char c;
//set_tris_c(0xBF);
// I don't think you need this since by default, the the pins are how you want them. But verify it.
while(TRUE) {
if (kbhit() ) {
c=getc();
putc(c);
}
}
}
|
I know getc() is supposed to check for a kbhit(), but I'm a big fan of making it obvious.
This way, getc doesn't time out. The loop spins as fast as it can. and what you get in should be what you get out.
-Ben
EDIT: I removed the BRG10K -- I spaced on that one, I don't think it's needed either. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D
Last edited by bkamen on Tue Sep 07, 2010 9:48 am; edited 1 time in total |
|
|
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
|
Posted: Mon Sep 06, 2010 11:00 pm |
|
|
Quote: |
You must _never_ enable an interrupt without an interrupt handler being present. Get rid of the lines doing this. I'm surprised it runs at all with these present.
|
1) We didn't use it initially but while debugging only we used it ...and after using it only we started getting some output.
Quote: |
The code talks about 'getc timeout', but you have not got this enabled in the USE RS232 line.
|
2) that is also part of our debugging spree ...we used timeout then removed it .
But comment is still lingering here.
Quote: | Get rid of BRG10K. This is only for chips that have the BRG problem. |
3) That I just used yesterday when someone suggested if there is some baud problem this flag deals with it .
Quote: | _Is_ your crystal 20MHz?.
|
4) I just know that in data sheet of pic18f4550 clock source is 20Mhz so only I used HS in fuses.
Quote: | Don't delay after getting the character. If another two characters arrive in this time, the UART _will_ overrun. This will hang the UART with the code as written (add 'ERRORS' to the USE RS232 line - this should always be used with the hardware UART, unless _you_ are handling error conditions yourself.
|
5) Yes that is wrong. I did that at last moment before posting post. Program was giving mentioned output without using delay only.
6) We added ERRORS but compiler gave compilation error: Sort of overrange error....so we removed ERRORS.
Please help Now
and
The program mentioned ahead we have tried that ...that doesn't give any output on H.T at all ...still I will try that today again and send you the output.
Thanks a lot for helping us to come out this confusing state.
Please note there doesn't seem to be hardware problem...as the assembly codes came with Kit are working perfectly fine. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Sep 07, 2010 1:52 am |
|
|
Put ERRORS back.
You will get a _warning_, not an 'error' from the compiler. This is because when 'errors' is added, the compiler generates a variable 'rs232_errors', containing the error information, and since this is not used (at this point), the compiler generates a warning, that there is an unused variable. Warnings are just to tell you that there is something perhaps wrong going on. Problem is that not having 'ERRORS', is much worse than having one unused variable.
If you don't want the warning, simply do a dummy read of the variable at the start of your code. This will get rid of the compiler warning.
When you were 'debugging', did you actually have an interrupt handler present?. If so, then this would explain how things could work. I think part of your problem, is the RS232 overrun error, if data is arriving, and not being read. Without the 'ERRORS' directive, this _will_ lockup the UART.
The PIC2550, can run from any clock source, from 0MHz (though rather pointless), up to 25MHz with a crystal, and from an external clock input up to 48MHz. You need to find out what the actual crystal is on the board you have. Unfortunately, Rhydolabz, doesn't seem to have a current website with details of their stuff, so we can't tell what you may have. If 'echoing' characters round, it is possible for it to work with some characters, if the baud rate is completely wrong. Simply get a magnifying glass, and look at the crystal on the board. This is really the vital 'first step'.
Best Wishes |
|
|
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
|
Posted: Tue Sep 07, 2010 2:27 am |
|
|
Kit Spec Says ---- On Board 20 MHz Crystal Oscillator Microchip PIC 18F4550 with 20 MHz Crystal Oscillator (With Boot loader Software)
at
http://www.rhydolabz.com/index.php?main_page=product_info&cPath=99_101&
products_id=297
When I used ERRORS ...BUILD FAILED due to error not warning ... still I will try it again today.
Quote: |
When you were 'debugging', did you actually have an interrupt handler present?. If so, then this would explain how things could work. I think part of your problem, is the RS232 overrun error, if data is arriving, and not being read. Without the 'ERRORS' directive, this _will_ lockup the UART.
|
No I had no interrupt handler. Still its working that way as I mentioned. |
|
|
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
|
Posted: Wed Sep 08, 2010 10:55 pm |
|
|
Hi
My program worked because I change use delay to 48 MHz from 20 MHz.
Thanks to you all.
As it was actually a clock problem as you all hinted.
My board
http://www.rhydolabz.com/index.php?main_page=product_info&cPath=99_101&products_id=297
says
On Board 20 MHz Crystal Oscillator.
Supports Up to 48 MHz Operation.
I was using 20MHz, thinking that is master clock
but changing it to 48 worked perfectly well. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Sep 09, 2010 2:27 am |
|
|
You can actually run at 48MHz, from the 20MHz crystal.
Fuses needed:
Code: |
#fuses HSPLL, NOWDT, PLL5, CPUDIV1, USBDIV, NOXINST
#use delay (clock=48000000)
|
This says to take the incoming clock, divide it by 5 (PLL5), and feed this to the master PLL. Then multiply this by 24 (the only ratio supported by the PLL), to give 96MHz. Then divide this by 2 (CPUDIV1 - totally confusing! - this fuse gives division by '1' if operating from the external clock, and by '2' if operating from the PLL - designed to really get you thinking.....). Finally sets the processor timing to run from this 48MHz clock.
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Sep 09, 2010 8:51 am |
|
|
Ttelmah wrote: | You can actually run at 48MHz, from the 20MHz crystal.
Fuses needed:
Code: |
#fuses HSPLL, NOWDT, PLL5, CPUDIV1, USBDIV, NOXINST
#use delay (clock=48000000)
|
This says to take the incoming clock, divide it by 5 (PLL5), and feed this to the master PLL. Then multiply this by 24 (the only ratio supported by the PLL), to give 96MHz. Then divide this by 2 (CPUDIV1 - totally confusing! - this fuse gives division by '1' if operating from the external clock, and by '2' if operating from the PLL - designed to really get you thinking.....). Finally sets the processor timing to run from this 48MHz clock.
Best Wishes |
I was thinking about the Oscillator config last night since his original code didn't specify so many fuses what the defaults would have been. Thus getting him into the trouble he was experiencing.
I've never had problems with the 18Fx550's as I was paranoid and was always explicit about the oscillator fuses.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
nidhimittalhada
Joined: 06 Sep 2010 Posts: 21
|
No need of FUSES |
Posted: Fri Sep 10, 2010 10:01 am |
|
|
My board
http://www.rhydolabz.com/index.php?main_page=product_info&cPath=99_101&products_id=297
says
On Board 20 MHz Crystal Oscillator.
Supports Up to 48 MHz Operation.
#include <18F4550.h>
#use delay (clock=48000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main()
{
char c;
while(TRUE)
{
c=getc();
putc(c);
}
}
See This is the program working perfectly right.
Now It seems that my crystal is 20MHz but without using any fuses as you mentioned
My program is working well.
Even i was doubtful that it was written "in use delay give master clock "
then
On Board 20 MHz Crystal Oscillator.
Supports Up to 48 MHz Operation.
which one out of these will be called master clock
as i used 20Mhz but it did not work rightly
i then changed it to 48 M Hz it worked !!!!!
pl clarify ... |
|
|
|
|
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
|