//****************************************************************************************** // PIC16F877A Sample Program // Project : // Program Name : // Programer : // Create date : // History : // Laungage : XC8 C Laungage Version 2.20 //****************************************************************************************** // Description //****************************************************************************************** // #include //#pragma config FOSC = HS, WDTE = OFF, PWRTE = OFF, CP = OFF, BOREN = OFF, LVP = OFF, CPD = OFF, WRT = OFF, DEBUG = OFF //For 16F877 #pragma config FOSC = HS, WDTE = OFF, PWRTE = OFF, BOREN = OFF, LVP = OFF, CPD = OFF, WRT = OFF, DEBUG = OFF, CP = OFF //For 16F877A #define PI_NORMAL 0 #define PI_FAILER 1 void Init_usart(void); char Recive_usart(char *); char Send_usart(char); void Send_usart_s(char *); void main() { char status; char io_data; // ************************* // 1.Initialize // ************************* // (1)Initialize UART Init_usart(); // (2)Put Start messages Send_usart_s((char *)"PIC16F877A OK >>"); // ************************* // 2.Main loop // ************************* while(1){ status = Recive_usart(&io_data); status = Send_usart(io_data); }; } //****************************************************************************************** // Function Name : Init_UART // Title : // Input : // Output : //****************************************************************************************** void Init_usart(void) { //******************************************************* // 1.Define TXSTA(TRANSMIT STATUS AND CONTROL) Register // TXSTA[7] CSRC: Clock Source Select bit // Asynchronous mode: // Don?t care. // Synchronous mode: // 1 = Master mode (clock generated internally from BRG) // 0 = Slave mode (clock from external source) // TXSTA[6] TX9: 9-bit Transmit Enable bit // 1 = Selects 9-bit transmission // 0 = Selects 8-bit transmission // TXSTA[5] TXEN: Transmit Enable bit // 1 = Transmit enabled // 0 = Transmit disabled // Note: SREN/CREN overrides TXEN in Sync mode. // TXSTA[4] SYNC: USART Mode Select bit // 1 = Synchronous mode // 0 = Asynchronous mode // TXSTA[3] Unimplemented: Read as ?0? // TXSTA[2] BRGH: High Baud Rate Select bit // Asynchronous mode: // 1 = High speed // 0 = Low speed // Synchronous mode: // Unused in this mode. // TXSTA[1] TRMT: Transmit Shift Register Status bit // 1 = TSR empty // 0 = TSR full // TXSTA[0] TX9D: 9th bit of Transmit Data, can be Parity bit TXSTA = 0b00100100 ; // TXSTA = 0b00100000 ; //******************************************************* // 2.Define RCSTA(RECEIVE STATUS AND CONTROL) Register // RCSTA[7] SPEN: Serial Port Enable bit // 1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins) // 0 = Serial port disabled // RCSTA[6] RX9: 9-bit Receive Enable bit // 1 = Selects 9-bit reception // 0 = Selects 8-bit reception // RCSTA[5] SREN: Single Receive Enable bit // Asynchronous mode: // Don?t care. // Synchronous mode ? Master: // 1 = Enables single receive // 0 = Disables single receive // This bit is cleared after reception is complete. // Synchronous mode ? Slave: // Don?t care. // RCSTA[4] CREN: Continuous Receive Enable bit // Asynchronous mode: // 1 = Enables continuous receive // 0 = Disables continuous receive // Synchronous mode: // 1 = Enables continuous receive until enable bit CREN is cleared (CREN overrides SREN) // 0 = Disables continuous receive // RCSTA[3] ADDEN: Address Detect Enable bit // Asynchronous mode 9-bit (RX9 = 1): // 1 = Enables address detection, enables interrupt and load of the receive buffer when RSR<8> is set // 0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit // RCSTA[2] FERR: Framing Error bit // 1 = Framing error (can be updated by reading RCREG register and receive next valid byte) // 0 = No framing error // RCSTA[1] OERR: Overrun Error bit // 1 = Overrun error (can be cleared by clearing bit CREN) // 0 = No overrun error // RCSTA[0] RX9D: 9th bit of Received Data (can be parity bit but must be calculated by user firmware) RCSTA = 0b10010000; // ******************************************************* // 3.Define BRG(EUSART Baud Rate Generator Register) Register // ******************************************************* // BRG 20MHz, 9600BPS SPBRG = 129; // BRG = 16000000(Hz)/4/9600(BPS)-1 ==> 0d0416 ==> 0x1A0 // BRG = 20000000(Hz)/4/9600(BPS)-1 ==> 0d0520 ==> 0x208 // (1)Define SPBRGH(EUSART Baud Rate Generator Register, High Byte) Register // SPBRGH = 0x20; // (2)Define SPBRG(EUSART Baud Rate Generator Register, Low Byte) Register // SPBRG = 0x08; } //****************************************************************************************** // Function Name : Recive_UART // Title : // Input : // Output : //****************************************************************************************** char Recive_usart(char *io_data) { unsigned char status; // **************************** // 1.Check Recvive Flagunsigned char status; // **************************** PIR1 = PIR1 & 0b11011111; while(1){ status = PIR1 & 0b00100000 ; if(status != 0x00)break; } // **************************** // 2.Check error // **************************** status = RCSTA & 0x06; //0000,0110 RCSTA(FERR,OERR) if(status != 0x00){ *io_data = 0x00; return(PI_FAILER); } // **************************** // 3.Set Recive Data // **************************** *io_data = RCREG; return(PI_NORMAL); } //****************************************************************************************** // Function Name : SendUART_s // Title : Send UART String // Input : // Output : //****************************************************************************************** void Send_usart_s(char *put_string) { while(*put_string!=0x00){ Send_usart(*put_string); put_string++; } } //****************************************************************************************** // Function Name : Send_UART // Title : // Input : // Output : //****************************************************************************************** char Send_usart(char in_char) { char status; // *************************** // 1.Check Send Flag // *************************** while(1){ status = TXSTA & 0b00100010; //0000,0010 TXSTA(TRMT) if(status != 0x00)break; } // *************************** // 2.Set data // *************************** TXREG = in_char; // **************************** // 3.Check Flagunsigned char status; // **************************** PIR1 = PIR1 & 0b11101111; while(1){ status = PIR1 & 0b00010000 ; if(status != 0x00)break; } return(PI_NORMAL); }