#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
#define LCM_CS_1 PORTB |= (1 << 2)
#define LCM_CS_0 PORTB &= ~(1 << 2)
#define LCM_SID_1 PORTB |= (1 << 3)
#define LCM_SID_0 PORTB &= ~(1 << 3)
#define LCM_SCLK_1 PORTB |= (1 << 4)
#define LCM_SCLK_0 PORTB &= ~(1 << 4)
//延时函数
void delay(uint a)
{
uint x,y;
for(x=0;x<1140;x++)
{
for(y=0;y<a;y++);
}
}
void delay_us(uchar i)
{
while(i--);
}
//串行数据传送
void SandByte(uchar bye)
{
int t;
for(t=8;t>0;t--)
{
LCM_SCLK_0;
if(bye & BIT(t-1)) LCM_SID_1;
else LCM_SID_0;
delay_us(2);
LCM_SCLK_1;
}
}
//写指令函数
void write_com(uchar com)
{
LCM_CS_1;
SandByte(0xf8);
delay_us(5);
SandByte(com&0xf0);
delay_us(5);
SandByte(com << 4);
delay_us(5);
LCM_CS_0;
}
//写数据函数
void write_data(uchar data)
{
LCM_CS_1;
SandByte(0xfa);
delay_us(5);
SandByte(data&0xf0);
delay_us(5);
SandByte(data << 4);
delay_us(5);
LCM_CS_0;
}
//在x行y列写汉字
void lcm_pos(uchar y,uchar x) // x:0~3;y:0~7
{
switch(y)
{
case 0: write_com(0x80+x);break;
case 1: write_com(0x90+x);break;
case 2: write_com(0x88+x);break;
case 3: write_com(0x98+x);break;
}
}
//写汉字函数
void write_hz(uchar *str)
{
while(*str>0)
{
write_data(*str);
str++;
}
}
//初始化 LCM
void init_lcm()
{
write_com(0x30); //功能设定:8位接口控制位,基本指令集
delay(5);
write_com(0x0c); //显示开关设置:整体显示开,游标显示关,正常显示
delay(5);
write_com(0x01); //清除显示
delay(5);
}
void main()
{
DDRB = 0XFF;
PORTB = 0X00;
init_lcm();
delay(10);
while(1)
{
lcm_pos(0,0);
write_hz("信春哥 ");
lcm_pos(0,4);
write_hz("得永生!");
lcm_pos(1,0);
write_hz("chunge520.com");
lcm_pos(2,0);
write_hz("今日你春哥了吗?");
}
}
文章评论(0条评论)
登录后参与讨论