#include
#include "iic.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit TX = P1^0; //发射引脚
sbit RX = P1^1; //接收引脚
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L8 = P0^7;
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
uchar jm = 0;
code uchar tab[] = {0xC0, 0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90, 0xff, 0xc1, 0x8c, 0xc3, 0x88};
//U P L A 11 12 13 14
//初始化为0,一次表示从左至右分别表示S5的参数选择为上限,超声波的连续测量标志不打开,L8灭
bit flag_para, flag_start, flag_L8 ;
float Vup = 4.5, Vdown = 0.5, Vrb2, Vdac;
uint dis = 5;
void sys_init();
void dac_pcf8591(uchar da);
uchar rd_pcf8591(uchar addr);
void Delay5ms(); //@12.000MHz
void Delay12us(); //@12.000MHz 用于延时与产生方波
void key_handle();
void delay_k(uchar t);
void Send_Sonic();//用于发送8个40kHz的方波
void Sonic_handle();
void led();
void dac_handle();
void dsp_smg_bit(uchar pos, val, dot);
void display();
void dsp_vol();
void dsp_para();
void dsp_dis();
void Delay1ms(); //@12.000MHz
void main()
{
sys_init();
while(1)
{
Vrb2 = rd_pcf8591(0x43) * 5 / 255.0;//读取通道3的电压,要同时允许DAC
key_handle();
display();
Sonic_handle();
led();
dac_handle();
}
}
void dac_handle()
{
if (!flag_start)
{
Vdac = 0.0;
}
else
{
if (dis <= 20)
Vdac = 1.0;
else if (dis >= 80)
Vdac = 5.0;
else
{
Vdac = 1.0 / 15 * (dis - 20) + 1;
}
}
dac_pcf8591((uchar)(Vdac * 51));
}
void led()
{
if (0 == jm)
{
P2 = (P2 & 0x1f) | 0x80;
L1 = 0;
}
else if (1 == jm)
{
P2 = (P2 & 0x1f) | 0x80;
L2 = 0;
}
else if (2 == jm)
{
P2 = (P2 & 0x1f) | 0x80;
L3 = 0;
}
if (flag_start)
{
if (flag_L8)
{
P2 = (P2 & 0x1f) | 0x80;
L8 = 0;
}
else
{
P2 = (P2 & 0x1f) | 0x80;
L8 = 1;
}
}
}
void Sonic_handle()
{
if (jm != 2)//电压上限、下限参数在参数调整过程中无效,通过 S4 按键退出参数界面时生效。
{
flag_start = (Vrb2 > Vdown && Vrb2 < Vup);
}
if (flag_start)//连续测量
{
uint t = 0;
Send_Sonic();
AUXR &= 0XBF;
TMOD &= 0X0F;
TL1 = 0;
TH1 = 0;
TF1 = 0;
TR1 = 1;
while((RX == 1) && (TF1 == 0));//等待超声波信号返回(RX引脚变为低电平)或等超出测量范围
TR1 = 0;
if(TF1 == 0) //正常范围内
{
t = (TH1 << 8) | TL1;
dis = t * 0.172 / 10;
}
else
{
TF1 = 0;
dis = 999;
}
}
}
void Send_Sonic()
{
uchar i;
for (i = 0; i < 8; i++)
{
TX = 1;
Delay12us();
TX = 0;
Delay12us();
}
}
void Delay12us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 33;
while (--i);
}
void dsp_dis()
{
dsp_smg_bit(1, 13, 0);
if (flag_start)
{
if (dis > 99)
dsp_smg_bit(6, dis / 100 % 10, 0);
if (dis > 9)
dsp_smg_bit(7, dis / 10 % 10, 0);
if (dis >= 0)
dsp_smg_bit(8, dis % 10, 0);
}
else
{
dsp_smg_bit(6, 14, 0);
dsp_smg_bit(7, 14, 0);
dsp_smg_bit(8, 14, 0);
}
}
void dsp_para()
{
uchar x = (uchar)(Vup * 10);
uchar y = (uchar)(Vdown * 10);
dsp_smg_bit(1, 12, 0);
dsp_smg_bit(4, x / 10, 1);
dsp_smg_bit(5, x % 10, 0);
dsp_smg_bit(7, y / 10, 1);
dsp_smg_bit(8, y % 10, 0);
}
void dsp_vol()
{
uint x = (uint)(Vrb2 * 100);
dsp_smg_bit(1, 11, 0);
dsp_smg_bit(6, x / 100, 1);
dsp_smg_bit(7, x / 10 % 10, 0);
dsp_smg_bit(8, x % 10, 0);
}
void dsp_smg_bit(uchar pos, val, dot)
{
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (pos - 1);
P2 = (P2 & 0x1f) | 0xe0;
if (!dot)
P0 = tab[val];
else
P0 = tab[val] & 0x7f;
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
}
void display()
{
if (0 == jm)
dsp_vol();
else if(2 == jm)
dsp_para();
else if (1 == jm)
dsp_dis();
}
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_handle()
{
if (!S7)//-
{
delay_k(15);
if (!S7)
{
while(!S7)
display();
if (2 == jm)
{
if (!flag_para)
{
if (Vup > 0.5)
Vup -= 0.5;
else
Vup = 5.0;
}
else
{
if (Vdown > 0.5)
Vdown -= 0.5;
else
Vdown = 5.0;
}
}
///
}
}
if (!S6)//+
{
delay_k(15);
if (!S6)
{
if (2 == jm)
{
if (!flag_para)
{
if (Vup <= 4.5)
Vup += 0.5;
else
Vup = 0.5;
}
else
{
if (Vdown <= 4.5)
Vdown += 0.5;
else
Vdown = 0.5;
}
}
while(!S6)
display();
}
}
if (!S5)//参数选择
{
delay_k(15);
if (!S5)
{
flag_para = !flag_para;
while(!S5)
display();
}
}
if (!S4)//界面切换
{
delay_k(15);
if (!S4)
{
if (++jm >= 3)
jm = 0;
if (2 == jm)
flag_para = 0;
while(!S4)
display();
}
}
}
uchar rd_pcf8591(uchar addr)
{
uchar da;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
Delay12us();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
da = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return da;
}
void dac_pcf8591(uchar da)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x43);
IIC_WaitAck();
IIC_SendByte(da);
IIC_WaitAck();
IIC_Stop();
Delay5ms();
}
void Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void timer0() interrupt 1
{
static uchar i1;
if (flag_start)
{
if (++i1 == 2)
{
i1 = 0;
flag_L8 = !flag_L8;
}
}
}
void sys_init()
{
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0xaf;
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 &= 0x1f;
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xB0; //设置定时初值
TH0 = 0x3C; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
#include
#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
iic.c
#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;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)