主程序代码:
#include "stm32f10x.h"
#include "lcd.h"
#include "seg.h"
#include "stdio.h"
#include "ds18b20.h"
#include "main.h"
#include "led.h"
u32 DelayTiming = 0;
//温度上下限
int TempLimit_UP = 39;
int TempLimit_DOWN = 26;
u8 Temp_LED_AlrmFlag = 0, Temp_BEEP_AlrmFlag = 0; //各标志位
int main(void)
{
u8 str[20];
float x = 0.0;
u8 y = 0;
u16 z = 0;
SysTick_Config(SystemCoreClock/1000); //打开SysTick 毫秒级中断
STM3210B_LCD_Init(); //液晶屏初始化
LCD_DisplayStringLine(Line1,(u8*)" DS18B20 DEMO ");
LCD_SetTextColor(Blue);
LCD_SetBackColor(White);
//数码管初始化
STM3210B_SEG_Init();
SEG_DisplayValue(16,16,16); //SEG OFF
//LED1、LED2、蜂鸣器初始化
LED1_AND_LED2_AND_BEEP_Configuration();
LOOP: //LOOP死循环
//温度读取与转换
z = (ds18b20_read()& 0x07FF);
x = z/16.;
y = z/16;
KeyScan(); //查询中断标志->SB
//显示温度上限与下限
snprintf((char*)str, sizeof(str), " Limit_H:%2d Limit_L:%2d", TempLimit_UP, TempLimit_DOWN);
LCD_DisplayStringLine(Line2,str); //液晶屏显示当前温度
snprintf((char*)str, sizeof(str), " Temperatrue:%2.2f", x);
LCD_DisplayStringLine(Line6,str); //液晶屏显示当前温度
//数码管显示
SEG_DisplayValue(y/10, y%10,12);
//上下限判断
if(x >= TempLimit_UP){ //高温蜂鸣器报警
Temp_BEEP_AlrmFlag = 1; //设置蜂鸣器报警标志
}else{
Temp_BEEP_AlrmFlag = 0;
GPIO_SetBits(GPIOC, BEEP);
}
if(x <= TempLimit_DOWN){ //低温LED2闪烁报警
Temp_LED_AlrmFlag = 1; //LED2闪烁标志
}else{
Temp_LED_AlrmFlag = 0;
GPIO_SetBits(GPIOC, LED2);
}
// Delay_Ms(100);
goto LOOP;
}
extern u32 DelayTiming;
void SysTick_Handler(void)//毫秒级中断
{
static unsigned int Time_ms = 0;
Time_ms += 1;
if(Time_ms >= 500){ //2Hz 频率
Time_ms = 0;
//LED1系统运行指示
GPIO_WriteBit(GPIOC, LED1 , (BitAction) (1 - GPIO_ReadOutputDataBit(GPIOC, LED1)));
if(Temp_BEEP_AlrmFlag == 1){
GPIO_WriteBit(GPIOC, BEEP , (BitAction) (1 - GPIO_ReadOutputDataBit(GPIOC, BEEP)));
}
if(Temp_LED_AlrmFlag == 1){
GPIO_WriteBit(GPIOC, LED1 , (BitAction) (1 - GPIO_ReadOutputDataBit(GPIOC, LED1)));
}
}
if(DelayTiming != 0){
DelayTiming--;
}
}
//毫秒级中断延时函数
void Delay_Ms(u32 nTime)
{
DelayTiming = nTime;
while(DelayTiming != 0);
}
温度读取函数:
//
s16 ds18b20_read(void)
{
u8 val[2];
u8 i = 0;
s16 x = 0;
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_CONVERT);
delay_us(750000);
ow_reset();
ow_byte_wr( OW_SKIP_ROM );
ow_byte_wr ( DS18B20_READ );
for ( i=0 ;i<2; i++)
{
val[i] = ow_byte_rd();
}
x = val[1];
x <<= 8;
x |= val[0];
return x;
}
数码管显示函数:
uc8 Seg7[17] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c, 0x39,0x4f,0x79,0x78,0x00};
void STM3210B_SEG_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//
void SEG_DisplayValue(u8 Bit1, u8 Bit2, u8 Bit3)
{
u8 i = 0; //
u8 code_tmp = 0;
code_tmp = Seg7[Bit3];
for(i=0;i<8;i++){
if(code_tmp & 0x80){
SER_H;
}else{
SER_L;
}
SCK_H;
code_tmp = code_tmp << 1;
SCK_L;
}
code_tmp = Seg7[Bit2];
for(i=0;i<8;i++){
if(code_tmp & 0x80){
SER_H;
}else{
SER_L;
}
SCK_H;
code_tmp = code_tmp << 1;
SCK_L;
}
code_tmp = Seg7[Bit1];
for(i=0;i<8;i++){
if(code_tmp & 0x80){
SER_H;
}else{
SER_L;
}
SCK_H;
code_tmp = code_tmp << 1;
SCK_L;
}
RCLK_H;
RCLK_L;
}
|
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.csdn.net/qq_40009517/article/details/103438547