总结
4t这份第三套的难度比较低。但仍然有坑,我觉得这个坑有部分题目表述的原因。那个旋钮模式,答案的要求是按下s8调整下限,且调整上限失效。s9同理。我刚开始的做法是s8按下调整上限,再按下关掉调整上限,不关掉按s9的话可以同时调整上下限。还有一个犯错点是,切出去旋钮模式的时候,要把旋钮调整上下限的功能关了。
代码的话就放改完满分的
代码
#include <STC15F2K60S2.H>
#include <INTRINS.H>
#include "iic.h"
#define control(x,y) P0=y;P2=x;P2=0
typedef unsigned char uchar ;
typedef unsigned int uint ;
sbit TX=P1^0;
sbit RX=P1^1;
code unsigned char Seg_Code[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xff,//Ãð10
0x88, //A11
0x8c,//P12
0x86, //E13
0xbf //- 14
};
uchar Seg_Bit[9]={10,10,10,10,10,10,10,10,10};
uchar interface;
uchar count_key;
uint dist;
uchar count_dist;
bit mode;
uchar set_high=60;
uchar set_low=10;
uint times;
bit state;
uint v;
uchar count_adc;
uchar L[9];
uchar flag_led;
uint count_100ms;
flag_high;
flag_low;
void Timer2Init(void)
{
AUXR |= 0x04; //?????1T??
T2L = 0x20; //??????
T2H = 0xD1; //??????
AUXR |= 0x10; //???2????
IE2|= 0x04;
EA=1;
}
void t2int() interrupt 12 //????
{ static Seg_Com=1;
///ÖÜÆÚË¢ÐÂ
++count_dist;
++count_key;
++count_adc;
++count_100ms;
///ÊýÂë¹Ü
control(0xc0,0x00);
control(0xe0,Seg_Code[Seg_Bit[Seg_Com]]);
control(0xc0,1<<(Seg_Com-1));
if(++Seg_Com>8) Seg_Com=1;
///led/
control(0x80,~(L[8]<<7|L[3]<<2|L[2]<<1|L[1]));
}
void Delay12us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 33;
while (--i);
}
uint Read_Distance()
{uint dist=0;
uchar wave=8;
TH1=TL1=0;
while(wave--)
{TX=1;
Delay12us();
TX=0;
Delay12us();
}
TR1=1;
while(RX==1 && TH1<0x61);
TR1=0;
if(TH1<0X61)
{
dist=( (uint)TH1<<8|TL1 )*0.017+0.5;
}
else
{dist=999;
}
return dist;
}
void System_Init()
{
control(0x80,0xff);
control(0xa0,0x00);
while(++count_adc<5)
{v=Read_ADC()*1.96+0.5;
}
Timer2Init();
}
void Data_Task()
{ if(T2H<0xd9)
{
if(count_dist>150)
{count_dist=0;
dist=Read_Distance();
}
if(count_adc>150)
{v=Read_ADC()*1.96+0.5;
count_adc=0;
}
}
}
void Logic_Task()
{
if(dist<=set_high && dist>=set_low)
{
state=1;
}
else
{if(state)
{times++;}
state=0;
}
if(interface!=1 || mode!=1)ÓÐÒ»¸ö²»´¦ÓÚ¾ÍʧЧ
{flag_low=flag_high=0;
}
/
if(flag_low)
{if(v<100)
{
set_low=0;
}
else if(v>100&&v<200)
{
set_low=10;
}
else if(v>200&&v<300)
{
set_low=20;
}
else if(v>300&&v<400)
{
set_low=30;
}
else if(v>400&&v<=500)
{
set_low=40;
}
}
if(flag_high)
{if(v<100)
{set_high=50;
}
else if(v>100&&v<200)
{set_high=60;
}
else if(v>200&&v<300)
{set_high=70;
}
else if(v>300&&v<400)
{set_high=80;
}
else if(v>400&&v<=500)
{set_high=90;
}
}
//
if(state==0)/²»´¦ÓÚ²ÎÊýÇø¼ä£¬¼ÆËãL8µÄÖµ£¬0.1s±äÒ»´Î
{if(count_100ms==100)
{count_100ms=0;
flag_led=!flag_led;
}
}
else
{count_100ms=0;
}
L[1]=(interface==0)?(1):(0);
L[2]=(interface==1)?(1):(0);
L[3]=(interface==2)?(1):(0);
L[8]=(state)?(1):(flag_led);//Èç¹û´¦ÓÚ²ÎÊýÇø¼äÒ»Ö±ÊÇÁÁ£¬²»´¦ÓÚÔòÉÁ˸
}
void Display_Task()
{
if(interface==0)
{
Seg_Bit[1]=11;
Seg_Bit[2]=10;
Seg_Bit[3]=10;
Seg_Bit[4]=10;
Seg_Bit[5]=10;
Seg_Bit[6]=(dist>99)?(dist/100%10):(10);
Seg_Bit[7]=(dist>9)?(dist/10%10):(10);
Seg_Bit[8]=dist%10;
}
if(interface==1)
{
Seg_Bit[1]=12;
Seg_Bit[2]=(mode)?(2):(1);
Seg_Bit[3]=10;
Seg_Bit[4]=set_low/10%10;
Seg_Bit[5]=set_low%10;
Seg_Bit[6]=14;
Seg_Bit[7]=set_high/10%10;
Seg_Bit[8]=set_high%10;
}
if(interface==2)
{
Seg_Bit[1]=13;
Seg_Bit[2]=10;
Seg_Bit[3]=10;
Seg_Bit[4]=10;
Seg_Bit[5]=10;
Seg_Bit[6]=10;
Seg_Bit[7]=10;
Seg_Bit[8]=(times<10)?(times):(14);
}
}
uchar Read_Key()
{ static uchar count_press=0;
static uchar flag_press=0;
uchar value;
uchar x=0,y=0;
P3=0x0f;P4=0x00;
if(P30==0) x=3;
if(P31==0) x=2;
if(P32==0) x=1;
if(P33==0) x=0;
P3=0xf0;P4=0xff;
if(P35==0) y=3;
if(P42==0) y=2;
if(P44==0) y=1;
if(y)
{
if((++count_press>=8)&&(flag_press==0))
{
count_press=0;
flag_press=1;
value=x+y*4;
}
}
else
{
count_press=0;
flag_press=0;
}
return value;
}
void Key_Task()
{uchar key_value=0;
if(count_key>5)
{count_key=0;
key_value=Read_Key();
}
if(key_value==4)
{
if(++interface==3)
{interface=0;
}
}
if(key_value==5)
{
if(interface==1)
{mode=!mode;
}
if(interface==2)
{times=0;
}
}
if(key_value==9)
{
if(interface==1)
{if(mode==0)
{set_high+=10;
if(set_high==100)
{set_high=50;
}
}
if(mode==1)
{flag_high=1;
flag_low=0;
}
}
}
if(key_value==8)
{
if(interface==1)
{if(mode==0)
{set_low+=10;
if(set_low==50)
{set_low=0;
}
}
if(mode==1)
{flag_low=1;
flag_high=0;
}
}
}
}
int main()
{
System_Init();
while(1)
{
Data_Task();
Logic_Task();
Display_Task();
Key_Task();
}
}
iic文件常规写法。