📄 lcd.c
字号:
#include<spce061a_chf.h>
#define Uint unsigned int
typedef struct
{
Uint bit0 : 1;
Uint bit1 : 1;
Uint bit2 : 1;
Uint bit3 : 1;
Uint bit4 : 1;
Uint bit5 : 1;
Uint bit6 : 1;
Uint bit7 : 1;
Uint bit8 : 1;
Uint bit9 : 1;
Uint bit10 : 1;
Uint bit11 : 1;
Uint bit12 : 1;
Uint bit13 : 1;
Uint bit14 : 1;
Uint bit15 : 1;
}Bit;
typedef struct
{
Uint bytel : 8;
Uint byteh : 8;
}Byte;
typedef union
{
Bit bit;
Byte byte;
Uint port;
}UNport;
#define PA ((volatile UNport *)(0x7000))
#define PA_Buffer ((volatile UNport *)(0x7001))
#define PA_Dir ((volatile UNport *)(0x7002))
#define PA_Attrib ((volatile UNport *)(0x7003))
#define PA_Latch ((volatile UNport *)(0x7004))
#define PB ((volatile UNport *)(0x7005))
#define PB_Buffer ((volatile UNport *)(0x7006))
#define PB_Dir ((volatile UNport *)(0x7007))
#define PB_Attrib ((volatile UNport *)(0x7008))
#define P0_0 PA->bit.bit0
#define P0_1 PA->bit.bit1
#define P0_2 PA->bit.bit2
#define P0_3 PA->bit.bit3
#define P0_4 PA->bit.bit4
#define P0_5 PA->bit.bit5
#define P0_6 PA->bit.bit6
#define P0_7 PA->bit.bit7
#define P0_8 PA->bit.bit8
#define P0_9 PA->bit.bit9
#define P0_10 PA->bit.bit10
#define P0_11 PA->bit.bit11
#define P0_12 PA->bit.bit12
#define P0_13 PA->bit.bit13
#define P0_14 PA->bit.bit14
#define P0_15 PA->bit.bit15
#define P1_0 PB->bit.bit0
#define P1_1 PB->bit.bit1
#define P1_2 PB->bit.bit2
#define P1_3 PB->bit.bit3
#define P1_4 PB->bit.bit4
#define P1_5 PB->bit.bit5
#define P1_6 PB->bit.bit6
#define P1_7 PB->bit.bit7
#define P1_8 PB->bit.bit8
#define P1_9 PB->bit.bit9
#define P1_10 PB->bit.bit10
#define P1_11 PB->bit.bit11
#define P1_12 PB->bit.bit12
#define P1_13 PB->bit.bit13
#define P1_14 PB->bit.bit14
#define P1_15 PB->bit.bit15
void write_command(unsigned int);//写命令函数
void write_data(unsigned int);//写数据函数
unsigned int read_data();//读出给定地址的数据
void check_busy(void);//检测busy信号的函数
void lce_intit();//lcd初始化函数
void lcd_set();//lcd显示格式的设置
void delay15(void);//延时15ms
void delay5(void);//延时5ms
unsigned int value1[16]="AAyeguiyou002AAA";
unsigned int value2[16]="AAAAAAAAAAAAAAA0";
int main()
{
unsigned int k;
unsigned int q;
lcd_intit();
lcd_set();
check_busy();
write_command(0x80);//写入显示的地址第一行
for(k=0;k<16;k++)//写入数据
{
check_busy();
write_data(value1[k]);
}
/*check_busy();
write_data(0x41);*/
check_busy();
write_command(0xc0);//写入显示的地址地二行
for(k=0;k<16;k++)//写入数据
{
check_busy();
write_data(value2[k]);
}
/*check_busy();
write_command(0xc0);//写入显示的地址地二行
check_busy();
write_data(0x41);*/
check_busy();
write_command(0x89);//要读出00x88地址的数据;
//check_busy();
q=read_data();
while(1)
{
Watchdog_Clear();
}
}
void lcd_intit(void)
{
delay15();
write_command(0x0038);
delay5();
write_command(0x0038);
delay5();
write_command(0x0038);
}
void lcd_set(void)
{
check_busy();
write_command(0x0038);//显示模式的设置
check_busy();
write_command(0x0008);//显示关闭*/
check_busy();
write_command(0x00001); //显示清屏
check_busy();
write_command(0x0006);//显示光标移动设置
check_busy();
write_command(0x000c);//显示开及光标设置
}
void write_command(unsigned int command)
{
Set_IOA_Dir(0x00ff);
Set_IOA_Attrib(0x00ff);
Set_IOA_Data(command);
Set_IOB_Dir(0x0007);
Set_IOB_Attrib(0x0007);
P1_0=0;//rs=0
P1_1=0;//rw=0
P1_2=0;//E=0
asm("nop");
asm("nop");
P1_2=1;//E=1;
asm("nop");
asm("nop");
}
void write_data(unsigned int data)
{
Set_IOA_Dir(0x00ff);
Set_IOA_Attrib(0x00ff);
Set_IOA_Data(data);
Set_IOB_Dir(0x0007);
Set_IOB_Attrib(0x0007);
P1_0=1;//rs=1
P1_1=0;//rw=0
P1_2=0;//E=0
asm("nop");
asm("nop");
P1_2=1;//E=1;
asm("nop");
asm("nop");
}
unsigned int read_data(void)
{
unsigned int add;
Set_IOA_Dir(0x0000);
Set_IOA_Attrib(0x00ff);
Set_IOA_Data(0x00ff);//下拉式输入
Set_IOB_Dir(0x0007);
Set_IOB_Attrib(0x0007);
P1_0=1;//rs=1
P1_1=0;//rw=1
P1_2=0;//E=0
asm("nop");
asm("nop");
P1_2=1;//E=1;
asm("nop");
asm("nop");
add=Get_IOA_Data();
return(add);
}
void check_busy()
{
unsigned int c;
Set_IOA_Dir(0x0000);
Set_IOA_Attrib(0x00ff);
Set_IOA_Data(0x00ff);//下拉式输入
do
{
Watchdog_Clear();
Set_IOB_Dir(0x0007);
Set_IOB_Attrib(0x0007);
P1_0=0;//rs=0
P1_1=1;//rw=1
P1_2=0;//E=0
asm("nop");
asm("nop");
P1_2=1;//E=1;
asm("nop");
asm("nop");
c=Get_IOA_Data();
c&=0x0080;
}while(c==0x0080);
}
void delay15()
{
unsigned int i,j;
i=2;
while(i--)
{
j=0x4fff;
while(j--)
{
Watchdog_Clear();
}
}
}
void delay5()
{
unsigned int i,j;
i=0;
while(i--)
{
j=0x4fff;
while(j--)
{
Watchdog_Clear();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -