?? keyboard_1.h
字號:
#include <reg52.h>
//#include <math.h>
#define uchar unsigned char
#define uint unsigned int
//本文件假定鍵盤接在P1口,P10~P1.3為輸入線,P1.4~P1.7為輸出線,輸出0
sbit P30=P3^0;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P31=P3^1;
uchar Key;//當前按下的鍵
uchar Val[10];//將輸入的數字存放到這個數組,以便計算
uchar Ni;//一個數由幾個位組成
uchar CO2str[8];//存放CO2濃度數據,以字符形式存放以例顯示
uint Num;//輸入的數(整數)或變量
unsigned char GetKey();
uchar a;//標志:計算Num的數據時用到
char NumStr[10];//這個數組存放輸入數字的ASCII碼,用來顯示
uint GetNum(); //從鍵盤獲得輸入的整數
void IntToStr(uint co2,char* s);//將整數CO2轉化成字符串放以數組S中。
uint ReadAD(bit b);//讀取AD轉換值
uint pow(uchar n);//求10的n次方的函數,不用math函提供的函數以減小Hex文件長度
//uint NumToVolt(uchar Num);//將AD轉換值轉換成電壓,單位是毫伏。
uint pow(uchar n)
{ uchar i;uint temp=1;
for(i=0;i<n;i++)
temp*=10;
return temp;
}
void IntToStr(uint co2,char* s)
{
uchar i,n;
for(i=0;i<8;i++) s[i]='\0';//將原來的內容清空
i=0;
while(co2/pow(i)>=1) {i++;}//獲得數據有多少數據位
for(n=0;n<i;n++)
s[n]=(uchar)(((int)(co2/pow(i-n-1)))%10+0x30);
}
uint GetNum()//從鍵盤獲得輸入的整數
{
Begin: Ni=0;
for(a=0;a<10;a++) NumStr[a]='\0';
while(GetKey()!='F')
{
Val[Ni]=Key;
//NumStr[Ni]=Key+0x30;
LCD_write_char(Ni,1,Key+0x30);
if(Val[Ni]>9)//如果輸入了字母就重新開始輸入
{
CLEARSCREEN;
LCD_write_string(0,0,"Enter Again:");
goto Begin;
}
Ni++;
}
if ((Ni>5)|| ((Ni==5)&&Val[0]>=3)&&(Val[1]>=2))
{
CLEARSCREEN;
LCD_write_string(0,0,"Enter Again:");
goto Begin;
}
Num=0;
for(a=0;a<Ni;a++)
Num+=Val[Ni-1-a]*pow(a);
return Num;
}
void s()
{
unsigned char a=0;
for(a=0;a<3;a++)
;
}
bit TestKey()
{
P1=0x0f;
s();
if (P1==0x0f)
return 0;
else
return 1; //有鍵按下返回1,否則返回0
}
uchar GetKey()
{
while(!TestKey())//無鍵按下什么都不做
{; }
//有鍵按下時判斷哪個鍵按下
P1=0xef;
s();
if(P1==0xee) {Key=0x01;goto NEXT;}//1
if(P1==0xed) {Key=0x05;goto NEXT;}//5
if(P1==0xeb) {Key=0x09;goto NEXT;}//9
if(P1==0xe7) {Key='C';goto NEXT;}//C
P1=0xdf;
s();
if(P1==0xde) {Key=0x02;goto NEXT;}//2
if(P1==0xdd) {Key=0x06;goto NEXT;}//6
if(P1==0xdb) {Key=0x00;goto NEXT;}//0
if(P1==0xd7) {Key='D';goto NEXT;}//'D';
P1=0xbf;
s();
if(P1==0xbe) {Key=0x03;goto NEXT;}//3;
if(P1==0xbd) {Key=0x07;goto NEXT;}//7
if(P1==0xbb) {Key='A';goto NEXT;}//A
if(P1==0xb7) {Key='E';goto NEXT;}//E
P1=0x7f;
s();
if(P1==0x7e) {Key=0x04;goto NEXT;}//4
if(P1==0x7d) {Key=0x08;goto NEXT;}//8
if(P1==0x7b) {Key='B';goto NEXT;}//B
if(P1==0x77) {Key='F';goto NEXT;}//F
NEXT: while(TestKey()) {;}
return Key;
}
/*********************************************************************/
/*********************************************************************/
//下面是ADC0832部分
sbit AD_CS=P2^0;
sbit AD_DI=P2^1;
sbit AD_DO=P2^2;
sbit CLK=P2^3;
uint ReadAD(bit b)
{
uchar tem=0x00;//AD轉換值
uchar i=0x00;//
uint uInt;
double d;
//初始化AD轉換
AD_DI=1;
AD_CS=0;
CLK=1;
s();
CLK=0;
s();
AD_DI=1;
s();
CLK=1;
s();
CLK=0;
s();s();
if (b==0)
AD_DI=0;//通道0
else
AD_DI=1;//通道1
CLK=1;
s();
CLK=0;
s();
// 讀AD數據,存入tem中
for(i=0;i<8;i++)
{ CLK=1;s();s();
CLK=0;s();s();
tem=(tem<<1)|AD_DO;
}
AD_CS=1;
// 讀AD完畢
//以下代碼將數字量轉化成對應的電壓值,單位是毫伏
// return (((uint)tem)*1000/51);
d=19.61*tem;
uInt=d;
return uInt;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -