1、概述
GD310开发板使用了GD32F310K8T6的芯片,芯片上的资源很多,而且板上还带了GD-LINK,很方便下载程序。KEIL环境配制很容易上手
板子上没有接口,后面自己焊上去的。
这次评测本打算出一期CNC控制,做个写字机器人、激光打印机。但是配件没有搞全,开发周期有点长。
这个开发板的接口兼容Arduino接口,可以完美把Arduino的扩展板插上去用,还是很人性化的。
接线还是很简单的。
2、开发环境
(1)WIN10系统
(2)Keil V5.29 (版本不能太低,不然装不上库文件)
(3)固件库-GD32F3x0_Firmware_Library_V2.2.0
3、程序
本程序是在点灯的基础上改的,添加了oled.c,oled.h,DHT11.c,DHT11.h的库程序。
oled.c和oled.h程序参考了本次评测的程序,只修改了延时程序。
DHT11.H程序
#ifndef DHT_H_
#define DHT_H_
#include "gd32f3x0.h"
#define DHT_TIMEOUT 10000
typedef struct {
float hum;
float temp;
} DHT_data;
typedef enum {
DHT11,
DHT22
} DHT_type;
typedef struct {
uint32_t DHT_Port;
uint16_t DHT_Pin;
DHT_type type;
uint8_t pullUp;
} DHT_sensor;
DHT_data DHT_getData(DHT_sensor *sensor);
#endif
DHT11.C程序
#include "DHT11.h"
#include "systick.h"
#define lineDown() gpio_bit_reset(sensor->DHT_Port, sensor->DHT_Pin)
#define lineUp() gpio_bit_set(sensor->DHT_Port, sensor->DHT_Pin)
#define getLine() (gpio_input_bit_get(sensor->DHT_Port, sensor->DHT_Pin) == SET)
#define Delay(d) delay_ms(d)
static void goToOutput(DHT_sensor *sensor)
{
lineUp();
if(sensor->pullUp == 1)
{
gpio_mode_set(sensor->DHT_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, sensor->DHT_Pin);
}
else
{
gpio_mode_set(sensor->DHT_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, sensor->DHT_Pin);
}
gpio_output_options_set(sensor->DHT_Port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, sensor->DHT_Pin);
}
static void goToInput(DHT_sensor *sensor)
{
if(sensor->pullUp == 1)
{
gpio_mode_set(sensor->DHT_Port, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, sensor->DHT_Pin);
}
else
{
gpio_mode_set(sensor->DHT_Port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, sensor->DHT_Pin);
}
}
DHT_data DHT_getData(DHT_sensor *sensor)
{
DHT_data data = {0.0f, 0.0f};
goToOutput(sensor);
lineDown();
delay_1ms(15);
lineUp();
goToInput(sensor);
uint16_t timeout = 0;
while(getLine()) {
timeout++;
if (timeout > DHT_TIMEOUT) return data;
}
timeout = 0;
while(!getLine()) {
timeout++;
if (timeout > DHT_TIMEOUT) return data;
}
timeout = 0;
while(getLine()) {
timeout++;
if (timeout > DHT_TIMEOUT) return data;
}
uint8_t rawData[5] = {0,0,0,0,0};
for(uint8_t a = 0; a < 5; a++) {
for(uint8_t b = 7; b != 255; b--) {
uint32_t hT = 0, lT = 0;
while(!getLine()) lT++;
timeout = 0;
while(getLine()) hT++;
if(hT > lT) rawData[a] |= (1<<b);
}
}
if((uint8_t)(rawData[0] + rawData[1] + rawData[2] + rawData[3]) == rawData[4]) {
if (sensor->type == DHT22) {
data.hum = (float)(((uint16_t)rawData[0]<<8) | rawData[1])*0.1f;
if(!(rawData[2] & (1<<7))) {
data.temp = (float)(((uint16_t)rawData[2]<<8) | rawData[3])*0.1f;
} else {
rawData[2] &= ~(1<<7);
data.temp = (float)(((uint16_t)rawData[2]<<8) | rawData[3])*-0.1f;
}
}
if (sensor->type == DHT11) {
data.hum = (float)rawData[0];
data.temp = (float)rawData[2];;
}
}
return data;
}
原作者:A.0王海智