本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
单片机型号:AVR ATMEGA16
显示:
#include <iom16v.h>
#include <macros.h>
#define RS_0 PORTD &= ~(1 << PD3)
#define RS_1 PORTD |= (1 << PD3)
#define RW_0 PORTD &= ~(1 << PD4)
#define RW_1 PORTD |= (1 << PD4)
#define EN_0 PORTD &= ~(1 << PD6)
#define EN_1 PORTD |= (1 << PD6)
//微秒级延时程序晶振8MHZ
void delay_us(int time)
{
do
{
time--;
}
while (time>1);
}
//毫秒级延时程序晶振8MHZ
void delay_ms(unsigned int time)
{
while(time!=0)
{
delay_us(1000);
time--;
}
}
/*显示屏命令写入函数*/
void LCD_write_com(unsigned char com)
{
RS_0;
RW_0;
PORTB = com;
EN_1;
delay_us(20);
EN_0;
}
/*显示屏命令写入函数*/
void LCD_write_data(unsigned char data) {
RS_1;
RW_0;
PORTB = data;
EN_1;
delay_us(200);
EN_0;
}
/*显示屏清空显示*/
void LCD_clear(void) {
LCD_write_com(0x01);
delay_ms(5);
}
/*显示屏字符串写入函数*/
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0) {
LCD_write_com(0x80 + x);
}
else {
LCD_write_com(0xC0 + x);
}
while (*s)
{
LCD_write_data( *s);
s ++;
}
}
/*显示屏单字符写入函数*/
void LCD_write_char(unsigned char x,unsigned char y,unsigned char data)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
LCD_write_data( data);
}
/*显示屏初始化函数*/
void LCD_init(void) {
DDRB = 0xFF; /*I/O口方向设置*/
DDRD |= (1 << PD3) | (1 << PD4) | (1 << PD6);
LCD_write_com(0x38); /*显示模式设置*/
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
LCD_write_com(0x08); /*显示关闭*/
LCD_write_com(0x01); /*显示清屏*/
LCD_write_com(0x06); /*显示光标移动设置*/
delay_ms(5);
LCD_write_com(0x0C); /*显示开及光标设置*/
}
void main(void) {
unsigned char i;
unsigned char *p;
PORTA = 0xFF; /*打开上拉*/
DDRA = 0x00; /*方向输入*/
PORTB = 0xFF; /*电平设置*/
DDRB = 0xFF; /*方向输出*/
PORTC = 0x7F;
DDRC = 0x80;
PORTD = 0xFF;
DDRD = 0x00;
delay_ms(100);
LCD_init();
while (1)
{
i = 1;
p = "yixiangongren";
//LCD_clear();
LCD_write_str(1,0,"www.eehome.cn");
delay_ms(50);
while (*p)
{
LCD_write_char(i,1,*p);
i ++;
p ++;
//delay_ms(50);
}
delay_ms(500);
}
} |