1.main.c文件
#include<stc15.h>
#include<intrins.h>
#include<IIC.H>
#define uchar unsigned char
#define uint unsigned int
unsigned char LED_Bit=0XFF;
unsigned char Actuator_Bit=0X00;
#define LEDx_ON(n) { LED_Bit&=_crol_(0XFE,n-1); P0=LED_Bit; P2|=0X80; P2&=0X9F; P2&=0X1F;}
#define LEDx_OFF(n) { LED_Bit|=_crol_(0X01,n-1); P0=LED_Bit; P2|=0X80; P2&=0X9F; P2&=0X1F;}
#define Buzzer_ON Actuator_Bit|=0x40; P0=Actuator_Bit; P2|=0XA0; P2&=0XBF; P2&=0X1F;
#define Buzzer_OFF Actuator_Bit&=0XBF; P0=Actuator_Bit; P2|=0XA0; P2&=0XBF; P2&=0X1F;
#define Relay_ON Actuator_Bit|=0x10; P0=Actuator_Bit; P2|=0XA0; P2&=0XBF; P2&=0X1F;
#define Relay_OFF Actuator_Bit&=0XEF; P0=Actuator_Bit; P2|=0XA0; P2&=0XBF; P2&=0X1F;
unsigned char code tab[]= {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,\
0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10,\
0XBF,0XFF};
unsigned char KEY_Value = 0 ;
unsigned char DigCom=0;
unsigned char DigBuf[8] = {10,10,10,10,10,10,10,10};
unsigned char LED = 1 ;
unsigned int LED_tt =0;
bit LED_Ref = 0 ;
unsigned int SEG_tt =0; //定义一个数码管计数时间标识位
bit SEG_Ref = 0 ; //定义一个数码管刷新标识位
bit SEG_Run = 0 ; //定义一个控制数码管运行标识位
unsigned int Num = 999 ;
unsigned char Open;
unsigned char EEPROM_Write[8] = {2,3,4,5,6,7,8,9};
unsigned char EEPROM_Read[8] = {0,0,0,0,0,0,0,0};
unsigned int AD_VALUE=0;
unsigned int AD_tt =0;
bit AD_Ref =0;
unsigned int XXX_tt =0;
bit XXX_Ref =0;
void IO_Init(void);
void ALL_Init(void);
void Delay_MS(unsigned int MS);
void KeyScan(void);
void ArrKeyScan(void);
void Timer0Init(void); //1毫秒@11.0592MHz
void main(void)
{
IO_Init();
ALL_Init();
Timer0Init();
EA=1;ET0=1;
// AT24C02_write(0X15,0);Delay_MS(1);
// Open = AT24C02_read(0X15); Delay_MS(5);
// AT24C02_write(0X15,++Open);Delay_MS(5);
// DigBuf[0]=Open/10;DigBuf[1]=Open%10;
// AT24C02_Write_Page(0X00);Delay_MS(5);
// AT24C02_Read_Page(0X00);Delay_MS(5);
// DigBuf[0]=EEPROM_Read[0];DigBuf[1]=EEPROM_Read[1];DigBuf[2]=EEPROM_Read[2];DigBuf[3]=EEPROM_Read[3];
// DigBuf[4]=EEPROM_Read[4];DigBuf[5]=EEPROM_Read[5];DigBuf[6]=EEPROM_Read[6];DigBuf[7]=EEPROM_Read[7];
while(1)
{
if(AD_Ref == 1)
{
AD_Ref = 0 ;
AD_VALUE = AD_read(0X01);//读取不同通道模拟信号的值
AD_write(AD_VALUE);
AD_VALUE = AD_VALUE* 1.96 ;
DigBuf[2]=21;DigBuf[3]=21;DigBuf[4]=21;
DigBuf[5]=AD_VALUE/100+ 10 ; DigBuf[6]=AD_VALUE%100/10;
DigBuf[7]=AD_VALUE%10;
}
KeyScan();
if(KEY_Value==7){KEY_Value=0;SEG_Run = 1 ;}
if(KEY_Value==6){KEY_Value=0;SEG_Run = 0 ;}
if(KEY_Value==5){KEY_Value=0;LEDx_ON(1);Buzzer_ON;}
if(KEY_Value==4){KEY_Value=0;LEDx_OFF(1);Buzzer_OFF;}
}
}
void KeyScan(void)
{
if(P30==0)
{
Delay_MS(10);
if(P30==0)KEY_Value = 7 ;
while(!P30);
}
else if(P31==0)
{
Delay_MS(10);
if(P31==0)KEY_Value = 6 ;
while(!P31);
}
else if(P32==0)
{
Delay_MS(10);
if(P32==0)KEY_Value = 5 ;
while(!P32);
}
else if(P33==0)
{
Delay_MS(10);
if(P33==0)KEY_Value = 4 ;
while(!P33);
}
}
void Timer0(void) interrupt 1
{
P0=0X00;
P2|=0XC0; // P2=P2|0XC0; XXXX XXXX | 1100 0000 = 11XX XXXX
P2&=0XDF; // P2=P2&0XDF; 11XX XXXX & 1101 1111 = 110X XXXX
P2&=0X1F; //关闭所有的74HC573锁存器
P0=tab[DigBuf[DigCom]];
P2|=0XE0; // P2=P2|0XE0; XXXX XXXX | 1110 0000 = 111X XXXX
P2&=0XFF; // P2=P2&0XDF; 11XX XXXX & 1101 1111 = 110X XXXX
P2&=0X1F; //关闭所有的74HC573锁存器
P0=(0X01<<DigCom); //然后选中第一个数码管
P2|=0XC0; // P2=P2|0XC0; XXXX XXXX | 1100 0000 = 11XX XXXX
P2&=0XDF; // P2=P2&0XDF; 11XX XXXX & 1101 1111 = 110X XXXX
P2&=0X1F; //关闭所有的74HC573锁存器
if(++DigCom == 8)DigCom = 0 ;
LED_tt++;
if(LED_tt == 999) {LED_tt = 0 ; LED_Ref = 1 ;}
if(++SEG_tt==1000){SEG_tt=0;SEG_Ref=1;}
if(++AD_tt==500){AD_tt=0;AD_Ref=1;}
// XXX_tt++;
// if(++XXX_tt==NNN){XXX_tt=0;XXX_Ref=1;}
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初始值
TH0 = 0xD4; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Delay_MS(unsigned int MS)
{
unsigned i,j;
for(i=0;i<MS;i++)
for(j=853;j>0;j--); //STC15单片机设置为853; STC8H单片机修改为1109;STC32G单片机修改为427;
}
void ALL_Init(void)
{
P0 =0X00; //先设置关闭蜂鸣器继电器的P0输出值(全关)
P2|=0XA0; // 将P27 P25 设置为1 其他位保持不变
P2&=0XBF; // 将P26设置为0 其他位保持不变
P2&=0X1F; //关闭所有的74HC573锁存器
P0 =0XFF; //先设置关闭所有的LED的P0输出值(全关)
P2|=0X80; // 将P27设置为1 其他位保持不变
P2&=0X9F; // 将P26 P25设置为0 其他位保持不变
P2&=0X1F; //关闭所有的74HC573锁存器
P0 =0X00; //先设置选择数码管位选的P0输出值(全不选)
P2|=0XC0; // 将P27 P26 设置为1 其他位保持不变
P2&=0XDF; // 将P26设置为0 其他位保持不变
P2&=0X1F; //关闭所有的74HC573锁存器
}
void IO_Init(void)
{
P0M0 = 0x00; P0M1 = 0x00; //IO初始化为准双向I/O模式,STC15F系列可以写
P1M0 = 0x00; P1M1 = 0x00; //IO初始化为准双向I/O模式,STC15F系列可不写
P2M0 = 0x00; P2M1 = 0x00; //IO初始化为准双向I/O模式,STC15F系列可不写
P3M0 = 0x00; P3M1 = 0x00; //IO初始化为准双向I/O模式,STC15F系列可不写
P4M0 = 0x00; P4M1 = 0x00; //IO初始化为准双向I/O模式,STC15F系列可不写
P0=0XFF; P1=0XFF; P2=0XFF; P3=0XFF; P4=0XFF; P5=0XFF; //P0、P1、P2、P3、P4、P5输出为高电平 STC15系列单片机可不写
}
/*
void ArrKeyScan(void)
{
unsigned char temp; //定义局部变量用来存储从P3口读出的数值?
P44=0; P42=1; P3=0X7F; // //0111 1111 扫描第一列
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
Delay_MS(5);
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
temp = P3 ;
switch(temp) //1110 1101 1011 0111
{
case 0X7E: KEY_Value = 7 ; break;
case 0X7D: KEY_Value = 6 ; break;
case 0X7B: KEY_Value = 5 ; break;
case 0X77: KEY_Value = 4 ; break;
default : break;
}
while(temp != 0X0F)
{
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
}
}
}
P44=1; P42=0; P3=0XBF; // //0111 1111 扫描第一列
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
Delay_MS(5);
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
temp = P3 ;
switch(temp) //1110 1101 1011 0111
{
case 0XBE: KEY_Value = 11 ; break;
case 0XBD: KEY_Value = 10 ; break;
case 0XBB: KEY_Value = 9 ; break;
case 0XB7: KEY_Value = 8 ; break;
default : break;
}
while(temp != 0X0F)
{
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
}
}
}
P44=1; P42=1; P3=0XDF; // //1101 1111 扫描第一列
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
Delay_MS(5);
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
temp = P3 ;
switch(temp) //1110 1101 1011 0111
{
case 0XDE: KEY_Value = 15 ; break;
case 0XDD: KEY_Value = 14 ; break;
case 0XDB: KEY_Value = 13 ; break;
case 0XD7: KEY_Value = 12 ; break;
default : break;
}
while(temp != 0X0F)
{
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
}
}
}
P44=1; P42=1; P3=0XEF; // //0111 1111 扫描第一列
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
Delay_MS(5);
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
if(temp != 0X0F)
{
temp = P3 ;
switch(temp) //1110 1101 1011 0111
{
case 0XEE: KEY_Value = 19 ; break;
case 0XED: KEY_Value = 18 ; break;
case 0XEB: KEY_Value = 17; break;
case 0XE7: KEY_Value = 16; break;
default : break;
}
while(temp != 0X0F)
{
temp = P3 ; //将P3口读出的数字赋值给temp
temp = temp & 0X0F ; // 0111 XXXX & 0000 1111 = 0000 XXXX
}
}
}
}
*/
2.iic.c文件
#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 AD_write(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(0X40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char AD_read(unsigned char add)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0X91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
3.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 AD_write(unsigned char dat);
unsigned char AD_read(unsigned char add);
#endif