The PIC16F877A has a single ISR function which is called every time some things generate an interrupt. Inside the ISR you have to check for individual flag bits to know which peripheral has generated that interrupt.
/*
* File: main.c
* Author: abhay
*
* Created on July 15, 2023, 11:48 PM
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 16000000
#include <xc.h>
#include <pic16f877a.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include<string.h>
void GPIO_init(void);
void UART_init(void) ;
void tx(unsigned char a);
unsigned char rx();
void txstr(unsigned char *s);
uint8_t RX_chr;
void clear_flag(void);
unsigned char msgACK[16] = "Received";
void __interrupt() myISR() {
if (RCIF == 1) {
if (RCSTAbits.OERR) {
CREN = 0;
NOP();
CREN = 1;
}
RX_chr = RCREG;
RCIF = 0;
}
}
/*
*
*/
int main(int argc, char** argv) {
GPIO_init();
UART_init();
/*
* Interrupt Setup START
*/
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
PIE1bits.TXIE = 0;
PIE1bits.RCIE = 1;
/*
* Interrupt Setup END
*/
while (1) {
if (RX_chr == '0') {
tx(RX_chr);
RD1 = 0 ; // LED OFF
txstr(msgACK);
clear_flag();
}
if (RX_chr == 'a') {
RD1 = 1; // LED ON
txstr(msgACK);
clear_flag();
}
}
return (EXIT_SUCCESS);
}
void clear_flag(void) {
RX_chr = 0;
}
void GPIO_init(void) {
TRISD &= ~(1 << 1);
RD1 = 0;
/*
* UART Pins Initialize
*/
TRISCbits.TRISC6 = 0; // TX
TRISCbits.TRISC7 = 1; //RX
}
void UART_init() {
TXSTAbits.CSRC = 0;
TXSTAbits.TX9 = 0;
TXSTAbits.TXEN = 1;
TXSTAbits.SYNC = 0;
/* BRGH - High Baud Rate Select Bit
* 1: High Speed
* 0: Low Speed
*/
TXSTAbits.BRGH = 1;
TXSTAbits.TRMT = 0;
TXSTAbits.TX9D = 0;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.SREN = 0;
RCSTAbits.CREN = 1;
RCSTAbits.ADDEN = 0;
RCSTAbits.FERR = 0;
RCSTAbits.OERR = 0;
RCSTAbits.RX9D = 0;
/*
* Baud Rate Formula
* Asynchronous
* Baud Rate = Fosc / (64 (x + 1))
* Baud Rate x (64 (x+1)) = FOSC
* SPBRG= (x) = ( Fosc/( Baud_Rate * 64 ) ) - 1
*/
SPBRG = 103; // Baud Rate 9600
TXIF = RCIF = 0;
}
void tx(unsigned char a) {
while (TXIF == 0); // Wait till the transmitter register becomes empty
TXIF = 0; // Clear transmitter flag
TXREG = a; // load the char to be transmitted into transmit reg
}
unsigned char rx() {
while (!RCIF);
RCIF = 0;
return RCREG;
}
void txstr(unsigned char *s) {
while (*s) {
tx(*s++);
__delay_us(10);
}
}