㈠ 利用AT89C51單片機設計簡易電子鍾(六位),通過8位LED數碼管實現時間顯示;系統可以通過三個按鍵實現時間
程序設計思路:1.利用單片機內部定時器作為計時器,調用timer函數;2、設計一套按鍵子程序;(3)設計一套數碼顯示管子程序。下列提供一套程序供參考。
#include<iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
const uchar table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9對應字元
const uchar key_value[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
uchar data[4]={0}; //顯示初始值為0
uint count=0; //初始計數值為0
uchar keynum;
void port_init() //埠初始化函數
{DDRA=0XFF; //埠A為推挽1輸出
PORTA=0XFF;
PORTB=0XFF; //埠B為推挽1輸出
DDRB=0XFF;
DDRC=0XFF; //埠C為不帶上拉電阻的輸入
PORTC=0XFF;
DDRD=0XFF; //埠D低4位為輸出
PORTD=0X0F;
}
void delay(uint j) //延時函數
{uint k;
while(j--)
{for(k=1000;k>0;k--) ;}
}
void display(uchar *p) //顯示函數
{uchar i,sel=0x01;
for(i=0;i<4;i++)
{PORTA=sel; //選通最右邊的數碼管
PORTB=table[p[i]]; //送字型碼
delay(1);
sel=sel<<1; //左移一位
}
}
uchar get_key(void)
{if((PIND&0X0F)!=0X0F) delay(3);
{
PORTD=0XEF;
switch(PIND&0X0F)
{case 0x0e: keynum=key_value[0];break;
case 0x0d: keynum=key_value[4];break;
case 0x0b: keynum=key_value[8];break;
case 0x07: keynum=key_value[12];break;
default:break;
}
PORTD=0xdf;
switch(PIND&0X0F)
{case 0x0e:keynum=key_value[1];break;
case 0x0d:keynum=key_value[5];break;
case 0x0b:keynum=key_value[9];break;
case 0x07:keynum=key_value[13];break;
default:break;
}
PORTD=0xbf;
switch(PIND&0X0F)
{case 0x0e:keynum=key_value[2];break;
case 0x0d:keynum=key_value[6];break;
case 0x0b:keynum=key_value[10];break;
case 0x07:keynum=key_value[14];break;
default:break;
}
PORTD=0x7f;
switch(PIND&0X0F)
{case 0x0e:keynum=key_value[3];break;
case 0x0d:keynum=key_value[7];break;
case 0x0b:keynum=key_value[11];break;
case 0x07:keynum=key_value[15];break;
default:break;
}
while((PIND&0x0f)!=0x0f);
}
return keynum;
}
void process(uint i,uchar *q) //拆分BCD碼函數,顯示千、百、十、個位
{ q[0]=i/1000;
i=i%1000;
q[1]=i/100;
i=i%100;
q[2]=i/10;
i=i%10;
q[3]=i;
}
void main(void)
{port_init();
PORTB=0xff;
PORTA=0X00;
delay(50);
PORTA=0xff;
while(1)
{get_key();
count=keynum;
process(count,data);
display(data);
}
}
㈡ 單片機實現電子鍾設計
你的分數給的太低了,沒有人願意為幫忙,這也從側面看出你根本就沒有學習過單片機,那麼這畢業設計就沒有多大意義,還不如去看看某些關於單片機應用設計的教材,那上面的例子都是很詳細的,並且有源代碼。
如果要簡單,DS12C887+AT89S52+LCD1602就足夠了,匯編代碼的長度不會超過200行。
㈢ 求一段為51單片機編寫的LCD電子時鍾的設計,簡單就好!高分伺候!
簡單的時鍾,不用1302即可辦到。
題目要求的設計已經完成,電路圖如下。
程序稍長,放在我的網路空間了。
可以用網路、網頁,進行查找:
做而論道用LCD1602顯示的時鍾
網路一下,即可查到。
㈣ 單片機電子時鍾設計
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>#define INT8U unsigned char
#define INT16U unsigned int
#define k1() ((PIND & (1<<PD0))==0x00)
#define k2() ((PIND & (1<<PD1))==0x00)
#define k3() ((PIND & (1<<PD2))==0x00)
#define k4() ((PIND & (1<<PD3))==0x00)
#define k5() ((PIND & (1<<PD4))==0x00)
#define k6() ((PIND & (1<<PD5))==0x00)
#define k7() ((PIND & (1<<PD6))==0x00)
#define k8() ((PIND & (1<<PD7))==0x00)const INT8U seg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
INT8U seg11[]={0,0,0x40,0,0,0x40,0,0};
INT8U ja;
INT8U key=0xff;
INT8U h,m,s,m1,d;
INT16U y; void hour1()
{
if (++h>23) {h=0;day();}
seg11[0]=seg[h/10];
seg11[1]=seg[h%10];
} void minute1()
{
if (++m>59)
{m=0;<br> hour1();<br> }
seg11[3]=seg[m/10];
seg11[4]=seg[m%10];
} void second1() {
if (++s>59)
{ s=0;
minute1();
}
seg11[6]=seg[s/10];
seg11[7]=seg[s%10];
} const INT8U Days1[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
const INT8U Days2[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
void year()
{if (++y>9999) y=0;<br> <br> seg11[0]=seg[y/1000];<br> seg11[1]=seg[y%1000/100];<br> seg11[2]=seg[y%100/10];<br> seg11[3]=seg[y%10];<br> } void month1()
{
if (++m1>12)
{m1=0;<br> year();<br> }
seg11[4]=seg[m1/10];
seg11[5]=seg[m1%10];
} void day()
{ INT8U DAY;
DAY=(( y%4==0 && y%100!=0 )||( y%400==0 ) ) ? Days1[m1]:Days2[m1];//判斷閏年
if(++d>DAY)
{ d=0;
month1();
}
seg11[6]=seg[d/10];
seg11[7]=seg[d%10];
} //時間函數
void time()
{
seg11[0]=seg[h/10];
seg11[1]=seg[h%10];
seg11[3]=seg[m/10];
seg11[4]=seg[m%10];
seg11[6]=seg[s/10];
seg11[7]=seg[s%10];
PORTA=0x00;
PORTA=seg11[ja]; PORTB=~(1<<ja);
ja=(ja+1)&0x07;
_delay_ms(5); }
//日期
void date()
{ seg11[0]=seg[y/1000];
seg11[1]=seg[y%1000/100];
seg11[2]=seg[y%100/10];
seg11[3]=seg[y%10];
seg11[4]=seg[m1/10];
seg11[5]=seg[m1%10];
seg11[6]=seg[d/10];
seg11[7]=seg[d%10];
//seg11[2]=seg11[5]=0x00;
PORTA=0x00;
PORTA=seg11[ja];
PORTB=~(1<<ja);
ja=(ja+1)&0x07;
_delay_ms(3);
}
int main()
{ INT8U ja=0;
DDRA=0xff; PORTA=0xff;
DDRB=0xff; PORTB=0xff;
DDRD=0x00; PORTD=0xff;PIND=0x00;
MCUCR=0X0A;//MCU 控制寄存器- MCUCR
GICR=0XC0; //通用中斷控制寄存器- GICR ASSR=0x08;
TCCR2=0x04;
TCNT2=0;
TIMSK=_BV(TOIE2)|_BV(TOIE0); d=26;y=2000;m1=2;
h=m=s=12;
sei();
while(1)
{ time();
if(k1())
/{ while(k1()); hour1(); }
if(k8())
{ while(k8());
while(2)//日期循環
{
date();
if(k8())
{while(k8());break;} }
}
}}
ISR(TIMER2_OVF_vect) {
if( seg11[2]==0x40)
{
seg11[2]=seg11[5]=0x00;
}
else
{ seg11[2]=seg11[5]=0x40;
second1();
}
}
㈤ 基於AT89C51單片機的6位數碼管顯示的簡易電子時鍾設計
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds1302_RST =P2^0;
sbit ds1302_IO =P2^1;
sbit ds1302_SCLK=P2^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit A1=P3^0;
sbit A2=P3^1;
sbit A3=P3^2;
sbit A4=P3^3;
sbit A5=P3^4;
sbit A6=P3^5;
sbit key1=P3^6;
sbit key2=P3^7;
uchar now_time[3],wei,d[3]={0,0,0};
uchar code s[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
void delay(uint x)
{
uchar i;
for(x;x>0;x--)
for(i=0;i<100;i++);
}
void disp()
{
P1=s[now_time[2]/16];
A1=d[2];
delay(5);
A1=1;
P1=s[now_time[2]%16];
A2=d[2];
delay(5);
A2=1;
P1=s[now_time[1]/16];
A3=d[1];
delay(5);
A3=1;
P1=s[now_time[1]%16];
A4=d[1];
delay(5);
A4=1;
P1=s[now_time[0]/16];
A5=d[0];
delay(5);
A5=1;
P1=s[now_time[0]%16];
A6=d[0];
delay(5);
A6=1;
}
/***********************************************************************/
uchar read_Byte()
{
uchar i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=ds1302_IO;
ds1302_SCLK=1;
ds1302_SCLK=0;
}
return(ACC);
}
void write_Byte(uchar tdata)
{
uchar i;
ACC=tdata;
for(i=8;i>0;i--)
{
ds1302_IO=ACC0;
ds1302_SCLK=1;
ds1302_SCLK=0;
ACC=ACC>>1;
}
}
/***********************************************************************************/
void write_data_ds1302(uchar taddr,uchar tdata)
{
ds1302_RST=0;
ds1302_SCLK=0;
ds1302_RST=1;
write_Byte(taddr);
write_Byte(tdata);
ds1302_RST=0;
ds1302_SCLK=1;
}
uchar read_data_ds1302(uchar taddr)
{
uchar tdata;
ds1302_RST=0;
ds1302_SCLK=0;
ds1302_RST=1;
write_Byte(taddr);
tdata=read_Byte();
ds1302_RST=0;
ds1302_SCLK=1;
return(tdata);
}
/***********************************************************************************/
void get_ds1302()
{
uchar k;
uchar taddr = 0x81;
for (k=0; k<3; k++)
{
now_time[k] = read_data_ds1302(taddr);
taddr+=2;
}
}
/***********************************************************************************/
void init_ds1302()
{
ds1302_RST=0;
ds1302_SCLK=0;
A1=1;
A2=1;
A3=1;
A4=1;
A5=1;
A6=1;
write_data_ds1302(0x80,0x00);
}
/***********************************************************************************/
void Time();
/***********************************************************************************/
main()
{
init_ds1302();
while(1)
{
disp();
get_ds1302();
if(key1==0);
{ delay(10);
if(key1==0)
Time();
}
}
}
void timer() interrupt 3
{
uchar i;i++;
TH1=(65535-50000)/256;
TL2=(65535-50000)%256;
if(i==50)
{
d[wei]=1;
}
if(i==100)
{ i=0;
d[wei]=0;
}
}
/***********************************************************************************/
void Time()
{
uchar temp;
uint r=0,p=1;
wei=2;
TMOD=0x01;
EA=1;
ET1=1;
TH1=(65535-50000)/256;
TL2=(65535-50000)%256;
TR1=1;
for(r=0;r<50;r++)
disp();
while( key1 )
{
wei=2;
disp();
if(key2==0)
{
disp();
if(key2==0)
{
temp=now_time[2]/16*10+now_time[2]%16;
temp++;
if(temp>=24)
temp=0;
now_time[2]=temp/10*16+temp%10;
write_data_ds1302(0x84,now_time[2]);
}
while(!key2);
}
}
wei=1;
d[2]=0;
for(r=0;r<50;r++)
disp();
while(key1 )
{ disp();
if(key2==0)
{
disp();
if(key2==0)
{
temp=now_time[1]/16*10+now_time[1]%16;
temp++;
if(temp>=60)
temp=0;
now_time[1]=temp/10*16+temp%10;
write_data_ds1302(0x82,now_time[1]);
}
while(!key2);
}
}
wei=0;
d[1]=0;
for(r=0;r<50;r++)
disp();
while(key1 )
{
disp();
if(key2==0)
{
now_time[0]=0;
write_data_ds1302(0x80,now_time[0]);
}
} TR1=0;
d[0]=0;
for(r=0;r<30;r++)
disp();
}
/*********************************************************************************/
㈥ 用單片機設計一個數字時鍾
#include <REG51.H>#include <intrins.h> #define uint unsigned int#define uchar unsigned charsbit DS1302_CLK = P1^7; //實時時鍾時鍾線引腳 sbit DS1302_IO = P1^6; //實時時鍾數據線引腳 sbit DS1302_RST = P1^5; //實時時鍾復位線引腳sbit wireless_1 = P3^0;sbit wireless_2 = P3^1;sbit wireless_3 = P3^2;sbit wireless_4 = P3^3; //無線控制sbit ACC0 = ACC^0;sbit ACC7 = ACC^7;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; //秒,分,時到日,月,年位閃的計數sbit Set = P2^0; //模式切換鍵sbit Up = P2^1; //加法按鈕sbit Down = P2^2; //減法按鈕sbit out = P2^3; //立刻跳出調整模式按鈕sbit DQ = P1^0; //溫度傳送數據IO口char done,count,temp,flag,up_flag,down_flag;uchar temp_value; //溫度值uchar TempBuffer[5],week_value[2]; void show_time(); //液晶顯示程序/***********1602液晶顯示部分子程序****************///Port Definitions**********************************************************sbit LcdRs = P2^5;sbit LcdRw = P2^6;sbit LcdEn = P2^7;sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.數據埠 //內部等待函數**************************************************************************unsigned char LCD_Wait(void){ LcdRs=0; LcdRw=1; _nop_(); LcdEn=1; _nop_(); LcdEn=0; return DBPort; }//向LCD寫入命令或數據************************************************************#define LCD_COMMAND 0 // Command#define LCD_DATA 1 // Data#define LCD_CLEAR_SCREEN 0x01 // 清屏#define LCD_HOMING 0x02 // 游標返回原點void LCD_Write(bit style, unsigned char input){ LcdEn=0; LcdRs=style; LcdRw=0; _nop_(); DBPort=input; _nop_();//注意順序 LcdEn=1; _nop_();//注意順序 LcdEn=0; _nop_(); LCD_Wait(); } //設置顯示模式************************************************************#define LCD_SHOW 0x04 //顯示開#define LCD_HIDE 0x00 //顯示關 #define LCD_CURSOR 0x02 //顯示游標#define LCD_NO_CURSOR 0x00 //無游標 #define LCD_FLASH 0x01 //游標閃動#define LCD_NO_FLASH 0x00 //游標不閃動 void LCD_SetDisplay(unsigned char DisplayMode){ LCD_Write(LCD_COMMAND, 0x08|DisplayMode); } //設置輸入模式************************************************************#define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 // default #define LCD_MOVE 0x01 // 畫面可平移#define LCD_NO_MOVE 0x00 //default void LCD_SetInput(unsigned char InputMode){ LCD_Write(LCD_COMMAND, 0x04|InputMode);} //初始化LCD************************************void LCD_Initial(){ LcdEn=0; LCD_Write(LCD_COMMAND,0x38); //8位數據埠,2行顯示,5*7點陣 LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //開啟顯示, 無游標 LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏 LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC遞增, 畫面不動} //液晶字元輸入的位置************************void GotoXY(unsigned char x, unsigned char y){ if(y==0) LCD_Write(LCD_COMMAND,0x80|x); if(y==1) LCD_Write(LCD_COMMAND,0x80|(x-0x40));} //將字元輸出到液晶顯示void Print(unsigned char *str){ while(*str!='\0') { LCD_Write(LCD_DATA,*str); str++; }} /***********DS1302時鍾部分子程序******************/typedef struct __SYSTEMTIME__{ unsigned char Second; unsigned char Minute; unsigned char Hour; unsigned char Week; unsigned char Day; unsigned char Month; unsigned char Year; unsigned char DateString[11]; unsigned char TimeString[9];}SYSTEMTIME; //定義的時間類型SYSTEMTIME CurrentTime; #define AM(X) X#define PM(X) (X+12) // 轉成24小時制#define DS1302_SECOND 0x80 //時鍾晶元的寄存器位置,存放時間#define DS1302_MINUTE 0x82#define DS1302_HOUR 0x84 #define DS1302_WEEK 0x8A#define DS1302_DAY 0x86#define DS1302_MONTH 0x88#define DS1302_YEAR 0x8C void DS1302InputByte(unsigned char d) //實時時鍾寫入一位元組(內部函數){ unsigned char i; ACC = d; for(i=8; i>0; i--) { DS1302_IO = ACC0; //相當於匯編中的 RRC DS1302_CLK = 1; DS1302_CLK = 0; ACC = ACC >> 1; } } unsigned char DS1302OutputByte(void) //實時時鍾讀取一位元組(內部函數){ unsigned char i; for(i=8; i>0; i--) { ACC = ACC >>1; //相當於匯編中的 RRC ACC7 = DS1302_IO; DS1302_CLK = 1; DS1302_CLK = 0; } return(ACC); } void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要寫的數據{ DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); // 地址,命令 DS1302InputByte(ucDa); // 寫1Byte數據 DS1302_CLK = 1; DS1302_RST = 0;} unsigned char Read1302(unsigned char ucAddr) //讀取DS1302某地址的數據{ unsigned char ucData; DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr|0x01); // 地址,命令 ucData = DS1302OutputByte(); // 讀1Byte數據 DS1302_CLK = 1; DS1302_RST = 0; return(ucData);} void DS1302_GetTime(SYSTEMTIME *Time) //獲取時鍾晶元的時鍾數據到自定義的結構型數組{ unsigned char ReadValue; ReadValue = Read1302(DS1302_SECOND); Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_MINUTE); Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_HOUR); Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_DAY); Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_WEEK); Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_MONTH); Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_YEAR); Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); } void DateToStr(SYSTEMTIME *Time) //將時間年,月,日,星期數據轉換成液晶顯示字元串,放到數組里DateString[]{ if(hide_year<2) //這里的if,else語句都是判斷位閃爍,<2顯示數據,>2就不顯示,輸出字元串為 2007/07/22 { Time->DateString[0] = '2'; Time->DateString[1] = '0'; Time->DateString[2] = Time->Year/10 + '0'; Time->DateString[3] = Time->Year%10 + '0'; } else { Time->DateString[0] = ' '; Time->DateString[1] = ' '; Time->DateString[2] = ' '; Time->DateString[3] = ' '; } Time->DateString[4] = '/'; if(hide_month<2) { Time->DateString[5] = Time->Month/10 + '0'; Time->DateString[6] = Time->Month%10 + '0'; } else { Time->DateString[5] = ' '; Time->DateString[6] = ' '; } Time->DateString[7] = '/'; if(hide_day<2) { Time->DateString[8] = Time->Day/10 + '0'; Time->DateString[9] = Time->Day%10 + '0'; } else { Time->DateString[8] = ' '; Time->DateString[9] = ' '; } if(hide_week<2) { week_value[0] = Time->Week%10 + '0'; //星期的數據另外放到 week_value[]數組里,跟年,月,日的分開存放,因為等一下要在最後顯示 } else { week_value[0] = ' '; } week_value[1] = '\0'; Time->DateString[10] = '\0'; //字元串末尾加 '\0' ,判斷結束字元} void TimeToStr(SYSTEMTIME *Time) //將時,分,秒數據轉換成液晶顯示字元放到數組 TimeString[];{ if(hide_hour<2) { Time->TimeString[0] = Time->Hour/10 + '0'; Time->TimeString[1] = Time->Hour%10 + '0'; } else { Time->TimeString[0] = ' '; Time->TimeString[1] = ' '; } Time->TimeString[2] = ':'; if(hide_min<2) { Time->TimeString[3] = Time->Minute/10 + '0'; Time->TimeString[4] = Time->Minute%10 + '0'; } else { Time->TimeString[3] = ' '; Time->TimeString[4] = ' '; } Time->TimeString[5] = ':'; if(hide_sec<2) { Time->TimeString[6] = Time->Second/10 + '0'; Time->TimeString[7] = Time->Second%10 + '0'; } else { Time->TimeString[6] = ' '; Time->TimeString[7] = ' '; } Time->DateString[8] = '\0';} void Initial_DS1302(void) //時鍾晶元初始化{ unsigned char Second=Read1302(DS1302_SECOND); if(Second&0x80) //判斷時鍾晶元是否關閉 { Write1302(0x8e,0x00); //寫入允許 Write1302(0x8c,0x07); //以下寫入初始化時間 日期:07/07/25.星期: 3. 時間: 23:59:55 Write1302(0x88,0x07); Write1302(0x86,0x25); Write1302(0x8a,0x07); Write1302(0x84,0x23); Write1302(0x82,0x59); Write1302(0x80,0x55); Write1302(0x8e,0x80); //禁止寫入 } } /***********ds18b20子程序*************************/ /***********ds18b20延遲子函數(晶振12MHz )*******/ void delay_18B20(unsigned int i){ while(i--);} /**********ds18b20初始化函數**********************/ void Init_DS18B20(void) { unsigned char x=0; DQ = 1; //DQ復位 delay_18B20(8); //稍做延時 DQ = 0; //單片機將DQ拉低 delay_18B20(80); //精確延時 大於 480us DQ = 1; //拉高匯流排 delay_18B20(14); x=DQ; //稍做延時後 如果x=0則初始化成功 x=1則初始化失敗 delay_18B20(20);} /***********ds18b20讀一個位元組**************/ unsigned char ReadOneChar(void){ uchar i=0; uchar dat = 0; for (i=8;i>0;i--) { DQ = 0; // 給脈沖信號 dat>>=1; DQ = 1; // 給脈沖信號 if(DQ) dat|=0x80; delay_18B20(4); } return(dat);} /*************ds18b20寫一個位元組****************/ void WriteOneChar(uchar dat){ unsigned char i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; delay_18B20(5); DQ = 1; dat>>=1; }} /**************讀取ds18b20當前溫度************/ void ReadTemp(void){ unsigned char a=0; unsigned char b=0; unsigned char t=0; Init_DS18B20(); WriteOneChar(0xCC); // 跳過讀序號列號的操作 WriteOneChar(0x44); // 啟動溫度轉換 delay_18B20(100); // this message is wery important Init_DS18B20(); WriteOneChar(0xCC); //跳過讀序號列號的操作 WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度 delay_18B20(100); a=ReadOneChar(); //讀取溫度值低位 b=ReadOneChar(); //讀取溫度值高位 temp_value=b<<4; temp_value+=(a&0xf0)>>4; }void temp_to_str() //溫度數據轉換成液晶字元顯示{ TempBuffer[0]=temp_value/10+'0'; //十位 TempBuffer[1]=temp_value%10+'0'; //個位 TempBuffer[2]=0xdf; //溫度符號 TempBuffer[3]='C'; TempBuffer[4]='\0';}void Delay1ms(unsigned int count){ unsigned int i,j; for(i=0;i<count;i++) for(j=0;j<120;j++);} /*延時子程序*/void mdelay(uint delay){ uint i; for(;delay>0;delay--) {for(i=0;i<62;i++) //1ms延時. {;} }}
㈦ 用單片機製作一個簡易的時鍾。。
#include<reg52.h>
#include"segnumca.h"
#define uchar unsigned char
sbit _speak=P1^0; //蜂鳴器P1^0
uchar hello_[]={0x89,0x86,0xc7,0xc7,0xc0,0xf7}; //HELLO_
uchar con[]={0x01,0x02,0x04,0x08,0x10,0x20}; //位控制
uchar time0=100;
uchar i=0;
uchar s,m,h;
void delay(); //0.3s
void intr_(); //int0\1初始化
/*********************************
主函數
*********************************/
void main()
{
s=m=h=0;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
TR0=1;
ET0=1;
EA=1;
intr_();
while(time0>10)
{
P2=con[i];
P0=hello_[i];
i++;
if(i>=6)i=0;
delay();
}
i=0;
while(1)
{
P2=con[i];
switch(i)
{
case 0:P0=seg[h/10];break;
case 1:P0=seg[h%10]+0x80;break;
case 2:P0=seg[m/10];break;
case 3:P0=seg[m%10]+0x80;break;
case 4:P0=seg[s/10];break;
case 5:P0=seg[s%10];break;
}
i++;
if(i>=6)i=0;
delay();
}
}
/****************************
T0
****************************/
void isr_time0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
time0--;
if(!time0)
{
time0=20;
s++;
if(s>=60)
{
s=0;
m++;
if(m>=60)
{
m=0;
h++;
if(h>=24)
{
h=0;
}
}
}
}
}
/*************************
外中斷初始化
*************************/
void intr_()
{
IT0=1;IT1=1;EX0=1;EX1=1;
}
/**************************
for延時
**************************/
void delay() //大約延時0.3秒
{
unsigned char delay0,delay1;
for(delay0=0;delay0<5;delay0++)
for(delay1=0;delay1<255;delay1++);
}
/*********************************************
INT0
*********************************************/
void intr_0() interrupt 0
{
m++;
if(m>=60)m=0;
}
/*********************************************
INT1
*********************************************/
void intr_1() interrupt 2
{
h++;
if(h>=24)h=0;
}
㈧ 求單片機簡易數字鍾的課程設計 :要求自製一個單片機最小系統,包括串口下載、復位電路,採用內部定時器計
下面的程序配合這個電路運行就是對的
這個是完整的程序,電路圖如下
#include<reg52.h>
//定義共陽極字型碼0123456789-
unsignedcharcodedispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsignedchartime[]={0,0,0,0};//用來儲存分秒
unsignedchardate[]={0,0,0,0};//用來儲存日時
unsignedcharyear[]={0,0,0,0};//用來儲存年月
typedefstruct__SYSTEMTIME__
{
unsignedcharSecond;
unsignedcharMinute;
unsignedcharHour;
unsignedcharWeek;
unsignedcharDay;
unsignedcharMonth;
unsignedcharYear;
unsignedcharDateString[9];
unsignedcharTimeString[9];
}SYSTEMTIME; //定義的時間類型
SYSTEMTIMEtime1;
sbitDS1302_CLK=P1^6;//實時時鍾時鍾線引腳
sbitDS1302_IO=P1^7;//實時時鍾數據線引腳
sbitDS1302_RST=P1^5;//實時時鍾復位線引腳
sbitACC0=ACC^0;
sbitACC7=ACC^7;
sbitP10=P1^0;
sbitP11=P1^1;
sbitP12=P1^2;
sbitP13=P1^3;
sbitP14=P1^4;
//#defineAM(X) X
//#definePM(X) (X+12) //轉成24小時制
#defineDS1302_SECOND 0x80//秒寄存器
#defineDS1302_MINUTE 0x82//分寄存器
#defineDS1302_HOUR 0x84
#defineDS1302_WEEK 0x8A
#defineDS1302_DAY 0x86
#defineDS1302_MONTH 0x88
#defineDS1302_YEAR 0x8C
#defineDS1302_RAM(X) (0xC0+(X)*2) //用於計算DS1302_RAM地址的宏
voidDS1302InputByte(unsignedchard) //實時時鍾寫入一位元組(內部函數)
{
unsignedchari;
ACC=d;
for(i=8;i>0;i--)
{
DS1302_IO=ACC0; //相當於匯編中的RRC
DS1302_CLK=1;
DS1302_CLK=0;//發一個高跳變到低的脈沖
ACC=ACC>>1;
}
}
unsignedcharDS1302OutputByte(void) //實時時鍾讀取一位元組(內部函數)
{
unsignedchari;
for(i=8;i>0;i--)
{
ACC=ACC>>1; //相當於匯編中的RRC
ACC7=DS1302_IO;
DS1302_CLK=1;
DS1302_CLK=0;//發一個高跳變到低的脈沖
}
return(ACC);
}
voidWrite1302(unsignedcharucAddr,unsignedcharucDa) //ucAddr:DS1302地址,ucData:要寫的數據
{
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
DS1302InputByte(ucAddr); //地址,命令
DS1302InputByte(ucDa); //寫1Byte數據
DS1302_CLK=1;
DS1302_RST=0;//RST0->1->0,CLK0->1
}
unsignedcharRead1302(unsignedcharucAddr) //讀取DS1302某地址的數據
{
unsignedcharucData;
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;//enable
DS1302InputByte(ucAddr|0x01);//地址,命令
ucData=DS1302OutputByte();//讀1Byte數據
DS1302_CLK=1;//RST0->1->0,CLK0->1
DS1302_RST=0;
return(ucData);
}
voidDS1302_SetProtect(bitflag)//是否防寫
{
if(flag)
Write1302(0x8E,0x10);//WP=1,不能寫入
else
Write1302(0x8E,0x00);//WP=0,可以寫入
}
voidDS1302_SetTime(unsignedcharAddress,unsignedcharValue)//設置時間函數
{
DS1302_SetProtect(0);
Write1302(Address,((Value/10)<<4|(Value%10)));//高4位為十位,低4位為個位
}
voidDS1302_GetTime(SYSTEMTIME*Time)
{
unsignedcharReadValue;
ReadValue=Read1302(DS1302_SECOND);
Time->Second=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);//轉換成10進制的秒
ReadValue=Read1302(DS1302_MINUTE);
Time->Minute=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_HOUR);
Time->Hour=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_DAY);
Time->Day=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_WEEK);
Time->Week=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_MONTH);
Time->Month=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_YEAR);
Time->Year=((ReadValue&0xf0)>>4)*10+(ReadValue&0x0F);
}
voidInitial_DS1302(void)
{
unsignedcharSecond=Read1302(DS1302_SECOND);
if(Second&0x80)//如果第七為1(表明沒有啟動),則啟動時鍾
DS1302_SetTime(DS1302_SECOND,0);
}
voiddelay(unsignedchari)//延時子程序
{
unsignedcharj;
while((i--)!=0)
{
for(j=625;j>0;j--);
}
}
/*unsignedcharbutton_time(n,x,y)//時鍾調整子程序
unsignedcharn,x,y;
{
if(P1^7==0)
{
delay(50);
if(P1^7==0)
{
n++;
if(n==x)
n=0;
while(P1^7==0);
}
}
if(P1^1==0)
{
delay(50);
if(P1^1==0)
{
if(n==0)
n=y;
else
n--;
while(P1^1==0);
}
}
returnn;
}
*/
/*unsignedcharbutton_date(n,x,y)//日期調整子程序
unsignedcharn,x,y;
{
if(P1^7==0)
{
delay(50);
if(P1^7==0)
{
n++;
if(n==x)
n=1;
while(P1^7==0);
}
}
if(P1^1==0)
{
delay(50);
if(P1^1==0)
{
if(n==1)
n=y;
else
n--;
while(P1^1==0);
}
}
returnn;
}*/
voiddisplay1(minute10,minute1,second10,second1) //顯示第一頁分秒子程序
//unsignedcharsecond10,second1,minute10,minute1;
{
P2=0x08;
P0=dispcode[second1];//顯示秒的個位
delay(1);
P2=0x04;
P0=dispcode[second10]; //顯示秒的十位
delay(1);
P2=0x02;
P0=dispcode[minute1]; //顯示分的個位
delay(1);
P2=0x01;
P0=dispcode[minute10];//顯示分的十位
delay(1);
}
voiddisplay2(data10,data1,hour10,hour1) //顯示第二頁天時子程序
//unsignedchardata10,data1,hour10,hour1;
{
P2=0xf8;
P0=dispcode[data1];//顯示天的個位
delay(1);
P2=0xf4;
P0=dispcode[data10]; //顯示天的十位
delay(1);
P2=0xf2;
P0=dispcode[hour1]; //顯示時的個位
delay(1);
P2=0xf1;
P0=dispcode[hour10];//顯示時的十位
delay(1);
}
voiddisplay3(year10,year1,month10,month1) //顯示第三頁年月子程序
//unsignedcharyear10,year1,month10,month1;
{
P2=0xf2;
P0=dispcode[month1];//顯示月的個位
delay(1);
P2=0xf1;
P0=dispcode[month10]; //顯示月的十位
delay(1);
P2=0xf8;
P0=dispcode[year1]; //顯示月的個位
delay(1);
P2=0xf4;
P0=dispcode[year10];//顯示月的十位
delay(1);
}
voidmain()
{
unsignedcharflag=0;
Initial_DS1302(); //初始化DS1302這個時鍾晶元,
P10=0;//點亮測試燈
while(1)
{
DS1302_GetTime(&time1); //讀取時間參數
time[3]=(time1.Second)%10; //把秒的個位數據存入time[3]
time[2]=(time1.Second)/10; //把秒的十位數據存入time[2]
time[1]=(time1.Minute)%10; //把分的個位數據存入time[1]
time[0]=(time1.Minute)/10; //把分的十位數據存入time[0]
date[3]=(time1.Day)%10;
date[2]=(time1.Day)/10;
date[1]=(time1.Hour)%10;
date[0]=(time1.Hour)/10;
year[3]=(time1.Year)%10;
year[2]=(time1.Year)/10;
year[1]=(time1.Month)%10;
year[0]=(time1.Month)/10;
//display1(time[0],time[1],time[2],time[3]);
if(P11==0)
{
delay(50);
if(P11==0)
{
flag++;
if(flag>2)//flag:1顯示第二頁日時;2顯示第三頁年月0:顯示第一頁分秒
{
flag=0;
}
}
while(P11==0);
}
/*if(P1^6==0) //如果按下TimeSet鍵一下,開始顯示日期,再按一下進入日期跟時鍾的調節模式
{
delay(50);
if(P1^6==0)
{
flag++;
if(flag>6)
{
flag=0;
}
}
while(P1^6==0);
}*/
switch(flag)
{
case0:display1(time[0],time[1],time[2],time[3]); //調用子函數display,把存入數組time的數據給顯示出來
break;
case1:display2(date[0],date[1],date[2],date[3]); //調用子函數display,把存入數組date的數據給顯示出來
break;
case2:display3(year[0],year[1],year[2],year[3]);
break;
/* case3:time1.Month=button_date(time1.Month,13,12); //調整月
DS1302_SetTime(0x88,time1.Month);
display(10,10,date[2],date[3]);
break;
case4:time1.Day=button_date(time1.Day,32,31); //調整日
DS1302_SetTime(0x86,time1.Day);
display(10,10,date[4],date[5]);
break;
case5:time1.Minute=button_time(time1.Minute,60,59); //調整分
DS1302_SetTime(0x82,time1.Minute);
display(time[2],time[3],10,10);
break;
case6:time1.Second=button_time(time1.Second,60,59); //調整秒
DS1302_SetTime(0x80,time1.Second);
display(10,10,time[4],time[5]);
break;*/
}
}
}
㈨ 求單片機電子時鍾程序設計
#include "../inc/DS1307.H"
#include "../inc/delay.H"
#include "../inc/i2c.H" unsigned char idata Sec,Min,Hr,Dy,Dt,Mn,Yr;
bit hour_mode=0;
/********************************************************************************
函數功能: 數碼轉換
bcd:BCD碼 z:為1轉換高位,否則轉換低位 Data:二進制數
********************************************************************************/
unsigned char BCD_ASCII(unsigned char bcd,bit z) //把BCD碼轉換為ASCII碼
{
unsigned char temp;
if(z)
temp=(bcd>>4)+'0'; //+48
else temp=(bcd&0x0f)+'0'; //+48
return temp;
}
unsigned char CHAR_BCD(unsigned char Data) //把二進制數轉換為BCD碼
{
unsigned char bcd; if(Data>100) Data=0;
bcd = Data/10;
bcd<<=4;
bcd = bcd+(Data%10);
return bcd;
}
/********************************************************************************
函數功能: 判斷日期是否合法
a:年(00-99之間) b:月 d:日
********************************************************************************/
bit Leap_year(unsigned char n) //判斷是否為閏年 計算范圍2000-2099年
{
if((((2000+n)%4 == 0)&&((2000+n)%100 != 0)) || ((2000+n)%400 == 0))
return 0;
else return 1; //是閏年返回1
}
bit Valid_date(unsigned char a,unsigned char b,unsigned char c)
{
unsigned char flag=1; if((b<=0)||(b>12)||(c<=0)||(c>31)) flag=0;
if(((b==4)||(b==6)||(b==9)||(b==11))&&(c>30)) flag=0;
if(Leap_year(a)) if((b==2)&&(c>29)) flag=0;
else if(b==2&&c>28) flag=0;
return flag; //合法返回1
}
/********************************************************************************
函數功能: 初始化時間和日期
********************************************************************************/
void RTC_init(void)
{
i2c_start();
i2c_sendbyte(ADDRTC); //寫器件地址
i2c_waitACK();
i2c_sendbyte(0); // 地址指針置為 0
i2c_waitACK();
i2c_sendbyte(Sec&0x7f); //CH(Sec^7)位置為 0,啟動時鍾
i2c_waitACK();
i2c_stop();
i2c_start();
i2c_sendbyte(ADDRTC);
i2c_waitACK();
i2c_sendbyte(2); // 地址指針置為 2
i2c_waitACK();
if(hour_mode)
i2c_sendbyte(Hr|0x40); // 12小時模式,Hr^6置1
else i2c_sendbyte(Hr&0xbf); // 24小時模式,Hr^6置0
i2c_waitACK();
i2c_stop();
i2c_start();
i2c_sendbyte(ADDRTC);
i2c_waitACK();
i2c_sendbyte(7); // 地址指針置為 7
i2c_waitACK();
i2c_sendbyte(0x10); // sqwe引腳1Hz的輸出
i2c_waitACK();
i2c_stop();
}
/********************************************************************************
函數功能: 寫RTC寄存器
********************************************************************************/
void write_RTC_time(void)
{
i2c_start();
i2c_sendbyte(ADDRTC); // 寫從機地址(器件地址)
i2c_waitACK();
i2c_sendbyte(0); // 地址指針置為 0
i2c_waitACK();
i2c_sendbyte(Sec); // Sec
i2c_waitACK();
i2c_sendbyte(Min); // Min
i2c_waitACK();
i2c_sendbyte(Hr); // Hr
i2c_waitACK();
i2c_sendbyte(Dy); // Dy
i2c_waitACK();
i2c_sendbyte(Dt); // Dt
i2c_waitACK();
i2c_sendbyte(Mn); // Mn
i2c_waitACK();
i2c_sendbyte(Yr); // Yr
i2c_waitACK();
i2c_stop();
}/********************************************************************************
********************************************************************************/
㈩ 51單片機簡易電子鍾設計。匯編語言編寫
KEYVALEQU 30H
KEYTMEQU 31H
KEYSCANEQU 32H
DATEQU 33H
SCANLEDEQU 39H
CLKEQU 77H
SECEQU 78H
MINEQU 79H
HOUREQU 7AH
PAUSEBIT 00H
DOTBIT 01H
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T0ISR;50ms定時
ORG 001BH
LJMP T1ISR;掃描顯示
ORG 0030H
MAIN:
MOV SP,#5FH
MOV TMOD,#11H
MOV TH0,#03CH
MOV TL0,#0B0H
MOV TH1,#0ECH
MOV TL1,#078H
MOV KEYVAL,#0
MOV SCANLED,#0
MOV 33H,#10H
MOV 34H,#10H
MOV 35H,#10H
MOV 36H,#10H
MOV 37H,#10H
MOV 38H,#10H
MOV SEC,#0
MOV MIN,#0
MOV HOUR,#0
MOV CLK,#0
CLR PAUSE
SETB EA
SETB ET1
SETB TR1
LOOP:
LCALL KEYSEL
MOV A,KEYVAL
CJNE A,#0FFH,LOOP1
SJMP LOOP
LOOP1:
CJNE A,#10,LOOP2;「ON」啟動
SETB TR0
SETB ET0
SETB PAUSE
SJMP LOOP
LOOP2:
CJNE A,#11,LOOP3;「=」清零
MOV SEC,#0
MOV MIN,#0
MOV HOUR,#0
LCALL DISCHG
SJMP LOOP
LOOP3:
CJNE A,#15,LOOP4;「+」暫停
CLR TR0
CLR ET0
CLR PAUSE
SJMP LOOP
LOOP4:
CJNE A,#14,LOOP5;「-」清顯示暫停
MOV 33H,#10H
MOV 34H,#10H
MOV 35H,#10H
MOV 36H,#10H
MOV 37H,#10H
MOV 38H,#10H
CLR TR0
CLR ET0
CLR PAUSE
SJMP LOOP
LOOP5:
CJNE A,#10,LOOP6;數字鍵
LOOP6:
JC LOOP7
LJMP LOOP
LOOP7:
JNB PAUSE,LOOP8;暫停狀態可以輸入數字鍵
LJMP LOOP
LOOP8:
MOV 33H,34H
MOV 34H,35H
MOV 35H,36H
MOV 36H,37H
MOV 37H,38H
MOV 38H,KEYVAL
MOV A,33H
SWAP A
ORL A,34H
LCALL BCDH
MOV HOUR,A
MOV A,35H
SWAP A
ORL A,36H
LCALL BCDH
MOV MIN,A
MOV A,37H
SWAP A
ORL A,38H
LCALL BCDH
MOV SEC,A
LJMP LOOP
;------------------
;BCD轉換為十六進制
BCDH:
MOV B,#10H
DIV AB
MOV R7,B
MOV B,#10
MUL AB
ADD A,R7
RET
;------------------
;十六進制轉換為BCD
HBCD:
MOV B,#10
DIV AB
SWAP A
ORL A,B
RET
;------------------
KEYSEL:
MOVKEYVAL,#0
MOVKEYSCAN,#0EFH
LCALLGETKEY
MOVA,KEYTM
JZKEYS1
MOVKEYVAL,A
SJMPKEYRTN
KEYS1:
MOVKEYSCAN,#0DFH
LCALLGETKEY
MOVA,KEYTM
JZKEYS2
CLRC
ADDA,#4
MOVKEYVAL,A
SJMPKEYRTN
KEYS2:
MOVKEYSCAN,#0BFH
LCALLGETKEY
MOVA,KEYTM
JZKEYS3
CLRC
ADDA,#8
MOVKEYVAL,A
SJMPKEYRTN
KEYS3:
MOVKEYSCAN,#7FH
LCALLGETKEY
MOVA,KEYTM
JZKEYRTN
CLRC
ADDA,#12
MOVKEYVAL,A
KEYRTN:
LCALL CHGKEY
RET
;--------------------
GETKEY:
MOV KEYTM,#0
MOVA,KEYSCAN
MOVP3,A
NOP
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY
MOV R2,#10
LCALL DELAY
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY
MOVA,P3
ANLA,#0FH
MOVR7,A
SF:
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JNZSF
MOVA,R7
CJNEA,#0EH,NK1
MOVKEYTM,#1
SJMPNOKEY
NK1:
CJNEA,#0DH,NK2
MOVKEYTM,#2
SJMPNOKEY
NK2:
CJNEA,#0BH,NK3
MOVKEYTM,#3
SJMPNOKEY
NK3:
CJNEA,#07H,NOKEY
MOVKEYTM,#4
NOKEY:RET
;--------------------
DELAY:
MOV R3,#50
DELAY1:
MOV R4,#100
DJNZ R4,$
DJNZ R3,DELAY1
DJNZ R2,DELAY
RET
;--------------------
T0ISR:
PUSH ACC
CLR TR0
MOV TH0,#3CH
MOV TL0,#0B0H
SETB TR0
INC CLK
MOV A,CLK
CJNE A,#20,T0ISRE
MOV CLK,#0
INC SEC
MOV A,SEC
CJNE A,#60,T0ISRE
MOV SEC,#0
INC MIN
MOV A,MIN
CJNE A,#60,T0ISRE
MOV MIN,#0
INC HOUR
MOV A,HOUR
CJNE A,#24,T0ISRE
MOV SEC,#0
MOV MIN,#0
MOV HOUR,#0
T0ISRE:
LCALL DISCHG
POP ACC
RETI
;--------------------
DISCHG:
MOV A,HOUR
LCALL HBCD
PUSH ACC
ANL A,#0FH
MOV 34H,A
POP ACC
ANL A,#0F0H
SWAP A
MOV 33H,A
MOV A,MIN
LCALL HBCD
PUSH ACC
ANL A,#0FH
MOV 36H,A
POP ACC
ANL A,#0F0H
SWAP A
MOV 35H,A
MOV A,SEC
LCALL HBCD
PUSH ACC
ANL A,#0FH
MOV 38H,A
POP ACC
ANL A,#0F0H
SWAP A
MOV 37H,A
RET
;--------------------
T1ISR:
PUSH ACC
CLR TR1
MOV TH1,#0ECH
MOV TL1,#78H
SETB TR1
MOV DPTR,#LEDTAB
T100:
MOV R0,#DAT
MOV A,SCANLED
ADD A,R0
MOV R0,A
MOV A,SCANLED
JNZ T101
MOV P2,#01H
CLR DOT
SJMP T1DIS
T101:
DEC A
JNZ T102
MOV P2,#02H
SETB DOT
SJMP T1DIS
T102:
DEC A
JNZ T103
MOV P2,#04H
CLR DOT
SJMP T1DIS
T103:
DEC A
JNZ T104
MOV P2,#08H
SETB DOT
SJMP T1DIS
T104:
DEC A
JNZ T105
MOV P2,#10H
CLR DOT
SJMP T1DIS
T105:
MOV P2,#20H
CLR DOT
T1DIS:
MOV A,@R0
MOVC A,@A+DPTR
JNB DOT,T1DIS1
ORL A,#01H
T1DIS1:
CPL A
MOV P0,A
INC SCANLED
MOV A,SCANLED
CJNE A,#6,T1END
MOV SCANLED,#0
T1END:
POP ACC
RETI
;--------------------
CHGKEY:
MOV A,KEYVAL
JZ KV16
DEC A
JNZ KV01
MOV KEYVAL,#7
RET
KV01:
DEC A
JNZ KV02
MOV KEYVAL,#4
RET
KV02:
DEC A
JNZ KV03
MOV KEYVAL,#1
RET
KV03:
DEC A
JNZ KV04
MOV KEYVAL,#10
RET
KV04:
DEC A
JNZ KV05
MOV KEYVAL,#8
RET
KV05:
DEC A
JNZ KV06
MOV KEYVAL,#5
RET
KV06:
DEC A
JNZ KV07
MOV KEYVAL,#2
RET
KV07:
DEC A
JNZ KV08
MOV KEYVAL,#0
RET
KV08:
DEC A
JNZ KV09
MOV KEYVAL,#9
RET
KV09:
DEC A
JNZ KV10
MOV KEYVAL,#6
RET
KV10:
DEC A
JNZ KV11
MOV KEYVAL,#3
RET
KV11:
DEC A
JNZ KV12
MOV KEYVAL,#11
RET
KV12:
DEC A
JNZ KV13
MOV KEYVAL,#12
RET
KV13:
DEC A
JNZ KV14
MOV KEYVAL,#13
RET
KV14:
DEC A
JNZ KV15
MOV KEYVAL,#14
RET
KV15:
DEC A
JNZ KV16
MOV KEYVAL,#15
RET
KV16:
MOV KEYVAL,#0FFH
RET
;--------------------
LEDTAB:DB 0FCH;"0"00H
DB 60H;"1"01H
DB 0DAH;"2"02H
DB 0F2H;"3"03H
DB 66H;"4"04H
DB 0B6H;"5"05H
DB 0BEH;"6"06H
DB 0E0H;"7"07H
DB 0FEH;"8"08H
DB 0F6H;"9"09H
DB 0EEH;"A"0AH
DB 3EH;"B"0BH
DB 9CH;"C"0CH
DB 7AH;"D"0DH
DB 9EH;"E"0EH
DB 8EH;"F"0FH
DB 00H;" "10H
;--------------------
END