注意:
源文件修改方面
:官方给的iic.h中使用的时C51的头文件"reg52.h",我们需要修改为对应的15系列头文件"STC15F2K60S2.h",这样才可以使用其中的一些特殊位寄存器
底层代码
:①rd_pcf8591()函数读取最后需调用iic.c中IIC_SendAck()发送非应答信号,即SDA发送一个高电平
重点
:计数值加1 可以利用定时器每100ms扫描比较Vp与Vain3的关系,实现代码如下:
void N_handle()
{
if (Vain3 > Vp)
h = 1;
else
l = 1;
if (h & l)//上一次为高(低),这一次为(低)高,则N 加1
{
if (++N > 99)
N = 0;
h = 0;
l = 0;
}
}
注:该代码中的写入和读出eeprom有问题,目前不知道是出错在哪里。尝试过在eeprom外增加延时2ms秒或5ms延时函数。也尝试过在写eeprom函数的结束的时候延长过100us。目前,这些方法好像都没用。
(ps:路过大佬捞捞`(>﹏<)′)
#include "STC15F2K60S2.h"
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
uchar jm = 0;//界面初始化数据界面
code uchar tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff, 0xc1, 0x8c, 0xc8};
//U P n 11 12 13
bit flag_5s;//Vain3 < Vp是否够 5s,初始化为0,不够
bit flag_100ms;//电压数据采样时间,用来判断N是否增加
bit h, l;//类比零点存在法则判断是否存在Vp 值在100ms前为高(低) 100ms后变为低(高),存在则+1,反之N不变化
float Vp, Vain3;
uchar key_val = 21, inv_key;//inv_key无效按键数 初始化为0
uchar N;//计数值初始化0
void sys_init();
uchar rd_eeprom(uchar addr);//
uchar rd_pcf8591(uchar addr);
void wr_eeprom(uchar addr, da);
void key_scanf();
void key_handle();
void led();
void N_handle();
void dsp_smg_bit(uchar pos, val);
void display();//显示功能,分三个大块
void dsp_dat();
void dsp_para();
void dsp_cnt();
void delay_k(uchar t);//延时t * 10us
void Delay1ms(); //[email protected],延时1ms用于给足数码管足够显示时间
void main()
{
Vp = rd_eeprom(0x00) * 5.0 / 255;
sys_init();
while(1)
{
Vain3 = rd_pcf8591(0x03) * 5.0 / 255;
if (flag_100ms)
{
flag_100ms = 0;
N_handle();
}
key_handle();
display();
led();
}
}
void N_handle()
{
if (Vain3 > Vp)
h = 1;
else
l = 1;
if (h & l)//上一次为高(低),这一次为(低)高,则N 加1
{
if (++N > 99)
N = 0;
h = 0;
l = 0;
}
}
void led()
{
if (flag_5s)
{
P2 = (P2 & 0x1f) | 0x80;
L1 = 0;
}
if (N % 2)
{
P2 = (P2 & 0x1f) | 0x80;
L2 = 0;
}
else
{
P2 = (P2 & 0x1f) | 0x80;
L2 = 1;
}
if (inv_key >= 3)
{
P2 = (P2 & 0x1f) | 0x80;
L3 = 0;
}
}
void key_handle()
{
key_scanf();
if (15 == key_val)//s12
{
key_val = 21;//初始化键值
if (++jm >= 3)
jm = 0;
inv_key = 0;//无效变为有效
if (2 == jm)//从参数界面保存Vp值
{
wr_eeprom(0x00, (uchar)(Vp * 10));
Delay1ms();
Delay1ms();
}
}
if (11 == key_val)//s13
{
key_val = 21;
if (2 == jm)
{
N = 0;
inv_key = 0;
}
else
inv_key++;
}
if (12 == key_val)//s17-
{
key_val = 21;
if (1 == jm)
{
if (Vp >= 0.5)
Vp -= 0.5;
else
Vp = 5.0;
inv_key = 0;
}
else
inv_key++;
}
if (16 == key_val)//s16+
{
key_val = 21;
if (1 == jm)
{
if (Vp <= 4.5)
Vp += 0.5;
else
Vp = 0.0;
inv_key = 0;
}
else
inv_key++;
}
}
void display()
{
if (0 == jm)
dsp_dat();
else if (1 == jm)
dsp_para();
else if (2 == jm)
dsp_cnt();
}
void dsp_cnt()
{
dsp_smg_bit(1, 13);//N
dsp_smg_bit(7, N / 10 % 10);
dsp_smg_bit(8, N % 10);
}
void dsp_para()
{
uint x = (uint)(Vp * 100);//参数Vp扩大100倍
dsp_smg_bit(1, 12);//P
//第六位数码管显示小数点
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (6 - 1);
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[x / 100 % 10] & 0x7f;
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
dsp_smg_bit(7, x / 10 % 10);
dsp_smg_bit(8, x % 10);
}
void dsp_dat()
{
uint x = (uint)(Vain3 * 100);//通道3所读电压扩大100倍
dsp_smg_bit(1, 11);//U
//第六位数码管显示小数点
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (6 - 1);
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[x / 100 % 10] & 0x7f;
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
dsp_smg_bit(7, x / 10 % 10);
dsp_smg_bit(8, x % 10);
}
void dsp_smg_bit(uchar pos, val)
{
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (pos - 1);
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[val];
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
}
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void delay_k(uchar t)
{
while(t--)
display();
}
void key_scanf()
{
uchar i;
P35 = P34 = 1;//变式矩阵键盘的对应两列拉高
for (i = 2; i < 4; i++)
{
P3 |= 0xc0;//先使得第三行与第四行拉高,其余行保持原来的状态
P3 = ~(1 << i);//拉低第三行或者第四行
if (!P35)
{
delay_k(20);
if (!P35)
{
while(!P35)
display();
key_val = 4 * i + 3;
break;
}
}
if (!P34)
{
delay_k(20);
if (!P34)
{
while(!P34)
display();
key_val = 4 * i + 4;
break;
}
}
}
}
uchar rd_pcf8591(uchar addr)
{
uchar da;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
da = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return da;
}
void wr_eeprom(uchar addr, da)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(da);
IIC_WaitAck();
IIC_Stop();
}
uchar rd_eeprom(uchar addr)
{
uchar da;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
da = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return da;
}
void timer0() interrupt 1
{
static uint i1, i2;
if (++i2 == 100)
{
i2 = 0;
flag_100ms = 1;
}
if (Vain3 < Vp)
{
if (++i1 == 5000)
{
flag_5s = 1;
i1 = 0;
}
}
else
{
flag_5s = 0;
i1 = 0;
}
}
void sys_init()
{
//关蜂鸣器,继电器
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0xaf;
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 &= 0x1f;
//1ms
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
ET1 = 0;//禁止定时器中断1中断
EA = 1;
}
#ifndef _IIC_H
#define _IIC_H
#include "STC15F2K60S2.h"
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
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);
#endif
#include "iic.h"
#define DELAY_TIME 5
//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//I2C总线启动信号
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//I2C总线停止信号
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;
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;
}