Author Topic: PIC UART help needed  (Read 6901 times)

0 Members and 1 Guest are viewing this topic.

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
PIC UART help needed
« on: February 25, 2014, 07:10:22 am »
I am trying to use the peripheral libraries & get a PIC18F2550 to send a character every second through UART. But either I receive invalid data bytes or I don't receive anything at all. Where am I going wrong?  |O I have attached the code. Please let me know if anything else is required. I am using XC8 compiler.

Code -

//peripheral libraries
#include <plib/usart.h>

#define _XTAL_FREQ 8000000
#define USE_AND_MASKS

void delay(unsigned long x);
unsigned char config, baud;
void main()
{
    config=USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_LOW;
    baud=0x1C;             //9600bps
    OpenUSART(config, baud);        //configure & enable UART
    while(1)
    {
        WriteUSART('A');
        delay(1000);
    }
}
//delay function
void delay(unsigned long x)
{
   unsigned long m;
   for(m=0; m<x; m++)
   {
      __delay_ms(1);
   }
}
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: PIC UART help needed
« Reply #1 on: February 25, 2014, 07:43:41 am »
Nothing like a schematic to help a guy figure something out.

Nothing like knowing what's at the receiving end of all of this.

Nothing like a decent description of everything.

Three things that apparently don't exist...
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2184
  • Country: au
Re: PIC UART help needed
« Reply #2 on: February 25, 2014, 09:14:30 am »
I don't see any clock initialisation code

You have defined _XTAL_FREQ as 8 meg but nothing in your code show it being used.

How did you calculated the baud rate it's dependant on Fosc. You need to know Fosc to calculate the baud value
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9928
  • Country: nz
Re: PIC UART help needed
« Reply #3 on: February 25, 2014, 09:27:29 am »
yeah, baud doesnt look right, try 0x0C  or check the datasheet formula
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #4 on: February 25, 2014, 10:12:05 am »
I don't see any clock initialisation code

You have defined _XTAL_FREQ as 8 meg but nothing in your code show it being used.

How did you calculated the baud rate it's dependant on Fosc. You need to know Fosc to calculate the baud value

_XTAL_FREQ is required for __delay_ms(). I have used UART on this PIC before (I had written the functions for the UART myself then) & had used 0x1C at that time. According to the datasheet, for an 8MHz crystal attached to the PIC, this gives a bit rate of 9600bps.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #5 on: February 25, 2014, 10:14:01 am »
yeah, baud doesnt look right, try 0x0C  or check the datasheet formula

Okay will try that but I don't think thats the problem. I saw the example in the documentation for the XC8 compiler. They used 51 (gives 2400 bps for an 8Mhz crystal). That didn't work either.
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2184
  • Country: au
Re: PIC UART help needed
« Reply #6 on: February 25, 2014, 10:20:35 am »
...had used 0x1C at that time. According to the datasheet, for an 8MHz crystal attached to the PIC, this gives a bit rate of 9600bps.
hmm I must of missed it. I didn't see 28 (0x1C) in any of the tables in the data sheet for 8MHz
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #7 on: February 25, 2014, 10:25:16 am »
Nothing like a schematic to help a guy figure something out.

Nothing like knowing what's at the receiving end of all of this.

Nothing like a decent description of everything.

Three things that apparently don't exist...

The TX of the PIC is connect to the RX of a PICkit-2. So I am reading the values on the serial monitor provided with the PICkit application.
And I have 100nF capacitors between Vdd & Vss. My guess is that the problem is with the code & not the circuit since I used the same circuit to blink an LED before trying out the UART. Feel free to look through the schematic though.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #8 on: February 25, 2014, 10:28:20 am »
...had used 0x1C at that time. According to the datasheet, for an 8MHz crystal attached to the PIC, this gives a bit rate of 9600bps.
hmm I must of missed it. I didn't see 28 (0x1C) in any of the tables in the data sheet for 8MHz

Hey no wait its 0x0C. My bad. Let me try with 0x0C.
 

Offline diyaudio

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: PIC UART help needed
« Reply #9 on: February 25, 2014, 10:33:27 am »
/** P R O C E S S O R   C O N F I G U R E **************************************************/
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                       // CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30                        // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                    // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF, DEBUG=OFF        // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF                // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                            // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF

#define _XTAL_FREQ 8000000

void send_usart_data(unsigned char* data)
{
   /** USART  ****************************************************************/
   // 86 At 8Mhz of oscillator frequency & baud rate of 115200.
    CloseUSART();
    BAUDCONbits.BRG16 = 1;
    OpenUSART(USART_TX_INT_OFF &
              USART_RX_INT_OFF &
              USART_ASYNCH_MODE &
              USART_EIGHT_BIT &
              USART_CONT_RX &
              USART_BRGH_HIGH,
              86);  //86 for baud of 115200
    putrsUSART (data);
    CloseUSART();  //turn off usart if was previously on
}


I used putrsUSART (data); you seem to have used  WriteUSART('A'); Im using XC-8










 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #10 on: February 25, 2014, 10:35:34 am »
I used putrsUSART (data); you seem to have used  WriteUSART('A'); Im using XC-8

Yeah because I am only sending a byte of data at a time. putrsUART() is used to send multiple characters.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #11 on: February 25, 2014, 10:36:19 am »
Got it working. The baud rate was wrong as pointed out. Working with 0x0C. Thanks a lot guys.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC UART help needed
« Reply #12 on: February 25, 2014, 11:51:38 am »
UART is very slow. So you may want to figure out how to use the interrupt to free up the mcu when it is transmitting on uart.
================================
https://dannyelectronics.wordpress.com/
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #13 on: February 25, 2014, 11:54:30 am »
UART is very slow. So you may want to figure out how to use the interrupt to free up the mcu when it is transmitting on uart.

Yes I know how to use interrupts. My aim here was to try & get a feel of the peripheral libraries. That is why I wrote such a simple code.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC UART help needed
« Reply #14 on: February 25, 2014, 12:06:02 pm »
As to your code, you can calculate the baud rate value from the oscillator frequency and desired baud rate. The code would then work any user-specified baud rate.
================================
https://dannyelectronics.wordpress.com/
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC UART help needed
« Reply #15 on: February 25, 2014, 12:18:26 pm »
As to your code, you can calculate the baud rate value from the oscillator frequency and desired baud rate. The code would then work any user-specified baud rate.

Hmmm you are right. Thanks for the advice.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf