?? 5d_keys.c
字號:
#include <c8051f020.h> // 特殊功能寄存器(包含位)聲明
#include <stdio.h>
//------------------------------------------------------------------------------------
// 定義16位SFR
//------------------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 數據
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 數據
sfr16 DAC1 = 0xd5; // DAC1 數據
//char display[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
char disp_lcd[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46};
//------------------------------------------------------------------------------------
// 定義全局常量和變量
//------------------------------------------------------------------------------------
#define SYSCLK 22118400 // 定義系統時鐘(Hz)
#define uint8 unsigned char
#define uint16 unsigned int
#define KEY P6
sfr Lcd_port =0x85; //LCd數據端口 P5
sfr Lcd_ctr =0x84; //LCd控制端口 P4
/*開漏 LCD_RS=P4.5 開漏 LCD_RW=P4.6 開漏 LCD_E=P4.7*/
uint8 code Welcome0[64] ={ "01234567 "
" "
" "
" "};
uint8 Ac;
uint8 RamData;
//------------------------------------------------------------------------------------
// 自定義函數
//------------------------------------------------------------------------------------
//延時子程序
//讀ADC
//void System_Init(void); //系統初始化
//uint8 keyscan0(); //鍵盤掃描
//--------gmyul768op7 8op----------------------------------------------------------------------------
void delay ()
{
uint8 k;
for(k=0;k<200;k++);
}
void Delay(uint16 time) //延時程序 ========不分通道========
{
uint16 i,t;
for(i=0;i<time;i++){for(t=0;t<300;t++) ;}
}
////////////////////////////////////////////////////////////////////////////////LCD程序
void Wait(uint8 dly)
{for(;dly>0;dly--);}
uint8 Lcd_Busy( void )
{
uint8 Lcd1;
Lcd_ctr=0x40; //RS=0,RW=1
Wait(50);
Lcd_ctr=0xc0;
Wait(50);
Lcd_port=0xff;
Lcd1=Lcd_port; //從LCD讀狀態
Lcd_ctr=0x40; //讀數據,操作允許脈沖信號
if(( Lcd1 & 0x80 )==0) return 0 ; //LCD不忙,返回0
return 1; //LCD忙,返回1
}
void LcdWriteCommand( uint8 comm)
{
while(Lcd_Busy()==1); //等LCD空閑
Lcd_ctr=0x00; //Lcd_RS=0,Lcd_RW=0
Wait(50);
Lcd_ctr=0x80;
Wait(50);
Lcd_port=comm; // 送控制字到LCD
Wait(50);
Lcd_ctr=0x00; // 下降沿打入數據,操作允許脈沖信號
Wait(50);
}
void Lcd_Ram_Write(uint8 Lcd_d)
{
while (Lcd_Busy()==1); // 檢測LCD忙信號,等LCD不忙
Lcd_ctr=0xa0; //RS=1,RW=0 數據寫入LCD
Wait(50);
Lcd_port=Lcd_d; //向LCD寫數據
Lcd_ctr=0x20;
Wait(50); // 下降沿打入數據
}
void Lcd_reset()
{
LcdWriteCommand(0x30); //寫命令字0x38,8位數據寬度
delay();
LcdWriteCommand(0x01); //寫命令字0x01,清顯示
delay();
LcdWriteCommand(0x06); //輸入方式設定 AC自動加1
delay();
LcdWriteCommand(0x0c); //開顯示,關 光標+閃動 D2,D1(0=無光標),D0(1=閃動)
delay();
}
void disp_flash(uint8 t)
{
uint8 j,m;
LcdWriteCommand(0x02); //***************************
Delay(1500);
LcdWriteCommand(0x0f); //閃爍
Delay(1500);
if(t == 0 )
{
for(j=0;j<4;j++){
for(m=0;m<15;m++) Lcd_Ram_Write(Welcome0[j*16+m]);}
//for(m=0;m<16;m++) Lcd_Ram_Write(0x03);}
}
}
uint8 Ac_Read()
{
Lcd_ctr=0x40; //RS=0,RW=1
Wait(50);
Lcd_ctr=0xc0;
Wait(50);
Lcd_port=0xff;
Ac=Lcd_port; //從LCD讀狀態
Lcd_ctr=0x40; //讀數據,操作允許脈沖信號
Ac=Ac&0x7f;
Ac=Ac+0x80;
return Ac;
}
void Cursor_Move(uint8 n)
{ uint8 flag;
flag=0;
Ac_Read();
if(n==10) //上移
{
if(Ac >=0x80&Ac <= 0x87)
{
Ac = Ac+0x18;
LcdWriteCommand(Ac);
delay();
}
else if(Ac >=0x90&Ac <= 0x97)
{
Ac = Ac-0x10;
LcdWriteCommand(Ac);
delay();
}
else if(Ac >=0x88&Ac <= 0x8f)
{
Ac = Ac+0x08;
LcdWriteCommand(Ac);
delay();
}
else
{
Ac = Ac-0x10;
LcdWriteCommand(Ac);
delay();
}
}
if(n==4) //左移
{
if(Ac ==0x80)
{
Ac =Ac+0x1f;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(Ac ==0x90)
{
Ac =Ac-0x09;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(Ac ==0x88)
{
Ac =Ac+0x0f;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(Ac ==0x98)
{
Ac =Ac-0x09;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(flag==0)
{
LcdWriteCommand(0x10);
delay();
}
}
if(n==14) //下移
{
if(Ac >=0x80&Ac <= 0x87)
{
Ac = Ac+0x10;
LcdWriteCommand(Ac);
delay();
}
else if(Ac >=0x90&Ac <= 0x97)
{
Ac = Ac-0x08;
LcdWriteCommand(Ac);
delay();
}
else if(Ac >=0x88&Ac <= 0x8f)
{
Ac = Ac+0x10;
LcdWriteCommand(Ac);
delay();
}
else
{
Ac = Ac-0x18;
LcdWriteCommand(Ac);
delay();
}
}
if(n==15) //右移
{
if(Ac==0x87)
{
Ac = Ac+0x09;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(Ac==0x97)
{
Ac = Ac-0x0f;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(Ac==0x8f)
{
Ac = Ac+0x09;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(Ac==0x9f)
{
Ac = Ac-0x1f;
LcdWriteCommand(Ac);
delay();
flag=1;
}
if(flag==0)
{
LcdWriteCommand(0x14);
delay();
}
}
if(n==12) //確認
{
Ac=Ac+0x01;
LcdWriteCommand(Ac);
delay();
}
if(n!=10&n!=11&n!=12&n!=13&n!=14&n!=15&n!=16) //寫數據
{
//if()
//{ }
Lcd_Ram_Write(disp_lcd[n]);
delay();
}
}
uint8 Lcd_ReadRam(void)
{
//uint8 Ac1;
//Ac1 = Ac_Read();
// =Lcd_ctr;
Lcd_ctr=0xe0;
Wait(50);
// if()
// {
RamData=Lcd_port;
Wait(50);
// }
RamData=Lcd_port;
Lcd_ctr=0x60;
//Wait(50);
return RamData;
}
//////////////////////////////////////////////////////////////////////////////
/**************************************************************************
4*4矩陣鍵盤掃描,返回值為按鍵對應的數值(0~F),若無鍵則返回0x10,輸入時無反相器
**************************************************************************/
unsigned char keyscan0() //4*4矩陣鍵盤掃描返回按鍵對應的數值
{
unsigned char i,k,out,in=1,j;
KEY=0xff;
KEY=0x0f;
i=KEY;
j=i=i&0x0f;
if(i!=0x0f)
{ delay();
if(i==j)
{ out=0xef;
for(i=0;i<4;i++)
{ KEY=0xff;
KEY=out;
k=KEY;
k=k&0x0f;
do{ j=KEY;
j=j&0x0f;
} while(i==k);
if(i==0){
switch(k)
{ case 0x0e : return 3;break;
case 0x0d : return 7;break;
case 0x0b : return 11;break;
case 0x07 : return 15;break;
} }
else if(i==1){
switch(k)
{ case 0x0e : return 0;break;
case 0x0d : return 4;break;
case 0x0b : return 8;break;
case 0x07 : return 12;break;
}}
else if(i==2){
switch(k)
{ case 0x0e : return 2;break;
case 0x0d : return 6;break;
case 0x0b : return 10;break;
case 0x07 : return 14;break;
} }
else if(i==3){
switch(k)
{ case 0x0e : return 1;break;
case 0x0d : return 5;break;
case 0x0b : return 9;break;
case 0x07 : return 13;break;
}}
out=out<<1;
out=out+0x01;
}
}
}
else return 16;
}
/////////////////////////////////////////////////////////////////////////////
void System_Init(void)
{
uint8 y;
OSCXCN=0xe7; //使能外晶體 >11.0592MHz
Delay(200);
y=0x00;
while(y!=0x80) //等外晶體穩定
{
y=OSCXCN;
y=y&0x80;
}
OSCICN=0x8c; //切換到外晶體 */ //
P0MDOUT = 0xff; //配置P0口 推拉輸出
P1MDOUT = 0x00; //配置P1口 推拉輸出
P2MDOUT = 0xff; //配置P2口 推拉輸出
P3MDOUT = 0xff; //配置P3口 推拉輸出
P74OUT = 0x00;
XBR0 = 0x00; // XBAR0 串口連接到端口 P0.0=TXD P0.1=RXD
XBR1 = 0x00; // XBAR1
XBR2 = 0x40; // 交叉開關使能(D6=1)
EA=0;
WDTCN = 0xde; //配置WT--看門狗定時器
WDTCN = 0xad;
P1=0x7f;
Delay(1000);
P1=0xff;
Lcd_port = 0xff;
while(Lcd_Busy()==1); // 檢測LCD忙信號,等LCD不忙
Lcd_reset();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void main (void)
{
uint8 m;
//uint8 Data;
System_Init();
disp_flash(0);
LcdWriteCommand(0x02);
delay();
while(1)
{
m=keyscan0();
Cursor_Move(m);
Delay(1000);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -