PIC Serial Communication using Interrupt
In my Last Post we studied the serial communication of the PIC18. All sample programs in that Post used the polling method.
In this Post we explore interrupt based serial communication, which allows the PIC18 to do many things, in addition to sending and receiving data from the serial communication port.
RCIF and TXIF flags and interrupts
TXIF (transfer interrupt) is raised when the last bit of the framed data, the stop bit, is transferred, indicating that the TXREG register is ready to transfer the next byte. RCIF (received interrupt) is raised when the entire frame of data, including the stop bit, is received.In other words, when the RCREG register has a byte, RCIF is raised to indicate that the received byte needs to be picked up before it is lost (overrun) by new incoming serial data.
As far as serial communication is concerned, all the concepts which we have discussed in Basic of serial communication apply equally when using either polling or an interrupt. The only difference is in how the serial communication needs are served.
In the polling method, we wait for the flag (TXIF or RCIF) to be raised; while we wait we cannot do anything else.
In the interrupt method, we are notified when the PIC18 has received a byte, or is ready to send the next byte; we can do other things while the serial communication needs are served.
In the PIC 18 two interrupts are set aside for serial communication. One
interrupt is used for send and the other for receive. If the corresponding interrupt bit of TXIE or RCIE is enabled, when TXIF or RCIF is raised the PIC 18 gets interrupted and jumps to memory address location 0008H to execute the ISR.
Why we Use Interrupt Specially in Receiving Data?
Generally in various applications, the serial interrupt is used mainly for receiving data and is seldom used for sending data serially. This is like receiving a telephone call, where we need a ring to be notified of an incoming call. If we need to make a phone call there are other ways to remind ourselves and so no need for ringing.
In receiving the phone call, however, we must respond immediately no matter what we are doing or we will miss the call. Similarly, we use the serial interrupt to receive incoming data so that it is not lost. Look at the example below. Notice that the role of PEIE (PEripheral Interrupt Enable) in allowing serial communication interrupts and other interrupts to come in. This is in addition to the GIE bit discussed in my Post of Frequency Counter.
Lets write a program , the PIC18 gets data(12345) from user by using Virtual Terminal and if the data is correct the LED will be ON.
Assume crystal frequency = 8 MHz and the baud rate= 9600. Proteus Simulation
miKroC Code:
signed int receive[10] ;
unsigned char nn,result;
void Interrupt (void)
{
nn++;
receive[nn]=UART1_Read();
RCIF_bit = 0; //Flag bit, RCIF, will be set
when reception is // complete
if (nn==6)
{
nn=0;
}
}
void main()
{
TRISA = 0;
porta =0;
TRISB = 0;
portb =0;
TRISD = 0;
portd = 0;
ADCON1=0xff;
CMCON = 0x07;
GIE_bit = 1; // Global Interrupt Enable bit
RCIE_bit = 1; // EUSART Receive Interrupt Enable bit
GIEL_bit=1; //Peripheral Interrupt Enable bit
UART1_Init(9600);
delay_ms(100);
nn= 0;
UART1_Write_Text("Enter Code to Turn ON Led\r");
while(1)
{
if(receive[1]==(1+48) &&receive[2]==(2+48) &&
receive[3]==(3+48) && receive[4]== (4+48) &&
receive[5]==(5+48))
{
portb.rb0 = 1;
}
else
{
portb.rb0 = 0;
}
}
}
CCS Code:
int8 receive[10] ;
int8 nn,result;
#int_RDA
void RDA_isr(void)
{
nn++;
receive[nn]=getc();
if (nn==6)
{
nn=0;
}
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
printf("Enter Code to Turn ON Led\r");
while(1)
{
if(receive[1]==(1+48)
&&receive[2]==(2+48) && receive[3]==(3+48) &&
receive[4]== (4+48) && receive[5]==(5+48))
{
output_bit( PIN_B0, 1);
}
else
{
output_bit( PIN_B0, 0);
}
}
}
Friends for further learning about PIC18 checkout
And transmit code? Help
ReplyDelete