说明,我目前手上没有蜂鸣器,蜂鸣器用led1点亮,熄灭代替报警功能
代码如下
#include "ti_msp_dl_config.h"
/* This results in approximately 0.5s of delay assuming 32MHz CPU_CLK */
#define DELAY (16000000)
uint8_t Disp_Dx[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x8c,0xbf,0xc6,0xa1,0x86,0xff};
uint8_t Disp_Px[8]={1,2,3,4,5,6,7,8};
uint16_t tempval =456;
uint8_t TimerCnt = 0 ;
uint16_t temp;
#define HC595_DAT(x) ((x)?(DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_DAT_PIN)):(DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_DAT_PIN)))
#define HC595_CLK(x) ((x)?(DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_CLK_PIN)):(DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_CLK_PIN)))
#define HC595_RCK(x) ((x)?(DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_RCK_PIN)):(DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_RCK_PIN)))
//DS18B20 DQ 相关定义
#define DS18B20_DQ_OUTH DL_GPIO_setPins(ds18b20_PORT, ds18b20_Gpio_pin_out_PIN)
#define DS18B20_DQ_OUTL DL_GPIO_clearPins(ds18b20_PORT, ds18b20_Gpio_pin_out_PIN)
#define DS18B20_DQ_READ DL_GPIO_readPins(ds18b20_PORT, ds18b20_Gpio_pin_out_PIN)
#define DS18B20_DQ_IN DL_GPIO_initDigitalInput(ds18b20_Gpio_pin_out_IOMUX);DL_GPIO_disableOutput(ds18b20_PORT, ds18b20_Gpio_pin_out_PIN)
#define DS18B20_DQ_OUT DL_GPIO_initDigitalOutput(ds18b20_Gpio_pin_out_IOMUX);DL_GPIO_enableOutput(ds18b20_PORT, ds18b20_Gpio_pin_out_PIN)
/* 数码管相关宏定义 */
#define DigitPoint 0x7F //小数点
/* 蜂鸣器相关宏定义 */
//#define BeepON DL_GPIO_clearPins(GPIO_BEEP_PORT, GPIO_BEEP_PIN_BEEP_PIN);
//#define BeepOFF DL_GPIO_setPins(GPIO_BEEP_PORT, GPIO_BEEP_PIN_BEEP_PIN);
#define delay_us_cycle 32
float realTemp = 0.0;
uint16_t testData = 0;
void display_out()
{
HC595_RCK(0);
delay_cycles(32);
HC595_RCK(1);
delay_cycles(32);
}
void hc595_writedata(uint8_t data)
{
uint8_t i;
for(i =0 ;i < 8; i++)
{
if(data&0x80)
{
HC595_DAT(1);
}
else
{
HC595_DAT(0);
}
data <<=1;
HC595_CLK(0);
delay_cycles(32);
HC595_CLK(1);
delay_cycles(32);
}
}
void hc_send_data(uint8_t dis_num,uint8_t dis_bit)
{
hc595_writedata(dis_num);
hc595_writedata(1<<dis_bit);
display_out();
}
void disp_data(uint16_t datal)
{
uint16_t templ;
uint8_t num_q,num_b,num_s,num_g;
templ = datal;
num_q = templ/1000;
num_b = templ/100%10;
num_s = templ/10%10;
num_g = templ%10;
hc_send_data(Disp_Dx[num_q],3);
hc_send_data(Disp_Dx[num_b],2);
hc_send_data(Disp_Dx[num_s],1);
hc_send_data(Disp_Dx[num_g],0);
}
void Disp_Temp(float tempValue)
{
uint16_t tempShow;
uint8_t num_q, num_b, num_s,num_g;
tempShow = tempValue;
num_q = tempShow/1000%10;
num_b = tempShow/100%10;
num_s = tempShow/10%10;
num_g = tempShow%10;
if (num_q) {
hc_send_data(Disp_Dx[num_q],7);
}
if (num_q || num_b) {
hc_send_data(Disp_Dx[num_b],6);
}
if (num_q || num_b || num_s) {
hc_send_data(Disp_Dx[num_s],5);
}
uint8_t disData = Disp_Dx[num_g] & 0x7F; //添加小数点
hc_send_data(disData,4);
tempShow = (tempValue - (float)tempShow) * 10000.0;
num_q = tempShow/1000%10;
num_b = tempShow/100%10;
num_s = tempShow/10%10;
num_g = tempShow%10;
if (tempValue) {
hc_send_data(Disp_Dx[num_q],3);
}
if (tempValue || num_b) {
hc_send_data(Disp_Dx[num_b],2);
}
if (tempValue || num_b || num_s) {
hc_send_data(Disp_Dx[num_s],1);
}
hc_send_data(Disp_Dx[num_g],0);
}
void inline delay_us(uint16_t us)
{
do{
delay_cycles(delay_us_cycle);
}while (us --);
}
/* 复位芯片 */
static void DS18B20_Rst(void)
{
DS18B20_DQ_OUT;
DS18B20_DQ_OUTL; /* 拉低DQ,复位 */
delay_us(750); /* 拉低750us */
DS18B20_DQ_OUTH; /* DQ=1, 释放复位 */
delay_us(15); /* 延迟15US */
}
/* 检查总线上是否存在温度芯片 */
uint8_t DS18B20_Check(void)
{
uint8_t retry = 0;
DS18B20_DQ_IN;
while (DS18B20_DQ_READ && retry < 200)
{
retry++;
delay_us(1);
};
if (retry >= 200)
return 1;
else
retry = 0;
while (!DS18B20_DQ_READ && retry < 240)
{
retry++;
delay_us(1);
};
if (retry >= 240)
return 1;
return 0;
}
/* 芯片初始化 */
uint8_t DS18B20_Init(void)
{
DS18B20_Rst();
return DS18B20_Check();
}
/* 读取单bit数据 */
uint8_t DS18B20_Read_Bit(void) // read one bit
{
DS18B20_DQ_OUT;
uint8_t data;
DS18B20_DQ_OUTL;
delay_cycles(2 * 32);
DS18B20_DQ_OUTH;
delay_cycles(12 * 32);
DS18B20_DQ_IN;
if (DS18B20_DQ_READ)
data = 1;
else
data = 0;
delay_cycles(50 * 32);
return data;
}
/* 读取一字节数据 */
uint8_t DS18B20_Read_Byte(void) // read one byte
{
uint8_t i, j, dat;
dat = 0;
for (i = 1; i <= 8; i++)
{
j = DS18B20_Read_Bit();
dat = (j << 7) | (dat >> 1);
}
return dat;
}
/* 写入一字节数据 */
void DS18B20_Write_Byte(uint8_t dat)
{
uint8_t j;
uint8_t testb;
DS18B20_DQ_OUT;
for (j = 1; j <= 8; j++)
{
testb = dat & 0x01;
dat = dat >> 1;
if (testb)
{
DS18B20_DQ_OUTL; // Write 1
// delay_cycles(2 * 32);
delay_us(2);
DS18B20_DQ_OUTH;
// delay_cycles(60 * 32);
delay_us(60);
}
else
{
DS18B20_DQ_OUTL; // Write 0
// delay_cycles(60 * 32);
delay_us(60);
DS18B20_DQ_OUTH;
// delay_cycles(2 * 32);
delay_us(12);
}
}
}
/* 启动芯片 */
void DS18B20_Start(void) // ds1820 start convert
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc); // skip rom
DS18B20_Write_Byte(0x44); // convert
}
float DS18B20_Get_Temp(void)
{
uint8_t temp;
uint8_t TL, TH;
int16_t tem;
float fValue = 0.0;
DS18B20_Start(); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc); // skip rom
DS18B20_Write_Byte(0xbe); // convert
TL = DS18B20_Read_Byte(); // LSB
TH = DS18B20_Read_Byte(); // MSB
if (TH > 7)
{
TH = ~TH;
TL = ~TL;
temp = 0; // 温度为负
}
else
temp = 1; // 温度为正
tem = TH; // 获得高八位
tem <<= 8;
tem += TL; // 获得底八位
fValue = (float)tem * 0.0625; // 转换
if (temp)
return fValue; // 返回温度值
else
return -fValue;
}
int main(void)
{
/* Power on GPIO, initialize pins as digital outputs */
int T=123;
SYSCFG_DL_init();
NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
delay_cycles(32000);
while (1) {
Disp_Temp(realTemp);
}
}
void TIMER_0_INST_IRQHandler(void)
{
switch(DL_TimerG_getPendingInterrupt(TIMER_0_INST))
{
case DL_TIMER_IIDX_ZERO:
TimerCnt++;
if(TimerCnt >=100)
{
TimerCnt=0;
tempval++;
if(tempval>500)tempval=0;
realTemp=DS18B20_Get_Temp();
if(realTemp>30)
DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_led_PIN);
else
DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_led_PIN);
}
break;
default:
break;
}
}
视频地址
https://www.bilibili.com/video/BV1pUhHeBEL3/?vd_source=4f8f0092a0967d5a44cc4790809b35be