rfid和lcd与atmega16连接?

时间:2015-08-19 17:28:01

标签: c

我在rfid模块(em18)上做项目。问题是我无法在lcd display.it上显示卡号。它显示随机值(十六进制或其他一些但是对于同一张卡的常数)。我怎么能管理它并在液晶显示器(16 * 2)上显示卡的正确值(准确值)?

#define    F_CPU    16000000UL
#define    USART_BAUDRATE    9600
#define    BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include <avr/io.h>
#include <util/delay.h>
#define LCD_DATA PORTA        // LCD data port
#define ctrl PORTB
#define en PB1 // enable signal
#define rw PB2 // 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();

unsigned char i, card[10];
void getcard_id(void);
void LCD_display(void);

int main(void)
{
DDRA=0xff; // LCD_DATA port as output port
DDRB=0x07; // ctrl as out put
init_LCD(); // initialization of LCD
_delay_ms(50); // delay of 50 mili seconds
LCD_write_string("hello world!!!");
LCD_cmd(0xC0);
_delay_ms(50);
LCD_write_string("RFID PROJECT");
usart_init();        // initiailztion of USART

while(1) 
{
    getcard_id();    // Function to get RFID card no. from serial port
    LCD_cmd(0xC0);    // to go in second line and zeroth position on LCD
    LCD_display();        // a function to write RFID card no. on LCD
}
return 0;
}

void getcard_id(void)    //Function to get 10 byte ID no. from rfid card
{    
for(i=0;i<10;i++) 
{
    card[i]= usart_getch();    // receive card value byte by byte
}
return;``
}
unsigned int usart_getch()
{
while ((UCSRA & (1 << RXC)) == 0); // Do nothing until data have been       received..
// and is ready to be read from UDR
return(UDR); // return the byte
}
void usart_init() //USART initialization
{
UBRRH=00;
//UBRRL=77; 
UBRRL=103;// for 16mhz crystal
UCSRB |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and    reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes

UBRRL = BAUD_PRESCALE;

UBRRH = (BAUD_PRESCALE >> 8);
}

void LCD_display(void)    //Function for displaying ID no. on LCD
{
for(i=0;i<10;i++)
{
    LCD_write(card[i]);    // display card value byte by byte
}
return;
}

void init_LCD(void)
{
LCD_cmd(0x38);        //initializtion 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_ms(5);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(5);
return;
}

void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);

_delay_ms(5);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(5);            
return ;
}
void LCD_write_string(unsigned char *str)    // take address value of the    string in pointer *str
{
int i=0;
while(str[i]!='\0')        // loop will go on till the NULL characters is soon in string 
 {
    LCD_write(str[i]);    // sending data on LCD byte by byte
    i++;
}
return;
}

0 个答案:

没有答案