天天看點

藍橋杯單片機 溫度記錄器

藍橋杯 溫度記錄器

題目要求

藍橋杯單片機 溫度記錄器
main.c
#include <stc15f2k60s2.h>
#include <delay.h>
#include <onewire.h>
#include <ds1302.h>
sbit s4=P3^3;
sbit s5=P3^2;
sbit s6=P3^1;
sbit s7=P3^0;
unsigned char code SMG_duanma[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned char code SMG_position[9]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned int tem[10];//存放讀取的10次溫度
unsigned char count,cnt,time,begin;//time為采集時間,begin為間隔閃爍狀态,cnt為溫度讀取的次數,從0開始到9
unsigned char temptime=1,SMGflag=1;//temptime為溫度采集間隔,SMGflag為數位管界面标志位
unsigned char yi,er,san,si,wu,liu,qi,ba;
void InitHC138(unsigned char channel)//74HC138初始化
{
	switch(channel)
	{
		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 InitSystem()
{
	InitHC138(4);       //關閉LED燈
	P0=0xff;
	InitHC138(5);				//關閉繼電器和蜂鳴器
	P0=0x00;
	InitHC138(6);				//關閉數位管
	P0=0xff;
	InitHC138(7);
	P0=0xff;
}
/*****************數位管顯示函數**********************/
void SMG_Select(unsigned char pos,unsigned char dat)
{
	InitHC138(6);
	P0=SMG_position[pos];
	InitHC138(7);
	P0=SMG_duanma[dat];
}
void SMG_control()
{
	if(SMGflag==1){yi=er=san=si=wu=11;liu=10;qi=temptime/10;ba=temptime%10;}//參數設定界面
	else if(SMGflag==2) //時鐘顯示界面
	{
		yi=Timer[2]/16;er=Timer[2]%16;si=Timer[1]/16;wu=Timer[1]%16;
		qi=Timer[0]/16;ba=Timer[0]%16;
		if(begin==1) {san=liu=10;}
		else if(begin==0)  {san=liu=11;}
	}
	else if(SMGflag==3)//采集完成界面
	{
		yi=liu=10;er=cnt/10;san=cnt%10;si=wu=11;qi=tem[cnt]/10;ba=tem[cnt]%10;
	}
	else if(SMGflag==4)//切換顯示界面
	{
		yi=liu=10;er=cnt/10;san=cnt%10;si=wu=11;qi=tem[cnt]/10;ba=tem[cnt]%10;
	}
}
void SMG_Display()
{
	SMG_control();       //狀态選擇
	SMG_Select(1,yi); Delay2ms();
	SMG_Select(2,er); Delay2ms();
	SMG_Select(3,san);Delay2ms();
	SMG_Select(4,si); Delay2ms();
	SMG_Select(5,wu); Delay2ms();
	SMG_Select(6,liu);Delay2ms();
	SMG_Select(7,qi); Delay2ms();
	SMG_Select(8,ba); Delay2ms();	
}
void LED()
{
 if(SMGflag==3)//采集完成,L1閃爍	
 {	 
		if(begin==1){InitHC138(4);P0=0xfe;}
		else if(begin==0){InitHC138(4);P0=0xff;}
	}
}
void InitTimer0()
{
	TMOD=0X01;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	ET0=1;
	EA=1;
}
void Timer0(void)interrupt 1
{
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	count++;
	if(count==20)//1s時間
	{
		begin=!begin;//形成一個方波信号
		count=0;
		if(SMGflag==2)//進入時鐘界面後開始采集溫度
		{
			time++;
			if(time==temptime)
			{
				time=0;
				Read_temperature();//設定的間隔時間到,開始讀取溫度
				tem[cnt++]=temp;//将讀取的溫度存放于tem[]數組中
				if(cnt==10) //采集10次後,進入采集完成界面,停止溫度采集
				{
					cnt=0;
					SMGflag=3;
				}
			}
		}
		if(SMGflag==4) //切換顯示下,索引号1s加1,即1s鐘顯示一次已讀取的溫度,顯示10次後,進行循環顯示
		{
			cnt++;
			if(cnt==10) cnt=0;
		}
	}
}
void KeyScan()
{
	if(s4==0)//設定采集間隔時間鍵
	{
		Delay10ms();
		if(s4==0&&SMGflag==1)//在參數設定界面下有效
		{
			
			switch(temptime)
			{
				case 1 :temptime=5 ;break;
				case 5 :temptime=30;break;
				case 30:temptime=60;break;
				case 60:temptime=1 ;break;
			}
		}
		while(s4==0) SMG_Display();
	}
	if(s5==0)//确認采集間隔時間,開始采集溫度并進入時鐘界面
	{
		Delay10ms();
		if(s5==0&&SMGflag==1)//在參數設定界面下有效
		{
			TR0=1;             //開始采集溫度
			SMGflag=2;         //進入時鐘界面
		}
		while(s5==0) SMG_Display();
	}
	if(s6==0)
	{
		Delay10ms();
		if(s6==0&&SMGflag==3)  //在采集完成界面下有效
		{
			InitHC138(4);P0=0xff;//關閉L1
			SMGflag=4;           //進入切換顯示界面
		}
		while(s6==0) SMG_Display();
	}
	if(s7==0)               
	{
		Delay10ms();
		if(s7==0&&SMGflag==4)
		{
			SMGflag=1;            //進入參數設定界面
		}
		while(s7==0) SMG_Display();
	}
}
void main()
{
	InitSystem();
	InitTimer0();
	Init_DS1302();
	while(1)
	{
		Read_DS1302();
		KeyScan();
		SMG_Display();
		LED();
	}
}
           

ds1302.c

#include <stc15f2k60s2.h>
#include <intrins.h>
#include <ds1302.h>
unsigned char code ds1302_Write[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char code ds1302_Read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char Timer[7]={0x50,0x59,0x23,0x00,0x00,0x00,0x00};

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302複位												

void Write_Ds1302_Byte(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK=0;
		SDA=temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

void Write_Ds1302( unsigned char address,unsigned char dat )     
{
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;	
   	_nop_();  
 	Write_Ds1302_Byte(address);	
 	Write_Ds1302_Byte(dat);		
 	RST=0; 
}

unsigned char Read_Ds1302 ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;
	_nop_();
 	Write_Ds1302_Byte(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;
	_nop_();
 	RST=0;
	SCK=0;
	_nop_();
	SCK=1;
	_nop_();
	SDA=0;
	_nop_();
	SDA=1;
	_nop_();
	return (temp);			
}

void Init_DS1302()
{
	unsigned char i;
	Write_Ds1302(0x8e,0x00);
	for(i=0;i<7;i++)
	{
		Write_Ds1302(ds1302_Write[i],Timer[i]);
	}
	Write_Ds1302(0x8e,0x80);
}
void Read_DS1302()
{
	unsigned char i;
	for(i=0;i<7;i++)
	{
		Timer[i]=Read_Ds1302(ds1302_Read[i]);
	}
}

           

onewire.c

#include "onewire.h"
unsigned int temp;
//單總線延時函數
void Delay_OneWire(unsigned int t)
{
  unsigned char aa;
  while(t--)
	{
		for(aa=0;aa<12;aa++);
	}
}

//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;
}

//通過單總線向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;
}
void Read_temperature()
{
	unsigned char LSB,MSB;
	Init_DS18B20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	Delay_OneWire(200);
	
	Init_DS18B20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xBE);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	Init_DS18B20();
	temp=(MSB<<8)|LSB;
	temp=temp>>4;
}
           

delay.c

#include <stc15f2k60s2.h>
#include <intrins.h>
#include <delay.h>
void Delay2ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 22;
	j = 128;
	do
	{
		while (--j);
	} while (--i);
}

void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}
           

完整工程檔案請摸我

繼續閱讀