Skip to main content

Serial Communication Interrupts using pic18f

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 

Comments

Post a Comment

Popular posts from this blog

How to use SPI Debugger

  SPI Debugger Hi Friends, in this post, we will discuss how to use SPI Debugger available in proteus for serial peripheral interface (SPI) which is a built-in feature of PIC microcontroller to communicate and data exchange between PIC and other devices. SPI Debugger Introduction to SPI Guys, SPI can be considered as a programmable shift  register, The SPI module is a synchronous serial I/O port that shifts a serial bit stream of variable length and data rate between the PIC and other peripheral devices. Here “synchronous” means that the data transmission is synchronized to a clock signal. To avoid further delay, let's see some important connections and different configurations to effectively use SPI Debugger. How to use SPI Debugger in MASTER & SLAVE configuration Select the virtual instrument and from the instrument, list selects "SPI Debugger". SPI Debugger Insert two SPI Debugger on the working area, select any one of them,

PIC18f4580 Timer0 calculation using miKroC , CSS and proteus

PIC18f4580 TIMER PROGRAMMING The PIC18f4580 has four timers. they're named as Timers zero, one, two, and three. they will be used either as timers to come up with a time delay or as counters to count events happening outside the microcontroller. First, we see however Timers zero is employed to come up with time delays. Every timer wants a clock pulse to tick. The clock supply will be internal or external. If we have a tendency to use the inner clock supply, then 1/ fourth of the frequency of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is fed into the timer. Therefore, it's used for time delay generation and for that reason is termed a timer. By selecting the external clock choice, we have a tendency to feed pulses through one among the PIC18's pins: this is often known as a counter. GIF  taken from  https://exploreembedded.com Basic registers of the timer Majority of t timers in 18F are 16 bits wide. Because the PIC 18 has an 8- bit ar

Frequency Counter using Interrupts PIC18f4580 Project

Frequency Counter  using PIC18f458 0 Proje ct This POST describes the construction of small frequency counter with a cheap PIC18f4580 microcontroller with 16 x 2 LCD.   Prerequisites: PIC18F4580 TIMER Programming. PIC18F4580 COUNTER Programming. PIC18F4580 Interfacing with 16x2 LCD. PIC18F4580 Interrupts Programming  ( we will cover in this POST) Concept: Frequency  is the number of occurrences of a repeating event per unit time. in our case we will measure a number of clocks generated by clock source per unit time. In this project, LCD is used to display the frequency and PIC timer 1 to measure the input signal and Timer0 to generate an indication that one second has gone. System software utilizes Timer-1 in the 16-bit counter mode to count the input signal and overflows of the counter are added to provide the aggregate count in multiples of 65536. Totaling the existing value of the counter at the conclusion provides the full count.