![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Guest
|
Software UART baud rate problem |
Posted: Mon Aug 07, 2006 3:30 pm |
|
|
Compiler: PCWH version 3.236
PIC: PIC16F628A
Oscillator: 7.3728 MHz
Target Voltage: 5V
I have set up my processor with a software UART on pins B6(RX) and B7(TX). I run those pins to two separate SN65176BD Texas Instruments Differential Bus Transceivers. This converts the UART to RS422. I then have a RS422 to RS232 converter going into my PC (I realize it would be easier just to use a RS232 driver instead of the RS422 drivers and then converting the RS422 to RS232, but I need RS422 for my application).
Things are acting weird. When I have the following three lines in my code then I can send data to HyperTerminal and everything works great in that direction. However, when I receive data from HyperTerminal, all I get on my LCD is garbage characters. I looked at both baud rates from the PC and from the PIC. The PC is sending bits at 58.70 khz and the PIC is sending bits at 54.23 khz. Is this the problem? Why is the PIC's baud rate not closer to 57600?
Code: |
char received_string[STRING_SIZE];
char received_header[HEADER_SIZE];
char lcd_line_one[LCD_LINE_SIZE + 1];
|
When I take those three lines out, then about 50% of the time I receive the correct charcter on the LCD and 50% of the time it is garbage. But I can no longer send anything to hyperterminal. Why do those lines affect anything, I don't even use them?
Here is my program:
Code: |
#include <16F628A.h>
#use delay(clock=7372800)
#use rs232(baud=57600,parity=N,xmit=PIN_B7,rcv=PIN_B6,bits=8,Force_SW, STREAM=tracker)
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#include "Project_Defs.h"
#include "lcd_driver.c"
#include "debounce.c"
typedef struct{
enum state {OFF, ON};
state button;
} toggle_power_button;
char received_string[STRING_SIZE];
char received_header[HEADER_SIZE];
char lcd_line_one[LCD_LINE_SIZE + 1];
toggle_power_button power;
char c;
int count;
#int_EXT
EXT_isr() {
debounce();
if(power.button == ON) //toggles button between on and off
power.button = OFF;
else
power.button = ON;
if(power.button == OFF) //Turning Power OFF
{
IDU_Asleep; //Puts all ICs except uP in sleep mode
}
else //Turning Power ON
{
IDU_Awake; //Puts all ICs in normal mode
delay_ms(10); // delay for motor controller to stablize – page 5 datasheet
lcd_init();
lcd_putch("System On);
delay_ms(1500);
lcd_clear();
}
}
void main()
{
enable_interrupts(global); //configures external interrupt
enable_interrupts(INT_EXT);
power.button = OFF;
count = 0;
while(TRUE)
{
while(power.button == ON)
{
if(kbhit(tracker) == TRUE){
c = fgetc(tracker);
count++;
if(count == 16){
lcd_clear();
count = 0;
}
printf(lcd_putch, "%c", c);
fprintf(tracker, "received: %c\r\n", c);
}
if(BT_MODE_Pushed){
lcd_clear();
lcd_putch("Mode Button");
fprintf(tracker, "M");
delay_ms(1500);
fprintf(tracker, "\r\n");
lcd_clear();
}
}
}
}
|
Here is my project_defs.h file
Code: |
// Buttons --> BT = Button
#define BT_MODE_and_AutoStow !input(PIN_B4)
#define BT_MODE_Pushed !input(PIN_A5)
#define BT_POWER_Pushed !input(PIN_B0)
//Sleep
#define IDU_Awake output_high (PIN_B5)
#define IDU_Asleep output_low (PIN_B5)
//LCD
#define LCD_RS PIN_A4 // Register select
#define LCD_EN PIN_A3 // Enable
#define LCD_D4 PIN_A2 // Data bits
#define LCD_D5 PIN_A1 // Data bits
#define LCD_D6 PIN_A0 // Data bits
#define LCD_D7 PIN_B3 // Data bits
//Constants
#define STRING_SIZE 42 //33 chars for LCD(in case space at 17), 8 for end header, 1 for null
#define HEADER_SIZE 4 //7 for header, 1 for null
#define LCD_LINE_SIZE 16 //LCD size is 16x2, 1 for null
#define START_OF_PACKET '$'
#define START_HEADER "$**"
#define END_HEADER "***"
|
|
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Mon Aug 07, 2006 6:17 pm |
|
|
You have a couple of problems. The CCS software UART uses NOPs for bit timing. If you have interrupts enabled then the interrtup handler processing will corrupt the bit timing.
57Kbps receive on a PIC running a around 7Mbps is a tall ask. You do not have the CPU cycles available. You might want to consider replacing the softwaer UART with an implementation using timer interrupts - bit hard on a 16F when you also have other interrupt sources. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
![](templates/subSilver/images/spacer.gif) |
Sensor
Joined: 04 Nov 2004 Posts: 33
|
|
Posted: Mon Aug 07, 2006 8:54 pm |
|
|
Can you explain how I don't have the CPU cycle available?
Correct me if I'm wrong... my clock is running at 7.3728MHz, so one instruction will occur every 1/7.3728MHz = 0.1356uS
Baud rate is 57600, so one bit will come in every 1/57600 = 17.36uS
So shouldn't I have (17.36 / 0.1356) = 128 instruction cycles per bit?
What if I increase the clock to 14.7456MHz? |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Mon Aug 07, 2006 9:25 pm |
|
|
I instruction takes four FOSC cycles. So one instruction takes 0.542us so at 57600baud you have only 34 instruction cycles between bits.
The ISR handler may take around 100 instruction cycles to deal with context switching.
Even if you double the clock frequency you can see that the context switching overhead of the ISR is far greater than the available CPU cycles.
You might want to consider coding your ISR such that it does not generate any cleanup code (you are responsilbe for saving and restoring your own registers). Using an 18F series processor and using the FAST directive with the ISR will help but even with an 18F series at this ocsillator frequency you will be struggling. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
![](templates/subSilver/images/spacer.gif) |
Sensor
Joined: 04 Nov 2004 Posts: 33
|
|
Posted: Tue Aug 08, 2006 8:04 am |
|
|
You were correct, when I slowed the baud rate to 9600 it worked perfect.
In my application I need a baud rate of 57600 so I may have to switch to an 18F which has two hardware UARTS.
Thanks for the help. |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Tue Aug 08, 2006 8:19 am |
|
|
An 18F with two uarts is a good option. You could also use an 18F with one hardware UART and one software UART based on timers and an external interrupt. At 40HMz you can reliably achieve 115Kbps using high priority interrupts for the task. This way you are free to use LP interrupts for other functions. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
Posted: Tue Aug 08, 2006 8:32 am |
|
|
Dallas Semiconductor has SPI UARTs I have used several times and would be a possible solution if you have a separate SPI output...
MAX3110/MAX3111 and MAX 3140. |
|
![](templates/subSilver/images/spacer.gif) |
Pulsartomi
Joined: 26 May 2010 Posts: 17
|
|
Posted: Wed Apr 20, 2011 9:02 am |
|
|
my pic rs-232 keeps running at Baud rate 4800 (tested with hyperterminal, and other terminal softwares), but this line should set it to 19200 Baud!
#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
using 20Mhz xtal
Please help, I tried to change the baud rate generator values manually, but no use.19200 is exactly divided by 4 but can't understand why. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Wed Apr 20, 2011 9:34 am |
|
|
Pulsartomi wrote: | my pic rs-232 keeps running at Baud rate 4800 (tested with hyperterminal, and other terminal softwares), but this line should set it to 19200 Baud!
#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
using 20Mhz xtal
Please help, I tried to change the baud rate generator values manually, but no use.19200 is exactly divided by 4 but can't understand why. |
Post the PIC involved.
Post the fuses line(s).
Post the compiler version.
Part number for the crystal?.
What loading capacitors are you using?.
Picture of PCB layout around the crystal?.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Pulsartomi
Joined: 26 May 2010 Posts: 17
|
|
Posted: Wed Apr 20, 2011 10:25 am |
|
|
Quote: | Post the PIC involved. |
18F4550
Quote: | Post the fuses line(s). |
Code: | #include <18F4550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //Crystal osc <= 4mhz
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES MCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES XINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL12 //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4 //System Clock by 4
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#FUSES ICPRT //ICPRT enabled
#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
Quote: | Post the compiler version. |
v4.038
Quote: | Part number for the crystal?. |
NSK 9C 20.000 AL T
Quote: | What loading capacitors are you using?. |
2*33pF
Quote: | Picture of PCB layout around the crystal?. |
it is a bit messy for photo, but it's as in the datasheet, crystal's wires to OSC1, OSC2, and two capacitors are connected to each, capacitors common to GND.
Thank You for helping
I managed to set the baud rate manually now, with these values:
Code: | SPBRG=16; //19200 BAUD SPEN=1
RCSTA=0x90;
TXSTA=0xA0; //BIT2 BRGH=1 SYNC=0;
BAUDCON=0;
bit_clear(TXSTA,4); //sync
bit_clear(TXSTA,2); //brgh
bit_set(BAUDCON,3); //brg16 |
(yeah, duplicate settings, but now I didn't took out the unneccessary parts, just got it working)
Now it's working, but the datasheet says SPBRG should be 64. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Apr 20, 2011 1:50 pm |
|
|
Is there some way you can make sure the PIC is running at the right speed? You could pulse a pin for 1 millisecond and check it with a scope, or pulse it for 10 seconds and check it with a watch. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Wed Apr 20, 2011 2:59 pm |
|
|
I have to say that 4.038, could be a problem. This is before V4, really started to work. At the point when this was released 3.249, was by far the better compiler.
Don't know that particular crystal (do you have a data sheet?), but it sounds as if it is probably an AT cut type. If so, you may need a series resistor to avoid it being overdriven. This may be your problem. Something like 50R.
It is unlikely that your loading or layout, is going to give accurate values, but the error should be small.
However the fuses have problems:
Get rid of the LVP fuse. Unless you are using LVP (very unlikely), and have the extra hardware to ensure the LVP lines do not float, NOLVP is normally the fuse needed.
Get rid of XINST. NOXINST, is _required_ by the CCS compiler. Code will not run correctly with this.
The dividers are 'wrong'. PLL5, and CPUDIV1 are needed. The latter would explain the processor running at 1/4 the expected frequency.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Wed Apr 20, 2011 9:39 pm |
|
|
I'd like to ring in and say that a PIC18 (like the 18FxxJ11 that come in small pin counts and DIP packages - good for experimenting) with 2 UART's is a great way to go.
Anytime I'm running 2 Async UARTs that require responsiveness, I don't even bother with single UART PIC's.
I write ISR's to handle my random incoming data and life is good.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
Pulsartomi
Joined: 26 May 2010 Posts: 17
|
|
Posted: Thu Apr 21, 2011 1:02 am |
|
|
SherpaDoug:I don't have a scope yet, but can check it with sound card
Ttelmah: Thank You for the information of the fuses, I was always confused about these.
Thank You guys for your help, much appreciated ![Razz](images/smiles/icon_razz.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|