Microcontroller › PIC › serial communication using PIC18f4550
- This topic has 4 replies, 5 voices, and was last updated 12 years, 4 months ago by AJISH ALFRED.
-
AuthorPosts
-
April 6, 2012 at 6:24 pm #4767Mayur NirmalParticipant
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 offMCLRE_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 tabvoid main(void){SPBRG=spbrg_value; // Fill the SPBRG register to set the Baud RateRCSTAbits.SPEN=1; // To activate Serial port (TX and RX pins)TXSTAbits.TXEN=1; // To enable transmissionRCSTAbits.CREN=1; // To enable continuous receptionwhile(1){serial_data=rx_data(); // Receive data from PCtx_data(serial_data); // Transmit the same data back to PC}}void tx_data(unsigned char data1){TXREG=data1; // Store data in Transmit registerwhile(PIR1bits.TXIF==0); // Wait until TXIF gets low}unsigned char rx_data(void){while(PIR1bits.RCIF==0); // Wait until RCIF gets lowreturn 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-circuitApril 17, 2012 at 6:58 am #7470faizalParticipantif 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); //transmit each character one by one
}
USART_Write(0x0d); //put a carriage return after finishing transmission
hope this help
May 17, 2012 at 10:50 am #7843AnonymousGuestwhen 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 FilesPICCProjectsNew FolderpradUART.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!!
};
}May 17, 2012 at 10:51 am #7844srikanthParticipantwhen 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 FilesPICCProjectsNew FolderpradUART.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!!
};
}May 18, 2012 at 5:58 pm #7852AJISH ALFREDParticipantFaisal 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.
-
AuthorPosts
- You must be logged in to reply to this topic.