/**********************************************《郭天祥新概念51單片機(jī)》P188,有改動(dòng)。利用定時(shí)器產(chǎn)生0-99計(jì)時(shí)秒表,每過(guò)1秒,將計(jì)數(shù)的的數(shù)值寫(xiě)入AT24c02,關(guān)閉電源再打開(kāi),單片機(jī)將原來(lái)寫(xiě)入的值讀出來(lái),接著此數(shù)繼續(xù)按秒變化,并顯示在數(shù)碼管上**********************************************//**********************************************數(shù)碼管顯示:數(shù)據(jù)P0 段選P2^6 位選 P2^7 AT24C02:地址設(shè)置為1010111x x為0表示寫(xiě) x為1表示讀SDA==P2^0 SCL==P2^1 WP接GND(表示允許讀/寫(xiě))**********************************************/#includereg51.h>#define uchar unsigned char#define uint unsigned intbit write=0; //寫(xiě)24C02的標(biāo)志;sbit sda=P2^0; sbit scl=P2^1;sbit dula=P2^6;sbit wela=P2^7;uchar sec,tcnt; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};/*******************************************延時(shí)函數(shù)*******************************************/void delay(){ ;; }void delay1ms(uint z){uint x,y;for(x=z;x>0;x--)for(y=120;y>0;y--);}/*******************************************開(kāi)始信號(hào)*******************************************/void start() { sda=1;delay();scl=1;delay();sda=0;delay();}/*******************************************停止信號(hào)*******************************************/void stop() {sda=0;delay();scl=1;delay();sda=1;delay();}/*******************************************應(yīng)答信號(hào)*******************************************/void respons() {uchar i;scl=1;delay();while((sda==1)(i250)) i++;scl=0;delay();}/*******************************************總線初始化*******************************************/void init(){sda=1;delay();scl=1;delay();}/*******************************************寫(xiě)入一個(gè)字節(jié)*******************************************/void write_byte(uchar date){uchar i,temp;temp=date;for(i=0;i8;i++){temp=temp1;scl=0;delay();sda=CY;delay();scl=1;delay();}scl=0;delay();sda=1;delay();}/*******************************************讀出一個(gè)字節(jié)*******************************************/uchar read_byte(){uchar i,k;scl=0;delay();sda=1;delay();for(i=0;i8;i++){scl=1;delay(); k=(k1)|sda;scl=0;delay(); }return k;}
/*******************************************指定地址寫(xiě)入數(shù)據(jù)*******************************************/void write_add(uchar address,uchar date){start();write_byte(0xae);respons();write_byte(address);respons();write_byte(date);respons();stop();}/*******************************************指定地址讀出數(shù)據(jù)*******************************************/uchar read_add(uchar address){uchar date;start();write_byte(0xae);respons();write_byte(address);respons();start();write_byte(0xaf);respons();date=read_byte();stop();return date;}/*******************************************顯示程序*******************************************/void display(uchar bai_c,uchar sh_c) {dula=0;P0=table[bai_c]; //顯示第一位dula=1;dula=0;wela=0;P0=0xfe;wela=1;wela=0;delay1ms(5);dula=0;P0=table[sh_c]; //顯示第二位dula=1;dula=0;wela=0;P0=0xfd;wela=1;wela=0;delay1ms(5);}/*******************************************MAIN*******************************************/void main(){init();sec=read_add(2); //讀出保存的數(shù)據(jù)賦于secif(sec>100) //防止首次讀取出錯(cuò)誤數(shù)據(jù)sec=0;TMOD=0x01; //定時(shí)器工作在方式1ET0=1; EA=1;TH0=(65536-50000)/256; //對(duì)TH0 TL0賦值TL0=(65536-50000)%256; //使定時(shí)器0.05秒中斷一次TR0=1; //開(kāi)始計(jì)時(shí)while(1){display(sec/10,sec%10);if(write==1) //判斷計(jì)時(shí)器是否計(jì)時(shí)一秒{write=0; //清零write_add(2,sec); //在24c02的地址2中寫(xiě)入數(shù)據(jù)sec}} }/*******************************************Interrupt service routine*******************************************/void t0() interrupt 1 //定時(shí)中斷服務(wù)函數(shù){TH0=(65536-50000)/256; //對(duì)TH0 TL0賦值TL0=(65536-50000)%256; //重裝計(jì)數(shù)初值tcnt++; //每過(guò)50ms tcnt加一if(tcnt==20) //計(jì)滿20次(1秒)時(shí){tcnt=0; //重新再計(jì)sec++;write=1; //1秒寫(xiě)一次24C02if(sec==100)//定時(shí)100秒,再?gòu)牧汩_(kāi)始計(jì)時(shí)sec=0;}}
評(píng)論