完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include"onewire.h" ***it L8 = P0^7; //工作LED ***it L1 = P0^0; ***it L2 = P0^1; ***it L3 = P0^2; //定义LED ***it S7 = P3^0; ***it S6 = P3^1; ***it S5 = P3^2; ***it S4 = P3^3; //定义独立按键 unsigned char state = 0; //风扇模式标志位 unsigned char stat = 0; //数码管模式显示位 unsigned char count = 0; //定时50ms计数 unsigned char countdown; //倒计时 unsigned char flag = 0; //定时标志位 unsigned char PWM = 0; //PWM unsigned char num = 0; //脉冲计数 unsigned int T_dat; //温度 unsigned char status = 0; //显示温度标志位 unsigned char code SEG_code[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e, 0xbf,0x7f}; //定义共阳数码管段码内容:0~F,—,. /*==============================系统初始化==============================*/ //端口选择 void Select_HC138(unsigned char n) { switch(n) { case 0: P2 = (P2 & 0x1f) | 0x00; break; 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 Init_system() { Select_HC138(5); P0 = 0x00; //关闭蜂鸣器和继电器 Select_HC138(4); P0 = 0xff; //关闭LED Select_HC138(0); } /*======================================================================*/ /*==============================数码管显示==============================*/ //数码管延时函数 void Delay_tube(unsigned char time) { while(--time); } //数码管显示 void Show_tube(unsigned char position,unsigned char value) { Select_HC138(7); P0 = 0xff; //选择端口前,先熄灭数码管 Select_HC138(6); P0 = 0x01 << position; Select_HC138(7); P0 = value; } void Show_all() { Select_HC138(7); P0 = 0xff; //选择端口前,先熄灭数码管 Select_HC138(6); P0 = 0xff; //选通所有数码管 } //动态显示(模式) void Display_mode() { Show_tube(0,SEG_code[16]); //显示"-" Delay_tube(100); Show_tube(1,SEG_code[stat]); //显示风扇模式 Delay_tube(100); Show_tube(2,SEG_code[16]); //显示"-" Delay_tube(100); //第4位熄灭 Show_tube(4,SEG_code[0]); //显示0 Delay_tube(100); Show_tube(5,SEG_code[countdown/100]); //显示倒计时的百位 Delay_tube(100); Show_tube(6,SEG_code[(countdown/10)%10]); //显示倒计时的十位 Delay_tube(100); Show_tube(7,SEG_code[countdown%10]); //显示倒计时的个位 Delay_tube(100); Show_all(); } //动态显示(温度) void Display_temp() { Show_tube(0,SEG_code[16]); //显示"-" Delay_tube(100); Show_tube(1,SEG_code[4]); //显示风扇模式 Delay_tube(100); Show_tube(2,SEG_code[16]); //显示"-" Delay_tube(100); //第4、5位熄灭 Show_tube(5,SEG_code[T_dat/10]); //显示温度的十位 Delay_tube(100); Show_tube(6,SEG_code[T_dat%10]); //显示温度的个位 Delay_tube(100); Show_tube(7,SEG_code[12]); //显示温度单位C Delay_tube(100); Show_all(); } /*======================================================================*/ /*===============================温度读取===============================*/ //读取温度延时函数 void Delay_temp(unsigned int t) { while(t--) { Display_temp(); } } //读取温度 void Read_temp() { unsigned char LSB,MSB; //存放温度 Init_DS18B20(); //复位 Write_DS18B20(0xcc); //跳过ROM操作指令 Write_DS18B20(0x44); //温度转换 Delay_temp(1000); //等待温度转换,延时700ms Init_DS18B20(); //复位 Write_DS18B20(0xcc); //跳过ROM操作指令 Write_DS18B20(0xbe); //读取RAM LSB = Read_DS18B20(); //读取温度数据的低8位(低位先读) MSB = Read_DS18B20(); //读取温度数据的高8位 Init_DS18B20(); //复位,停止读取数据 T_dat = 0x0000; T_dat = MSB; T_dat <<= 8; T_dat |= LSB; //整合温度 if((T_dat & 0xf800) == 0x0000) //根据T_dat高5位判断温度正负,若温度为正 { T_dat >>= 4; //右移4位,取温度的整数部分 } } /*======================================================================*/ /*===============================PWM输出================================*/ //定时器0中断初始化 void Init_timer0() { TMOD = 0x11; //同时打开定时器0和定时器1 TH0 = (65535 - 100) / 256; TL0 = (65535 - 100) % 256; EA = 1; //打开总中断 ET0 = 1; //打开定时器0的中断 TR0 = 0; //关闭定时器0 } //定时器0中断服务函数 void Service_timer0() interrupt 1 { TH0 = (65535 - 100) / 256; TL0 = (65535 - 100) % 256; num++; if(num == (100-PWM)) //占空比后点亮L8 { Select_HC138(4); L8 = 0; Select_HC138(0); } if(num == 100) //一个脉冲周期结束 { Select_HC138(4); L8 = 1; Select_HC138(0); num = 0; } } /*======================================================================*/ /*===============================分钟定时===============================*/ //定时器1中断初始化 void Init_timer1() { TMOD = 0x11; //同时打开定时器0和定时器1 TH1 = (65535 - 50000) / 256; TL1 = (65535 - 50000) % 256; EA = 1; //打开总中断 ET1 = 1; //打开定时器1的中断 TR1 = 0; //关闭定时器1 } //定时器1中断服务函数 void Service_timer1() interrupt 3 { TH1 = (65535 - 50000) / 256; TL1 = (65535 - 50000) % 256; count++; if(count == 20) //定时满1s { countdown--; count = 0; } if(countdown == 0) { TR0 = 0; //PWM停止输出 TR1 = 0; //停止计时 Select_HC138(4); P0 = 0xff; //关闭所有LED Select_HC138(0); } } /*======================================================================*/ /*===============================按键处理===============================*/ //去抖动延时函数 void Delay_keys() //10ms延时 { unsigned char i = 108,j = 145; do { while(--j); }while(--i); } //按键处理 void Press_keys() { if(S4 == 0) //模式选择 { Delay_keys(); //去抖动 if(S4 == 0) { switch(state) { case 0: //模式1:睡眠风 stat = 1; //数码管模式显示位 PWM = 20; //PWM为20% state = 1; Select_HC138(4); L1 = 0; //点亮L1 L2 = 1; L3 = 1; Select_HC138(0); break; case 1: //模式2:自然风 stat = 2; //数码管模式显示位 PWM = 50; //PWM为50% state = 2; Select_HC138(4); L2 = 0; //点亮L2 L1 = 1; L3 = 1; Select_HC138(0); break; case 2: //模式3:常风 stat = 3; //数码管模式显示位 PWM = 80; //PWM为80% state = 0; Select_HC138(4); L3 = 0; //点亮L3 L1 = 1; L2 = 1; Select_HC138(0); break; } while(S4 == 0) { Display_mode(); } } } else if(S5 == 0) //设置定时时间(开关) { Delay_keys(); if(S5 == 0) { switch(flag) { case 0: //定时1min countdown = 60; TR0 = 1; //打开PWM输出 TR1 = 1; //打开定时器1开始定时 flag = 1; break; case 1: //定时2min countdown = 120; TR0 = 1; //打开PWM输出 TR1 = 1; //打开定时器1开始定时 flag = 2; break; case 2: //停止定时 countdown = 0; //倒计时清0 Select_HC138(4); P0 = 0xff; //关闭所有LED Select_HC138(0); TR0 = 0; //PWM停止输出 TR1 = 0; //关闭定时器1停止计时 flag = 0; break; } while(S5 == 0) { Display_mode(); } } } else if(S6 == 0) //停止 { Delay_keys(); if(S6 == 0) { countdown = 0; //倒计时清0 TR0 = 0; //PWM停止输出 TR1 = 0; //停止计时 flag = 0; Select_HC138(4); //关闭所有LED P0 = 0xff; // L8 = 1; // L1 = 1; // L2 = 1; // L3 = 1; Select_HC138(0); while(S6 == 0) { Display_mode(); } } } else if(S7 == 0) //查看温度 { Delay_keys(); if(S7 == 0) { if(status == 0) { status = 1; //显示温度 } else if(status == 1) { status = 0; //显示风扇模式 } } while(S7 == 0); } } /*======================================================================*/ /*================================主函数================================*/ void main() { Init_system(); Init_timer0(); Init_timer1(); while(1) { Press_keys(); switch(status) { case 0: Display_mode(); break; case 1: Read_temp(); Display_temp(); break; } } } /*======================================================================*/ |
|
|
|
只有小组成员才能发言,加入小组>>
3310 浏览 9 评论
2991 浏览 16 评论
3492 浏览 1 评论
9057 浏览 16 评论
4086 浏览 18 评论
1175浏览 3评论
603浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
596浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2333浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1894浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 15:30 , Processed in 1.265775 second(s), Total 81, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号