天天看點

藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)

藍橋杯第十一屆省賽(第二次)

藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)
藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)
藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)
藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)
藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)
藍橋杯第十一屆省賽(第二次)藍橋杯第十一屆省賽(第二次)

onewire.c

/*
  程式說明: 單總線驅動程式
  軟體環境: Keil uVision 4.10 
  硬體環境: CT107單片機綜合實訓平台(外部晶振12MHz) STC89C52RC單片機
  日    期: 2011-8-9
*/
#include "reg52.h"

sbit DQ = P1^4;  //單總線接口

//單總線延時函數
void Delay_OneWire(unsigned int t);  //STC89C52RC


//通過單總線向DS18B20寫一個位元組
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);
}

//從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(5);
	}
	return dat;
}

//DS18B20裝置初始化
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;
}

void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	t*=11;
	while(t--);
}

float read_temp()
{
	unsigned char low,high;
	float temp;
	
	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<<8|low)*0.0625;
	
	return temp;
	
}
           

common.c

#include<common.h>

uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc6};
uchar dsp_show[8];
uint mode=0;
uint count_temp=0;
uint temp;
uint tmax=30,tmin=20;
uchar cnt,trg;
uint pos=0;
uchar L1,L2,L3,L4;


void system_init()
{
	uint i;
	
	P0=0xff;P2=0x80;P2=0;
	P0=0;P2=0xa0;P2=0;
	
	for(i=0;i<70;i++)
		temp=read_temp()+0.5;
	
	
	Timer0Init();
}

void Timer0Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定時器時鐘1T模式
	TMOD &= 0xF0;		//設定定時器模式
	TL0 = 0xCD;		//設定定時初值
	TH0 = 0xD4;		//設定定時初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定時器0開始計時
	
	EA=1;
	ET0=1;
}

void tm0(void) interrupt 1
{
	static uint bit_com;
	
	P0=~(8*L4+4*L3+2*L2+L1);P2=0x80;P2=0;  //控制LED
	
	
	P0=0;P2=0xc0;P2=0;
	P0=dsp_show[bit_com];P2=0xe0;P2=0;
	P0=1<<bit_com;P2=0xc0;P2=0;
	
	if(bit_com==7)
		bit_com=0;
	else
		bit_com++;
	
	count_temp++;
}

void scan_button(void)
{
	uchar readdat=P3^0xff;
	trg=readdat&(readdat^cnt);
	cnt=readdat;
	
	if(trg&0x08)   //S4
	{
		mode=~mode;
	}
	else if(trg&0x04) //S5
	{
		pos=~pos;
	}
	else if(trg&0x02) //S6
	{
		if(tmax<100&&tmin<100)
		{
			if(pos==0)
			{
				tmax++;
			}
			else
			{
				tmin++;
			}
		}
		
	}
	else if(trg&0x01) //S7
	{
		if(tmin>=0&&tmax>=0)
		{
			if(pos==0)
			{
				tmax--;
			}
			else
			{
				tmin--;
			}
		}
		
	}
}
           

main.c

#include<main.h>

extern uchar dsp_code[];
extern uchar dsp_show[];
extern uint mode;
extern uint count_temp;
extern uint temp;
extern uint tmax,tmin;
extern uchar cnt,trg;
extern uint pos;
extern uchar L1,L2,L3,L4;

void main()
{
	system_init();
	
	while(1)
	{
		if(mode==0)
		{
			if(count_temp>500)
			{
				count_temp=0;
				temp=read_temp()+0.5;
			}
			
			dsp_show[0]=0xc6;
			dsp_show[1]=dsp_code[10];
			dsp_show[2]=dsp_code[10];
			dsp_show[3]=dsp_code[10];
			dsp_show[4]=dsp_code[10];
			dsp_show[5]=dsp_code[10];
			dsp_show[6]=dsp_code[temp/10];
			dsp_show[7]=dsp_code[temp%10];
			
		}
		else
		{
			dsp_show[0]=0x8c;
			dsp_show[1]=dsp_code[10];
			dsp_show[2]=dsp_code[10];
			dsp_show[3]=dsp_code[tmax/10];
			dsp_show[4]=dsp_code[tmax%10];
			dsp_show[5]=dsp_code[10];
			dsp_show[6]=dsp_code[tmin/10];
			dsp_show[7]=dsp_code[tmin%10];
		
			if(temp>tmax)
			{
				L1=1;L2=0;L3=0;L4=0;
			}
			else if(temp<tmax&&temp>tmin)
			{
				L1=0;L2=1;L3=0;L4=0;
			}
			else if(temp<tmin)
			{
				L1=0;L2=0;L3=1;L4=0;
			}
			else
			{
				L1=0;L2=0;L3=0;L4=1;
			}
		}
			
		scan_button();
		
	}
}
           

繼續閱讀