⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 -ͦ

📁 手把手教你学AVR单片机C程序设计实验程序
💻
字号:
#include <iom16.h>
#include<intrinsics.h>
#define xtal 8
#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
//******************************
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 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(0x38,0);
Delay_nms(5);
LcdWriteCommand(0x38,0);
Delay_nms(5);
LcdWriteCommand(0x38,0);
Delay_nms(5);
LcdWriteCommand(0x38,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;
}
//***************************************
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;
}
//****************************************
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 + -