Jump to content

Using 3.2768mhz Crystal With Uart- Help!


Recommended Posts

Hi everyone,

 

I am using PIC16F88 with a 3.2768MHz crystal, MAX232 and my hardware UART seems not to work perfectly. The pragma CLOCK_FREQ would not compile, so I had to comment it out. The error said: Can't generate RS232 with the current parameters. But it did compile if I changed the clock freq to 4Mhz or 8Mhz or 20 MHz(whole number).

I know that the MAX232 generates correct signal levels and even transmitts the number of bits correctly, but the BAUD rate is wrong, so when I used HyperTerminal and tried to echo the character, it would return the wrong character from the one I sent. This is the initialization bit from my code:

 

#include "convert.c"

 

//#pragma CLOCK_FREQ 3276800

 

#pragma RS232_RXPORT PORTB

#pragma RS232_TXPORT PORTB

#pragma RS232_TXPIN 5

#pragma RS232_RXPIN 2

#pragma RS232_BAUD 9600

 

void main(void)

{

 

set_bit(status,RP0); // BANK 1 select

option_reg=10000011b; //RB0-falling edge, internal clk for TMR0, Psc=1:16

trisa=10100011b; // porta set in/out

trisb=01000101b; // portb set in/out

spbrg = 20;

txsta = 0x24; //BRG = 1, no parity, 1 stop bit, 8 bits

rcsta = 0x90; //RB5- xmit, RB2 - rcv

ansel= 00000001b; //A0 is analogue input

adcon1 = 10000000b; //ADC

tmr2 = 0; //clear tmr2 reg value

clear_bit(status,RP0); // BANK 0 select

porta=0; //set values on porta=0

portb=0;

adcon0 = 01000001b; //bit 2 GO/DONE bit set to 0, have to be 1 when start A/D conv

char rec =0;

while(1)

{

rec = getchar();

//some other code inside, which is not related to rs232

putchar(rec);

}

 

My main questions are: is the UART limited to certain clock frequencies? And do I have to use pragmas or simply writing to registers is enough? ( it seems not to work without pragmas)

 

I would really appreciate any help, since my project is with tight deadline. Thank you very much!

Link to post
Share on other sites

Anna,

 

Are you using the SourceBoost rs232_driver library ?

 

Your crystal is not exactly divisible down to 9600 baud.

 

The value, 3.2768MHz is very similar to a watch crystal at 32.768kHz with 2 extra zeroes.

 

I have no problems compiling with your #pragma CLOCK_FREQ.

 

The UART will run at any frequency where the crystal is divisible by the BRG.

 

3.8684MHz is a standard value for generating baud rates with no frequency error.

 

Setting the registers to the correct values is all you need to get the UART going. No extra pragmas required.

 

Cheers

 

Reynard

Link to post
Share on other sites

Hi Reynard,

 

Thanks for your reply.

 

Answering your questions:

 

I am not using rs232_driver library.

 

I realise that there is an error with BAUD rate when I am using that particular crystal. Previously I used 20MHz and programmed in MPLAB at 9600 rate and it worked fine. Is my problem just a difference in compilers, that does not allow any frequency errors?

 

The code I posted compiles with #pragma CLOCK_FREQ 3276800 line commented out, it does not compile otherwise. Do I need this pragma at all? as I set the clock rate in the settings for the device anyway.

 

Please correct me if I am wrong, all pragmas I am already using in my code are obligatory and no extra are required? Or I can take them all out and use just registors? Appologies if I am asking too many stupid questions.

 

Anna

Link to post
Share on other sites

Hi Anna,

 

I have no problems compiling with your CLOCK_FREQ pragma value.

 

According to the manual BoostC only recognises 3 pragma directives so I don't know where your RS232 pragmas came from.

 

The CLOCK_FREQ pragma is not required unless you are using one of the libraries that automatically calculate a value based on it such as the rs232 library. Delay routines (delay_us(X) etc.) use this pragma to let the compiler calculate software time loops.

 

The only pragma you need to use is DATA to set your config settings.

 

Cheers

 

Reynard

Link to post
Share on other sites

I think Reynard is on to something in that your set up is ok but your crystal is really 32768 Hz instead of 3276800. If it was 3.2768 MHz, your baud rate error is only -1.5%. RS-232 can handle about a 5% error if I remember correctly (I did the math a few years ago and am too lazy to do it again).

 

Setting SPBRG to 6 would give a baud rate of 32768/(16(6+1)) or 292 which is 2.5% slow for 300 baud. That is if your crystal is 32.768KHz. You could give it a try and see if it communicates at 300 baud. If it does, your crystal is the problem.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...