1. 程式人生 > >電話鍵盤及撥號的模擬 c程式

電話鍵盤及撥號的模擬 c程式

程式
#include “reg51.h”
#include “intrins.h”
#define uint unsigned int
#define uchar unsigned char
#define out P0
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
sbit Beep=P3^7;
uint n,num=0;
uchar key;
uchar code table1[]=“HelloHake”;
void write_com(uint com);
void write_data (uint date);
void LCD_Init(void);
void delay(uint xms);
void string(uint ad,uchar *s);
//void check_busy(void); //不使用
void Sound(void);
void KeyScan(void);
void Action(void);

void main(){
LCD_Init();
write_com(0x80);
for(n=0;n<sizeof(table1);n++){
write_data(table1[n]);
delay(10);
}
while(1){
KeyScan();
Action();
}
}

/LCD驅動**/
////忙檢查函式
//void check_busy(void){
// uint a;
//
// do{
// a=0xff;
// LCD_EN=0;
// LCD_RS=1;//data
// LCD_RW=1;//read
// LCD_EN=1;
// a=out;
//// delay(10);
// }while(a&0x80);//忙標誌位為1則一直等待
// LCD_EN=0;
//}
void write_com(uint com){
// check_busy();
LCD_EN=0;
out=com;//必須延時等待資料指令穩定
delay(5);
LCD_RS=0;//com
LCD_RW=0;//write
LCD_EN=1;
nop

();//wait writing
LCD_EN=0;

}
void write_data(uint date){
// check_busy();
LCD_EN=0;
out=date;
delay(5);//必須延時等待資料指令穩定
LCD_RS=1;//data
LCD_RW=0;//write
LCD_EN=1;
nop();//wait writing
LCD_EN=0;

}
void LCD_Init(void){
write_com(0x38);//00111000;
nop();
write_com(0x0c);//00001100;
nop();
write_com(0x06);//00000110;
nop();
write_com(0x01);//clean screen
delay(10);
}
//字元顯示函式
void string(uint ad,uchar *s){
write_com(ad);
while(*s>0){
write_data(*s++);
delay(5);
}
}

/延時函式****/
void delay(uint xms){
uint i,j;
for(i=xms;i>0;i–)
for(j=110;j>0;j–);
}
/蜂鳴器函式/
void Sound(void){
uchar i;
for(i=0;i<10;i++){
Beep=1;
delay(15);
Beep=0;
delay(5);
}
}
/按鍵列掃描函式
/
void KeyScan(void){
char temp;
key=13;
// if(num<0){
// num=0;
// }
P1=0x3f;
temp=P1;
if(temp!=0x3f){
delay(50);
temp=P1;
if(temp!=0x3f){
switch(temp){
case (0x3e):
key=1;
break;
case (0x3d):
key=4;
break;
case (0x3b):
key=7;
break;
case (0x37):
key=10; //號鍵
break;
}
while(temp!=0x3f) //鬆手檢測
{
temp=P1;
}
}
}
P1=0x5f;
temp=P1;
if(temp!=0x5f){
delay(50);
temp=P1;
if(temp!=0x5f){
switch(temp){
case (0x5e):
key=2;
break;
case (0x5d):
key=5;
break;
case (0x5b):
key=8;
break;
case (0x57):
key=0;
break;
}
while(temp!=0x5f) //鬆手檢測
{
temp=P1;
}
}
}
P1=0x6f;
temp=P1;
if(temp!=0x6f){
delay(50);
temp=P1;
if(temp!=0x6f){
switch(temp){
case (0x6e):
key=3;
break;
case (0x6d):
key=6;
break;
case (0x6b):
key=9;
break;
case (0x67):
key=11; //#號鍵
break;
}
while(temp!=0x6f) //鬆手檢測
{
temp=P1;
}
}
}
}
/響應函式
/
void Action(void){
if(key13){
//空
}
if(key
0){
Sound();
string(0xc0+num,“0”);
num++;
}
if(key1){
Sound();
string(0xc0+num,“1”);
num++;
}
if(key
2){
Sound();
string(0xc0+num,“2”);
num++;
}
if(key3){
Sound();
string(0xc0+num,“3”);
num++;
}
if(key
4){
Sound();
string(0xc0+num,“4”);
num++;
}
if(key5){
Sound();
string(0xc0+num,“5”);
num++;
}
if(key
6){
Sound();
string(0xc0+num,“6”);
num++;
}
if(key7){
Sound();
string(0xc0+num,“7”);
num++;
}
if(key
8){
Sound();
string(0xc0+num,“8”);
num++;
}
if(key9){
Sound();
string(0xc0+num,“9”);
num++;
}
if(key
10){
Sound();
num=num-1;
string(0xc0+num," “);
// num++;
}
if(key==11){
Sound();
write_com(0xc0);
for(n=0;n<16;n++){
string(0xc0+n,” ");
}
num=0;
}
}電路原理圖