|
本帖最后由 xiao_yp2014 于 2016-1-21 14:33 編輯
功能需求:
開機后兩位數(shù)碼管顯示0到99之間的某一個數(shù)值,用一個按鍵可以不斷的遞增,用另外一個按鍵可以不斷的遞減。斷電后此數(shù)據(jù)可保存,下次通電 開機的時候還是從那個保存的數(shù)據(jù)開始顯示。
程序如下:
main.c
- /*EEPROM篇--利用AT24C02存儲數(shù)據(jù)
- 功能需求:
- 開機后兩位數(shù)碼管顯示0到99之間的某一個數(shù)值,用一個按鍵可以不斷的遞增,
- 用另外一個按鍵可以不斷的遞減。斷電后此數(shù)據(jù)可保存,下次通電開機的時候
- 還是從那個保存的數(shù)據(jù)開始顯示。
- */
- #include<reg52.h> //頭文件
- #include"AT24C02.h" //IIC自定義頭文件
- #include"Key_Scan.h" //按鍵自定義頭文件
- #include"display.h" //數(shù)碼管顯示自定義頭文件
- sbit beep_dr = P3^6; //蜂鳴器
- unsigned int uiVoiceCnt = 0; //蜂鳴器時間變量
- unsigned char ucSetNumber = 0; //設(shè)定值變量
-
- void Init_All(); //初始化全部程序
- void Timer_T0(); //TO定時器中斷
- void main()
- {
- Init_All(); //初始化全部
- while(1)
- {
- display(); //調(diào)用數(shù)碼管顯示函數(shù)
- Key_Service(); //調(diào)用按鍵服務(wù)函數(shù),放在主函數(shù)里
- }
- }
- void Init_All()
- {
- TH0=0xf8; //重裝初始值(65535-2000)=63535=0xf82f
- TL0=0x2f;
- EA = 1;
- ET0 = 1;
- TR0=1; //開中斷
- ucSetNumber = read_eeprom(128); //上電開機讀取EEPROM數(shù)據(jù),128是一個我任意選的一個存儲地址
- if(ucSetNumber > 99)
- {
- write_eeprom(128,0); //往一個地址存入一個字節(jié)數(shù)據(jù),128是一個我任意選的一個存儲地址
- }
- }
- void Timer_T0() interrupt 1
- {
- TF0=0; //清除中斷標志
- TR0=0; //關(guān)中斷
- Key_Scan(); //按鍵掃描放在中斷里
- if(uiVoiceCnt!=0) //蜂鳴器
- {
- uiVoiceCnt--; //每次進入定時中斷都自減1,直到等于零為止。才停止鳴叫
- beep_dr = 0; //蜂鳴器是PNP三極管控制,低電平就開始鳴叫
- }
- else
- {
- beep_dr = 1; //蜂鳴器是PNP三極管控制,高電平就停止鳴叫。
- }
- TH0=0xf8; //重裝初始值(65535-2000)=63535=0xf82f
- TL0=0x2f;
- TR0=1; //開中斷
- }
復(fù)制代碼
AT24C02.c
AT24C02.h
- #ifndef __AT24C02_H__
- #define __AT24C02_H__
- #include<reg52.h>
- unsigned char read_eeprom(unsigned int address); //從一個地址讀取出一個字節(jié)數(shù)據(jù)
- void start24(); //開始位
- void ack24(); //確認位
- void stop24(); //停止位
- unsigned char read24(); //讀取一個字節(jié)的時序
- void write24(unsigned char dd); //發(fā)送一個字節(jié)的時序
- void write_eeprom(unsigned int address,unsigned char dd); //往一個地址存入一個字節(jié)數(shù)據(jù)
- void Delay_time(unsigned int Delay11_MS); //延時程序
- #endif
復(fù)制代碼
Key_Scan.c
- #include"Key_Scan.h" //按鍵自定義頭文件
- #include"AT24C02.h" //IIC自定義頭文件
- #define const_KeyTimeLevel 100 //按鍵消抖的時間,可以修改
- #define const_VoiceCntLevel 150 //蜂鳴器鳴叫的時間
- sbit Key_s1 = P1^6; //數(shù)據(jù)加
- sbit Key_s2 = P1^7; //數(shù)據(jù)減
- unsigned int uiKeyTimeCnt1 = 0; //按鍵延時計數(shù)器
- unsigned int uiKeyTimeCnt2 = 0; //按鍵延時計數(shù)器
- unsigned char ucKeyLock1 = 0; //按鍵自鎖標志位,防止按鍵一直觸發(fā)
- unsigned char ucKeyLock2 = 0; //按鍵自鎖標志位,防止按鍵一直觸發(fā)
- unsigned char KeySec = 0; //按鍵編號
- void Key_Scan() //按鍵掃描程序
- {
- if(Key_s1 == 1)
- {
- uiKeyTimeCnt1 = 0; //延時計數(shù)器清零
- ucKeyLock1 = 0; //自鎖標志位清零
- }
- else if(ucKeyLock1 == 0) //當有按鍵按下
- {
- uiKeyTimeCnt1++; //延時計數(shù)器加1
- if(uiKeyTimeCnt1 >= const_KeyTimeLevel) //去抖動時間到
- {
- uiKeyTimeCnt1 = 0; //延時計數(shù)器清零
- ucKeyLock1 = 1; //按鍵自鎖
- KeySec = 1; //觸發(fā)1號鍵
- }
- }
- if(Key_s2 == 1)
- {
- uiKeyTimeCnt2 = 0; //延時計數(shù)器清零
- ucKeyLock2 = 0; //自鎖標志位清零
- }
- else if(ucKeyLock2 == 0) //當有按鍵按下
- {
- uiKeyTimeCnt2++; //延時計數(shù)器加1
- if(uiKeyTimeCnt2 >= const_KeyTimeLevel) //去抖動時間到
- {
- uiKeyTimeCnt2 = 0; //延時計數(shù)器清零
- ucKeyLock2 = 1; //按鍵自鎖
- KeySec = 2; //觸發(fā)2號鍵
- }
- }
- }
- void Key_Service() //按鍵服務(wù)函數(shù)
- {
- switch(KeySec)
- {
- case 0:break;
- case 1:
- ucSetNumber++; //設(shè)定值自加1
- if(ucSetNumber >= 99) //當加到99,就賦值成0
- {
- ucSetNumber = 0; //賦值
- }
- write_eeprom(128,ucSetNumber); //往一個地址存入一個字節(jié)數(shù)據(jù)
- uiVoiceCnt = const_VoiceCntLevel;//蜂鳴器
- KeySec = 0; //清零按鍵標志
- break; //跳出
-
- case 2:
- ucSetNumber--; //設(shè)定值自減1
- if(ucSetNumber>=99) //減到大于99,賦值成99
- {
- ucSetNumber = 99;
- }
- write_eeprom(128,ucSetNumber); //往一個地址存入一個字節(jié)數(shù)據(jù)
- uiVoiceCnt = const_VoiceCntLevel; //蜂鳴器
- KeySec = 0; //清零按鍵標志位
- break;
- default : break;
- }
- }
復(fù)制代碼
Key_Scan.h
- #ifndef __Key_Scan_H__
- #define __Key_Scan_H__
- #include<reg52.h>
- void Key_Scan(); //按鍵掃描程序
- void Key_Service(); //按鍵服務(wù)程序
- extern unsigned int uiVoiceCnt; //變量全局聲名
- extern unsigned char ucSetNumber; //變量全局聲名
- extern unsigned char ucSetNumber; //變量全局聲名extern可置于變量或者函數(shù)前,以表示變量或者函數(shù)的定義在別的文件中,
- //提示編譯器遇到此變量和函數(shù)時在其他模塊中尋找其定義
- #endif
復(fù)制代碼
Display.c
- #include"display.h" //顯示頭文件
- #include"delay.h" //延時頭文件
- unsigned char code array[]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
- void display()
- {
- static unsigned char ucNumber_left = 0; //左邊數(shù)碼管顯示的實際內(nèi)容
- static unsigned char ucNumber_right = 0; //右邊數(shù)碼管顯示的實際內(nèi)容
- static unsigned char ucDisplayStep = 1; //顯示步驟切換變量
- if(ucSetNumber >= 10) //在顯示的數(shù)值小于10時,左邊數(shù)碼管不顯示
- {
- ucNumber_left = ucSetNumber/10; //得到左邊顯示的值
- }
- else
- {
- ucNumber_left = 10; //小于10就不顯示
- }
- ucNumber_right = ucSetNumber%10; //右邊數(shù)碼管顯示的值
- switch(ucDisplayStep)
- {
- case 1: //左邊數(shù)碼管顯示驅(qū)動
- P0 = array[ucNumber_left];
- P2 = 0xfe;
- ucDisplayStep = 2;
- delay_10ms();
- P2 = 0xff;
- break;
- case 2:
- P0 = array[ucNumber_right]; //右邊數(shù)碼管顯示驅(qū)動
- P2 = 0xfd;
- ucDisplayStep = 1;
- delay_10ms();
- P2 = 0xff;
- break;
- }
- }
復(fù)制代碼
Display.h
- #ifndef __display_h__
- #define __display_h__
- #include<reg52.h>
- void display();
- extern unsigned char ucIICDataTemp; //聲名全局變量
- extern unsigned char ucSetNumber; //聲名全局變量
- #endif
復(fù)制代碼
delay.c
- #include"delay.h"
- void delay_10ms() //延時函數(shù)
- {
- unsigned int x,y;
- for(x = 0;x<10;x++)
- for(y = 0;y<10;y++);
-
- }
復(fù)制代碼
delay.h
- #ifndef __delay_h__
- #define __delay_h__
- void delay_10ms(); //函數(shù)聲名
- #endif
復(fù)制代碼
打包程序下載——>
EEPROM篇—用AT24C02數(shù)據(jù)保存數(shù)據(jù).zip
(108.42 KB, 下載次數(shù): 264)
2015-1-10 23:55 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|