Pran Kumar
Newbie level 5
can anyone help me to write a user defined function to transmit data via usart in pic 16f877a,20mhz in mikro c?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 void USART_init(); void USART_trans_char(unsigned char x); void USART_trans_string(char *z); char AT[] = "AT\r\n"; void main() { USART_init(); Delay_ms(200); while(1) { USART_trans_string(AT); Delay_ms(1000); } } void USART_init() { TXEN_bit=1; SYN_bit=0; SPEN_bit=1; // SPBRG=0x1F; //9600 bps ,DECIMAL VALUE IN REGISTER 31 TRISC.B6=1; //RC7 is Rx while RC6 is Tx } void USART_trans_char( char x) { while(TRMT!==0) TXREG=x; } void USART_trans_string(char* z) { while(*z) { USART_trans_char(*z++); } }
so you may also have to make TRISC.B7 = 1.Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
set in order to configure pins RC6/TX/CK and RC7/RX/DT
as the Universal Synchronous Asynchronous Receiver
Transmitter.
void USART_init()
{
TXEN_bit=1;
SYN_bit=0;
SPEN_bit=1; //
SPBRG=0x1F; //9600 bps ,DECIMAL VALUE IN REGISTER 31
TRISC.B6=1; //RC7 is Rx while RC6 is Tx
}
UART_Init(9600);
void USART_init();
void USART_trans_char( char x);
void USART_trans_string(char *z);
char AT[] = "AT\r\n";
void main()
{
USART_init();
Delay_ms(200);
while(1)
{
USART_trans_string(AT);
Delay_ms(1000);
}
}
void USART_init()
{
CSRC_bit=1;
TX9_bit=0;
SYN_bit=0;
BRGH_bit=0;
SPBRG=0x1F;
TRISC.B6=0;
TXEN_bit=1;
}
void USART_trans_char( char x)
{
while(TRMT==0)
TXREG=x;
}
void USART_trans_string(char *z)
{
while(*z)
{
USART_trans_char(*z++);
}
}
#define _XTAL_FREQ 20000000
#define BAUDRATE 9600
void InitUART(void);
void SendByteSerially(unsigned char);
unsigned char ReceiveByteSerially(void);
void SendStringSerially(const unsigned char*);
// Main Function
void main(void)
{
InitUART(); // Intialize UART
SendStringSerially("AT"); // Send string on UART
INTCON.B7 = 1; // Enable global interrupts
INTCON.B6 = 1; // Enable Peripheral Interrupts
while(1)
{
// Do nothing, as Received character is echoed back in the ISR
// If you decide to disable interrupts, then you can
// echo back characters by uncommenting the line below
// SendByteSerially(ReceiveByteSerially()); //Echo Back
}
}
void InitUART()
{
TRISC.B6 = 0; // TX Pin
TRISC.B7 = 1; // RX Pin
SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
TXSTA.B2 = 1; // Fast baudrate
TXSTA.B4 = 0; // Asynchronous
RCSTA.B7 = 1; // Enable serial port pins
RCSTA.B4 = 1; // Enable reception
RCSTA.B5 = 0; // No effect
PIE1.B4 = 0; // Disable tx interrupts
PIE1.B5 = 1; // Enable rx interrupts
TXSTA.B0 = 0; // 8-bit transmission
RCSTA.B6 = 0; // 8-bit reception
TXSTA.B5 = 0; // Reset transmitter
TXSTA.B5 = 1; // Enable the transmitter
}
void SendByteSerially(unsigned char Byte) // Writes a character to the serial port
{
while(!PIR1.B4) // wait for previous transmission to finish
TXREG = Byte;
}
unsigned char ReceiveByteSerially( ) // Reads a character from the serial port
{
if(RCSTA.B1) // If over run error, then reset the receiver
{
RCSTA.B4 = 0;
RCSTA.B4 = 1;
}
while(!PIR1.B5) // Wait for transmission to receive
return (RCREG);
}
void SendStringSerially(const unsigned char *st)
{
while(*st)
{
void SendByteSerially(unsigned char *st);
st++;
}
}
void interrupt( )
{
if(PIR1.B5) // If UART Rx Interrupt
{
if(RCSTA.B1) // If over run error, then reset the receiver
{
RCSTA.B4 = 0;
RCSTA.B4 = 1;
}
SendByteSerially(RCREG); // Echo back received char
}
}
void interrupt( )
{
if(PIR1.B5) // If UART Rx Interrupt
{
if(RCSTA.B1) // If over run error, then reset the receiver
{
RCSTA.B4 = 0;
RCSTA.B4 = 1;
}
SendByteSerially(RCREG); // Echo back received char
}
}
void interrupt( )
{
char c1;
if((RCIF_bit) && (RCIE_bit) // If UART Rx Interrupt flag + Uart interrupt authorised
{
if(RCSTA.B1) // If over run error, then reset the receiver
{
RCSTA.B4 = 0;
RCSTA.B4 = 1;
}
c1=RCREG;
SendByteSerially(c1); // Echo back received char
}
}
while(!PIR1.TXIF) // wait for previous transmission to finish
TXREG = Byte;