This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] TM4C123GH6PM:dht11,tm4c123ghpm

Guru**** 2042920 points
Other Parts Discussed in Thread: TM4C123GH6PM
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1076095/tm4c123gh6pm-dht11-with-tm4c123gh6pm

部件号:TM4C123GH6PM

//这是带有 TM4c123GH6PM 的 DHT11传感器的代码  

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#包括"inc/tm4c123gh6a.h"
#include "inc/HW_memmap.h"(#include "inc/HW_memmap.h")
#include "inc/HW_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupe.h"
#include "driverlib/GPIO.h"
#include "driverlib/timer.h"
#include "driverlib/pin_map.h"
#include "driverlib/UART.h"(#include "driverlib/UART.h")
#include "driverlib/adc.h"
#include "driverlib/FPC.h"
#include "utils/uartstdi.h"(#include "utils/uartstdi.h")

挥发性 int temp[43];
挥发性 int diff [43];
volatile unsigned int i=0;
易失性无符号 int j=0;
挥发性无符号 int hh = 0;
挥发性无符号 int HL = 0;
挥发性无符号 int th =0;
volatile unsigned int tl = 0;
易失性无符号 int 校验和=0;
易失性无符号 int check =0;
易失性无符号 int dataok =0;
//函数原型
void init_timer(void);
void Duty_cycle (void);

//全局变量
UINT32_t sys_clock;
UINT32_t start = 0,end = 0,length = 0;

Int main (无效)

//将系统时钟配置为40 MHz。
Sysctl 时钟集(sysctl_SYSDIV_5|sysctl_use_PLL |sysctl_XT_16MHz|sysctl_OSC 主);
sys_clock = SysClockGet();

//使处理器能够响应中断。
IntMasterEnable();
SysPeripheralEnable (sysctl_Periph_GPIOB);//启用端口 B
GPIOPinTypeGPIOOutput (GPIO _PORTB_BASE,GPIO _PIN_6);//连接 PB6处的传感器
GPIOPinWrite (GPIO _PORTB_BASE,GPIO _PIN_6,0x00);//关闭 PB6 18毫秒
delayMs (18);//18ms 延迟
GPIOPinWrite (GPIO _PORTB_BASE,GPIO _PIN_6,0xff);//在 PB6上
delayUS(40);//延迟40US
init_timer();//计时器初始化并将 PB6作为输入计时器

TimerEnable(TIMER0_base, timer_both );
int k,l,mul = 1;

While (1)(同时)

如果(i >=42)

对于(j = 1;j <= 8;j++)//第一个8位数据为第一个数组索引
{//数组{0,0,1,0,0,1}
如果(diff[j]=1)//整数值为41

对于(l=0;l<8-j;l++)
Mul=mul*2;
hhh=hhh+mul;/hh=41 (如果高于数组)
}
MUL=1;//hh 是湿度整数
}
Mul=1;
对于(j = 9;j <= 16;j++)

IF (diff [j]=1)

对于(l=0;l<16-j;l++)
Mul=mul*2;
HL=HL+mul;//HL 为0.1倍数后的湿度
}
Mul=1;
}
Mul=1;
对于(j = 17;j <= 24;j++)

IF (diff [j]=1)

对于(l=0;l<24-j;l++)
Mul=mul*2;
th=th+mul;//th temp integer
}
Mul=1;
}
Mul=1;
对于(j = 25;j <= 32;j++)

IF (diff [j]=1)

对于(l=0;l<32-j;l++)
Mul=mul*2;
tl=tl+mul;//tl 后。
}
Mul=1;
}
Mul=1;
对于(j = 33;j <= 40;j++)

IF (diff [j]=1)

对于(l=0;l<40-j;l++)
Mul=mul*2;
checksum=checksum+mul;//最后8位(数组的最后8个索引)
}//将最后8个索引转换为一个整数
Mul=1;
}
Check = hhh+hl+th+tl;
如果(check == checksum)//check parity (检查=校验和)//last 8 index (最后8个索引)组合整数//增加32个索引整数

dataok = 1;
休息;
}
否则
dataok = 0;
}
}
浮动湿度=hhh+(0.1*HL);//湿度值(以%为单位)
浮点温度=TH+(0.1*tl);//温度值(以腹腔计)
}


void init_timer (void)(无效)

//启用和配置 Timer0外围设备。
SysPeripheralEnable (sysctl_Periph_TIMER0);

//初始化计时器 A 以在边缘时间模式下计数
TimerConfigure(TIMER0_base, Timer_CFG_Split_Pair| timer_CFG_A_CAP_TIME_UP ));

//计时器 A 记录位置边缘时间
TimerControlEvent(TIMER0_BASE,TIMER _A,TIMER EVENT_EVENT_POS_EDGE );

//将计时器计数的值设置为最大0xFFFF
TimerLoadSet (TIMER0_BASE,TIMER A,0xFFFF);

//配置计时器读取的引脚(PB6)
SysPeripheralEnable (sysctl_Periph_GPIOB);
GPIOPinConfigure (GPIO _PB6_T0CCP0);
GPIOPinTypeTimer (GPIO _PORTB_BASE,GPIO _PIN_6);

//寄存器在计时器 A 遇到 Pos EDGE 事件时要调用的中断函数
国际寄存器(INT_TIMER0A,占空比);//正边缘计时器
//确保中断被清除
TimerIntClear (TIMER0_BASE,TIMER CAP_EVENT);
//启用指定的计时器中断源。
TimerIntEnable(TIMER0_base, timer_CAP_event);
//在中断控制器中启用指定的中断。
IntEnable(INT_TIMER0A);
}

//当正值边缘被命中时,记录这些值并找到差异,找到的数据为0或1
空占空比(空值)

TimerIntClear (TIMER0_BASE,TIMER CAP_EVENT);
开始=时间值集(TIMER0_BASE,TIMER _A);
temp[i]=启动;
i += 1;
如果(i>=3)

diff [I-2]= temp[i-1]- temp[i-2];//找到+ve 边时的计时器间隔
如果(diff[I-2]<4000)//计时器间隔小于4000表示读取数据0
diff [I-2]= 0;
否则
diff [I-2]= 1;
}
}
无效 delayMs (int ui32M)

SystlDelay((ui32MS * SysClockGet()/3/1000));/ms 延迟
}

作废 delayU (UINT32_t ui32U)

SystlDelay(ui32U *(SystlClockGet()/3/1000000);//us 延迟
}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好,

     感谢您提供与 DHT11接口的解决方案。 我相信,这将有益于社区中正在寻找相同解决方案的其他人。