第十三届的逻辑还是比较简单的,考的东西也比较多,练手非常不错。那么话不多说,上题!
main文件
#include <STC15F2K60S2.H>
#include "Key.h"
#include "Nixie.h"
#include "stdio.h"
#include "onewire.h"
#include "ds1302.h"
unsigned char String[10];
unsigned char String_Buff[9];
unsigned char keynum;
unsigned int T;
unsigned char T_CS=23;
unsigned char showmode;
bit work_mode;
bit CA_Flag;
bit wumiao;
bit JDQ_LOW;
unsigned char led_char=0xff;
bit LED_LOW;
bit led_wumiao;
bit JDQ_KG;
unsigned char LED_SS;
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=EA=1;
}
void Init_All()
{
P0=0x00;
P2=(P2&0x1f)|0xa0;
P2&=0x1f;
Timer0Init();
Set_Time();
Read_T();
}
void Nixie_Logic()
{
switch(showmode)
{
case 0x00:sprintf(String,"U1 %02d.%01d",T/10,T%10);break;
case 0x10:
if(CA_Flag)
sprintf(String,"U2 %02d-%02d",(unsigned int)Time_Tab[1],(unsigned int)Time_Tab[2]);
else sprintf(String,"U2 %02d-%02d",(unsigned int)Time_Tab[0],(unsigned int)Time_Tab[1]);
break;
case 0x20:sprintf(String,"U3 %02d",(unsigned int)T_CS);break;
}
Nixie_Set(String,String_Buff);
}
void Key_Logic()
{
keynum=Key();
switch(showmode)
{
case 0x00:
if(keynum==1)
{
showmode=0x10;
}
break;
case 0x10:
if(keynum==1)
{
showmode=0x20;
}
break;
case 0x20:
if(keynum==1)
{
showmode=0x00;
}
else if(keynum==2)
{
T_CS++;
if(T_CS>99) T_CS=10;
}
else if(keynum==4)
{
T_CS--;
if(T_CS<10) T_CS=99;
}
break;
}
if(keynum==3) work_mode=!work_mode;
}
void JDQ_Logic()
{
unsigned char temp;
if(!work_mode)
{
if((T/10)>T_CS)
{
JDQ_KG=1;
temp=0x10;
}
else
{
temp=0x00;
JDQ_KG=0;
}
}
else
{
if(Time_Tab[1]==0 && Time_Tab[2]==0)
wumiao=1;
if(wumiao)
{
temp=0x10;
JDQ_KG=1;
}
else
{
temp=0x00;
JDQ_KG=0;
}
}
if(JDQ_LOW)
{
JDQ_LOW=0;
P0=temp;
P2=(P2&0x1f)|0xa0;
P2&=0x1f;
}
}
void LED_Logic()
{
if(Time_Tab[1]==0 && Time_Tab[2]==0)
led_wumiao=1;
if(led_wumiao)
led_char&=0xfe;
else led_char|=0x01;
if(!work_mode)
led_char&=0xfd;
else led_char|=0x02;
if(JDQ_KG && LED_SS<100)
led_char&=0xfb;
else if(JDQ_KG && ((LED_SS>=100)&&(LED_SS<=200)))
led_char|=0x04;
else
{
led_char|=0x04;
LED_SS=0;
}
if(LED_LOW)
{
LED_LOW=0;
P0=led_char;
P2=(P2&0x1f)|0x80;
P2&=0x1f;
}
}
void main()
{
Init_All();
while(1)
{
Nixie_Logic();
Key_Logic();
JDQ_Logic();
LED_Logic();
}
}
void T0_Loop() interrupt 1
{
unsigned int Count;
unsigned int JDQ_WU;
unsigned int LED_WU;
Nixie_Loop(String_Buff);
Key_Loop();
Count++;
JDQ_LOW=1;
LED_LOW=1;
if(Count>=1000)
{
Count=0;
Read_Time();
T=Read_T();
}
if(showmode==0x10 && Key_Get[1][1]==0)
{
CA_Flag=1;
}
else CA_Flag=0;
if(wumiao)
{
JDQ_WU++;
if(JDQ_WU>=5000)
{
JDQ_WU=0;
wumiao=0;
}
}
if(led_wumiao)
{
LED_WU++;
if(LED_WU>=5000)
{
LED_WU=0;
led_wumiao=0;
}
}
if(JDQ_KG)
{
LED_SS++;
}
}
Key文件
#include <STC15F2K60S2.H>
sbit H1=P3^3;
sbit H2=P3^2;
sbit L3=P3^5;
sbit L4=P3^4;
unsigned char Key_Get[2][2]={{1,1},{1,1}};
unsigned char Last_Key_Get[2][2]={{1,1},{1,1}};
unsigned char Key()
{
unsigned char i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(Key_Get[i][j]==0)
{
if(Last_Key_Get[i][j]==1)
{
Last_Key_Get[i][j]=0;
return i*2+j+1;
}
else return 0;
}
}
}
return 0;
}
void Key_Loop()
{
static unsigned char Buff[2][2]={{0xff,0xff},{0xff,0xff}};
static unsigned char loop;
unsigned char i;
Buff[loop][0]=(Buff[loop][0]<<1)|L3;
Buff[loop][1]=(Buff[loop][1]<<1)|L4;
for(i=0;i<2;i++)
{
if(Buff[loop][i]==0xff)
{
Last_Key_Get[loop][i]=1;
Key_Get[loop][i]=1;
}
else if(Buff[loop][i]==0x00)
Key_Get[loop][i]=0;
}
loop++;
loop%=2;
switch(loop)
{
case 0:H2=1;H1=0;break;
case 1:H1=1;H2=0;break;
}
}
Nixie文件
#include <STC15F2K60S2.H>
void Nixie_Set(unsigned char* str,unsigned char* buff)
{
unsigned char i,j,temp;
for(i=1,j=0;i<9;i++,j++)
{
switch(str[j])
{
case '0':temp=0xc0;break;
case '1':temp=0xf9;break;
case '2':temp=0xa4;break;
case '3':temp=0xb0;break;
case '4':temp=0x99;break;
case '5':temp=0x92;break;
case '6':temp=0x82;break;
case '7':temp=0xf8;break;
case '8':temp=0x80;break;
case '9':temp=0x90;break;
case ' ':temp=0xff;break;
case '-':temp=0xbf;break;
case 'U':temp=0xc1;break;
}
if(str[j+1]=='.')
{
temp&=0x7f;
j++;
}
buff[i]=temp;
}
}
void Nixie_Loop(unsigned char* buff)
{
static unsigned char i=1;
P0=0xff;
P2=(P2&0x1f)|0xe0;
P2&=0x1f;
P0=0x01<<(i-1);
P2=(P2&0x1f)|0xc0;
P2&=0x1f;
P0=buff[i];
P2=(P2&0x1f)|0xe0;
P2&=0x1f;
i++;
if(i>8) i=1;
}
ds1302
/* # DS1302代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include <STC15F2K60S2.H>
#include "intrins.h"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST=P1^3;
unsigned char Time_Tab[3]={23,59,55};
//
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
//
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
//
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
unsigned char BCDtoDEC(unsigned char num)
{
return num/16*10+num%16;
}
unsigned char DECtoBCD(unsigned char num)
{
return num/10*16+num%10;
}
void Set_Time()
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,DECtoBCD(Time_Tab[0]));
Write_Ds1302_Byte(0x82,DECtoBCD(Time_Tab[1]));
Write_Ds1302_Byte(0x80,DECtoBCD(Time_Tab[2]));
Write_Ds1302_Byte(0x8e,0x80);
}
void Read_Time()
{
Write_Ds1302_Byte(0x8e,0x00);
Time_Tab[0]=BCDtoDEC(Read_Ds1302_Byte(0x85));
Time_Tab[1]=BCDtoDEC(Read_Ds1302_Byte(0x83));
Time_Tab[2]=BCDtoDEC(Read_Ds1302_Byte(0x81));
Write_Ds1302_Byte(0x8e,0x80);
}
onewire
/* # 单总线代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include <STC15F2K60S2.H>
sbit DQ=P1^4;
//
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
//
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int Read_T()
{
unsigned char TL,TH;
unsigned int T;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
TL=Read_DS18B20();
TH=Read_DS18B20();
T=(TH<<8)|TL;
T=T>>4;
T*=10;
T+=(TL&0x0f)*0.625;
return T;
}
除了串口和超声波,我应该把所有的模块都发了出来,后面就只贴主文件了,如果需要的话,可以单独给模块发几篇。
功能测试基本我感觉肥肠完美!如果我错了,那我也不认可!
这篇就到这里,等我省一了就发国赛的!加纳!