well thanks engshahrul for sharing your code with me.
Now I must tell you guys that my problem of communicating with the UART has been solved with my code .
The code is here:
Code:
#include <pic.h>
//#define OSCILLATOR (OSCCON = 0x71)
#define _XTAL_FREQ 4000000
#define BAUD 2400
#define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -1))
__CONFIG(WDTE_OFF & FOSC_INTOSCCLK & MCLRE_ON);
void EUSART_init(void)
{
// Set the baud rate
SPBRG = DIVIDER;
TXSTAbits.SYNC = 0; // asynchronous mode
TXSTAbits.BRGH = 1; // low baud rate
BAUDCTLbits.BRG16 = 0; // 8 bit baud rate generator
// Disable all interrupts
INTCONbits.GIE = 0;
INTCONbits.PEIE = 0;
// Only 8-bit mode for the receiver
RCSTAbits.RX9 = 0;
// Enable the transmitter and receiver
TXSTAbits.TXEN = 1;
RCSTAbits.CREN = 1;
// Enable the Serial communication
RCSTAbits.SPEN = 1;
}
void EUSART_transmit_data(unsigned char data)
{
while(!TXIF)
continue;
NOP();
_nop();
_nop();
TXREG = data;
return;
}
unsigned char EUSART_recieve_data()
{
unsigned char data;
// RCREG = data;
/* if(FERR==1)
{
RCSTAbits.SPEN = 0;
EUSART_receiver_init();
}*/
while(!RCIF)
continue;
data = RCREG;
// return RCREG;
return data ;
}
void main(void) {
unsigned char x;
OSCCON = 0x61; /// has choosen 4 MHz internal clock
// Initialise the portc bits
TRISCbits.TRISC2 = 0; // LED connected to this pin for output
TRISCbits.TRISC3 = 0; // LED connected to this pin for output
TRISCbits.TRISC4 = 0; // transmitter pin for output
TRISCbits.TRISC5 = 1; // the receiver input pin
// Disable the analog inputs
ANSEL = 0x00;
// Disable the comparator
CMCON0 = 0x07;
EUSART_init();
while(1)
x = EUSART_recieve_data();
if(x=='g')
{
PORTCbits.RC2 = 1;
__delay_ms(1000);
}
else if (x=='a')
{
PORTCbits.RC3 = 1;
__delay_ms(1000);
}
else
{
RC2 = 0;
RC3 = 0;
__delay_ms(1000);
RC2 = 1;
RC3 = 1;
__delay_ms(1000);
RC2 = 0;
RC3 = 0;
__delay_ms(1000);
}
EUSART_transmit_data(x);
__delay_ms(10);
}
}
This is my code which I have also posted earlier as you can see I have not changed the main receiver and transmitter function codes and all of the main is also same just few changes of adding couple of delays and nothing much rest of the working is almost same.
But now the whole code is running fine and working fine.
Actually freinds I want to know that why my this code was not running well .
Let me tell you what I observed when I was testing:
The code was not showing the received value well that is it was not able to receive the code in correct format whereas the receiver code is well and good and have been check many times , same thing is with the transmitter as well.
But now all of sudden same code has started working well , which was showing error before writing this article here........................
But now my freinds I am not still able to find out that why the code was not working well at that time and why it has started working in good manner now.
Please you guys if have some suggestion or reason for the pic16f688 with HiTech compiler in the MPLAB X IDE and with the bray terminal showing this unusual and strange behaviour , than please do tell me the reason for it.
Thank you