有关题目
实现代码
main.c
#include <STC15F2K60S2.H>
#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};
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();
void Delay12us();
void key_handle();
void delay_k(uchar t);
void Send_Sonic();
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();
void main()
{
sys_init();
while(1)
{
Vrb2 = rd_pcf8591(0x43) * 5 / 255.0;
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)
{
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));
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()
{
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()
{
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()
{
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;
TMOD &= 0xF0;
TL0 = 0xB0;
TH0 = 0x3C;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
iic.h
#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
iic.c
#include "iic.h"
#define DELAY_TIME 5
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;
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;
}
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;
}
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;
}
|