这次比赛比较简单,都是基础的模块
有数码管、LED、矩阵键盘、DS1302、频率测量。
频率测量是这次的核心内容
如果把十四届的题目做熟练基本能做出了吧
比赛的时候CPU在燃烧!
P:代码还有漏洞,勉强能看,我之后再完善
代码如下:
#include "reg52.h"
#include "ds1302.h"
#include "iic.h"
#define TSMG 500
//sfr AUXR = 0x8e;
sfr P4 = 0xC0;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P4^4;
sbit C2 = P4^2;
float freq = 0;
unsigned int freq_max = 0;
unsigned int freq_smg = 0;
unsigned char dac;
unsigned char stat_smg = 1;
unsigned char stat_led = 0xff;
unsigned int chaoxian = 2000;
unsigned int jiaozhun = 0;
unsigned char flag_jiaozhun = 0;
unsigned char flag_d_fj = 0;
unsigned char flag_L1 = 0;
unsigned char flag_L2 = 0;
unsigned char flag_warning = 0;
unsigned char hour = 0;
unsigned char min = 0;
unsigned char sec = 0;
unsigned char hour_log = 0;
unsigned char min_log = 0;
unsigned char sec_log = 0;
unsigned char count_t1 = 0;
unsigned char count_t2 = 0;
unsigned int count_f = 5;
unsigned char Write_DS1302[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e};
unsigned char Read_DS1302[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d,0x8f};
unsigned char Time[] = {0x30,0x36,0x10,0x13,0x04,0x06,0x24};
code unsigned char Seg_Table_noDot[] =
{
0xc0,
//0
0xf9,
//1
0xa4,
//2
0xb0,
//3
0x99,
//4
0x92,
//5
0x82,
//6
0xf8,
//7
0x80,
//8
0x90,
//9
0x88,
//A
0x83,
//b
0xc6,
//C
0xa1,
//d
0x86,
//E
0x8e,
//F
0xbf,
//-
0x7f,
//.
0x8c,
//P
0x89,
//H
0xc7
//L
};
code unsigned char Seg_Table_Dot[] =
{
0x40,
//0
0x79,
//1
0x24,
//2
0x30,
//3
0x19,
//4
0x12,
//5
0x02,
//6
0x78,
//7
0x00,
//8
0x10
//9
};
void Delay(unsigned int t)
{
while(t--);
}
void SelectHC573(unsigned char channel,unsigned char dat)
{
P0 = dat;
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
case 0:
P2 = (P2 & 0x1f) | 0x00;
break;
}
P2 = (P2 & 0x1f) | 0x00;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char value)
{
SelectHC573(6,0x01 << pos);
SelectHC573(7,value);
DelaySMG(TSMG);
SelectHC573(6,0x01 << pos);
SelectHC573(7,0xff);
}
void DisplaySMG_All(unsigned char value)
{
SelectHC573(6,0xff);
SelectHC573(7,value);
}
void Set_Ds1302()
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i = 0;i<8;i++)
{
Write_Ds1302_Byte(Write_DS1302[i],Time[i]);
}
Write_Ds1302_Byte(0x8e,0xff);
}
void Read_Ds1302_Time(unsigned char i)
{
if(i == 0)
{
hour = Read_Ds1302_Byte(Read_DS1302[2]);
min = Read_Ds1302_Byte(Read_DS1302[1]);
sec = Read_Ds1302_Byte(Read_DS1302[0]);
}
else
{
hour_log = Read_Ds1302_Byte(Read_DS1302[2]);
min_log = Read_Ds1302_Byte(Read_DS1302[1]);
sec_log = Read_Ds1302_Byte(Read_DS1302[0]);
}
}
void Handle_Freq()
{
if(flag_jiaozhun == 0)
{
if(freq >= jiaozhun)
{
flag_d_fj = 0;
freq_smg = freq-jiaozhun;
}
else
flag_d_fj = 1;
}
else
freq_smg = freq + jiaozhun;
if(freq_smg >freq_max)
{
freq_max=freq_smg;
Read_Ds1302_Time(1);
}
}
void Handle_DAC()
{
if(flag_d_fj == 1)
{
dac = 0;
}
else if(freq_smg > chaoxian)
dac = 5;
else if(freq_smg >= 500)
dac = freq_smg * (4 / (chaoxian - 500)) + 1 - 2000 / (chaoxian - 500);
else if(freq_smg >= 0)
dac = 1;
}
void Init_Timer0_Timer1()
{
TMOD = 0x16;
//AUXR &=0x7f;
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
TH0 = 0xff;
TL0 = 0xff;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
}
void Service_Timer0() interrupt 1
{
count_f++;
stat_led &= ~0x03;
SelectHC573(4,stat_led);
}
void Service_Timer1() interrupt 3
{
TH1 = (65536 - 10000 ) / 256;
TL1 = (65536 - 10000 ) % 256;
count_t1++;
count_t2++;
if(count_t1 == 20)
{
count_t1 = 0;
if(stat_smg == 1 && flag_L1 == 0)
{
flag_L1=1;
stat_led &= ~0x01;
SelectHC573(4,stat_led);
}
else
{
flag_L1=0;
stat_led |= 0x01;
SelectHC573(4,stat_led);
}
if(flag_warning == 1 && flag_L2 == 0)
{
flag_L2=1;
stat_led &= ~0x02;
SelectHC573(4,stat_led);
}
else
{
flag_L2=0;
stat_led |= 0x02;
SelectHC573(4,stat_led);
}
}
if(count_t2 == 100)
{
unsigned char dat = 0;
count_t2 = 0;
dat = count_f;
count_f = 0;
freq = dat/(10.0/100.0);
Handle_Freq();
Handle_DAC();
}
}
void Write_PCF8591_DAC(unsigned char ain)
{
I2CStart();
I2CSendByte(90);
I2CWaitAck();
I2CSendByte(ain);
I2CWaitAck();
I2CStop();
}
unsigned char Read_PCF8591_DAC()
{
unsigned char tmp;
I2CStart();
I2CSendByte(91);
I2CWaitAck();
tmp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return tmp;
}
void Scan_Keys()
{
R3 = 1;
R4 = 0;
if(C1 == 0)
{
Delay(500);
if(C1 == 0)
{
if(stat_smg == 1)
{
stat_smg = 2;
}
else if(stat_smg ==2)
{
stat_smg =4;
}
else if(stat_smg ==4)
{
stat_smg =5;
}
else if(stat_smg ==5)
{
stat_smg =1;
}
while(C1 == 0);
}
}
if(C2 == 0)
{
Delay(500);
if(C2 == 0)
{
if(stat_smg == 2)
{
if(chaoxian < 9000)
chaoxian += 1000;
}
if(stat_smg ==3)
{
if(flag_jiaozhun == 1 && jiaozhun > 0)
{
if(jiaozhun == 100)
flag_jiaozhun = 0;
jiaozhun -= 100;
}
else if(jiaozhun <900)
{
flag_jiaozhun = 0;
jiaozhun += 100;
}
}
while(C2 == 0);
}
}
R3 = 0;
R4 = 1;
if(C1 == 0)
{
Delay(500);
if(C1 == 0)
{
if(stat_smg == 2)
{
stat_smg = 3;
}
else if(stat_smg ==3)
{
stat_smg =2;
}
if(stat_smg == 5)
{
stat_smg = 6;
}
else if(stat_smg ==6)
{
stat_smg =5;
}
while(C1 == 0);
}
}
if(C2 == 0)
{
Delay(500);
if(C2 == 0)
{
if(stat_smg == 2)
{
if(chaoxian > 1000)
chaoxian -= 1000;
}
if(stat_smg ==3)
{
if(flag_jiaozhun == 0 && jiaozhun > 0)
jiaozhun -= 100;
else if(jiaozhun <900)
{
flag_jiaozhun = 1;
jiaozhun += 100;
}
}
while(C2 == 0);
}
}
}
void Display_SMG()
{
switch(stat_smg)
{
case 1:
DisplaySMG_Bit(0,Seg_Table_noDot[15]);
DisplaySMG_Bit(1,0xff);
DisplaySMG_Bit(2,0xff);
DisplaySMG_Bit(3,0xff);
if(flag_d_fj == 0)
{
stat_led |= 0x02;
SelectHC573(4,stat_led);
DisplaySMG_Bit(4,Seg_Table_noDot[freq_smg / 1000]);
DisplaySMG_Bit(5,Seg_Table_noDot[freq_smg / 100 % 10]);
DisplaySMG_Bit(6,Seg_Table_noDot[freq_smg / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table_noDot[freq_smg % 10]);
}
else
{
stat_led &= ~0x02;
SelectHC573(4,stat_led);
DisplaySMG_Bit(4,0xff);
DisplaySMG_Bit(5,0xff);
DisplaySMG_Bit(6,Seg_Table_noDot[20]);
DisplaySMG_Bit(7,Seg_Table_noDot[20]);
}
break;
case 2:
DisplaySMG_Bit(0,Seg_Table_noDot[18]);
DisplaySMG_Bit(1,Seg_Table_noDot[1]);
DisplaySMG_Bit(2,0xff);
DisplaySMG_Bit(3,0xff);
DisplaySMG_Bit(4,Seg_Table_noDot[chaoxian / 1000]);
DisplaySMG_Bit(5,Seg_Table_noDot[chaoxian / 100 % 10]);
DisplaySMG_Bit(6,Seg_Table_noDot[chaoxian / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table_noDot[chaoxian % 10]);
break;
case 3:
DisplaySMG_Bit(0,Seg_Table_noDot[18]);
DisplaySMG_Bit(1,Seg_Table_noDot[2]);
DisplaySMG_Bit(2,0xff);
DisplaySMG_Bit(3,0xff);
if(flag_jiaozhun == 1)
DisplaySMG_Bit(4,Seg_Table_noDot[16]);
else
DisplaySMG_Bit(4,0xff);
if(jiaozhun >99)
{
DisplaySMG_Bit(5,Seg_Table_noDot[jiaozhun / 100]);
DisplaySMG_Bit(6,Seg_Table_noDot[jiaozhun / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table_noDot[jiaozhun % 10]);
}
else if(jiaozhun > 9)
{
DisplaySMG_Bit(5,0xff);
DisplaySMG_Bit(6,Seg_Table_noDot[jiaozhun / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table_noDot[jiaozhun % 10]);
}
else
{
DisplaySMG_Bit(5,0xff);
DisplaySMG_Bit(6,0xff);
DisplaySMG_Bit(7,Seg_Table_noDot[jiaozhun % 10]);
}
break;
case 4:
DisplaySMG_Bit(0,Seg_Table_noDot[hour / 16]);
DisplaySMG_Bit(1,Seg_Table_noDot[hour % 16]);
DisplaySMG_Bit(2,Seg_Table_noDot[16]);
DisplaySMG_Bit(3,Seg_Table_noDot[min / 16]);
DisplaySMG_Bit(4,Seg_Table_noDot[min % 16]);
DisplaySMG_Bit(5,Seg_Table_noDot[16]);
DisplaySMG_Bit(6,Seg_Table_noDot[sec / 16]);
DisplaySMG_Bit(7,Seg_Table_noDot[sec % 16]);
break;
case 5:
DisplaySMG_Bit(0,Seg_Table_noDot[19]);
DisplaySMG_Bit(1,Seg_Table_noDot[15]);
DisplaySMG_Bit(2,0xff);
DisplaySMG_Bit(3,0xff);
DisplaySMG_Bit(4,Seg_Table_noDot[freq_max / 1000]);
DisplaySMG_Bit(5,Seg_Table_noDot[freq_max / 100 % 10]);
DisplaySMG_Bit(6,Seg_Table_noDot[freq_max / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table_noDot[freq_max % 10]);
break;
case 6:
DisplaySMG_Bit(0,Seg_Table_noDot[19]);
DisplaySMG_Bit(1,Seg_Table_noDot[10]);
DisplaySMG_Bit(2,Seg_Table_noDot[hour_log / 16]);
DisplaySMG_Bit(3,Seg_Table_noDot[hour_log % 16]);
DisplaySMG_Bit(4,Seg_Table_noDot[min_log / 16]);
DisplaySMG_Bit(5,Seg_Table_noDot[min_log % 16]);
DisplaySMG_Bit(6,Seg_Table_noDot[sec_log / 16]);
DisplaySMG_Bit(7,Seg_Table_noDot[sec_log % 16]);
break;
}
DisplaySMG_All(0xff);
}
void main()
{
SelectHC573(4,0xff);
SelectHC573(5,0x00);
Write_PCF8591_DAC(0x09);
Init_Timer0_Timer1();
Set_Ds1302();
freq_smg = Read_PCF8591_DAC();
Display_SMG();
while(1)
{
Display_SMG();
Read_Ds1302_Time(0);
if(freq_smg > chaoxian)
flag_warning=1;
else
flag_warning=0;
Display_SMG();
Scan_Keys();
}
}