📄 -ͦ
字号:
#include <iom16.h>
#include<intrinsics.h>
#define PB0 0
#define PB1 1
#define PB2 2
//----------------------------
#define uchar unsigned char
#define uint unsigned int
#define SET_BIT(x,y) (x|=(1<<y))
#define CLR_BIT(x,y) (x&=~(1<<y))
#define GET_BIT(x,y) (x&(1<<y))
//----------------------------
#define LCM_RS_1 SET_BIT(PORTB,PB0)
#define LCM_RS_0 CLR_BIT(PORTB,PB0)
#define LCM_RW_1 SET_BIT(PORTB,PB1)
#define LCM_RW_0 CLR_BIT(PORTB,PB1)
#define LCM_EN_1 SET_BIT(PORTB,PB2)
#define LCM_EN_0 CLR_BIT(PORTB,PB2)
//------------------------------
#define DataPort PORTA
#define Busy 0x80
#define xtal 8
//---------------------------------
uchar __flash exampl[]="--ATmega16(L)-- Testing...... \n";
void Delay_1ms(void);
void Delay_nms(uint n);
void WaitForEnable(void);
void LcdWriteData(uchar W);
void LcdWriteCommand(uchar CMD,uchar Attribc);
void InitLcd(void);
void DisplayLine2(uchar dd);
void DisplayOneChar(uchar x,uchar y,uchar Wdata);
void ePutstr(uchar x,uchar y,uchar __flash *ptr);
//******************************************
void main(void)
{
uchar temp;
Delay_nms(400);
DDRA=0xf0;PORTA=0x00;
DDRB=0xff;PORTB=0x00;
InitLcd();
temp=32;
ePutstr(0,0,exampl);
Delay_nms(3200);
while(1)
{
temp&=0x7f;
if(temp<32)temp=32;
DisplayLine2(temp++);
Delay_nms(400);
}
}
//************************************
void ePutstr(uchar x,uchar y,uchar __flash *ptr)
{
uchar i,l=0;
while(ptr[l]>31){l++;}
for(i=0;i<l;i++){
DisplayOneChar(x++,y,ptr[i]);
if(x==16){
x=0;y^=1;
}
}
}
//*************************************
void DisplayLine2(uchar dd)
{
uchar i;
for(i=0;i<16;i++){
DisplayOneChar(i,1,dd++);
dd&=0x7f;
if(dd<32)dd=32;
}
}
//*************************************
void LocateXY(char posx,char posy)
{
uchar temp=0;
temp&=0x7f;
temp=posx&0x0f;
posy&=0x01;
if(posy)temp|=0x40;
temp|=0x80;
LcdWriteCommand(temp,1);
}
//**************************************
void DisplayOneChar(uchar x,uchar y,uchar Wdata)
{
LocateXY(x,y);
LcdWriteData(Wdata);
}
//****************************************
void InitLcd(void)
{
LcdWriteCommand(0x28,0);
Delay_nms(5);
LcdWriteCommand(0x28,0);
Delay_nms(5);
LcdWriteCommand(0x28,0);
Delay_nms(5);
LcdWriteCommand(0x28,1);
LcdWriteCommand(0x08,1);
LcdWriteCommand(0x01,1);
LcdWriteCommand(0x06,1);
LcdWriteCommand(0x0c,1);
}
//****************************************
void LcdWriteCommand(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS_0;LCM_RW_0;__no_operation();
DataPort=CMD;__no_operation();
LCM_EN_1;__no_operation();__no_operation();LCM_EN_0;
if(Attribc)WaitForEnable();
LCM_RS_0;LCM_RW_0;__no_operation();
DataPort=(CMD<<4);__no_operation();
LCM_EN_1;__no_operation();__no_operation();LCM_EN_0;
}
//***************************************
void LcdWriteData(uchar dataW)
{
WaitForEnable();
LCM_RS_1;LCM_RW_0;__no_operation();
DataPort=dataW;__no_operation();
LCM_EN_1;__no_operation();__no_operation();LCM_EN_0;
WaitForEnable();
LCM_RS_1;LCM_RW_0;__no_operation();
DataPort=(dataW<<4);__no_operation();
LCM_EN_1;__no_operation();__no_operation();LCM_EN_0;
}
//****************************************
void WaitForEnable(void)
{
uchar val;
DataPort=0xff;
LCM_RS_0;__no_operation();__no_operation();
LCM_RW_1;__no_operation();__no_operation();
LCM_EN_1;__no_operation();__no_operation();
DDRA=0x00;__no_operation();__no_operation();
val=PINA;__no_operation();__no_operation();
while(val&Busy){val=PINA;__no_operation();__no_operation();}
__no_operation();__no_operation();
LCM_EN_0;__no_operation();__no_operation();
DDRA=0xff;
}
//****************************************
void Delay_1ms(void)
{ uint i;
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
//=============================================
void Delay_nms(uint n)
{
uint i=0;
while(i<n)
{Delay_1ms();
i++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -