本例液晶屏基于HD44780近制芯片,它连接单片机串口,显示单片机串口所发送的字符信息。
程序执行时串行液晶上显示:Serial LCD DEMO 当光标在第二行闪烁时,虚拟终端中输入的字符显示
在LCD上,按下退格键进光标左移,按回车键时清屏
Proteus截图:
Atmel Studio6.2运行时截图:
程序清单:
/*
* GccApplication28.c
*
* Created: 2014-12-5 20:39:58
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
void Init_USART()
{
UCSRB = _BV(RXEN)| _BV(TXEN)|_BV(RXCIE);//允许接收和发送,接收中断使能
UCSRC = _BV(URSEL)| _BV(UCSZ1)| _BV(UCSZ0);
UBRRL = (F_CPU/9600/16 - 1) % 256;
UBRRH = (F_CPU/9600/16 - 1) /256;
}
void PutChar(char c)
{
if( c == '\n') PutChar('\r');
UDR = c;
while(!(UCSRA & _BV(UDRE)));
}
void PutStr(char *s)
{
uint8_t i = 0;
while(s[i] !='\0')
{
PutChar(s[i++]);
_delay_ms(5);
}
}
void Write_LCD_COMMAND(uint8_t comm)
{
PutChar(0xFE);
PutChar(comm);
}
int main(void)
{
Init_USART();
sei();
_delay_ms(300);
PutStr(" Serial LCD DEMO ");
Write_LCD_COMMAND(0xC0);
Write_LCD_COMMAND(0x0d);
while(1);
}
ISR(USART_RXC_vect)
{
uint8_t c = UDR;
if(c == 0x0D)
{
Write_LCD_COMMAND(0x01);
return;
}
if(c == 0x08)
{
Write_LCD_COMMAND(0x10);
return;
}
PutChar(c);
}
|