實(shí)驗(yàn)7 利用51單片機(jī)的定時(shí)器設(shè)計(jì)一個(gè)時(shí)鐘
=============Clock.h=============
#ifndef __CLOCK_H__
#define __CLOCK_H__
//========全局變量區(qū)============================================
unsignedcharT_High_50ms=(65536-45872)/256;
unsignedintT_Low_50ms=(65536-45872)%256;
unsignedcharCount,Count_T1,Count_1s;//Count用來(lái)記錄每50ms的計(jì)數(shù),Count_T1用來(lái)記
//========全局變量區(qū)結(jié)束============================================
#endif
=============I2C.H=============
#ifndef __I2C_H__
#define __I2C_H__
#include
sbit sda=P2^0;
sbit scl=P2^1;
//========函數(shù)區(qū)============================================
voidnop();
voidinitI2C();
externvoidI2C_start();//開(kāi)始信號(hào)
externvoidI2C_stop();//停止
externvoidI2C_respons();//應(yīng)答
externvoidI2C_write_byte(unsignedchardate);
externunsignedcharI2C_read_byte();
externvoidI2C_write_address(unsignedcharaddress,unsignedchardate);
externunsignedcharI2C_read_address(unsignedcharaddress);
//========函數(shù)區(qū)結(jié)束============================================
#endif
=============OrphanKey.h=============
#ifndef __ORPHANKEY_H__
#define __ORPHANKEY_H__
sbit FUNCTION_KEY=P3^2;//功能鍵--按下暫停,再按開(kāi)始
sbit Key_ADD=P3^3;//加1
sbit KEY_MINUS=P3^4;//減1
constbit PRESSED=0;//按下
bit SUSPEND=0;//0的時(shí)候運(yùn)行,1的時(shí)候暫停
#endif
=============I2C.C=============
#include "head/I2C.h"
//=========全局變量區(qū)============================================
#define uchar unsigned char
#define uint unsigned int
bit I2C_write=0;//寫(xiě)24C02的標(biāo)志;
unsignedcharsec,tcnt;
//=========全局變量區(qū)結(jié)束============================================
voidnop()
{;;}
/***********************************************************
I2C的初始化
***********************************************************/
voidinitI2C(){
sda=1;
nop();
scl=1;
nop();
}
voidI2C_start()//開(kāi)始信號(hào)
{
sda=1;
nop();
scl=1;
nop();
sda=0;
nop();
}
voidI2C_stop()//停止
{
sda=0;
nop();
scl=1;
nop();
sda=1;
nop();
}
voidI2C_respons()//應(yīng)答
{
uchar i;
scl=1;
nop();
while((sda==1)&&(i<250))i++;
scl=0;
nop();
}
voidI2C_write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
nop();
sda=CY;
nop();
scl=1;
nop();
}
scl=0;
nop();
sda=1;
nop();
}
uchar I2C_read_byte()
{
uchar i,k;
scl=0;
nop();
sda=1;
nop();
for(i=0;i<8;i++)
{
scl=1;
nop();
k=(k<<1)sda;
scl=0;
nop();
}
returnk;
}
voidI2C_write_address(uchar address,uchar date)
{
I2C_start();
I2C_write_byte(0xa0);
I2C_respons();
I2C_write_byte(address);
I2C_respons();
I2C_write_byte(date);
I2C_respons();
I2C_stop();
}
uchar I2C_read_address(uchar address)
{
uchar date;
I2C_start();
I2C_write_byte(0xa0);
I2C_respons();
I2C_write_byte(address);
I2C_respons();
I2C_start();
I2C_write_byte(0xa1);
I2C_respons();
date=I2C_read_byte();
I2C_stop();
returndate;
}
=============lab8_1.c=================
#include
#include "head/Clock.h"
#include "head/OrphanKey.h"
#include "head/I2C.H"
/*
1.利用實(shí)驗(yàn)板和配件,設(shè)計(jì)一個(gè)時(shí)鐘,時(shí)間顯示在LCD1602上,并按秒更新,能夠在實(shí)驗(yàn)板上設(shè)計(jì)3個(gè)按鍵調(diào)整時(shí),分,秒。其功能為:功能選擇鍵,數(shù)值增大和數(shù)值減小鍵。利用板上AT24C02設(shè)計(jì)實(shí)現(xiàn)斷電保護(hù)顯示數(shù)據(jù)的功能。
*/
/*
步驟:
1、設(shè)計(jì)一個(gè)中斷,用來(lái)計(jì)時(shí)
2、設(shè)計(jì)一個(gè)字符生成函數(shù),用來(lái)生成所需的時(shí)間
3、獨(dú)立按鍵監(jiān)測(cè)功能,用來(lái)監(jiān)測(cè)按下了什么鍵
4、一個(gè)外部中斷
5、一個(gè)斷點(diǎn)保護(hù)的功能,其實(shí)就是通過(guò)I2C寫(xiě)入,讀取數(shù)值
*/
//=========全局變量區(qū)============================================
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="12:23:12";
uchar code table1[]="I am a boy!";
sbit lcden=P2^7;//液晶使能端
sbit lcdrs=P2^6;//數(shù)據(jù)或命令控制(0代表命令,1代表數(shù)據(jù))
sbit LCDWR=P2^5;//讀寫(xiě)控制(0代表寫(xiě),1代表讀)
uchar num;
uchar FIRST_LINE=0x80;
uchar SECOND_LINE=0xc0;
uchar Current_Time[9];
uchar Hour,Minute,Second=0;//時(shí)、分、秒
sbit beer=P1^4;//蜂鳴器
// sbit led1=P1^3;
//=========全局變量區(qū)結(jié)束============================================
//=========函數(shù)區(qū)============================================
/***********************************************************
延時(shí)函數(shù)
參數(shù)說(shuō)明:
z代表要延時(shí)z ms
***********************************************************/
voiddelay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***********************************************************
寫(xiě)命令或數(shù)據(jù)
參數(shù)說(shuō)明:
isRs=1代表寫(xiě)數(shù)據(jù),isRs=0代表寫(xiě)命令
***********************************************************/
voidwrite(uchar mdata,uchar isRs)
{
lcdrs=isRs;
P0=mdata;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/***********************************************************
寫(xiě)命令
參數(shù)說(shuō)明:
data代表要寫(xiě)的命令
***********************************************************/
voidwrite_command(uchar com)
{
write(com,0);
}
/***********************************************************
寫(xiě)數(shù)據(jù)
參數(shù)說(shuō)明:
data代表要寫(xiě)的數(shù)據(jù)
***********************************************************/
voidwrite_data(uchar mdata)
{
write(mdata,1);
}
/***********************************************************
初始化時(shí)鐘
***********************************************************/
voidInitTime(){
//時(shí)間的轉(zhuǎn)換
Current_Time[0]=Hour/10+0;
Current_Time[1]=Hour%10+0;
Current_Time[2]=:;
Current_Time[3]=Minute/10+0;
Current_Time[4]=Minute%10+0;
Current_Time[5]=:;
Current_Time[6]=Second/10+0;
Current_Time[7]=Second%10+0;
Current_Time[8]=