蓝桥杯单片机第十二届省赛代码
初学
#include <STC15F2K60S2.H>
#include "onewire.h"
#include "iic.h"
void DisplaySMG();
sbit R0 = P3^2;
sbit R1 = P3^3;
sbit C0 = P4^4;
sbit C1 = P4^2;
sbit LED0 = P0^0;
unsigned char code SMG[] = { //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,
}; // 0x77,0x7C,0x39,0x5E,0x79,0x71,
black - H J K L N o P U t G Q r M y
// 0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
unsigned char code SMGd[] = {0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};//0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1};
long temp ,SMGtemp,cstemp = 25;
float DACv;
long DAC;
int jm = 1;
int state = 1;
int ms = 1;
int outv = 0;
void SMGdelay(unsigned char nn)
{
while(nn--);
}
void delay(unsigned char nn)
{
while(nn--)
{
DisplaySMG();
}
}
void H138(int select)
{
switch(select)
{
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 PlaySMG(int wei,unsigned char value)
{
P0 = 0x01 << wei;
H138(6);
P0 = value;
H138(7);
}
void DisplaySMG()
{
switch(jm)
{
case 1:
{
PlaySMG(0,~0x39);
SMGdelay(100);
PlaySMG(0,0xff);
PlaySMG(4,~SMG[(SMGtemp / 1000)]);
SMGdelay(100);
PlaySMG(4,0xff);
PlaySMG(5,~SMGd[(SMGtemp / 100)%10]);
SMGdelay(100);
PlaySMG(5,0xff);
PlaySMG(6,~SMG[(SMGtemp / 10)%10]);
SMGdelay(100);
PlaySMG(6,0xff);
PlaySMG(7,~SMG[SMGtemp % 10]);
SMGdelay(100);
PlaySMG(7,0xff);
}
break;
case 2:
{
PlaySMG(0,~0x73);
SMGdelay(100);
PlaySMG(0,0xff);
PlaySMG(6,~SMG[cstemp / 10]);
SMGdelay(100);
PlaySMG(6,0xff);
PlaySMG(7,~SMG[cstemp % 10]);
SMGdelay(100);
PlaySMG(7,0xff);
}
break;
case 3:
{
PlaySMG(0,~0x77);
SMGdelay(100);
PlaySMG(0,0xff);
PlaySMG(5,~SMGd[DAC / 100]);
SMGdelay(100);
PlaySMG(5,0xff);
PlaySMG(6,~SMG[(DAC / 10)%10]);
SMGdelay(100);
PlaySMG(6,0xff);
PlaySMG(7,~SMG[DAC % 10]);
SMGdelay(100);
PlaySMG(7,0xff);
}
break;
}
}
long ds18b20()
{
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay(10);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
// temp = MSB << 4;
// temp = temp | (LSB >> 4);
// SMGtemp = temp;
temp = MSB & 0x0f ;
temp = temp << 8;
temp = temp | LSB;
temp = temp * 6.25;
return temp;
}
void pcf8591(unsigned char v)
{
IIC_Start();//IIC总线起始信号
IIC_SendByte(0x90);//发送PCF8591的写设备地址
IIC_WaitAck();//等待从机应答
IIC_SendByte(0x40);//发送控制字节,选择DA模式
IIC_WaitAck();//等待从机应答
IIC_SendByte(v);//发送
IIC_WaitAck();//等待从机应答
IIC_Stop();//IIC总线起始信号
DACv = v *(5.0/255);
DAC = DACv * 100;
}
void Key()
{
R0 = 0;
R1 = C0 = C1 = 1;
if(C0 == 0) // s5
{
delay(10);
if(C0 == 0)
{
if(jm == 3)
{
switch(ms)
{
case 2:
{
ms = 1;
}
break;
case 1:
ms = 2;
break;
}
if(ms == 1)
{
}
if(ms == 2)
{
}
}
}
while(!C0)
{
DisplaySMG();
}
}
if(C1 == 0) // s9
{
delay(10);
if(C1 == 0)
{
if(jm == 2)
{
cstemp = cstemp + 1;
}
}
while(!C1)
{
DisplaySMG();
}
}
R1= 0;
R0 = C0 = C1 = 1;
if(C0 == 0) // s4
{
delay(10);
if(C0 == 0)
{
// ds18b20();
switch(state)
{
case 1:
jm = 2;
state = 2;
break;
case 2:
jm = 3;
state = 3;
break;
case 3:
jm = 1;
state = 1;
break;
}
}
while(!C0)
{
DisplaySMG();
}
}
if(C1 == 0) // s8
{
delay(10);
if(C1 == 0)
{
if(jm == 2)
{
cstemp = cstemp - 1;
}
}
while(!C1)
{
DisplaySMG();
}
}
}
void LED()
{
if(jm == 1)
{
P0 = ~0x02;
H138(4);
}
if(jm == 2)
{
P0 = ~0x04;
H138(4);
}
if(jm == 3)
{
if(ms == 1)
{
P0 = ~0x09;
H138(4);
}
if(ms == 2)
{
P0 = ~0x08;
H138(4);
}
}
}
void star()
{
P0 = 0x00;
H138(5);
P0 = 0xff;
H138(4);
}
void Huanms()
{
if(ms == 1)
{
if((SMGtemp/100) >= cstemp)
{
outv = 255;
}
if((SMGtemp/100) < cstemp)
{
outv = 0;
}
}
if(ms == 2)
{
if((SMGtemp/100) <= 20)
{
outv = 51;
}
if((SMGtemp/100) > 20)
{
outv = 51 + ((SMGtemp/100) - 20)*8;
}
}
}
void main ()
{
SMGtemp = ds18b20();
star();
while(1)
{
SMGtemp = ds18b20();
LED();
pcf8591(outv);
DisplaySMG();
Key();
Huanms();
}
}