serial communication using PIC18f4550

5 posts / 0 new
Last post
Mayur Nirmal
Mayur_Nirmal's picture
Offline
Joined: 12/08/2011
Posts: 7
Points: 1960
serial communication using PIC18f4550

hi folks,

I am trying to communicate with PC using RS232 and PIC18F4550. I am using following circuit diagram as it is and code attached. But I am not getting desired output. I really can't understand what is the problem. Please help me as early as possible.

 

The Code I am exactly using is..

 

 

// Program to depict the configuration of EUSART in PIC18F4550
// This code receives and then transmits the same data back to the PC .. // ..through PC's Serial Port
#include<p18f4550.h>
#include<usart.h>
 
// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
   _FOSC_HS_1H,           // Select High Speed (HS) oscillator
   _WDT_OFF_2H,           // Watchdog Timer off
   MCLRE_ON_3H            // Master Clear on
*/
 
void tx_data(unsigned char);
 
unsigned char rx_data(void);
unsigned char serial_data;
unsigned int i=0;
 
#define FREQ 12000000    // Frequency = 12MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1)    // Refer to the formula for Baud rate calculation in Description tab
 
void main(void)
{
    SPBRG=spbrg_value;                                // Fill the SPBRG register to set the Baud Rate
    RCSTAbits.SPEN=1;                                     // To activate Serial port (TX and RX pins)
    TXSTAbits.TXEN=1;                                     // To enable transmission
    RCSTAbits.CREN=1;                                     // To enable continuous reception
    while(1)
    {
        serial_data=rx_data();    // Receive data from PC
        tx_data(serial_data);        // Transmit the same data back to PC
    }
}
 
 
void tx_data(unsigned char data1)
{
    TXREG=data1;                                     // Store data in Transmit register
    while(PIR1bits.TXIF==0);                             // Wait until TXIF gets low
}
 
 
unsigned char rx_data(void)
{
    while(PIR1bits.RCIF==0);                            // Wait until RCIF gets low
    return RCREG;                                   // Retrieve data from reception register
}
 
 
Please tell me what is the mistake and what should I do to get the desired output..!!!!
 
Also refer to the link as I am using it for my project..http://www.engineersgarage.com/embedded/pic-microcontroller-projects/eusart-circuit

SERIAL COMMUNICATION USING PIC18F4550

faizal
fa_a2z's picture
Offline
Joined: 16/10/2011
Posts: 9
Points: 1005

if your output is at the hyperterminal:

1) check com port baud rate

2) check if the port you're trying to connect is being used by other program.

3) check if your config bits is applied in the project setting

 

if you have access to oscilloscope:

1) probe the tx pin if its tranmitting

 

i have tested above code, and its working.

if its too hard, consider using the following built in function for mikroc:

 

char string[20];                                  //put any size here

int i=0;

USART_init(9600);                          //set baud rate, built in

for(i=0;i<sizeof(string);i++)

{

  USART_Write(string[i]);                //transmit each character one by one

}

USART_Write(0x0d);                     //put a carriage return after finishing transmission

hope this help :)

Anonymous
Anonymous's picture

 when i loaded my code, when i enterd any character through keybord the output is not in proper format , its giving like this  ---

                                                                                                                                                                                           --- .

                                                                                                                                                                                           ---

 

my code is : #include "C:\Program Files\PICC\Projects\New Folder\prad\UART.h"
#include <stdio.h>
//#include <18f452.h>
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=2000000)



void main(void)
{

unsigned char k;

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_oscillator(False);

  while(1)
  {
 k= fgetc();
 fputs(k);
 printf("41H");


   // TODO: USER CODE!!
};
}

srikanth
shubham436's picture
Offline
Joined: 03/05/2012
Posts: 11
Points: 55

when i loaded my code, when i enterd any character through keybord the output is not in proper format , its giving like this  ---

                                                                                                                                                                                           --- .

                                                                                                                                                                                           ---

 

my code is : #include "C:\Program Files\PICC\Projects\New Folder\prad\UART.h"
#include <stdio.h>
//#include <18f452.h>
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=2000000)



void main(void)
{

unsigned char k;

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_oscillator(False);

  while(1)
  {
 k= fgetc();
 fputs(k);
 printf("41H");


   // TODO: USER CODE!!
};
}

AJISH ALFRED
ajishalfred's picture
Offline
Joined: 10/01/2012
Posts: 790
Points: 3590

 

Faisal said your code is working. Then there might be some problems in your hardware. Check the following,

 

Short the wires coming from the pin 2 and 3 of DB9, type something on the hyperterminal and the same should be displayed.

 

Short pin no. 25 and 26 while, serially connected to the system. Type something on the hyperterminal and you should get the same displayed.

 

Check whether your crystal is 12mhz itself.

Login or register to post comments
You are here