0
这是iic模块的源文件
#include"iic.h"
#include
#include
***it SCL = P2^1;
***it SDA = P2^0;
/**************************/
//名 称:Delay5us()
//功 能:延时5us
//入口参数:无
//出口参数:无
/**************************/
void Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
}
/**************************/
//名 称:IIC_Init()
//功 能:初始化iic
//入口参数:无
//出口参数:无
/**************************/
void IIC_Init()
{
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}
/**************************/
//名 称:IIC_Start()
//功 能:启动iic
//入口参数:无
//出口参数:无
/**************************/
void IIC_Start()
{
SDA = 1;
Delay5us();
SCL = 1;
Delay5us();
SDA = 0;
Delay5us();
}
/**************************/
//名 称:IIC_Stop()
//功 能: 停止iic
//入口参数:无
//出口参数:无
/**************************/
void IIC_Stop()
{
SDA = 0;
Delay5us();
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}
/**************************/
//名 称:IIC_ACK()
//功 能: 应答信号
//入口参数:无
//出口参数:无
/**************************/
void IIC_ACK()
/* SCL = 0;
SDA = 0;
Delay5us();
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
SDA = 1;
*/
{
uChar8 i; /*应答前 时钟为低 数据为高*/
SCL = 1;
Delay5us();
while((SDA == 1)&&(i<200)) i++;
SCL = 0;
Delay5us(); /*应答完后 scl 为高 sda 为 低*/
}
/**************************/
//名 称:IIC_WriteByte(uChar8 DataToIIC )
//功 能: 向总线写入1byte数据
//入口参数:uChar8 DataToIIC
//出口参数:无
/**************************/
void IIC_WriteByte(uChar8 DataToIIC)
{
uChar8 i,Temp;
Temp = DataToIIC;
for(i = 0; i < 8 ; i++)
{
/* SCL = 0;
Delay5us();
SDA = DataToIIC & 0x80;
DataToIIC = DataToIIC << 1 ;
SCL = 1;
Delay5us();
*/
Temp = Temp << 1;
SCL = 0;
Delay5us();
SDA = CY;
Delay5us();
SCL = 1;
Delay5us();
}
SCL =0;
Delay5us();
SDA = 1;
Delay5us();
}
/**************************/
//名 称:uChar8 IIC_ReadByte( )
//功 能: 从iic总线读出1byte数据
//入口参数:无
//出口参数:DataFromIIC
/**************************/
uChar8 IIC_ReadByte()
{
uChar8 i,j,DataFromIIC;
SCL = 0;
Delay5us();
SDA = 1;
Delay5us();
for(i = 0; i < 8;i++)
{
SCL = 1;
Delay5us();
j = SDA;
DataFromIIC = (DataFromIIC<<1)|j;
SCL = 0;
Delay5us();
}
return(DataFromIIC);
}
这是iic模块的头文件
#ifndef __IIC_H__
#define __IIC_H__
typedef unsigned char uChar8;
typedef unsigned int uInt16;
extern void IIC_Init();
extern void IIC_Start();
extern void IIC_Stop();
extern void IIC_ACK();
extern void IIC_WriteByte(uChar8 DataToIIC);
extern uChar8 IIC_ReadByte();
#endif
这是at24c02模块的源文件
#include
#include"iic.h"
#include"at24c02.h"
/********************************************************/
//名 称:WriteByteToAt24c02(uChar8 DataAdd,uChar8 DataWriteToAt24c02)
//功 能: 向at24c02写入1byte数据
//入口参数:uChar8 DataWriteToAt24c02 ,DataAdd
//出口参数:无
/*******************************************************/
void WriteByteToAt24c02(uChar8 DataAdd,uChar8 DataWriteToAt24c02)
{
IIC_Start();
IIC_WriteByte(0xa0);
IIC_ACK();
IIC_WriteByte(DataAdd);
IIC_ACK();
IIC_WriteByte(DataWriteToAt24c02);
IIC_ACK();
IIC_Stop();
}
/********************************************************/
//名 称:uChar8 ReadByteFromAt24c02(uChar8 DataAdd)
//功 能: 从at24c02读出1byte数据
//入口参数:uChar8 DataAdd
//出口参数:DataFromAt24c02
/********************************************************/
uChar8 ReadByteFromAt24c02(uChar8 DataAdd)
{
uChar8 DataFromAt24c02;
IIC_Start();
IIC_WriteByte(0xa0);
IIC_ACK();
IIC_WriteByte(DataAdd);
IIC_ACK();
IIC_Start();
IIC_WriteByte(0xa1);
IIC_ACK();
DataFromAt24c02 = IIC_ReadByte();
IIC_ACK();
IIC_Stop();
return(DataFromAt24c02);
}
这是at24c02模块的头文件
#ifndef __AT24CO2_H__
#define __AT24C02_H__
extern void WriteByteToAt24c02(uChar8 DataAdd,uChar8 DataWriteToAt24c02);
extern uChar8 ReadByteFromAt24c02(uChar8 DataAdd);
#endif
这是延时模块的头文件
#ifndef __DELAYMS_H__
#define __DELAYMS_H__
extern void DelayMs(unsigned int Val);
#endif
这是延时模块的源文件
/**************************/
//名 称:DelayMs(unsigned int Val)
//功 能:延时Val毫秒
//入口参数:unsigned int Val
//出口参数:无
/**************************/
#include"delayms.h"
void DelayMs(unsigned int Val)
{
unsigned char i,j;
for(i = Val; i > 0 ;i--)
for(j = 110; j > 0;j--);
}
这是显示模块的源文件
#include
#include"display.h"
#include"delayms.h"
***it Dula=P2^6;
***it Wela=P2^7;
unsigned char code segment[]={//共阴极0~f数码管编码
0x3f,0x06,0x5b,0x4f, //0~3
0x66,0x6d,0x7d,0x07, //4~7
0x7f,0x6f,0x77,0x7c, //8~b
0x39,0x5e,0x79,0x71 //c~f
};
/*******************************************************/
//函数名称:void Display(unsigned char Val1,unsigned char Val2)
//功 能:数码管显示送入的两个数据
//入口参数:unsigned char Val1,unsigned char Val2
//出口参数:无
/******************************************************/
void Display(unsigned char Val1,unsigned char Val2)
{
Wela = 1; //显示十位
P0 = 0xfe;
Wela = 0;
Dula = 1;
P0 =segment[Val1];
Dula = 0;
DelayMs(40);
P0 = 0xff;
Wela = 1;
P0 = 0xfd;
Wela = 0;
Dula = 1;
P0 = segment[Val2]; //显示个位
Dula = 0;
DelayMs(40);
P0 = 0xff;
}
这是显示模块的头文件
#ifndef __DISPLAY_H__
#define __DISPLAY_H__
extern void Display(unsigned char Val1,unsigned char Val2);
#endif
最后是main函数
/****************************************/
/*功 能:记录单片机的开机次数 */
/*晶 振:11.0592MHZ */
/****************************************/
#include
#include"iic.h"
#include"at24c02.h"
#include"delayms.h"
#include"display.h"
int main()
{
uChar8 Boottimes; //开机次数
uChar8 TensDigit,OnesDigit; //用于显示的十位和个位
IIC_Init();
//以下下两句在第一次烧录单片机时使用。后面就可以注释掉
// WriteByteToAt24c02(5,0x00); //对原有的at24c02的第5位进行清零。
// DelayMs(300);
BootTimes = ReadByteFromAt24c02(5);
BootTimes++;
WriteByteToAt24c02(5,BootTimes);
DelayMs(300);
BootTimes = BootTimes - 1;
OnesDigit = BootTimes%10;
TensDigit = BootTimes/10;
while(1)
{
Display(TensDigit,OnesDigit);
}
return 0;
}
|
|