標(biāo)題:
單片機(jī)定時器0+MXA7219數(shù)碼管顯示+AT24C02時鐘斷電累計(jì)工作計(jì)時 源程序
[打印本頁]
作者:
a123470
時間:
2022-5-12 08:47
標(biāo)題:
單片機(jī)定時器0+MXA7219數(shù)碼管顯示+AT24C02時鐘斷電累計(jì)工作計(jì)時 源程序
#include<STC15.h> // 引用標(biāo)準(zhǔn)庫的頭文件
#include <intrins.h> //包含_nop_()函數(shù)定義的頭文件
sbit SCL=P4^3;
sbit SDA=P4^4;
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define uchar unsigned char // 定義一下方便使用
#define uint unsigned int
unsigned char cntb=0,n=0,m=0,k=0,ns=0,ks=0,n1=0,k1=0,k2=0;
sbit EC16_KEY=P1^3; //EC16編碼器按鍵信號輸入定義
sbit DIN = P2^7; // 串行數(shù)據(jù)輸入
sbit CLK = P2^5; // 串行時鐘
sbit LOAD = P2^6; // 顯示數(shù)據(jù)鎖存控制
sbit led=P2^4;
/****************************
MAX7219特殊功能寄存器定義
************************/
#define DECODE_MODE 0x09 // 譯碼模式寄存器
#define INTENSITY 0x0a // 亮度寄存器
#define SCAN_LIMIT 0x0b // 掃描位數(shù)寄存器
#define SHUT_DOWN 0x0c // 低功耗模式寄存器
#define DISPLAY_TEST 0x0f // 顯示測試寄存器
void Write_Max7219_byte(unsigned char temp);
void Write_Max7219(unsigned char address,unsigned char dat);
void Init_Max7219(void);
void delay_msIIC(unsigned int time);
void Init_interrupt();
void show();
void SGM_showLJ() ;
void I2cStart();
void I2cStop();
unsigned char I2cSendByte(unsigned char dat);
unsigned char I2cReadByte();
void At24c02Write(unsigned char addr,unsigned char dat);
unsigned char At24c02Read(unsigned char addr);
/****************************
共陰二位十腳數(shù)碼管段碼數(shù)組
******************************/
uchar code LEDcode[]=
{0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b /*0,1,2,3,4,5,6,7,8,9*/
/* 0x77,0x1f,0x4e,0x3d,0x4f,0x47,0x67,0x3e,0xff, */ }; /*A,B,C,D,E,F,P,U,全亮*/
/*******************************************************************************
* 函數(shù)名 : Delay10us()
* 函數(shù)功能 : 延時10us
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Delay10us_IIC()
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
/*******************************************************************************
* 函數(shù)名 : I2cStart()
* 函數(shù)功能 : 起始信號:在SCL時鐘信號在高電平期間SDA信號產(chǎn)生一個下降沿
* 輸入 : 無
* 輸出 : 無
* 備注 : 起始之后SDA和SCL都為0
*******************************************************************************/
void I2cStart()
{
SDA=1;
Delay10us_IIC();
SCL=1;
Delay10us_IIC();//建立時間是SDA保持時間>4.7us
SDA=0;
Delay10us_IIC();//保持時間是>4us
SCL=0;
Delay10us_IIC();
}
/*******************************************************************************
* 函數(shù)名 : I2cStop()
* 函數(shù)功能 : 終止信號:在SCL時鐘信號高電平期間SDA信號產(chǎn)生一個上升沿
* 輸入 : 無
* 輸出 : 無
* 備注 : 結(jié)束之后保持SDA和SCL都為1;表示總線空閑
*******************************************************************************/
void I2cStop()
{
SDA=0;
Delay10us_IIC();
SCL=1;
Delay10us_IIC();//建立時間大于4.7us
SDA=1;
Delay10us_IIC();
}
/*******************************************************************************
* 函數(shù)名 : I2cSendByte(unsigned char dat)
* 函數(shù)功能 : 通過I2C發(fā)送一個字節(jié)。在SCL時鐘信號高電平期間,保持發(fā)送信號SDA保持穩(wěn)定
* 輸入 : num
* 輸出 : 0或1。發(fā)送成功返回1,發(fā)送失敗返回0
* 備注 : 發(fā)送完一個字節(jié)SCL=0,SDA=1
*******************************************************************************/
unsigned char I2cSendByte(unsigned char dat)
{
unsigned char a=0,b=0;//最大255,一個機(jī)器周期為1us,最大延時255us。
for(a=0;a<8;a++)//要發(fā)送8位,從最高位開始
{
SDA=dat>>7; //起始信號之后SCL=0,所以可以直接改變SDA信號
dat=dat<<1;
Delay10us_IIC();
SCL=1;
Delay10us_IIC();//建立時間>4.7us
SCL=0;
Delay10us_IIC();//時間大于4us
}
SDA=1;
Delay10us_IIC();
SCL=1;
while(SDA)//等待應(yīng)答,也就是等待從設(shè)備把SDA拉低
{
b++;
if(b>200) //如果超過2000us沒有應(yīng)答發(fā)送失敗,或者為非應(yīng)答,表示接收結(jié)束
{
SCL=0;
Delay10us_IIC();
return 0;
}
}
SCL=0;
Delay10us_IIC();
return 1;
}
/*******************************************************************************
* 函數(shù)名 : I2cReadByte()
* 函數(shù)功能 : 使用I2c讀取一個字節(jié)
* 輸入 : 無
* 輸出 : dat
* 備注 : 接收完一個字節(jié)SCL=0,SDA=1.
*******************************************************************************/
unsigned char I2cReadByte()
{
unsigned char a=0,dat=0;
SDA=1; //起始和發(fā)送一個字節(jié)之后SCL都是0
Delay10us_IIC();
for(a=0;a<8;a++)//接收8個字節(jié)
{
SCL=1;
Delay10us_IIC();
dat<<=1;
dat|=SDA;
Delay10us_IIC();
SCL=0;
Delay10us_IIC();
}
return dat;
}
/*******************************************************************************
* 函數(shù)名 : void At24c02Write(unsigned char addr,unsigned char dat)
* 函數(shù)功能 : 往24c02的一個地址寫入一個數(shù)據(jù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void At24c02Write(unsigned char addr,unsigned char dat)
{
I2cStart();
I2cSendByte(0xa0);//發(fā)送寫器件地址
I2cSendByte(addr);//發(fā)送要寫入內(nèi)存地址
I2cSendByte(dat); //發(fā)送數(shù)據(jù)
I2cStop();
}
/*******************************************************************************
* 函數(shù)名 : unsigned char At24c02Read(unsigned char addr)
* 函數(shù)功能 : 讀取24c02的一個地址的一個數(shù)據(jù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
unsigned char At24c02Read(unsigned char addr)
{
unsigned char num;
I2cStart();
I2cSendByte(0xa0); //發(fā)送寫器件地址
I2cSendByte(addr); //發(fā)送要讀取的地址
I2cStart();
I2cSendByte(0xa1); //發(fā)送讀器件地址
num=I2cReadByte(); //讀取數(shù)據(jù)
I2cStop();
return num;
}
/******************
MAX7219讀數(shù)據(jù)子程序
*********************/
void Write_Max7219_byte(unsigned char temp)
{
unsigned char i;
for(i=0;i<8;i++) //移位循環(huán)8次
{
CLK=0; //清零時鐘總線
DIN=(bit)(temp&0x80); //每次取高字節(jié)亮
temp=temp<<1; //左移移位
CLK=1; //時鐘上升沿,發(fā)送高字節(jié)位
}
}
/*****************************
MAX7219寫數(shù)據(jù)子程序
*************************/
void Write_Max7219(unsigned char address,unsigned char dat)
{
LOAD=0; //拉低片選線,選中器件
Write_Max7219_byte(address); //讀取高字節(jié)地址位
Write_Max7219_byte(dat); //讀取高字節(jié)數(shù)據(jù)位
LOAD=1; //讀取結(jié)束,上升沿鎖存數(shù)據(jù)
}
/*****************************
MAX7219初始化子程序
*************************/
void Init_Max7219(void)
{
Write_Max7219(SHUT_DOWN,0x01); //開啟正常工作模式
Write_Max7219(DISPLAY_TEST,0x00); //選中工作模式
Write_Max7219(DECODE_MODE,0x00); //選用全譯碼模式
Write_Max7219(SCAN_LIMIT,0x07); //8只LED全用
Write_Max7219(INTENSITY,0x0a); //設(shè)置初始亮度
}
/*******************************
MAX7219顯示子程序
*******************************/
/********************************
ms級延時子程序
******************************/
void delay_msIIC(unsigned int time)
{
unsigned int i,j;
for(i=time;i>0;i--)
for(j=125;j>0;j--);
}
void show()
{
At24c02Write(1,n); //在地址1內(nèi)寫入分鐘數(shù)據(jù)n
delay_msIIC(30);
Write_Max7219(4,LEDcode[n%10]); //數(shù)碼管分鐘個位數(shù)值分離
delay_msIIC(1);
Write_Max7219(3,LEDcode[n%100/10]); //數(shù)碼管分鐘十位數(shù)值分離
delay_msIIC(1);
At24c02Write(3,k); //在地址3內(nèi)寫入小時數(shù)據(jù)k
delay_msIIC(30);
Write_Max7219(2,LEDcode[k%10]); //數(shù)碼管小時個位數(shù)值分離
delay_msIIC(1);
Write_Max7219(1,LEDcode[k%100/10]); //數(shù)碼管小時十位數(shù)值分離
delay_msIIC(1);
}
/********************************************************
MAX7219數(shù)碼管開機(jī)累計(jì)計(jì)時顯示分,小時數(shù)值分離子程序
********************************************************/
void SGM_showLJ()
{
led=0; //中間小數(shù)點(diǎn)信號為低電平
Write_Max7219(4,LEDcode[ns%10]); //數(shù)碼管編碼器脈沖計(jì)數(shù)值個位數(shù)值分離
delay_msIIC(1);
Write_Max7219(3,LEDcode[ns%100/10]); //數(shù)碼管編碼器脈沖計(jì)數(shù)值十位數(shù)值分離
delay_msIIC(1);
Write_Max7219(2,LEDcode[ks%10]); //數(shù)碼管編碼器脈沖計(jì)數(shù)值百位數(shù)值分離
delay_msIIC(1);
Write_Max7219(1,LEDcode[ks%100/10]); //數(shù)碼管編碼器脈沖計(jì)數(shù)值千位數(shù)值分離
delay_msIIC(1);
}
/**************************
定時器0初始化子程序
*****************************/
void Init_interrupt()
{
TMOD=0X01;
TH0=(65536-46080)/256; //定時器賦初值11.0592MHZ晶振,賦初值TH0=(65536- 1000*50*(11.0592/12)/256) 50ms
TL0=(65536-46080)/256;
ET0=1;
IT0=1;
IT1=1;
EX0=1;
EX1=1;
EA=1;
}
void t0(void) interrupt 1 using 0
{
TH0=(65536-46080)/256;
TL0=(65536-46080)/256;
cntb ++;
m++;
if(m==30) ////m執(zhí)行30次,led燈1s交替閃爍
{
led=~led;
m=0;
}
if(cntb==1200) //cntb執(zhí)行1200次,一分鐘時間到
{
n=n+1; //分鐘變量加1操作
cntb=0; //定時器計(jì)數(shù)變量清零
}
if(n>59) //分鐘 顯示大于59秒
{
k=k+1; //小時變量加1操作
n=0; //分鐘變量清零
}
if(k>99) //時間顯示大于99小時
{
k=99;
}
}
/***************************************************
函數(shù)功能主函數(shù)
***************************************************/
void main(void)
{ /**************************
定義特殊端口功能寄存器
**************************/
P0M0 = 0x00;
P0M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
Init_interrupt(); //定時器初始化
Init_Max7219(); //7219初始化
/*****************此段程序?yàn)殚_機(jī)讀取累計(jì)工作分鐘數(shù)據(jù)程序************/
n1=At24c02Read(1); //讀取EEPROM地址1內(nèi)的數(shù)據(jù)保存在n1中
delay_msIIC(30);
ns=At24c02Read(2); //讀取EEPROM地址2內(nèi)的數(shù)據(jù)保存在ns中
delay_msIIC(30);
ns=ns+n1;
if(ns>=60)
{
ns=ns-60; //累計(jì)計(jì)時60分鐘,累計(jì)變量分鐘-60,
k2=k2+1; //累計(jì)小時數(shù)加1
}
/*****************此段程序?yàn)殚_機(jī)讀取累計(jì)工作小時數(shù)據(jù)程序************/
k1=At24c02Read(3); //讀取EEPROM地址3內(nèi)的數(shù)據(jù)保存在k1中
delay_msIIC(30);
ks=At24c02Read(4); //讀取EEPROM地址4內(nèi)的數(shù)據(jù)保存在ks中
delay_msIIC(30);
ks=ks+k1+k2;
if(ks>=99)
ks=99; //累計(jì)計(jì)時99小時
SGM_showLJ(); //調(diào)用累計(jì)計(jì)時函數(shù)
/******************下列此程序?yàn)槔塾?jì)存儲分鐘變量程序**************/
At24c02Write(2,0); //在地址2內(nèi)寫入數(shù)據(jù)0,清除2地址
delay_msIIC(30);
At24c02Write(2,ns); //在地址2內(nèi)寫入數(shù)據(jù)ns
delay_msIIC(30);
At24c02Write(1,0); //在地址1內(nèi)寫入數(shù)據(jù)0,清除1地址
delay_msIIC(30);
n=0; //清零分鐘
/******************下列此程序?yàn)槔塾?jì)存儲分鐘變量程序**************/
At24c02Write(4,0); //在地址4內(nèi)寫入數(shù)據(jù)0,清除4地址
delay_msIIC(30);
At24c02Write(4,ks); //在地址4內(nèi)寫入數(shù)據(jù)ks
delay_msIIC(30);
At24c02Write(3,0); //在地址3內(nèi)寫入數(shù)據(jù)0,清除3地址
delay_msIIC(30);
k=0; //清零小時
while(1)
{
if(EC16_KEY==0) //檢測開機(jī)信號
{
TR0=1;
show(); //調(diào)用數(shù)碼管顯示
}
TR0=0;
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1