GD春季芯耀+仓库温度报警系统

[复制链接]
2148|9
 楼主| yinwuqing110 发表于 2019-3-29 00:45 | 显示全部楼层 |阅读模式
     最近做的小实验主要运用GD32F350作为主控MCU,通过一线式通讯接口,外接一个DS18B20进行温度采集。由于是单线式传输,因此对时序要求比较严苛。本次小实验要实现的功能比较简单,大概的执行流程就是通过DS18B20实时采集室内温度,并通过内部寄存器将模拟信号转换成数字信号,编写一线式通讯协议,使用GPIOB_12将完成数据的收发。当采集的温度值在10℃~33℃时为正常范围,显示内部LED灯为0.3秒亮灭间隔,外部7盏灯会依次以0.05秒的时间间隔亮灭。如果大于33℃或者小于10℃时,则内部LED灯以0.05秒的时间间隔依次两灭,同时外部LED的第8盏灯也会以0.3秒亮灭频率闪烁,并且外部蜂鸣器器会持续响起,用到的是GPIOC_6管脚。当然这温度的范围根据用户要求可以适时设定。
      主要配置一些GPIO的代码如下:
  1. #include <stdio.h>
  2. #include "bsp_ds18b20.h"
  3. #include "gd32f3x0.h"
  4. #include "colibri_bsp.h"

  5. #define DS18B20_SKIP_ROM       0xcc   //跳过序列号
  6. #define DS18B20_COVERTT        0x44   //数值转换
  7. #define DS18B20_READ_MEM       0xbe   //读取数据

  8. uint32_t Tick;
  9. uint16_t buffer[3]={249,499,749};
  10. GPIO_InitPara GPIO_InitStructure;

  11. static volatile uint32_t uwTick;
  12. void systick_config(void)
  13. {
  14.         /* setup systick timer for 1Mhz interrupts */
  15.         if (SysTick_Config(SystemCoreClock / 1000000U))        while (1);        //1Mhz
  16.       /* configure the systick handler priority */
  17.       NVIC_SetPriority(SysTick_IRQn, 0x00U);
  18. }
  19. void GD_decTick(void)
  20. {
  21.         if(0U != uwTick){
  22.         uwTick--;
  23.         }
  24. }
  25. void delayus(uint32_t nTime)
  26. {
  27.         uwTick = nTime;
  28.         while(0U != uwTick){
  29.     }
  30. }
  31. void delayms(uint32_t nTime)
  32. {
  33.         uwTick = nTime*1000;
  34.         while(0U != uwTick){
  35.     }
  36. }
  37. void SysTick_Callback(void)
  38. {
  39. }

  40. void SysTick_Handler(void)  //1Mhz
  41. {
  42.         static uint32_t time =0;  
  43.         time++;
  44.         GD_decTick();
  45.         if(time == LOOP_TIME){                //1M/LOOP_TIME=1M/10000=100hz
  46.                 SysTick_Callback();
  47.     time = 0;
  48.   }
  49. }

  50. /*!
  51.     \brief      configure the TIMER peripheral
  52.     \param[in]  none
  53.     \param[out] none
  54.     \retval     none
  55.   */
  56. void timer_config(void)
  57. {
  58.     /* TIMER0 DMA Transfer example -------------------------------------------------
  59.     TIMER0CLK = 84MHz(GD32F330) or 108MHz(GD32F350), Prescaler = 84(GD32F330) or 108(GD32F350)
  60.     TIMER0 counter clock 1MHz.

  61.     the objective is to configure TIMER0 channel 1 to generate PWM
  62.     signal with a frequency equal to 1KHz and a variable duty cycle(25%,50%,75%) that is
  63.     changed by the DMA after a specific number of update DMA request.

  64.     the number of this repetitive requests is defined by the TIMER0 repetition counter,
  65.     each 2 update requests, the TIMER0 Channel 0 duty cycle changes to the next new
  66.     value defined by the buffer .
  67.     -----------------------------------------------------------------------------*/
  68.     timer_oc_parameter_struct timer_ocintpara;
  69.     timer_parameter_struct timer_initpara;

  70.     rcu_periph_clock_enable(RCU_TIMER0);

  71.     timer_deinit(TIMER0);

  72.     /* TIMER0 configuration */
  73. #ifdef GD32F330
  74.     timer_initpara.prescaler         = 83;
  75. #endif /* GD32F330 */
  76. #ifdef GD32F350
  77.     timer_initpara.prescaler         = 107;
  78. #endif /* GD32F350 */
  79.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  80.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  81.     timer_initpara.period            = 999;
  82.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  83.     timer_initpara.repetitioncounter = 1;
  84.     timer_init(TIMER0,&timer_initpara);

  85.     /* CH0 configuration in PWM0 mode */
  86.     timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
  87.     timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  88.     timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
  89.     timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
  90.     timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_HIGH;
  91.     timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  92.     timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);

  93.     timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,buffer[0]);
  94.     timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  95.     timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

  96.     /* TIMER0 primary output enable */
  97.     timer_primary_output_config(TIMER0,ENABLE);

  98.     /* TIMER0 update DMA request enable */
  99.     timer_dma_enable(TIMER0,TIMER_DMA_UPD);

  100.     /* auto-reload preload enable */
  101.     timer_auto_reload_shadow_enable(TIMER0);

  102.     /* TIMER0 counter enable */
  103.     timer_enable(TIMER0);
  104. }

  105. void GPIO_Init(uint32_t gpio_periph,GPIO_InitPara *Para)
  106. {
  107.     rcu_periph_clock_enable(RCU_GPIOB);
  108.    
  109.     rcu_periph_clock_enable(RCU_GPIOB);
  110.     GPIO_BOP(gpio_periph) = Para->GPIO_Pin;
  111.     GPIO_OSPD0(gpio_periph) = Para->GPIO_Speed;
  112.     GPIO_OMODE(gpio_periph) = Para->GPIO_Mode;
  113.     GPIO_BC(gpio_periph) = Para->GPIO_Pin;
  114. }

  115. void TempGPIOInit(void)
  116. {
  117.         rcu_periph_clock_enable(RCU_GPIOB);
  118.         GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
  119.         GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
  120.         GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
  121.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  122. }

  123. void TempGPIOin(void)
  124. {
  125.         GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
  126.         GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
  127.         GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
  128.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  129. }

  130. void TempGPIOout(void)
  131. {
  132.         GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
  133.         GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
  134.         GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
  135.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  136. }
  外部的蜂鸣器代码如下:
  1. void RunBeepExternal(unsigned int TimeCounter)
  2. {
  3.           gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_6);
  4.           delayus(TimeCounter);
  5.         EvbBeepExternalControl(BEEP_ON);
  6.         delayus(TimeCounter);        
  7.           EvbBeepExternalControl(BEEP_OFF);
  8.         delayus(TimeCounter);
  9.         EvbBeepExternalControl(BEEP_ON);
  10.         delayus(TimeCounter);        
  11.           EvbBeepExternalControl(BEEP_OFF);
  12.           delayus(TimeCounter);
  13.         EvbBeepExternalControl(BEEP_ON);
  14.         delayus(TimeCounter);        
  15.           EvbBeepExternalControl(BEEP_OFF);
  16.           delayus(TimeCounter);        
  17. }
  外部蜂鸣器亮起,报警提示。
   此次实验的时序很重要,对于DS18B20,我象征性的截了几张图如下:
   DS18B20时序图.png DS18B20硬件结构框图.png DS18B20硬件连接图示.png

     GD32F350的正面照及硬件连接如下:
      GD32F350.jpg
    最后祝福GD32兆易创新能够越来越红火,将来举办越来越多的优惠活动,祝愿兆易创新在2019年能出更多创新型产品。最近的GD32E231也问世了,希望开发资源丰富,让工程师越来越容易上手,灵活使用。
零三翟邢止胃 发表于 2019-3-29 08:32 | 显示全部楼层
感谢分享啊! 真是不错啊!
 楼主| yinwuqing110 发表于 2019-3-29 10:04 | 显示全部楼层
零三翟邢止胃 发表于 2019-3-29 08:32
感谢分享啊! 真是不错啊!

谢谢关顾!
HKingS 发表于 2019-3-29 17:07 | 显示全部楼层
支持下,谢谢分享!
HKingS 发表于 2019-3-29 17:07 | 显示全部楼层
这个小红板是论坛送的吗
 楼主| yinwuqing110 发表于 2019-3-29 19:21 | 显示全部楼层
HKingS 发表于 2019-3-29 17:07
这个小红板是论坛送的吗

小伙计,不是的哦,是我在官网申购的哟。
tomyoct 发表于 2019-3-31 11:30 | 显示全部楼层
yinwuqing110 发表于 2019-3-29 19:21
小伙计,不是的哦,是我在官网申购的哟。

免费的吗?我也申请个去。
 楼主| yinwuqing110 发表于 2019-4-1 08:39 | 显示全部楼层
tomyoct 发表于 2019-3-31 11:30
免费的吗?我也申请个去。

是啊,可以申请免费试用的,网友!
AdaA 发表于 2019-4-3 10:18 | 显示全部楼层
大哥,大侠,中奖没
 楼主| yinwuqing110 发表于 2019-4-3 21:12 | 显示全部楼层
AdaA 发表于 2019-4-3 10:18
大哥,大侠,中奖没

小奖而已,在技术交流群中高手还是挺多的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

107

主题

1102

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部