|
為了幫助老年人在用藥時(shí)比較容易忘記的難題,本人特結(jié)合用藥特點(diǎn),設(shè)計(jì)了本程序。本程序特點(diǎn):
程序有四個(gè)按鍵,其功能分別為:
1、用藥一次,即可按此鍵,以便總用藥次數(shù)加1;
2、用藥次數(shù)有誤時(shí),可按此鍵,讓用藥次數(shù)減1;
3、用藥次數(shù)置零;
3、用藥次數(shù)及時(shí)間清零,并啟用時(shí)間計(jì)時(shí)器,計(jì)時(shí)器自00-00-00,時(shí)-分-秒格式計(jì)時(shí)計(jì)數(shù)
本程序功能有:
1、程序硬件有AT24C02存儲(chǔ)器,以便對(duì)每次用藥的參數(shù)進(jìn)行存儲(chǔ),關(guān)閉電源后,相關(guān)數(shù)據(jù)不會(huì)丟失;
2、程序硬件有時(shí)間芯片DS1302,可通過按鍵,啟用時(shí)間計(jì)時(shí),方便記錄類似于熱敷、浸泡類事項(xiàng)的時(shí)間;
3、相關(guān)信息,通過液晶1602顯示出來。
4、本程序是單片機(jī)愛好者的練習(xí)之作,亦可為家里老人盡一份孝心的小物件。
單片機(jī)源程序如下:
- #include<reg51.h>
- #include"lcd.h"
- #include"ds1302.h"
- #include "i2c.h"
- #include "delay.h"
- #include "string.h" /*利用C語言庫函數(shù)memcpy可以整體復(fù)制。memcpy聲明與string.h中,形式為:
- void * memcpy(void *dst, void *src, int length);
- 其功能為將src地址上長度為length字節(jié)的數(shù)據(jù),復(fù)制到dst上。 */
- void LcdDisplay(unsigned char addr);
- void Scan_KEY();
- void num_display(void);
- sbit KEY1=P1^3; //加1 四個(gè)按鍵共了,其再與74LS21與門并聯(lián),相與后聯(lián)接P3.2口,實(shí)現(xiàn)INT0中斷
- sbit KEY2=P1^4; //減1
- sbit KEY3=P1^5; //次數(shù)置0
- sbit KEY4=P1^6; //次數(shù)與時(shí)間均置0
- sbit LED1=P2^5; //按鍵1存儲(chǔ)指示燈
- bit KeyPressFlag; //定義按鍵標(biāo)志位
- //---存儲(chǔ)順序是 秒 分 時(shí) 日 月 周 年, 存儲(chǔ)格式是用BCD碼---//
- //uchar TIME2[7] = {10, 20, 0x00, 0x07, 0x07, 0x07, 0x68};
- //uchar *jxhj[]= {"Mon","Tues","Wed","Thur","Fri","Sat","Sun"};
- unsigned char num=0; //用于記錄次數(shù)
-
- void main()
- {
- IRcvStr(0xae,0,&num,1); //&符號(hào)的作用是取一個(gè)變量的地址。
- LcdInit();
- Ds1302Init();
- EA=1; //中斷總開關(guān)
- EX0=1; //外部中斷INT0使能
- IT0=1; //下降沿觸發(fā) IT0=0為低電平觸發(fā)
- while(1)
- {
- if(KeyPressFlag==1)
- {
- KeyPressFlag=0; //按鍵標(biāo)志清零,以便下次檢測
- Scan_KEY();
- }
- else
- {
- Ds1302ReadTime(); //讀取時(shí)間參數(shù)
- LcdDisplay(0x00); //第一行地址是0x00,第二行是0x40;
- LcdWriteCom(0xC0);
- LcdWriteData('F');
- LcdWriteData('R');
- LcdWriteData('E');
- LcdWriteData('Q');
- LcdWriteData('U');
- LcdWriteData('E');
- LcdWriteData('N');
- LcdWriteData('C');
- LcdWriteData('Y');
- LcdWriteData(':');
- LcdWriteCom(0xC0+13); //移動(dòng)光標(biāo)指令,0x80是固定指令值,后面是移動(dòng)的絕對(duì)值
- LcdWriteData(num/10%10+0x30);
- LcdWriteData(num%10+0x30); //+0x30就是把個(gè)位數(shù)值轉(zhuǎn)換成 字符0~9的ASCII碼
- }
- }
-
- }
- /*******************************************************************************
- * 函數(shù)名 : LcdDisplay()
- * 函數(shù)功能 : 顯示函數(shù)
- * 輸入 : 無
- * 輸出 : 無
- *******************************************************************************/
- void LcdDisplay(unsigned char addr)
- {
- // LcdWriteCom(0x80); //第一行地址
- /* LcdWriteData('1'); //確定日期的年代轉(zhuǎn)換
- LcdWriteData('9');
- LcdWriteData('0'+TIME[6]/16); //年
- LcdWriteData('0'+(TIME[6]&0x0f));
- LcdWriteData('-');
- LcdWriteData('0'+TIME[4]/16); //月
- LcdWriteData('0'+(TIME[4]&0x0f));
- LcdWriteData('-');
- LcdWriteData('0'+TIME[3]/16); //日
- LcdWriteData('0'+(TIME[3]&0x0f));
- LcdWriteCom(0x80+13); //移動(dòng)光標(biāo)指令,前面是固定的,后面是移動(dòng)的數(shù)值
- LcdWriteData('0'+(TIME[5]&0x07)); //星期
- */
- LcdWriteCom(addr+0X80); //書寫的位置
- LcdWriteData('S');
- LcdWriteData('T');
- LcdWriteData('A');
- LcdWriteData('R');
- LcdWriteData('T');
- LcdWriteData(':');
- LcdWriteCom(0x80+7);
- LcdWriteData('0'+TIME[2]/16); //時(shí) ds1302輸出的是壓縮BCD碼,除16就得到十位數(shù)的10進(jìn)制數(shù)
- LcdWriteData('0'+(TIME[2]&0x0f)); //8位壓縮BCD碼,即高四位是一個(gè)0-9的BCD碼,低四位亦同樣是一個(gè)BCD碼
- LcdWriteData('-');
- LcdWriteData('0'+TIME[1]/16); //分
- LcdWriteData('0'+(TIME[1]&0x0f)); //相與后保留低四位,即個(gè)位
- LcdWriteData('-');
- LcdWriteData('0'+TIME[0]/16); //秒
- LcdWriteData('0'+(TIME[0]&0x0f)); //數(shù)值同0x0f相與后就是個(gè)位數(shù)了,這只對(duì)BCD碼有效。
- }
- void Scan_KEY()
- {
- unsigned char i;
- if(KEY1==0) //按鍵按下
- {
- DelayMs(20); //延時(shí),去抖
- if(KEY1==0) //確認(rèn)按鍵按下
- {
- for(i=0;i<100;i++) //此處消抖程序非常經(jīng)典,怎么按都絕對(duì)不會(huì)產(chǎn)生抖動(dòng)。
- {
- if(KEY1==0) i=0;
- } //自下面開始寫程序
- if(num<100) //加操作 次數(shù)控制在99以內(nèi)
- {
- num++;
- ISendStr(0xa0,0,&num,1); //寫入24c02
- DelayMs(10);
- num_display();
- }
- }
- }
- if(KEY2==0) //按鍵按下
- {
- DelayMs(20); //延時(shí),去抖
- if(KEY2==0) // //確認(rèn)按鍵按下
- {
- for(i=0;i<100;i++) //此處消抖程序非常經(jīng)典,怎么按都絕對(duì)不會(huì)產(chǎn)生抖動(dòng)。而且直到松手后再執(zhí)行后面程序
- {
- if(KEY2==0) i=0;
- }
- if(num>0) //減操作
- {
- num--;
- ISendStr(0xa0,0,&num,1); //寫入24c02
- DelayMs(10);
- num_display();
- }
- }
- }
- if(KEY3==0) //按鍵按下
- {
- DelayMs(20); //延時(shí),去抖
- if(KEY3==0) // //確認(rèn)按鍵按下
- {
- for(i=0;i<100;i++) //此處消抖程序非常經(jīng)典,怎么按都絕對(duì)不會(huì)產(chǎn)生抖動(dòng)。
- {
- if(KEY3==0) i=0;
- }
- Ds1302Init();
- LcdDisplay(0x40); //第一行地址是0x00,第二行是0x40;
- }
- }
- if(KEY4==0) //按鍵按下
- {
- DelayMs(20); //延時(shí),去抖
- if(KEY4==0) // //確認(rèn)按鍵按下
- {
- for(i=0;i<100;i++) //此處消抖程序非常經(jīng)典,怎么按都絕對(duì)不會(huì)產(chǎn)生抖動(dòng)。
- {
- if(KEY4==0)i=0;
- }
- Ds1302Init();
- LcdDisplay(0x40); //第一行地址是0x00,第二行是0x40;
- num=0;
- ISendStr(0xa0,0,&num,1); //寫入24c02
- num_display();
- }
- }
- }
- void num_display(void) //專門用來顯示num值的
- {
- LcdDisplay(0x40); //第一行地址是0x00,第二行是0x40;
- LcdWriteCom(0xC0+13); //移動(dòng)光標(biāo)指令,0x80是固定指令值,后面是移動(dòng)的絕對(duì)值
- LcdWriteData(num/10%10+0x30);
- LcdWriteData(num%10+0x30); //+0x30就是把個(gè)位數(shù)值轉(zhuǎn)換成字符了
- }
- /*INT0中斷函數(shù) */
- void counter0(void) interrupt 0 //INT0 是四個(gè)按鍵接入74LS21四與門輸入,然后輸出口接入P3.2口
- {
- EX0=0; //關(guān)閉中斷
- KeyPressFlag=1; //表明按鍵有動(dòng)作,可以進(jìn)行按鍵掃描
- EX0=1; //再次打開中斷
- }
- /*------------------------------------------------
- 定時(shí)器中斷子程序
- ------------------------------------------------*/
- void Timer0_isr(void) interrupt 1 //定時(shí)對(duì)液晶屏數(shù)值進(jìn)行更新
- {
- TH0=(65536-12000)/256; //重新賦值 2ms
- TL0=(65536-2000)%256;
- LcdDisplay(0x00); //第一行地址是0x00,第二行是0x40;
- LcdWriteCom(0x80+11); //移動(dòng)光標(biāo)指令,后面是移動(dòng)的絕對(duì)值
- LcdWriteData(num%10+0x30); //+0x30就是把個(gè)位數(shù)值轉(zhuǎn)換成字符了
- }
復(fù)制代碼
所有代碼51hei提供下載:
老年人用藥助記神器.rar
(66.16 KB, 下載次數(shù): 22)
2019-9-6 19:38 上傳
點(diǎn)擊文件名下載附件
練習(xí)中斷 計(jì)時(shí) LCD1602液晶顯示 DS1302時(shí)間芯片 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|