若代码有误,欢迎在评论区批评指正!
main.c
#include <REGX52.H>
#include <onewire.H>
#include <iic.H>
#define uchar unsigned char
#define uint unsigned int
sbit K1 = P4^4;
sbit K2 = P4^2;
sbit K3 = P3^3;
sbit K4 = P3^2;
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
code uchar Seg_Table[] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x8c}; //10A 11b 12c 13d 14e 15f
code uchar Seg_Table_dot[] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e}; //10A 11b 12c 13d 14e 15f
uchar flag_S4,key_up=1,cs=25,dat,model;
uint temp,dac;
//==================函数声明区=======================================
void hc573(uchar channel);
void Init_system();
void delay_s(uchar t);
void delay_l(uchar xms);
void Nixie(uchar loc,num);
void Nixie_dot(uchar loc,num);
void display();
void LED();
//===================================================================
void hc573(uchar channel)
{
switch(channel)
{
case 4: P2 = (P2 & 0x1f) | 0x80; break; //LED
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;
}
}
void delay_s(uchar t)
{
while(t--);
}
void delay_l(uchar xms)
{
unsigned char i, j;
i = 12;
j = 169;
for(;xms>0;xms--)
{
do
{
while (--j);
} while (--i);
}
}
uchar Key_Scan()
{
K1=0;K2=K3=K4=1;
if(K3==0||K4==0)
{
delay_s(100);
if(K3==0)//S4按下
{
flag_S4=(flag_S4+1)%3;
while(K3 == 0)
{
temp=Read_Temp();
LED();
display();
}
return;
}
if(K4==0)//S5按下
{
model = (model+1)%2;;
while(K4 == 0)
{
temp=Read_Temp();
LED();
display();
}
return;
}
}
K2=0;K1=K3=K4=1;
if(K3==0||K4==0)
{
delay_s(100);
if(K3==0&&flag_S4==1)//S8按下
{
cs--;
while(K3 == 0)
{
temp=Read_Temp();
LED();
display();
}
return;
}
if(K4==0&&flag_S4==1)//S9按下
{
cs++;
while(K4 == 0)
{
temp=Read_Temp();
LED();
display();
}
return;
}
}
}
void Nixie(uchar loc,num)
{
hc573(0);
P0 = 0x01<<(loc-1);
hc573(6);
hc573(0);
P0 = Seg_Table[num];
hc573(7);
delay_s(500);
P0 = 0xff;
hc573(0);
}
void Nixie_dot(uchar loc,num)
{
hc573(0);
P0 = 0x01<<(loc-1);
hc573(6);
hc573(0);
P0 = Seg_Table_dot[num];
hc573(7);
delay_s(500);
P0 = 0xff;
hc573(0);
}
void Init_system()
{
hc573(0);
P0 = 0x00;
hc573(5);
hc573(0);
P0 = 0xff;
hc573(4);
hc573(0);
}
void display()
{
if(flag_S4 == 0)
{
Nixie(1,12);
Nixie(5,temp%10000/1000);
Nixie_dot(6,(temp%1000)/100);
Nixie(7,(temp%100/10));
Nixie(8,temp%10);
}
if(flag_S4 == 1)
{
Nixie(1,16);
Nixie(7,cs/10);
Nixie(8,cs%10);
}
if(flag_S4 == 2)
{
if(model == 0)
{
if(temp%10000/1000*10+(temp%1000)/100> cs)
{
dat = 255;
Nixie_dot(6,5);
Nixie(7,0);
Nixie(8,0);
}
else
{
dat=0;
Nixie_dot(6,0);
Nixie(7,0);
Nixie(8,0);
}
Write_PCF(dat);
Nixie(1,10);
}
if(model == 1)
{
if(temp%10000/1000*10+(temp%1000)/100<20) dac = 100;
if((temp%10000/1000*10+(temp%1000)/100>=20)&&(temp%10000/1000*10+(temp%1000)/100<=40))
dac = 0.15*(temp%10000/1000*1000+(temp%1000)/100*100+temp%100/10*10+temp%10) - 200;
if(temp%10000/1000*10+(temp%1000)/100>40) dac = 400;
dat = dac * 51;
Write_PCF(dat);
Nixie(1,10);
Nixie_dot(6,dac/100);
Nixie(7,dac / 10 %10);
Nixie(8,dac % 10);
}
}
}
void LED()
{
if(flag_S4==0)
{
hc573(0);
L2 = 0;
hc573(4);
hc573(0);
}
else
{
hc573(0);
L2 = 1;
hc573(4);
hc573(0);
}
if(flag_S4 == 1)
{
hc573(0);
L3 = 0;
hc573(4);
hc573(0);
}
else
{
hc573(0);
L3 = 1;
hc573(4);
hc573(0);
}
if(flag_S4 == 2)
{
hc573(0);
L4 = 0;
hc573(4);
hc573(0);
}
else
{
hc573(0);
L4 = 1;
hc573(4);
hc573(0);
}
if(model == 0)
{
hc573(0);
L1 = 0;
hc573(4);
hc573(0);
}
else
{
hc573(0);
L1 = 1;
hc573(4);
hc573(0);
}
}
void main()
{
Init_system();
temp=Read_Temp();
delay_l(85);
while(1)
{
temp=Read_Temp();
display();
Key_Scan();
LED();
}
}
iic.c
/* # I2C代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include <REGX52.H>
#include <intrins.H>
#define DELAY_TIME 5
sbit scl = P2^0;
sbit sda = P2^1;
static void I2C_Delay(unsigned char n)
{
do
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_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);
}
void Write_PCF(unsigned char dat)
{
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x40);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
iic.h
#ifndef __iic_h__
#define __iic_h__
void Write_PCF(unsigned char dat);
#endif
onewire.c
/* # 单总线代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
//
#include <REGX52.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_Temp()
{
unsigned int temp;
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
init_ds18b20();
temp=MSB;
temp = (temp<<8)|LSB;
temp = temp*0.0625*100;
return temp;
}
onewire.h
#ifndef __onewire_h__
#define __onewire_h__
unsigned int Read_Temp();
#endif
文章来源地址https://www.toymoban.com/news/detail-763193.html
文章来源:https://www.toymoban.com/news/detail-763193.html
到了这里,关于【蓝桥杯单片机】第十二届省赛(含题目和解答代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!