程序如下
#include<iom16v.h>
#include<macros.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
//*******延时函数*******//
void delay(uint MS) //约为1MS的延时函数
{
uint i,j;
for(i=0;i<MS;i++)
for(j=0;j<1141;j++); //1141是在8MHz晶振下为MS毫秒
}
//******1602写地址********//
void write_com(uchar com)
{
PORTD&=~BIT(5); // RS=0
PORTD&=~BIT(6); //RW=0
PORTC=com; //送地址
PORTD|=BIT(7); //EN=1
delay(1);
PORTD&=~BIT(7); //EN=0
}
//******1602写数据********//
void write_dat(uchar dat)
{
PORTD|=BIT(5); // RS=1
PORTD&=~BIT(6); //RW=0
PORTC=dat; //送数据
PORTD|=BIT(7); //EN=1
delay(1);
PORTD&=~BIT(7); //EN=0
}
//********1602初始化*********//
void LCD_init()
{
DDRC=0XFF;
DDRD=0xFF;
DDRD|=BIT(5)|BIT(6)|BIT(7);
PORTD&=~BIT(7);
write_com(0X38);
delay(5);
write_com(0X01);
delay(5);
write_com(0X0c);
delay(5);
write_com(0X06);
delay(5);
}
void main()
{
LCD_init();
while(1)
{
write_com(0x80);
write_dat(1+0x30);
}
}
|