main.c程序
#include
#include "ds18b20.h"
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6,0x8c};
uchar yi,er,san,si,wu,liu,qi,ba;
uchar s4=0,s5=0,jia=0,jian=0,T_max,T_min;
bit fla=0,LED4_flag=0;
uint tt=0;
uchar wendu,AD,MAX=30,MIN=20;
void delayms(int ms);
void allinit();
void display1(uchar yi,uchar er);
void display2(uchar san,uchar si);
void display3(uchar wu,uchar liu);
void display4(uchar qi,uchar ba);
void keyscan();
void Timer0Init(void);
void main()
{
allinit();
Timer0Init();
while(1)
{
AD=AD_read(0x40);
wendu=Tempget();
if(s4==0)
{
if(LED4_flag==0)
{
if(MAXelse if((MIN else if(wendu }
yi=12;er=11;san=11;si=11;wu=11;liu=11;qi=wendu/10;ba=wendu%10;
}
else if(s4==1)
{
if((jia==1)&&(s5==0)){jia=0;
if(T_max==99)T_max=99;
else T_max=T_max+1;}
if((jia==1)&&(s5==1)){jia=0;
if(T_min==99)T_min=99;
else T_min=T_min+1;}
if((jian==1)&&(s5==0)){jian=0;
if(T_max==0)T_max=0;
else T_max=T_max-1;}
if((jian==1)&&(s5==1)){jian=0;
if(T_min==0)T_min=0;
else T_min=T_min-1;}
yi=13;er=11;san=11;si=T_max/10;wu=T_max%10;liu=11;qi=T_min/10;ba=T_min%10;
}
keyscan();
display1(yi,er);
display2(san,si);
display3(wu,liu);
display4(qi,ba);
}
}
void Timer0Init(void) //5毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xA0; //设置定时初值
TH0 = 0x15; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Time0() interrupt 1
{
tt++;
if((tt==1)&&(LED4_flag==1))
{
tt=0;
P2|=0X80;P2&=0X9F;P0=0XFF;
P2|=0X80;P2&=0X9F;P0=0XF7;
}
}
void delayms(int ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=845;j>0;j--);
}
void allinit()
{
P2=0XA0;P0=0X00;
P2=0X80;P0=0XFF;
P2=0XC0;P0=0XFF;
P2=0XFF;P0=0XFF;
}
void display1(uchar yi,uchar er)
{
P2=0XC0;P0=0X01;
P2=0XFF;P0=tab[yi];
delayms(1);
P2=0XC0;P0=0X02;
P2=0XFF;P0=tab[er];
delayms(1);
}
void display2(uchar san,uchar si)
{
P2=0XC0;P0=0X04;
P2=0XFF;P0=tab[san];
delayms(1);
P2=0XC0;P0=0X08;
P2=0XFF;P0=tab[si];
delayms(1);
}
void display3(uchar wu,uchar liu)
{
P2=0XC0;P0=0X10;
P2=0XFF;P0=tab[wu];
delayms(1);
P2=0XC0;P0=0X20;
P2=0XFF;P0=tab[liu];
delayms(1);
}
void display4(uchar qi,uchar ba)
{
P2=0XC0;P0=0X40;
P2=0XFF;P0=tab[qi];
delayms(1);
P2=0XC0;P0=0X80;
P2=0XFF;P0=tab[ba];
delayms(1);
P2=0XC0;P0=0X80;
P2=0XFF;P0=0XFF;
}
void keyscan()
{
if(P30==0)
{
delayms(5);
if(P30==0)
{
if((jian==0)&&(s4==1)){jian=jian+1;}
}
while(!P30);
}
else if(P31==0)
{
delayms(5);
if(P31==0)
{
if((jia==0)&&(s4==1)){jia=jia+1;}
}
while(!P31);
}
else if(P32==0)
{
delayms(5);
if(P32==0)
{
if((s5==0)&&(s4==1)){s5=1;}
else if((s5==1)&&(s4==1)){s5=0;}
}
while(!P32);
}
else if(P33==0)
{
delayms(5);
if(P33==0)
{
if(s4==0){s4=1;T_max=MAX;T_min=MIN;}
else if(s4==1){s4=0;
if(T_max>=T_min){MAX=T_max;MIN=T_min;LED4_flag=0;EA=0;ET0=0;tt=0;}
else{MAX=MAX;MIN=MIN;LED4_flag=1;EA=1;ET0=1;tt=0;}
}
}
while(!P33);
}
}
ds18b20.c程序
#include
#include "ds18b20.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t)
{
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;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 {
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;
}
unsigned char Tempget()
{
unsigned char temp,low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=high temp|=(low>>4);
return temp;
}
iic.c程序
#include
#include "iic.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i {
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i {
SCL = 1;
somenop;
da if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
unsigned char AD_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void AD_write(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
ds18b20.h程序
#ifndef __DS18B20_H
#define __DS18B20_H
//单总线延时函数
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
unsigned char Tempget();
#endif
iic.h程序
#ifndef _IIC_H
#define _IIC_H
//函数声明
void IIC_Start(void);
void IIC_Stop(void);
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);
unsigned char IIC_RecByte(void);
unsigned char AD_read(unsigned char add);
void AD_write(unsigned char dat);
#endif
第十一届蓝桥杯单片机(第二场)作品演示
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net