欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

蓝桥杯单片机第6届省赛_温度记录器

程序员文章站 2022-06-08 20:48:02
...

题目

蓝桥杯单片机第6届省赛_温度记录器

蓝桥杯单片机第6届省赛_温度记录器
蓝桥杯单片机第6届省赛_温度记录器

代码

timer.c

#include <timer.h>

void Timer0Init(void)  //1毫秒@12.000MHz
{
 AUXR |= 0x80;  //定时器时钟1T模式
 TMOD &= 0xF0;  //设置定时器模式
 TL0 = 0x20;  //设置定时初值
 TH0 = 0xD1;  //设置定时初值
 TF0 = 0;  //清除TF0标志
 TR0 = 1;  //定时器0开始计时
 EA=ET0=1;
}

timer.h

#ifndef _TIMER_H_
#define _TIMER_H_

#include <STC15F2K60S2.H>

void Timer0Init(void);

#endif

smg.c

#include <smg.h>

unsigned char code smgduan[12]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,
0X80,0X90,0XBF,0XFF};

unsigned char dspbuf[8];

void smg_display(void)
{
 static unsigned char i=0;
 
 P2=(P2&=0X1F)|0XE0;
 P0=0XFF;
 P2&=0X1F;
 
 P2=(P2&=0X1F)|0XC0;
 P0=0x01<<i;
 P2&=0X1F;
 
 P2=(P2&=0X1F)|0XE0;
 P0=smgduan[dspbuf[i]];
 P2&=0X1F;
 
 if(++i==8)
  i=0;
}

smg.h

#ifndef _SMG_H_
#define _SMG_H_

#include <STC15F2K60S2.H>
void smg_display(void);

extern unsigned char dspbuf[8];

#endif

key.c

#include <key.h>

unsigned char key_sum=0;
unsigned char k4_press,k5_press,k6_press,k7_press;

void key_read(void)
{
  static unsigned char k4_time=0,k5_time=0,k6_time=0,k7_time=0;
 key_sum=0;
 if(k4==0)
 {
   k4_time++;
  if(k4_time==1)
    key_sum=4;
  else if(k4_time==20)
  {
    key_sum=0;
   k4_time=1;
  }
 }
 else
  k4_time=0; 
  
  if(k5==0)
 {
   k5_time++;
  if(k5_time==1)
    key_sum=5;
  else if(k5_time==20)
  {
    key_sum=0;
   k5_time=1;
  }
 }
 else
  k5_time=0;
  
  if(k6==0)
 {
   k6_time++;
  if(k6_time==1)
    key_sum=6;
   else if(k6_time==20)
  {
    key_sum=0;
   k6_time=1;
  }
 }
 else
  k6_time=0;
  
  if(k7==0)
 {
   k7_time++;
  if(k7_time==1)
    key_sum=7;
  else if(k7_time==20)
  {
    key_sum=0;
   k7_time=1;
  }
 }
 else
  k7_time=0;
}
void key_control(void)
{
  if(key_sum==4)
   k4_press=1;
 else if(key_sum==5)
   k5_press=1;
 else if(key_sum==6)
   k6_press=1;
 else if(key_sum==7)
   k7_press=1;
}
 

key.h

#ifndef __KEY_H_
#define __KEY_H_

#include <STC15F2K60S2.H>

sbit k7=P3^0;
sbit k6=P3^1;
sbit k5=P3^2;
sbit k4=P3^3;

extern unsigned char k4_press;
extern unsigned char k5_press;
extern unsigned char k6_press;
extern unsigned char k7_press;

void key_read(void);
void key_control(void);

#endif

ds18b20.c

#include <ds18b20.h>
#include <intrins.h>

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
 t*=12;
 while(t--);
}

void Delay1ms()  //@12.000MHz
{
 unsigned char i, j;
 i = 12;
 j = 169;
 do
 {
  while (--j);
 } while (--i);
}

//通过单总线向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;
}

unsigned int temp_read(void)
{
 unsigned char TH,TL;
 unsigned int temp;
 
 init_ds18b20();
 Delay1ms();
 Write_DS18B20(0XCC);
 Write_DS18B20(0X44);
 
 init_ds18b20();
 Delay1ms();
 Write_DS18B20(0XCC);
 Write_DS18B20(0XBE);
 
 TL=Read_DS18B20();
 TH=Read_DS18B20();
 
 temp=TH;
 temp<<=8;
 temp|=TL;
 temp*=0.0625;
 return temp;
}

ds18b20.h

#ifndef _DS18B20_H_
#define _DS18B20_H_

#include <STC15F2K60S2.H>

unsigned int temp_read(void);

#endif

ds1302.c

#include <ds1302.h>
#include <intrins.h>

sbit SCK=P1^7;  
sbit SDA=P2^3;  
sbit RST = P1^3;   // DS1302复位   
         
unsigned char code addr_write[3]={0X80,0X82,0X84};
unsigned char code addr_read[3]={0X81,0X83,0X85};
unsigned char time[3]={0X50,0X59,0X023};

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 ds1302_init(void)
{
 unsigned char i=0;
  EA=0;
 Write_Ds1302( 0X8E,0X00 );
 for(i=0;i<3;i++)
 {
   Write_Ds1302( addr_write[i], time[i]);
 }
 Write_Ds1302( 0X8E,0X80 );
  EA=1;  
}
void ds1302_read_time(void)
{
 unsigned char i=0;
  EA=0;
 for(i=0;i<3;i++)
 {
   time[i]=Read_Ds1302 ( addr_read[i] );
 }
  EA=1;  
}

ds1302.h

#ifndef __DS1302_H_
#define __DS1302_H_

#include <STC15F2K60S2.H>

void ds1302_init(void);
void ds1302_read_time(void);

extern unsigned char time[3];

#endif

main.c

#include <STC15F2K60S2.H>
#include <timer.h>
#include <smg.h>
#include <key.h>
#include <ds18b20.h>
#include <ds1302.h>

unsigned char flag_smg=0;
unsigned char flag_key;
unsigned char k4_count=1,k6_count=0;
unsigned char record_time=1;
unsigned char flag_smg_count;
unsigned char temp_number=0;
unsigned char flag_time=0;
unsigned char flag_record=0;
unsigned char n=0;
unsigned int temp1=0;
unsigned int temp[10];

void bee_init(void)
{
  P2=(P2&0X1F)|0XA0;
 P0=0X00;
 P2&=0X1F;
}
void led1_on(void)
{
  P2=(P2&0X1F)|0X80;
 P0=0XFE;
 P2&=0X1F;
}
void led_off(void)
{
  P2=(P2&0X1F)|0X80;
 P0=0XFF;
 P2&=0X1F;
}
void main(void)
{
 bee_init();
 led_off();
  Timer0Init();
 ds1302_write();
  while(1)
 {
  /**********key**********/
  if(flag_key)
  {
    flag_key=0;
   key_read();
   key_control();
  }
  /*****k4*****/
  if(k4_press)
  {
    k4_press=0;
   
   if(flag_smg==0)
   {
     if(++k4_count==5) k4_count=1;
      switch(k4_count)
     {
       case 1: record_time=1;break;
      case 2: record_time=5;break;
      case 3: record_time=30;break;
      case 4: record_time=60;break;
     }
   }
  }
  /*****k5*****/
  else if(k5_press)
  {
    k5_press=0;
   if(flag_smg==0)
   {
     flag_smg=1;
    flag_record=1;
    n=0;
   }
  }
  /*****k6*****/
  else if(k6_press)
  {
    k6_press=0;
   if(flag_smg==1)
   {
    flag_record=0;
     flag_smg=2;
     led_off();
     k6_count=0;
   }  
   if(flag_smg==2)
   {
     if(++k6_count==11) k6_count=1;
      switch(k6_count)
     {
       case 1: temp_number=0;break;
       case 2: temp_number=1;break;
       case 3: temp_number=2;break;
       case 4: temp_number=3;break;
       case 5: temp_number=4;break;
       case 6: temp_number=5;break;
       case 7: temp_number=6;break;
       case 8: temp_number=7;break;
       case 9: temp_number=8;break;
       case 10: temp_number=9;break;
     }
   }
  }
  /*****k7*****/
  else if(k7_press)
  {
    k7_press=0;
   
   if(flag_smg==2)
   {
     flag_smg=0;
   }
  }
  /**********ds1302**********/
   ds1302_read();
  /**********ds18b20**********/
  temp1=ds18b20_temp_read();
   if(flag_record==1)
  {
    if(flag_time>=record_time)
   {
     flag_time=0;
    temp[n]=temp1;
    n++;
   }
   else if(n>=9)
   {
     led1_on();
    flag_time=0;
   }
  }
   /**********smg**********/
  if(flag_smg==0)
  {
   dspbuf[0]=11;
   dspbuf[1]=11;
   dspbuf[2]=11;
   dspbuf[3]=11;
   dspbuf[4]=11;
   dspbuf[5]=10;
   dspbuf[6]=record_time/10;
   dspbuf[7]=record_time%10;
  }
  else if(flag_smg==1)
  {
   if(flag_smg_count%2)
   {
     dspbuf[0]=time[2]/16;
     dspbuf[1]=time[2]%16;
     dspbuf[2]=10;
     dspbuf[3]=time[1]/16;
     dspbuf[4]=time[1]%16;
     dspbuf[5]=10;
     dspbuf[6]=time[0]/16;
     dspbuf[7]=time[0]%16;
   }
   else 
   {
     dspbuf[0]=time[2]/16;
     dspbuf[1]=time[2]%16;
     dspbuf[2]=11;
     dspbuf[3]=time[1]/16;
     dspbuf[4]=time[1]%16;
     dspbuf[5]=11;
     dspbuf[6]=time[0]/16;
     dspbuf[7]=time[0]%16;
   }
  }
void timer0_isr(void)  interrupt 1
{
 static unsigned char flag_key_time=0;
 static unsigned int flag_1s_time=0;
 if(++flag_key_time==30)
 {
   flag_key_time=0;
   flag_key=1;
 }
 if(++flag_1s_time==1000)
 {
   flag_1s_time=0;
  flag_smg_count++;
  flag_time++;
 }
  smg_display();
} 
相关标签: 单片机