|
第一步:仿真
繪制好原理圖,仿真時單片機(jī)最小系統(tǒng)只放一個單片機(jī)就可以了,但實際電路中需要加上電源模塊、復(fù)位模塊。如下圖:
03-5.PNG (255.65 KB, 下載次數(shù): 76)
下載附件
2020-6-23 11:12 上傳
原理圖文件請在附件中找。
原理圖繪制好后,編寫代碼并生成hex文件。參考代碼如下:
/*功能/
/使用外部中斷INTO改變LED狀態(tài),并且用1602顯示出其狀態(tài)/
#include<reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]=" HELLO 1602 ";
uchar code table0[]="1602 TEST ";
uchar code table1[]="RESULT: "; //空格是寫到LCD了,即空格也是內(nèi)容
uchar code table2[]=“OK!”;
uchar code table3[]=“l(fā)iang”;
uchar code table4[]=“mie”;
uchar num;
sbit led=P1^7;
sbit lcden=P2^0; //液晶使能端
sbit lcdrs=P2^2; //液晶數(shù)據(jù)命令選擇端選擇寫數(shù)據(jù)還是寫命令
void delay(uint z) // 延時函數(shù)
{
uint i,j;
for(i=z;i>0;i–)
for(j=110;j>0;j–);
}
void write_com(uchar com) //寫入
{
lcdrs=0; //LCD 選擇輸入命令
P0=com; //向P0 口輸入命令 39-32端口
delay(5); //延時
lcden=1; //打開LCD 使能
delay(5); //一個高脈沖
lcden=0; //關(guān)閉LCD 使能
}
void write_data(uchar date)
{
lcdrs=1; //設(shè)置為輸入數(shù)據(jù)
P0=date; //將數(shù)據(jù)賦給P0 口
delay(5); //延時
lcden=1; //置高
delay(5); //高脈沖
lcden=0; //置低完成高脈沖
}
void init() //LED 初始化
{
lcden=0;
write_com(0x38);//設(shè)置16x2 顯示5x7 點陣,8 位數(shù)據(jù)接口
write_com(0x0c); //設(shè)置開始顯示不顯示光標(biāo) (指令4)
write_com(0x06); //寫一個字符后地址指針加1 (指令3)
write_com(0x01); //顯示清零數(shù)據(jù)指針清零 (指令1)
}
void display(unsigned char *p)//按指針 地址寫入數(shù)據(jù)
{
while(*p!=’\0’)
{
write_data(*p); //寫數(shù)據(jù)
p++;
delay(1);
}
}
void main()
{
TCON=0x01; //設(shè)置中斷為INTO
IE=0x81;
led=0;
init();
write_com(0x80); //將數(shù)據(jù)指針第一行第一個字處,
display(table); //調(diào)用函數(shù)的方式寫入數(shù)據(jù)
delay(800);
write_com(0x80); //將數(shù)據(jù)指針第一行第一個字處,
display(table0); //調(diào)用函數(shù)的方式寫入數(shù)據(jù)
write_com(0x80+0x40); //定義指針位置 將數(shù)據(jù)指針第二行第一個字處
for(num=0;num<7;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x4c); //按1602的地址單個寫入數(shù)據(jù)
write_data(table2[0]);
write_com(0x80+0x4d); //按1602的地址單個寫入數(shù)據(jù)
write_data(table2[1]);
write_com(0x80+0x4e); //按1602的地址單個寫入數(shù)據(jù)
write_data(table2[2]);
while(1);
}
void key_can() interrupt 0
{
delay(20); // 消除按鍵抖動
if(P3^30)
{
if(P3^30)
{
led=!led;
}
if(led1)
{
write_com(0x80+0x4b);
display(table3);
}
else if(led0)
{
write_com(0x80+0x4b); //按1602的地址單個寫入數(shù)據(jù)
display(table4);
write_com(0x80+0x4f);
write_data(table1[8]); //按1602的地址單個寫入數(shù)據(jù)
write_com(0x80+0x4e);
write_data(table1[8]);
}
}
}
原理圖和代碼都有了,仿真的準(zhǔn)備工作就完了,現(xiàn)在正式進(jìn)入仿真。
將代碼加載進(jìn)單片機(jī)中,仿真結(jié)果如下圖:
03-2.PNG (254.91 KB, 下載次數(shù): 87)
下載附件
2020-6-23 11:13 上傳
03-3.PNG (264 KB, 下載次數(shù): 74)
下載附件
2020-6-23 11:13 上傳
|
評分
-
查看全部評分
|