题目
main.c
#include<stc15f2k60s2.h>
#include "intrins.h"
#define uchar unsigned char
#define s0 0
#define s1 1
int read_vol();
void write_vol(int vol);
code uchar semg[13] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90,
0xc1, 0x8e, 0xff};
uchar semg_temp[8] = {12, 12, 12, 12, 12, 12, 12, 12};
code uchar semg_bit[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
uchar temp[6] = {0, 5, 3, 0, 0, 0};
uchar state = s0;
uchar led_flag = 1;
uchar flow_flag = 0;
uchar semg_flag = 1;
int vol = 100;
long count = 0;
long fre = 0;
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x40;
TH1 = 0xA2;
TF1 = 0;
TR1 = 1;
EA = 1;
ET1 = 1;
}
void Count0Init(void)
{
AUXR |= 0x80;
TMOD |= 0x06;
TL0 = TH0 = 255;
TR0 = 1;
ET0 = 1;
}
void display()
{
uchar i;
if(state == s0 && i == 5)
{
P2 = 0xe0;
P0 = semg[semg_temp[i]] & 0x7f;
}
else
{
P2 = 0xe0;
P0 = semg[semg_temp[i]];
}
P2 = 0xc0; P0 = semg_bit[i];
P2 = 0x00; P0 = 0xff;
i++;
if(i == 8)
i = 0;
}
void led( )
{
if(state == s0)
{
P00 = 0;
P01 = 1;
}
else
{
P00 = 1;
P01 = 0;
}
if(vol < 150)
P02 = 1;
else if(vol < 250)
P02 = 0;
else if(vol < 350)
P02 = 1;
else
P02 = 0;
if(flow_flag)
P04 = 0;
else
P04 = 1;
if(fre < 1000)
P03 = 1;
else if(fre < 5000)
P03 = 0;
else if(fre < 10000)
P03 = 1;
else
P03 = 0;
P0 = P0 | 0xc0; P2 = 0x80;
P2 = 0X00; P0 = 0Xff;
}
void fun_count() interrupt 1
{
count++;
}
void fun() interrupt 3
{
uchar i, j, k, m;
long div;
i++;
if(semg_flag)
display();
else
{
P2 = 0xe0; P0 = 0xff;
P2 = 0xc0; P0 = 0xff;
}
if(i == 50)
{
i = 0;
if(led_flag)
led( );
else
P2 = 0x80; P0 = 0xff;
j++;
k++;
}
if(j == 4)
{
j = 0;
vol = read_vol();
if(flow_flag)
write_vol(vol);
else
write_vol(200);
}
if(k == 10)
{
ET0 = 0; TR0 = 0;
k = 0;
fre = count;
div = 100000;
for(m = 0; m < 6; m++)
{
temp[m] = count / div;
count = count - temp[m] * div;
div = div / 10;
}
count = 0;
ET0 = 1; TR0 = 1;
}
}
char keyscan()
{
static char keyvalue = -1;
uchar keycode, keybefore, trg;
P44 = 0; P42 = 0; P35 = 0;
keycode = (P3 ^ 0xff) & 0x0f;
trg = keycode & (keycode ^ keybefore);
keybefore = keycode;
if(trg == 0x01) keyvalue = 7;
else if(trg == 0x02) keyvalue = 6;
else if(trg == 0x04) keyvalue = 5;
else if(trg == 0x08) keyvalue = 4;
else keyvalue = -1;
return keyvalue;
}
void menu(char keyvalue)
{
uchar i, j;
if(keyvalue == 5)
flow_flag = !flow_flag;
else if(keyvalue == 6)
led_flag = !led_flag;
else if(keyvalue == 7)
semg_flag = !semg_flag;
switch(state)
{
case s0:
{
if(keyvalue == 4)
state = s1;
semg_temp[0] = 10;
semg_temp[1] = 12;
semg_temp[2] = 12;
semg_temp[3] = 12;
semg_temp[4] = 12;
semg_temp[5] = vol / 100;
semg_temp[6] = (vol - semg_temp[5] * 100) / 10;
semg_temp[7] = vol % 10;
} break;
case s1:
{
if(keyvalue == 4)
state = s0;
semg_temp[0] = 11;
semg_temp[1] = 12;
for(i = 2; i < 8; i++)
{
if(temp[i - 2])
break;
else
semg_temp[i] = 12;
}
for(j = i; j < 8; j++)
semg_temp[j] = temp[j - 2];
} break;
default: state = s0; break;
}
}
void allinit()
{
P2 = 0x80; P0 = 0xff;
P2 = 0xa0; P0 = 0x00;
P2 = 0xc0; P0 = 0xff;
P2 = 0xe0; P0 = 0xff;
}
void Delay7ms()
{
unsigned char i, j;
i = 82;
j = 179;
do
{
while (--j);
} while (--i);
}
void main()
{
char keyvalue;
allinit();
Count0Init( );
Timer1Init( );
while(1)
{
keyvalue = keyscan();
menu(keyvalue);
Delay7ms();
}
}
iic.c
#include<stc15f2k60s2.h>
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
int read_vol( )
{
int dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x43);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
dat=dat*1.96078;
return dat;
}
void write_vol(int dat)
{
IIC_Start( );
IIC_SendByte(0x90);
IIC_WaitAck( );
IIC_SendByte(0x40);
IIC_WaitAck( );
IIC_SendByte(dat / 1.96078);
IIC_WaitAck( );
IIC_Stop();
}