十五届蓝桥杯省赛真题及代码
LED部分可能有点问题
程序设计题:
代码如下:
main.c:
#include <STC15F2K60S2.H>
#include <stdio.h>
#include <intrins.h>
#include "Timer.h"
#include "bsp_init.h"
#include "bsp_ds1302.h"
#include "bsp_iic.h"
#include "bsp_key.h"
#include "bsp_led.h"
#include "bsp_NE555.h"
#include "bsp_seg.h"
void Key_Proc();
void Seg_Proc();
void Led_Proc();
unsigned int Key_Slow_Down=0;
unsigned int Seg_Slow_Down=0;
unsigned int Led_Slow_Down=0;
unsigned long ms_tick=0;
unsigned char Key_Value,Key_Down,Key_Old;
unsigned char seg_buf[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char seg_string[10];
unsigned char pos;
unsigned char ucled=0xff;
//ds1302
unsigned char Rtc[3]={23,59,55};
unsigned char max_freq_hour=0;
unsigned char max_freq_min=0;
unsigned char max_freq_sec=0;
//555
int freq=0;
int max_freq=0;
int beyond_freq=2000;
int fix_freq=0;
int fixed_freq=0;
//DAC
float k=0;
float b=0;
unsigned char dac=0;
unsigned char screen_flag=1;//1是频率,2是参数,3是时间,4是回显
unsigned char screen_set_num=1;//参数设置切换
unsigned char screen_back=1;//回显设置切换
void main()
{
Cls();
Timer1Init();
EA=1;
NE555_Timer0Init();
Set_Rtc(Rtc);
//Led_Disp(ucled);
while(1)
{
Key_Proc();
Seg_Proc();
Led_Proc();
}
}
void tm0_isr() interrupt 3
{
ms_tick++;
if(++Key_Slow_Down==10) Key_Slow_Down=0;
if(++Seg_Slow_Down==80) Seg_Slow_Down=0;
if(++Led_Slow_Down==80) Led_Slow_Down=0;
if((ms_tick%1000)==0)
{
freq=((TH0<<8)|TL0);
TH0=0;
TL0=0;
}
Seg_Disp(seg_buf,pos);
if(++pos==8) pos=0;
Led_Disp(ucled);
}
void Key_Proc()
{
if(Key_Slow_Down) return;
Key_Slow_Down=1;
//
Key_Value=Key_Read();
Key_Down=Key_Value&(Key_Value^Key_Old);
Key_Old=Key_Value;
switch(Key_Down)
{
case 4:
if(++screen_flag==5) screen_flag=1;;
break;
case 5:
if(screen_flag==2)
{
if(++screen_set_num==3)
screen_set_num=1;
}
if(screen_flag==4)
{
if(++screen_back==3)
screen_back=1;
}
break;
case 8:
if(screen_set_num==1) //超限
{
beyond_freq+=2000;
if(beyond_freq==10000) beyond_freq=9000;
}
else if(screen_set_num==2) //校准
{
fix_freq+=200;
if(fix_freq==1000) fix_freq=900;
}
case 9:
if(screen_set_num==1) //超限
{
beyond_freq-=1000;
if(beyond_freq==0) beyond_freq=1000;
}
else if(screen_set_num==2) //校准
{
fix_freq-=100;
if(fix_freq==-1000) fix_freq=-900;
}
}
}
void Seg_Proc()
{
if(Seg_Slow_Down) return;
Seg_Slow_Down=1;
/*数据采集*/
fixed_freq = freq+fix_freq;
if(fixed_freq>max_freq)//最大频率
{
max_freq=fixed_freq;
Read_Rtc(Rtc);
max_freq_hour=Rtc[0];
max_freq_min=Rtc[1];
max_freq_sec=Rtc[2];
}
/*DAC*/
if(fixed_freq<beyond_freq)
dac=((4/((float)beyond_freq-500.0))*fixed_freq+(1-(2000/((float)beyond_freq-500))))*51;
else if(fixed_freq<0)
{
dac=0;
}
Pcf8591_Dac(dac);
/*显示区*/
if(screen_flag==1)//频率界面
{
if(fixed_freq>0)
{
sprintf(seg_string,"F %5d",fixed_freq);
}
else {
sprintf(seg_string,"F LL");
//ucled&=(~0x02);
//ucled|=0x02;
};
}
else if(screen_flag==2)//参数界面
{
if(screen_set_num==1)//超限
sprintf(seg_string,"P1 %4d",beyond_freq);
else //校准
{
sprintf(seg_string,"P2 %4d",fix_freq);
}
}
else if(screen_flag==3)//时间界面
{
Read_Rtc(Rtc);
sprintf(seg_string,"%02d-%02d-%02d",(unsigned int)Rtc[0],(unsigned int)Rtc[1],(unsigned int)Rtc[2]);
}
else if(screen_flag==4)//回显界面
{
if(screen_back==1)
sprintf(seg_string,"HF %5d",max_freq);
else
{
sprintf(seg_string,"HA%02d%02d%02d",(unsigned int)max_freq_hour,(unsigned int)max_freq_min,(unsigned int)max_freq_sec);
}
}
Seg_Tran(seg_string,seg_buf);
}
void Led_Proc()
{
if(Led_Slow_Down) return;
Led_Slow_Down=1;
if((screen_flag==1)&&(fix_freq<beyond_freq))
{
ucled&=0x01;
ucled^=0x01;
if(fix_freq<0)
ucled|=0x02;
}
else if(fix_freq<0)
{
ucled&=0x02;
ucled|=0x02;
}
else if((screen_flag!=1)&&(fix_freq>beyond_freq))ucled&=0xfe;
else
{
ucled&=0x02;
ucled^=0x02;
if(fix_freq<0)
ucled|=0x02;
}
}
Timer.c
#include "Timer.h"
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初值
TH1 = 0xFC; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1=1;
}
bsp_init.c
#include "bsp_init.h"
void Cls()
{
P0=0xff;//LED
P2=P2&0x1f|0x80;
P2&=0x1f;
P0=0x00;//relay
P2=P2&0x1f|0xa0;
P2&=0x1f;
}
bsp_ds1302.c
/* # DS1302代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include "intrins.h"
#include "bsp_ds1302.h"
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST =P1^3;
//
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);
}
void Set_Rtc(unsigned char *Rtc)
{
unsigned char temp;
Write_Ds1302_Byte(0x8e,0);
temp=(((Rtc[0]/10)<<4)|(Rtc[0]%10));
Write_Ds1302_Byte(0x84,temp);
temp=(((Rtc[1]/10)<<4)|(Rtc[1]%10));
Write_Ds1302_Byte(0x82,temp);
temp=(((Rtc[2]/10)<<4)|(Rtc[2]%10));
Write_Ds1302_Byte(0x80,temp);
Write_Ds1302_Byte(0x8e,0x80);
}
void Read_Rtc(unsigned char *Rtc)
{
unsigned char temp;
temp=Read_Ds1302_Byte(0x85);
Rtc[0]=(((temp>>4)*10)+((temp&0x0f)));
temp=Read_Ds1302_Byte(0x83);
Rtc[1]=(((temp>>4)*10)+((temp&0x0f)));
temp=Read_Ds1302_Byte(0x81);
Rtc[2]=(((temp>>4)*10)+((temp&0x0f)));
}
bsp_iic.c
/* # I2C代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include "bsp_iic.h"
#include "intrins.h"
#define DELAY_TIME 5
sbit sda=P2^1;
sbit scl=P2^0;
//
static void I2C_Delay(unsigned char n)
{
do
{
_nop_();
}
while(n--);
}
//
void I2CStart(void)
{
sda = 1;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 0;
I2C_Delay(DELAY_TIME);
scl = 0;
}
//
void I2CStop(void)
{
sda = 0;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 1;
I2C_Delay(DELAY_TIME);
}
//
void I2CSendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++){
scl = 0;
I2C_Delay(DELAY_TIME);
if(byt & 0x80){
sda = 1;
}
else{
sda = 0;
}
I2C_Delay(DELAY_TIME);
scl = 1;
byt <<= 1;
I2C_Delay(DELAY_TIME);
}
scl = 0;
}
//
//unsigned char I2CReceiveByte(void)
//{
// unsigned char da;
// unsigned char i;
// for(i=0;i<8;i++){
// scl = 1;
// I2C_Delay(DELAY_TIME);
// da <<= 1;
// if(sda)
// da |= 0x01;
// scl = 0;
// I2C_Delay(DELAY_TIME);
// }
// return da;
//}
//
unsigned char I2CWaitAck(void)
{
unsigned char ackbit;
scl = 1;
I2C_Delay(DELAY_TIME);
ackbit = sda;
scl = 0;
I2C_Delay(DELAY_TIME);
return ackbit;
}
//
void I2CSendAck(unsigned char ackbit)
{
scl = 0;
sda = ackbit;
I2C_Delay(DELAY_TIME);
scl = 1;
I2C_Delay(DELAY_TIME);
scl = 0;
sda = 1;
I2C_Delay(DELAY_TIME);
}
//=======Pcf8591=================
void Pcf8591_Dac(unsigned char dat)
{
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x41);
I2CWaitAck();
I2CSendByte(dat);
I2CSendAck(1);
I2CStop();
}
bsp_key.c
#include "bsp_key.h"
unsigned char Key_Read()
{
unsigned char Key_Value;
unsigned int temp;
P44=0;P42=1;//P35=1;//P34=1;
temp=P3&0x0c;
P44=1;P42=0;//P35=1;//P34=1;
temp=((temp<<4)|(P3&0x0c));
switch(temp)
{
case 0x4c: Key_Value= 4;break;
case 0x8c: Key_Value= 5;break;
case 0xc4: Key_Value= 8;break;
case 0xc8: Key_Value= 9;break;
default:Key_Value= 0;break;
}
return Key_Value;
}
bsp_led.c
#include "bsp_led.h"
void Led_Disp(unsigned char ucled)
{
P0=(~ucled);//LED
P2=P2&0x1f|0x80;
P2&=0x1f;
}
NE_555.c
#include "bsp_NE555.h"
void NE555_Timer0Init(void) //1毫秒@12.000MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x05; //设置定时器模式
TL0 = 0; //设置定时初值
TH0 = 0; //设置定时初值
TR0 = 1; //定时器0开始计时
}
bsp_seg.c
#include "bsp_seg.h"
void Seg_Disp(unsigned char *seg_buf,unsigned char pos)
{
P0=1<<pos;//LED
P2=P2&0x1f|0xc0;
P2&=0x1f;
P0=0xff;//消影
P2=P2&0x1f|0xe0;
P2&=0x1f;
P0=seg_buf[pos];//数码
P2=P2&0x1f|0xe0;
P2&=0x1f;
}
void Seg_Tran(unsigned char* seg_string,unsigned char *seg_buf)
{
unsigned char i=0;
unsigned char j=0;
unsigned char temp;
for(i=0;i<=7;i++,j++)
{
switch(seg_string[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 'A':temp=0x88;break;
case 'F':temp=0x8e;break;
case 'H':temp=0x89;break;
case 'L':temp=0xC7;break;
case 'N':temp=0xC8;break;
case 'P':temp=0x8C;break;
case 'U':temp=0xC1;break;
case '-':temp=0xBF;break;
case ' ':temp=0xFF;break;
default:temp=0xff;break;
}
if(seg_string[j+1]=='.')
{
temp&=0x7f;
j++;
}
seg_buf[i]=temp;
}
}
==================================2024-04-14==============================
寄了,省二前几名,突击失败
==================================2024-04-29==============================