有幸获得兆易创新的开发板GD32F310的试用机会,这次就用一个GPIO来读取数字温度传感器DS18B20,以“一线总线”数字方式输出,os用的rtthread nano。
首先移植rtthread nano,我使用的nano版本不是最新的,是3.1.3,建立工程架构以及添加头文件路径
修改一些配置,nvic_vector_table_set的定义和finsh所需要的串口的配置
!
1650719773(1).png
void gd_eval_com_init(uint32_t com)
{
uint32_t COM_ID = 0U;
if(EVAL_COM == com){
COM_ID = 0U;
}else{
}
rcu_periph_clock_enable(EVAL_COM_GPIO_CLK);
rcu_periph_clock_enable(COM_CLK[COM_ID]);
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);
usart_deinit(com);
usart_baudrate_set(com, 115200U);
usart_receive_config(com, USART_RECEIVE_ENABLE);
usart_transmit_config(com, USART_TRANSMIT_ENABLE);
usart_enable(com);
}
void rt_hw_console_output(const char *str)
{
rt_enter_critical();
while(*str!='\0')
{
if (*str == '\n')
{
usart_data_transmit(USART0, '\r');
while(usart_flag_get(USART0, USART_FLAG_TC)== RESET);
}
usart_data_transmit(USART0, *(str++));
while(usart_flag_get(USART0, USART_FLAG_TC)== RESET);
}
rt_exit_critical();
}
char rt_hw_console_getchar(void)
{
int ch = -1;
if(usart_flag_get(USART0, USART_FLAG_RBNE) != RESET)
{
ch = (int)usart_data_receive(USART0);
usart_flag_clear(USART0, USART_FLAG_RBNE);
}
else
{
if(usart_flag_get(USART0, USART_FLAG_ORERR) != RESET)
{
usart_flag_clear(USART0, USART_FLAG_ORERR);
}
rt_thread_mdelay(10);
}
return ch;
}
main函数创建一个led线程
#include <rthw.h>
#include <rtthread.h>
#include "gd32f3x0_rcu.h"
#include "gd32f3x0_gpio.h"
#include "Uart.h"
static rt_thread_t led_thread;
void led_init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
}
void led_thread_entry(void *parameter)
{
led_init();
while(1)
{
gpio_bit_write(GPIOA, GPIO_PIN_8, SET);
rt_thread_mdelay(2000);
gpio_bit_write(GPIOA, GPIO_PIN_8, RESET);
rt_thread_mdelay(2000);
}
}
void TaskInit(void)
{
led_thread = rt_thread_create("ledThread",
led_thread_entry,
RT_NULL,
256,
2,
10
);
if(led_thread != RT_NULL)
{
rt_thread_startup(led_thread);
}
}
int main()
{
TaskInit();
}
用GDlink下载代码,用finsh可查看led线程创建成功。
同样地,再创建一个ds18b20的读取任务,ds18b20的温度读取流程:复位->发 SKIP ROM 命令(0XCC)->发开始转换命令(0X44)->延时->复位->发送 SKIP ROM 命令(0XCC)->发读存储器命令(0XBE)->连续读出两个字节数据(即
温度)->结束。
贴出部分关键代码
static void DS18B20_Rst(void)
{
DS18B20_Mode_Out_PP();
macDS18B20_DQ_0;
rt_hw_us_delay(750);
macDS18B20_DQ_1;
rt_hw_us_delay(15);
}
static uint8_t DS18B20_Presence(void)
{
uint8_t pulse_time = 0;
DS18B20_Mode_IPU();
while( macDS18B20_DQ_IN() && pulse_time<100 )
{
pulse_time++;
rt_hw_us_delay(1);
}
if( pulse_time >=100 )
return 1;
else
pulse_time = 0;
while( !macDS18B20_DQ_IN() && pulse_time<240 )
{
pulse_time++;
rt_hw_us_delay(1);
}
if( pulse_time >=240 )
return 1;
else
return 0;
}
static uint8_t DS18B20_ReadBit(void)
{
uint8_t dat;
DS18B20_Mode_Out_PP();
macDS18B20_DQ_0;
rt_hw_us_delay(10);
DS18B20_Mode_IPU();
if( macDS18B20_DQ_IN() == SET )
dat = 1;
else
dat = 0;
rt_hw_us_delay(45);
return dat;
}
static uint8_t DS18B20_ReadByte(void)
{
uint8_t i, j, dat = 0;
for(i=0; i<8; i++)
{
j = DS18B20_ReadBit();
dat = (dat) | (j<<i);
}
return dat;
}
static void DS18B20_WriteByte(uint8_t dat)
{
uint8_t i, testb;
DS18B20_Mode_Out_PP();
for( i=0; i<8; i++ )
{
testb = dat&0x01;
dat = dat>>1;
if (testb)
{
macDS18B20_DQ_0;
rt_hw_us_delay(8);
macDS18B20_DQ_1;
rt_hw_us_delay(58);
}
else
{
macDS18B20_DQ_0;
rt_hw_us_delay(70);
macDS18B20_DQ_1;
rt_hw_us_delay(2);
}
}
}
static void DS18B20_SkipRom ( void )
{
DS18B20_Rst();
DS18B20_Presence();
DS18B20_WriteByte(0XCC);
}
static void DS18B20_MatchRom ( void )
{
DS18B20_Rst();
DS18B20_Presence();
DS18B20_WriteByte(0X55);
}
float DS18B20_GetTemp_SkipRom ( void )
{
uint8_t tpmsb, tplsb;
short s_tem;
float f_tem;
DS18B20_SkipRom ();
DS18B20_WriteByte(0X44);
DS18B20_SkipRom ();
DS18B20_WriteByte(0XBE);
tplsb = DS18B20_ReadByte();
tpmsb = DS18B20_ReadByte();
s_tem = tpmsb<<8;
s_tem = s_tem | tplsb;
if( s_tem < 0 )
f_tem = (~s_tem+1) * 0.0625;
else
f_tem = s_tem * 0.0625;
return f_tem;
}
原作者:守勤