刚刚参加完这届的比赛,试题不难,因此一出考场就把代码发出来了,仅供参考!
程序设计题:
客观题:
我的答案:1.B 2.B 3.C 4.B 5.C 6.B 7.A 8.AC 9.B 10.D
#include "reg52.h"
#include "iic.h"
#include "onewire.h"
#define char unsigned char
#define int unsigned int
sfr P4=0xc0;
sbit R1=P3^3;
sbit R2=P3^2;
sbit C1=P4^4;
sbit C2=P4^2;
void read_temp();
char code smg_dat[13]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc6,0x8c,0x88};
char code smg_dot[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
int temp=0;
int temp_p[2]={25,25}; //temp_p[0]温度参数 temp_p[1]温度参数的加减
int volt=0;
char s4_state=0;
bit s5_state=0;
bit set_t=0;
void select573(char channel)
{
switch(channel)
{
case 0:
P2=(P2 & 0x1f) | 0x00;break;
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;
}
}
void delay_smg(char t)
{
while(t--);
}
void display_onesmg(char value,char pos)
{
select573(7);
P0=0xff;
select573(6);
P0=0x01 << pos;
select573(7);
P0=value;
}
void close_allsmg()
{
select573(6);
P0=0xff;
select573(7);
P0=0xff;
}
void display_t()
{
display_onesmg(smg_dat[10],0);
delay_smg(500);
display_onesmg(smg_dat[temp / 1000],4);
delay_smg(500);
display_onesmg(smg_dot[(temp / 100) % 10],5);
delay_smg(500);
display_onesmg(smg_dat[(temp / 10) % 10],6);
delay_smg(500);
display_onesmg(smg_dat[temp % 10],7);
delay_smg(500);
close_allsmg();
}
void display_p()
{
display_onesmg(smg_dat[11],0);
delay_smg(500);
display_onesmg(smg_dat[temp_p[1] / 10],6);
delay_smg(500);
display_onesmg(smg_dat[temp_p[1] % 10],7);
delay_smg(500);
close_allsmg();
}
void display_dac()
{
display_onesmg(smg_dat[12],0);
delay_smg(500);
display_onesmg(smg_dot[volt / 100],5);
delay_smg(500);
display_onesmg(smg_dat[(volt / 10) % 10],6);
delay_smg(500);
display_onesmg(smg_dat[volt % 10],7);
delay_smg(500);
close_allsmg();
}
void display_smg()
{
switch(s4_state)
{
case 0:
read_temp();
display_t();break;
case 1:
display_p();break;
case 2:
set_t=0;
display_dac();break;
}
}
void key()
{
R1=0;
R2=1;
C1=C2=1;
if(C1 == 0) //S4
{
while(C1 == 0);
s4_state++;
if(s4_state == 1)
{
set_t=1;
}
if(s4_state == 3)
{
s4_state=0;
}
}
if(C2 == 0) //S8
{
temp_p[0]=temp_p[1];
while(C2 == 0)
{
display_smg();
}
if(set_t == 1)
{
temp_p[1]--;
}
else temp_p[0]=temp_p[1];
}
R2=0;
R1=C1=C2=1;
if(C1 == 0) //s5
{
while(C1 == 0);
s5_state=~s5_state;
}
if(C2 == 0) //s9
{
temp_p[0]=temp_p[1];
while(C2 == 0)
{
display_smg();
}
if(set_t == 1)
{
temp_p[1]++;
}
else temp_p[0]=temp_p[1];
}
}
void pcf8591_dac(char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void read_temp()
{
char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay_smg(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
temp=(MSB << 8) | LSB;
if((temp & 0xf800) == 0x0000)
{
temp = temp >> 4;
temp=temp*100;
temp=temp + (LSB & 0x0f) * 6.25;
}
}
void led_dac()
{
select573(4);
if(s5_state == 0)
{
P0 &=0xfe ;
if(temp < temp_p[0]*100)
{
volt=0;
pcf8591_dac(0x00);
}
else
{
volt=500;
pcf8591_dac(0xff);
}
}
if(s5_state == 1)
{
if(temp <= 2000)
{
volt=100 ;
pcf8591_dac(0x51);
}
else if(temp <= 4000)
{
volt=(0.15 * temp) - 200;
pcf8591_dac(volt * 0.51);
}
else
{
volt = 400;
pcf8591_dac(204);
}
}
if(s4_state == 0)
{
P0 &=0xfd;
}
else if(s4_state == 1)
{
P0 &= 0xfb;
}
else P0 &=0xf7;
}
void init_system()
{
select573(4);
P0=0xff;
select573(5);
P0=0x00;
select573(0);
}
void main ()
{
init_system();
while(1)
{
key();
led_dac();
display_smg();
}
}
///
IIC.H
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
//void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
//unsigned char IIC_RecByte(void);
#endif
///
IIC.C
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
//void IIC_SendAck(bit ackbit)
//{
// SCL = 0;
// SDA = ackbit; // 0:应答,1:非应答
// IIC_Delay(DELAY_TIME);
// SCL = 1;
// IIC_Delay(DELAY_TIME);
// SCL = 0;
// SDA = 1;
// IIC_Delay(DELAY_TIME);
//}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
//unsigned char IIC_RecByte(void)
//{
// unsigned char i, da;
// for(i=0; i<8; i++)
// {
// SCL = 1;
// IIC_Delay(DELAY_TIME);
// da <<= 1;
// if(SDA) da |= 1;
// SCL = 0;
// IIC_Delay(DELAY_TIME);
// }
// return da;
//}
///
ONEWIRE.H
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned char rd_temperature(void); //; ;
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif
///
ONEWIRE.C
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(50);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(50);
}
//从DS18B20读取一个字节
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(50);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(120);
DQ = 0;
Delay_OneWire(800);
DQ = 1;
Delay_OneWire(100);
initflag = DQ;
Delay_OneWire(50);
return initflag;
}
版权声明:本文为CSDN博主「yinyandong」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yyd0922/article/details/115828815
暂无评论