Thứ Sáu, 14 tháng 12, 2018

RFID interfacing with AVR microcontroller (ATmega16) using interrupts

Developed By: 

Akshay Daga
This article covers how to extract and display the twelve byte unique tag ID received by RFID module on LCD using interrupt method. Before proceeding to this article readers must have knowledge of serial interrupt and LCD. In the previous article of RFID, polling method was used where the microcontroller was continuously monitoring the RXC flag. Keeping the microcontroller busy in monitoring the flag is not a good programming technique, so instead of polling method a programmer should prefer using interrupts.
Interfacing RFID with AVR microcontroller ATmega16 using interrupts Prototype



Since the output data of the RFID module uses RS232 protocol and is serial in nature, serial interrupt is used to receive the twelve byte unique ID.  Whenever an RFID tag comes in the proximity of the RFID reader module, the module transmits the twelve byte unique ID. Every time one byte of data is received, the controller is interrupted and the corresponding ISR gets executed, which stores the byte in a temporary variable and sends it to the LCD for display.
 
Since the length of the tag is twelve characters (and hence the serial interrupt will be generated twelve times one after the other) it must be noted that while defining ISR it should not be too long or consume more time. If ISR executes for a longer time then it may skip some data in case another interrupt is generated. In simple words the first ISR would not have executed completely and second ISR will be generated.  This would cause data loss.
 
The following is an ISR for the serial interrupt. When an interrupt is received, the variable get incremented and if the value of i is greater the 13, the incoming serial value will start writing from 0th position of second line. Since the unique id of the tag coming from RFID module is of twelve byte so, when the value of variable 'i' reaches 12 (i= 12) it means that one set of data is complete and the next coming value of data must be from next card.

ISR (USART_RXC_vect)
{
i++;
if(i==13)
{
i=1;
LCD_cmd(0xC0);
}
value = UDR;  // Put the received byte value into the variable "value"
LCD_write(value);    // write receive data to LCD
}

Circuit:

Circuit Diagram of RFID interfacing with AVR microcontroller (ATmega16) using interrupts
Code:
 //Program to receive a 12 byte string from RFID and display it on LCD using serial interrupt 
/*
LCD DATA port----PORT B
ctrl port------PORT D
rs-------PD0
rw-------PD1
en-------PD2
*/
 
#define F_CPU 12000000UL
 
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
 
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
 
#define LCD_DATA PORTA // LCD data port
#define ctrl PORTB
#define en PB2 // enable signal
#define rw PB1 // read/write signal
#define rs PB0 // register select signal
 
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void usart_init();
unsigned int usart_getch();
 
void LCD_write_string(unsigned char *str);
 
unsigned char value, i=0;
 
int main(void)
{
DDRA=0xff; // LCD_DATA port as output port
DDRB=0x07; // ctrl as out put
cli();
init_LCD(); // initialization of LCD
_delay_ms(50); // delay of 50 mili seconds
LCD_write_string("Unique ID No.");
LCD_cmd(0xC0);
usart_init(); // initialization of USART
sei();
while(1);
return 0;
}
 
void init_LCD(void)
{
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
 
LCD_cmd(0x01); // clear LCD
_delay_ms(1);
 
LCD_cmd(0x0E); // cursor ON
_delay_ms(1);
 
LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position
_delay_ms(1);
return;
}
 
void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_us(40);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
return;
}
 
void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_us(40);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_us(50); //delay to get things executed
return ;
}
 
void usart_init()
{
UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and..
// reception circuitry
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
UBRRL = BAUD_PRESCALE;  // Load lower 8-bits of the baud rate value.. 
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8);  // Load upper 8-bits of the baud rate value.. 
// into the high byte of the UBRR register
}
 
void LCD_write_string(unsigned char *str) //take address vaue of the string in pionter *str
{
int i=0;
while(str[i]!='\0') // loop will go on till the NULL charaters is soon in string 
{
LCD_write(str[i]); // sending data on CD byte by byte
i++;
}
return;
}
 
ISR (USART_RXC_vect)
{
i++;
if(i==13)
{
i=1;
LCD_cmd(0xC0);
}
value = UDR;  // Put the received byte value into the variable "value"
LCD_write(value);    // write receive data to LCD
}
 
 



Không có nhận xét nào:

Đăng nhận xét

Bài đăng mới nhất

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01 # candientu ,  # fujihatsu ,  # candientufujihatsu  #candientu,  # candientufujhatsu , #fuji...

Bài đăng phổ biến