Discussion in "PIC Microcontroller Discussion" started by    abdul991    May 28, 2013.
Tue May 28 2013, 03:44 pm
#1
I'VE TRIED 2-3 COMBINATIONS WITH BRGH, 8BIT AND 16 BIT SPBRG..
I'M UNABLE TO TRANSMIT A STRING TO HYPERTERMINAL...
ALL I GET AT THE PC IS GARBAGE VALUES


#pragma config PWRT = OFF
#pragma config WDT = OFF
#pragma config DEBUG = OFF,LVP = OFF //,STVR = OFF

#include <P18F4550.h>
                    // USING 12MHZ CLOCK
#include<stdio.h>

#pragma <string.h>

#pragma <stdlib.h>


/*     SIMPLE UART SERIAL TRANSMIT   */

void SerTx(unsigned char c);
void  Delay(unsigned char a);

void main()
{

     int i;
     char c[] = "DIGI ELECTRONICS";
     Delay(250);
      
     TXSTA = 0x24;   //   HIGH SPEED BRGH =1 for asynchronous , tx enable settings
   	 SPBRG = 77;  //for BRGH =1,12mhz baud rate.
	 TXSTAbits.TXEN = 1;
	 RCSTAbits.SPEN = 1; 
     BAUDCON = 0x40;

      
      while(1)  
      {
       Delay(250);
        for(i=0; i<strlen(c); i++)
       SerTx(c[i]); 
       //sprintf(str,"the string is");
		
      }

    }

void  Delay(unsigned char a)
{
   int i,j;
   for(i = 0; i < a; i++)
   for(j = 0; j < 255; j++);
}  

void SerTx(unsigned char c)
{
	while(PIR1bits.TXIF == 0);
	TXREG = c;
}	
Tue May 28 2013, 10:28 pm
#2
What baud rate are you aiming for ?
12MHz is an odd crystal for a PIC, are you sure its 12MHz ?
Wed May 29 2013, 10:07 am
#3
there is a table given for baudrate generation in PIC datasheet. Did you try some of those values?
Fri May 31 2013, 05:47 pm
#4
yes i'm sure its 12 MHz and i've tried various combinations if baudrate table

// TRIED FOR HSPEED BRG AND 16 BIT
/* 
     SPBRGH=0x01;
     SPBRG =0x36;
     TXSTA = 0x24;
     BAUDCON = 0x48;
     RCSTAbits.SPEN = 1; 
	 TXSTAbits.TXEN = 1;
	 */
// FOR BRG =0 , 16 BIT ASYNCHR
     SPBRGH=0;
     SPBRG =77;
     TXSTA = 0x20;
     BAUDCON = 0x48;
     RCSTAbits.SPEN = 1; 
	 TXSTAbits.TXEN = 1;


and the thing is i'm getting same kind of junk values for every combination i tried above.. pl check the attachment


Fri May 31 2013, 11:15 pm
#5
PICs have various clock sources and dividers outside of the baud rate registers.
Often they are set by the compiler or even the programmer.
What software are you using ?

Please post the full code you are trying to work at 9600 baud.

Actually the srceen shot looks close to working.
Try different baud rates in Hyperterminal, you should get a match.
 abdul991 like this.
Sat Jun 01 2013, 04:29 pm
#6
i assumed the same( as ExperimenterUK : Actually the srceen shot looks close to working...)
i tried different baudrates with -7 to +7 of the values i calculated.....
but didn;t get the o/p... i think the capacitor values for the xtal used has to be changed according to the datasheet..
not sure.... But 12Mhz Xtal is working good as the same gives perfect o/p for the PIC18f452.
Ii've entire code is given already
Sun Jun 02 2013, 07:31 pm
#7
here is the equation to find baudrate generator value for uart..

For a device with FOSC of 16 MHz, desired baud rate of 9600, Asynchronous mode, 8-bit BRG:
Desired Baud Rate = FOSC/(64 ([SPBRGH:SPBRG] + 1))
Solving for SPBRGH:SPBRG: X = ((FOSC/Desired Baud Rate)/64) – 1

example @16Mhz for 9600bps:
X = ((16000000/9600)/64) – 1
X = [25.042] = 25

Calculated Baud Rate = 16000000/(64 (25 + 1)) = 9615
Error = (Calculated Baud Rate – Desired Baud Rate)/Desired Baud Rate
Error = (9615 – 9600)/9600 = 0.16%
Mon Jun 03 2013, 11:58 pm
#8


PICs have various clock sources and dividers outside of the baud rate registers.
Often they are set by the compiler or even the programmer.
What software are you using ?

ExperimenterUK


Try flashing an LED, one for a second off for a second.
If the flash rate is wrong ,it probably means you have a clock rate selection error.
Or your program is wrong
Tue Jun 04 2013, 02:42 pm
#9
flashing LED works perfectly...
so you mean to say problem is with the code.. i don't know where..?
Or
Is 12Mhz not compatible?
I'm skeptical on the capacitor use for oscillator circuit where i was using 33pF for 18F452..
Do i have to change capacitor as per the datasheet of 18f4550 mentioned in page 25 under Oscillator Configurations
Tue Jun 04 2013, 09:47 pm
#10


flashing LED works perfectly...
so you mean to say problem is with the code.. i don't know where..?
Or
Is 12Mhz not compatible?
I'm skeptical on the capacitor use for oscillator circuit where i was using 33pF for 18F452..
Do i have to change capacitor as per the datasheet of 18f4550 mentioned in page 25 under Oscillator Configurations

abdul991


Actually I should have said flash an LED using a timer.
Can you post the code you used.

If you use a built in delay or a loop it may not mean anything.

If the LED flashes at the calculated rate the crystal must be working at the right speed.
I think your value of 77 is correct, and should be accurate enough to work.

Try using the attached serial program on your PC




Attachment

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am