1题目
只是一次小练习
注意:PWM(100HZ)我用在Led灯上了,没有P34输出
2代码
main.c
******************************************************************************
* 文件名:模拟风扇控制系统
* 描 述:
* 作 者:思索与猫
* 日 期: 19/3/19
* 备 注:
*
******************************************************************************
#include<stc15f2k60s2.h>
#include<sys.h>
bit flag_temp;
void main()
{
Timer0Init();
CloseFucker();
while(1)
{
if(flag_temp == 1) //读取温度
{
flag_temp = 0;
temp = ReadTemp();
}
KeyDriver();
ShowTable();
}
}
sys.h
#ifndef __SYS_H_
#define __SYS_H_
#include<stc15f2k60s2.h>
#include<onewire.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//初始
void Timer0Init(); //1ms@12.000MHz
void CloseFucker();
//显示
void Display();
void ShowTable();
void ShowLed(uchar temp);
//按键
void KeyDriver();
void KeyAction(uchar key_value);
void KeyScan();
extern uchar mode, time, temp;
extern bit view, flag_temp;
extern uchar pwm, led_dat;
#endif
sys.c
#include<sys.h>
uchar pwm = 2;
uchar led_dat = 0x01;
bit flag_led = 0;
void Timer0Init() //1ms@12.000MHz
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x20;
TH0 = 0xD1;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void T0_time() interrupt 1
{
static uint count_temp = 0,count = 0,count_time = 0;
TL0 = 0x20;
TH0 = 0xD1;
Display();
KeyScan();
if(++count_time >= 10) //这里是100HZ的频率,也就是周期10ms
{
count_time = 0;
}
if(++count_temp >= 200) //200ms读取一次温度
{
count_temp = 0;
flag_temp = 1;
}
if(++count >= 1000) //减少1秒钟
{
count = 0;
time--;
if(time == 255)
{
time = 0;
flag_led = 0; //关灯
}
else
{
flag_led = 1; //开灯
}
}
if(flag_led == 1)
{
if(count_time < pwm) //pwm输出
{
ShowLed(led_dat);
}
else
{
ShowLed(0x00);
}
}
}
void CloseFucker()
{
P2 = P2&0x1f|0xa0;
P0 = 0xaf;
P2 = P2&0x1f;
}
display.c
#include<sys.h>
uchar code duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40,0x39};//10black,11-,12C
uchar Table[8];
uchar mode = 1,time,temp;
bit view = 0;
void Display()
{
static uchar index = 0;
P2 = P2&0x1f|0xe0;
P0 = 0xff;
P2 = P2&0x1f;
P2 = P2&0x1f|0xc0;
P0 = 1<<index;
P2 = P2&0x1f;
P2 = P2&0x1f|0xe0;
P0 = ~duan[Table[index]];
P2 = P2&0x1f;
index++;
index &= 0x07;
}
void ShowTable()
{
if(view == 0) //工作界面
{
Table[0] = 11;
Table[1] = mode;
Table[2] = 11;
Table[3] = 10;
Table[4] = 0;
Table[5] = time/100;
Table[6] = time/10%10;
Table[7] = time%10;
}
else if(view == 1) //温度界面
{
Table[0] = 11;
Table[1] = 4;
Table[2] = 11;
Table[3] = 10;
Table[4] = 0;
Table[5] = temp/10;
Table[6] = temp%10;
Table[7] = 12;
}
}
void ShowLed(uchar temp)
{
P2 = P2&0x1f|0x80;
P0 = ~temp;
P2 = P2&0x1f;
}
key.c
#include<sys.h>
sbit Key_In_1 = P3^0;
sbit Key_In_2 = P3^1;
sbit Key_In_3 = P3^2;
sbit Key_In_4 = P3^3;
uchar KeySta[4] = {1, 1, 1, 1};
uchar KeyCodeMap[4] = {7, 6, 5, 4};
void KeyDriver()
{
uchar i = 0;
static uchar KeyBack[4] = {1, 1, 1, 1};
for(i=0; i<4; i++)
{
if(KeySta[i] != KeyBack[i])
{
if(KeySta[i] == 1)
{
KeyAction(KeyCodeMap[i]);
}
}
KeyBack[i] = KeySta[i];
}
}
void KeyAction(uchar key_value)
{
if(view == 0&&key_value == 4) //按下S4切换模式
{
mode++;
switch(mode)
{
case 1:led_dat = 0x01;pwm = 2;break;
case 2:led_dat = 0x02;pwm = 3;break;
case 3:led_dat = 0x04;pwm = 7;break;
case 4:mode = 1;led_dat = 0x01;pwm = 2;break;
}
}
else if(view == 0&&key_value == 5) //加时间
{
time = time+60;
if(time > 120)
{
time = 120;
}
}
else if(view == 0&&key_value == 6) //清零时间
{
time = 0;
}
else if(key_value == 7) //切换界面
{
view = ~view;
}
}
void KeyScan()
{
uchar i = 0;
static uchar KeyBuf[4] = {0xff,0xff,0xff,0xff};
KeyBuf[0] = KeyBuf[0]<<1|Key_In_1;
KeyBuf[1] = KeyBuf[1]<<1|Key_In_2;
KeyBuf[2] = KeyBuf[2]<<1|Key_In_3;
KeyBuf[3] = KeyBuf[3]<<1|Key_In_4;
for(i=0; i<4; i++)
{
if(KeyBuf[i] == 0xff)
{
KeySta[i] = 1;
}
else if(KeyBuf[i] == 0x00)
{
KeySta[i] = 0;
}
else
{
;
}
}
}
onewire.h
#ifndef _ONEWIRE_H
#define _ONEWIRE_H
#include "stc15f2k60s2.h"
#define OW_SKIP_ROM 0xcc
#define DS18B20_CONVERT 0x44
#define DS18B20_READ 0xbe
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
bit Init_DS18B20(void);
unsigned char Read_DS18B20(void);
unsigned char ReadTemp();
#endif
onewire.c
#include "onewire.h"
void Delay_OneWire(unsigned int t)
{
t*=12;
while(t--);
}
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 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);
}
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;
}
unsigned char ReadTemp() //读取温度
{
unsigned char low,high;
unsigned char temp;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay_OneWire(200);
low = Read_DS18B20();
high = Read_DS18B20();
temp = high<<4;
temp |= (low&0xf0)>>4;
return temp;
}