實(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用來記錄每50ms的計(jì)數(shù),Count_T1用來記
//========全局變量區(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();//開始信號
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;//功能鍵--按下暫停,再按開始
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;//寫24C02的標(biāo)志;
unsignedcharsec,tcnt;
//=========全局變量區(qū)結(jié)束============================================
voidnop()
{;;}
/***********************************************************
I2C的初始化
***********************************************************/
voidinitI2C(){
sda=1;
nop();
scl=1;
nop();
}
voidI2C_start()//開始信號
{
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è)中斷,用來計(jì)時(shí)
2、設(shè)計(jì)一個(gè)字符生成函數(shù),用來生成所需的時(shí)間
3、獨(dú)立按鍵監(jiān)測功能,用來監(jiān)測按下了什么鍵
4、一個(gè)外部中斷
5、一個(gè)斷點(diǎn)保護(hù)的功能,其實(shí)就是通過I2C寫入,讀取數(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;//讀寫控制(0代表寫,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ù)說明:
z代表要延時(shí)z ms
***********************************************************/
voiddelay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***********************************************************
寫命令或數(shù)據(jù)
參數(shù)說明:
isRs=1代表寫數(shù)據(jù),isRs=0代表寫命令
***********************************************************/
voidwrite(uchar mdata,uchar isRs)
{
lcdrs=isRs;
P0=mdata;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/***********************************************************
寫命令
參數(shù)說明:
data代表要寫的命令
***********************************************************/
voidwrite_command(uchar com)
{
write(com,0);
}
/***********************************************************
寫數(shù)據(jù)
參數(shù)說明:
data代表要寫的數(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]=看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();