如果有用请点赞,还会继续更新的
题目:
思路:
这个程序用到的iic总线技术完成了EEPROM断电存储和ADC的模拟,然后通过了DS1302完成了时钟单元,这个程序最困扰我的就是蜂鸣器了,不得不写了一个子函数,确实没有想到其他可行的好方法,如果各位大佬有,欢迎留言
源代码:
main.c
#include"reg52.h"
#include"intrins.h"
#include"iic.h"
#include"ds1302.h"
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
sbit buz=P0^6;
sbit L1=P0^0;
sbit L2=P0^1;
char code xianshi[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
unsigned int code write_add[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned int code read_add[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
int Time[3]={0x00,0x30,0x08};
int smg_mode=0;
int shidu=0;
int temp_buzz=0;
int init_number=50;
unsigned char add=0x00;
void choose_573(int n)
{
switch(n)
{
case(0):P2=(P2&0x1f)|0x00;break;
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
}
}
void Init_system()
{
choose_573(4);
P0=0xff;
choose_573(5);
P0=0x00;
choose_573(0);
P0=0xff;
}
//---------------------------------ds1302
void write_time()
{
int i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_Ds1302_Byte (write_add[i],Time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void read_time()
{
int i;
for(i=0;i<3;i++)
{
Time[i]=Read_Ds1302_Byte(read_add[i]);
}
}
//---------------------------------
//---------------------------------湿度读取
void read_shidu()
{
shidu=read_v();
shidu=(shidu*99)/255;
}
//---------------------------------
//--------------------------------- 按键
int k6=0;
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void key_board()
{
if(S7==0)
{
Delay5ms();
if(S7==0)
{
if(smg_mode==0)
{
smg_mode=1;
choose_573(4);
P0=0xff;
choose_573(0);
}
else
{
smg_mode=0;
}
}
while(!S7);
}
if(S6==0&&smg_mode==1)
{
Delay5ms();
if(S6==0&&smg_mode==1)
{
if(temp_buzz==0)
{
temp_buzz=1;
}
else
{
temp_buzz=0;
}
}
while(!S6);
}
if(S5==0&&smg_mode==1)
{
Delay5ms();
if(S5==0&&smg_mode==1)
{
choose_573(4);
P0=0xfd;
choose_573(0);
}
while(!S5);
}
if(S4==0&&smg_mode==1)
{
Delay5ms();
if(S4==0&&smg_mode==1)
{
choose_573(4);
P0=0xff;
choose_573(0);
}
while(!S4);
}
if(S6==0&&smg_mode==0)
{
Delay5ms();
if(S6==0&&smg_mode==0)
{
if(k6==0)
{
k6=1;
}
else
{
k6=0;
write_EEPROM(add,init_number);
}
}
while(!S6);
}
if(S5==0&&smg_mode==0&&k6==1)
{
Delay5ms();
if(S5==0&&smg_mode==0&&k6==1)
{
init_number++;
write_EEPROM(add,init_number);
}
while(!S5);
}
if(S4==0&&smg_mode==0&&k6==1)
{
Delay5ms();
if(S4==0&&smg_mode==0&&k6==1)
{
init_number--;
write_EEPROM(add,init_number);
}
while(!S4);
}
}
//---------------------------------
//---------------------------------显示
void Delay300us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 4;
j = 54;
do
{
while (--j);
} while (--i);
}
void SMG(int wei,int number)
{
choose_573(6);
P0=0x80>>(wei-1);
choose_573(7);
P0=xianshi[number];
choose_573(0);
P0=0xff;
}
void SMG_display()
{
if(smg_mode==0&&k6==0)
{
SMG(1,shidu%10);
Delay300us();
SMG(2,shidu/10);
Delay300us();
SMG(3,10);
Delay300us();
SMG(4,Time[1]%16);
Delay300us();
SMG(5,Time[1]/16);
Delay300us();
SMG(6,11);
Delay300us();
SMG(7,Time[2]%16);
Delay300us();
SMG(8,Time[2]/16);
Delay300us();
}
if(smg_mode==0&&k6==1)
{
SMG(1,init_number%10);
Delay300us();
SMG(2,init_number/10);
Delay300us();
SMG(3,10);
Delay300us();
SMG(4,10);
Delay300us();
SMG(5,10);
Delay300us();
SMG(6,10);
Delay300us();
SMG(7,11);
Delay300us();
SMG(8,11);
Delay300us();
}
if(smg_mode==1)
{
SMG(1,shidu%10);
Delay300us();
SMG(2,shidu/10);
Delay300us();
SMG(3,10);
Delay300us();
SMG(4,Time[1]%16);
Delay300us();
SMG(5,Time[1]/16);
Delay300us();
SMG(6,11);
Delay300us();
SMG(7,Time[2]%16);
Delay300us();
SMG(8,Time[2]/16);
Delay300us();
}
}
//---------------------------------
//--------------------------------- 工作
void work()
{
if(smg_mode==0)
{
choose_573(4);
P0=0xfe;
choose_573(0);
P0=0xff;
}
}
void buzz_diaplay()
{
if(temp_buzz==0)
{
choose_573(5);
P0=0x00;
choose_573(0);
}
if(temp_buzz==1)
{
choose_573(5);
P0=0xff;
choose_573(0);
}
}
//---------------------------------
void main()
{
Init_system();
write_time();
init_number=read_EEPROM(add);
while(1)
{
SMG_display();
buzz_diaplay();
read_shidu();
read_time();
key_board();
work();
}
}
iic.c
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
void write_EEPROM(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_EEPROM(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
unsigned int read_v()
{
unsigned int temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
void write_EEPROM(unsigned char add,unsigned char dat);
unsigned char read_EEPROM(unsigned char add);
unsigned int read_v();
#endif
ds1302.c
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif
**
码字不容易,点个赞再走!!!!
**
同时也欢迎大家积极留言,指出错误,和提出更好的解决方案!!!