ARM技术论坛
直播中

卢松涛

12年用户 419经验值
擅长:嵌入式技术
私信 关注

【星空派GD32F303开发板试用体验】控温(2)测试ADC

本帖最后由 lustao 于 2021-10-31 14:52 编辑

接上稿,
在之前基础上加入ADC程序(温度传感器和内部参考电压 VREFINT )
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date           Author       Notes
  8. * 2006-03-23     Bernard      the first version
  9. * 2010-11-10     Bernard      add cleanup callback function in thread exit.
  10. * 2012-12-29     Bernard      fix compiling warning.
  11. * 2013-12-21     Grissiom     let rt_thread_idle_excute loop until there is no
  12. *                             dead thread.
  13. * 2016-08-09     ArdaFu       add method to get the handler of the idle thread.
  14. * 2018-02-07     Bernard      lock scheduler to protect tid->cleanup.
  15. * 2018-07-14     armink       add idle hook list
  16. * 2018-11-22     Jesven       add per cpu idle task
  17. *                             combine the code of primary and secondary cpu
  18. */

  19. #include
  20. #include

  21. #ifdef RT_USING_MODULE
  22. #include
  23. #endif

  24. #if defined (RT_USING_HOOK)
  25. #ifndef RT_USING_IDLE_HOOK
  26. #define RT_USING_IDLE_HOOK
  27. #endif
  28. #endif

  29. #ifndef IDLE_THREAD_STACK_SIZE
  30. #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
  31. #define IDLE_THREAD_STACK_SIZE  256
  32. #else
  33. #define IDLE_THREAD_STACK_SIZE  128
  34. #endif
  35. #endif

  36. #ifdef RT_USING_SMP
  37. #define _CPUS_NR                RT_CPUS_NR
  38. #else
  39. #define _CPUS_NR                1
  40. #endif

  41. extern rt_list_t rt_thread_defunct;

  42. static struct rt_thread idle[_CPUS_NR];
  43. ALIGN(RT_ALIGN_SIZE)
  44. static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];

  45. #ifdef RT_USING_IDLE_HOOK
  46. #ifndef RT_IDLE_HOOK_LIST_SIZE
  47. #define RT_IDLE_HOOK_LIST_SIZE  4
  48. #endif

  49. static void (*idle_hook_list[RT_IDLE_HOOK_LIST_SIZE])(void);

  50. /**
  51. * @ingroup Hook
  52. * This function sets a hook function to idle thread loop. When the system performs
  53. * idle loop, this hook function should be invoked.
  54. *
  55. * [url=home.php?mod=space&uid=3142012]@param[/url] hook the specified hook function
  56. *
  57. * [url=home.php?mod=space&uid=1141835]@Return[/url] RT_EOK: set OK
  58. *         -RT_EFULL: hook list is full
  59. *
  60. * [url=home.php?mod=space&uid=1902110]@NOTE[/url] the hook function must be simple and never be blocked or suspend.
  61. */
  62. rt_err_t rt_thread_idle_sethook(void (*hook)(void))
  63. {
  64.     rt_size_t i;
  65.     rt_base_t level;
  66.     rt_err_t ret = -RT_EFULL;

  67.     /* disable interrupt */
  68.     level = rt_hw_interrupt_disable();

  69.     for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  70.     {
  71.         if (idle_hook_list[i] == RT_NULL)
  72.         {
  73.             idle_hook_list[i] = hook;
  74.             ret = RT_EOK;
  75.             break;
  76.         }
  77.     }
  78.     /* enable interrupt */
  79.     rt_hw_interrupt_enable(level);

  80.     return ret;
  81. }

  82. /**
  83. * delete the idle hook on hook list
  84. *
  85. * @param hook the specified hook function
  86. *
  87. * @return RT_EOK: delete OK
  88. *         -RT_ENOSYS: hook was not found
  89. */
  90. rt_err_t rt_thread_idle_delhook(void (*hook)(void))
  91. {
  92.     rt_size_t i;
  93.     rt_base_t level;
  94.     rt_err_t ret = -RT_ENOSYS;

  95.     /* disable interrupt */
  96.     level = rt_hw_interrupt_disable();

  97.     for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  98.     {
  99.         if (idle_hook_list[i] == hook)
  100.         {
  101.             idle_hook_list[i] = RT_NULL;
  102.             ret = RT_EOK;
  103.             break;
  104.         }
  105.     }
  106.     /* enable interrupt */
  107.     rt_hw_interrupt_enable(level);

  108.     return ret;
  109. }

  110. #endif

  111. /* Return whether there is defunctional thread to be deleted. */
  112. rt_inline int _has_defunct_thread(void)
  113. {
  114.     /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
  115.      * So the compiler has a good reason that the rt_thread_defunct list does
  116.      * not change within rt_thread_idle_excute thus optimize the "while" loop
  117.      * into a "if".
  118.      *
  119.      * So add the volatile qualifier here. */
  120.     const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;

  121.     return l->next != l;
  122. }

  123. /**
  124. * @ingroup Thread
  125. *
  126. * This function will perform system background job when system idle.
  127. */
  128. void rt_thread_idle_excute(void)
  129. {
  130.     /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
  131.      * will do all the cleanups. */
  132.     while (_has_defunct_thread())
  133.     {
  134.         rt_base_t lock;
  135.         rt_thread_t thread;
  136. #ifdef RT_USING_MODULE
  137.         struct rt_dlmodule *module = RT_NULL;
  138. #endif
  139.         RT_DEBUG_NOT_IN_INTERRUPT;

  140.         /* disable interrupt */
  141.         lock = rt_hw_interrupt_disable();

  142.         /* re-check whether list is empty */
  143.         if (_has_defunct_thread())
  144.         {
  145.             /* get defunct thread */
  146.             thread = rt_list_entry(rt_thread_defunct.next,
  147.                                    struct rt_thread,
  148.                                    tlist);
  149. #ifdef RT_USING_MODULE
  150.             module = (struct rt_dlmodule*)thread->module_id;
  151.             if (module)
  152.             {
  153.                 dlmodule_destroy(module);
  154.             }
  155. #endif
  156.             /* remove defunct thread */
  157.             rt_list_remove(&(thread->tlist));

  158.             /* lock scheduler to prevent scheduling in cleanup function. */
  159.             rt_enter_critical();

  160.             /* invoke thread cleanup */
  161.             if (thread->cleanup != RT_NULL)
  162.                 thread->cleanup(thread);

  163. #ifdef RT_USING_SIGNALS
  164.             rt_thread_free_sig(thread);
  165. #endif

  166.             /* if it's a system object, not delete it */
  167.             if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  168.             {
  169.                 /* detach this object */
  170.                 rt_object_detach((rt_object_t)thread);
  171.                 /* unlock scheduler */
  172.                 rt_exit_critical();

  173.                 /* enable interrupt */
  174.                 rt_hw_interrupt_enable(lock);

  175.                 return;
  176.             }

  177.             /* unlock scheduler */
  178.             rt_exit_critical();
  179.         }
  180.         else
  181.         {
  182.             /* enable interrupt */
  183.             rt_hw_interrupt_enable(lock);

  184.             /* may the defunct thread list is removed by others, just return */
  185.             return;
  186.         }

  187.         /* enable interrupt */
  188.         rt_hw_interrupt_enable(lock);

  189. #ifdef RT_USING_HEAP
  190.         /* release thread's stack */
  191.         RT_KERNEL_FREE(thread->stack_addr);
  192.         /* delete thread object */
  193.         rt_object_delete((rt_object_t)thread);
  194. #endif
  195.     }
  196. }

  197. extern void rt_system_power_manager(void);
  198. static void rt_thread_idle_entry(void *parameter)
  199. {
  200. #ifdef RT_USING_SMP
  201.     if (rt_hw_cpu_id() != 0)
  202.     {
  203.         while (1)
  204.         {
  205.             rt_hw_secondary_cpu_idle_exec();
  206.         }
  207.     }
  208. #endif

  209.     while (1)
  210.     {
  211. #ifdef RT_USING_IDLE_HOOK
  212.         rt_size_t i;

  213.         for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  214.         {
  215.             if (idle_hook_list[i] != RT_NULL)
  216.             {
  217.                 idle_hook_list[i]();
  218.             }
  219.         }
  220. #endif

  221.         rt_thread_idle_excute();
  222. #ifdef RT_USING_PM        
  223.         rt_system_power_manager();
  224. #endif
  225.     }
  226. }

  227. /**
  228. * @ingroup SystemInit
  229. *
  230. * This function will initialize idle thread, then start it.
  231. *
  232. * @note this function must be invoked when system init.
  233. */
  234. void rt_thread_idle_init(void)
  235. {
  236.     rt_ubase_t i;
  237.     char tidle_name[RT_NAME_MAX];

  238.     for (i = 0; i < _CPUS_NR; i++)
  239.     {
  240.         rt_sprintf(tidle_name, "tidle%d", i);
  241.         rt_thread_init(&idle[i],
  242.                 tidle_name,
  243.                 rt_thread_idle_entry,
  244.                 RT_NULL,
  245.                 &rt_thread_stack[i][0],
  246.                 sizeof(rt_thread_stack[i]),
  247.                 RT_THREAD_PRIORITY_MAX - 1,
  248.                 32);
  249. #ifdef RT_USING_SMP
  250.         rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
  251. #endif
  252.         /* startup */
  253.         rt_thread_startup(&idle[i]);
  254.     }
  255. }

  256. /**
  257. * @ingroup Thread
  258. *
  259. * This function will get the handler of the idle thread.
  260. *
  261. */
  262. rt_thread_t rt_thread_idle_gethandler(void)
  263. {
  264. #ifdef RT_USING_SMP
  265.     register int id = rt_hw_cpu_id();
  266. #else
  267.     register int id = 0;
  268. #endif

  269.     return (rt_thread_t)(&idle[id]);
  270. }
Build started: Project: project
*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'g:Keil_v5ARMARMCCBin'
Build target 'rt-thread_gd32f30x'
compiling main.c...
applicationsmain.c(559): warning:  #177-D: variable "mind_a0_on1"  was declared but never referenced
    uint16_t  mind_a0_on, mind_a0_on1;
applicationsmain.c: 1 warning, 0 errors
linking...
Program Size: Code=106640 RO-data=11448 RW-data=1016 ZI-data=5672  
After Build - User command #1: fromelf --bin .buildrtthread-gd32f30x.axf --output rtthread.bin
".buildrtthread-gd32f30x.axf" - 0 Error(s), 1 Warning(s).
Build Time Elapsed:  00:00:02
Load "C:\rt-thread\bsp\gd32303e-eval\build\rtthread-gd32f30x.axf"
Dll Verison 1.06  Build date: 2019-06-23
Emulaor: MM32-LINK, SN: 0023557, Hardware: A, Firmware: 1.23
Manufacturer: MindMotion, Date: 2020-01-08
Appliaction: MM32-LINK, Version: 1.51
Connect Mode SWD, Speed Auto
Out put voltage 3.34V, current 0.00mA, target voltage 3.34V
Found SWD-DP with ID 0x410FC241
TPIU fitted.
ETM fitted.
Found Cortex-M4 , Little endian
FPUnit: 6 code (BP) slots and 2 literal slots
Data Watchpoint: 4
Erase Done.
Programming Done.
Verify OK.
Flash Load finished at 11:49:50




下后运行
com口

the temperature data is 26 degrees Celsius

the reference voltage data is 1.197V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.199V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.200V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.198V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.198V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.199V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.198V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.198V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.198V



Button2 Dowm_!a=11 the temperature data is 26 degrees Celsius

the reference voltage data is 1.200V



Button3 _Dowm!b=11 the temperature data is 26 degrees Celsius

the reference voltage data is 1.200V



Button4 _Dowm! the temperature data is 26 degrees Celsius

the reference voltage data is 1.197V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.196V



the temperature data is 26 degrees Celsius

the reference voltage data is 1.198V


键盘显示及内部两路测试正常。









加入(PC3)模拟输入程序
  1. /*
  2. * File      : main.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *想用星空派gd32F303开发板资源  (P  F 7)输出(19),(PC3)模拟输入(29),板(P  F0~3)LED,板(PA 0 、PC13 、 P  F 5 、 P  F 4)button,来通过光耦中间继电器控制加热设备。
  10. * Change Logs:
  11. * Date           Author       Notes
  12. * 2021-10-29                                  first implementation
  13. */
  14. #include
  15. #include
  16. #include "stdio.h"
  17. #include
  18. #include
  19. #include "gd32f303e_eval.h"
  20. //#include "systick.h"
  21. #include "button.h"


  22. //adc
  23. float temperature;
  24. float vref_value;

  25. void rcu_config(void);
  26. void adc_config(void);
  27. void adc_init(void);

  28. /*!
  29.     brief      main function
  30.     param[in]  none
  31.     param[out] none
  32.     retval     none
  33. */
  34. //adc

  35.         uint8_t pin_count = 0;
  36.         volatile long  bootCount = 0;  //DF-A进行 1分,1.5分,10分状态。
  37. volatile uint8_t mind_n_As1;      //
  38. unsigned long startProducing, endProducing;
  39. volatile float  mind_n_key1, mind_n_all, mind_n_oninall;
  40. // 函数声明
  41. void DF_B(void*);
  42. void DF_onoff(uint16_t  mind_n_on, uint16_t  mind_n_off, uint8_t  mind_n_key);
  43. void DF_A(void*);
  44. static   rt_thread_t tid_dfa,tid_dfb;
  45.                         
  46. void  key_init (void)//(PA 0 、PC13 、 P  F 5 、 P  F 4)
  47. {
  48.         
  49.     /* 使能 GPIOC 时钟源 */
  50.     rcu_periph_clock_enable(RCU_GPIOA);
  51.     rcu_periph_clock_enable(RCU_GPIOC);        
  52.     rcu_periph_clock_enable(RCU_GPIOF);        

  53.     /* 配置按键为 浮空输入 */
  54.     gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_0);        //34
  55.     gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_13);        //7
  56.     gpio_init(GPIOF, GPIO_MODE_IN_FLOATING , GPIO_OSPEED_50MHZ,GPIO_PIN_4|GPIO_PIN_5);        //14,15
  57.         
  58. }

  59. void  key_exit_mode_init (void)
  60. {
  61.         //复用功能时钟源
  62.         rcu_periph_clock_enable(RCU_AF);
  63.         
  64.         /* 配置中断优先级 */
  65.         nvic_irq_enable(EXTI10_15_IRQn, 2U, 0U);

  66.         /* 把对应的引脚连接到 exit外部中断  */
  67.         gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_13);

  68.         /* 配置外部中断13 */
  69.         exti_init(EXTI_13, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  70.         exti_interrupt_flag_clear(EXTI_13);
  71. }


  72. uint8_t key_state_get(void)
  73. {
  74.     return gpio_input_bit_get(GPIOC, GPIO_PIN_13);
  75. }


  76. void  led_init (void)
  77. {
  78.                 /* 使能对应得时钟源 */
  79.     rcu_periph_clock_enable(RCU_GPIOF);
  80.     /* 配置引脚 */
  81.     gpio_init(GPIOF, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);//10~13
  82. }



  83. void led_on(int lednum)
  84. {
  85.         switch(lednum)
  86.         {
  87.                 case 0:
  88.                     gpio_bit_set(GPIOF,GPIO_PIN_0);
  89.                         break;
  90.                         
  91.                 case 1:
  92.                     gpio_bit_set(GPIOF,GPIO_PIN_1);
  93.                         break;
  94.                         
  95.                 case 2:
  96.                     gpio_bit_set(GPIOF,GPIO_PIN_2);
  97.                         break;

  98.                 case 3:
  99.                     gpio_bit_set(GPIOF,GPIO_PIN_3);
  100.                         break;
  101.         }
  102. }


  103. void led_off(int lednum)
  104. {
  105.         switch(lednum)
  106.     {
  107.                 case 0:
  108.                     gpio_bit_reset(GPIOF,GPIO_PIN_0);
  109.                         break;
  110.                         
  111.                 case 1:
  112.                     gpio_bit_reset(GPIOF,GPIO_PIN_1);
  113.                         break;
  114.                         
  115.                 case 2:
  116.                     gpio_bit_reset(GPIOF,GPIO_PIN_2);
  117.                         break;

  118.                 case 3:
  119.                     gpio_bit_reset(GPIOF,GPIO_PIN_3);
  120.                         break;
  121.         }
  122. }



  123. uint8_t Button_2 ,Button_3 ,Button_4 ;
  124.     Button_t Button2;
  125.     Button_t Button3;
  126.     Button_t Button4;
  127. uint8_t a,b,c;
  128. uint8_t a_v,b_v,c_v;
  129. uint8_t Button2_Dowm_v=0,Button2_Dowm_rgbv;
  130. //返回句柄
  131. uint8_t pin_r ,pin_g, pin_b , pin_4 , pinout_19;






  132. //GPIO模拟PWM的实体
  133. void pwm_entry(void* p){
  134. uint8_t count;
  135. while(1)    {
  136.     //设定脉宽200ms
  137.     count++;
  138.     count %=10;
  139. rt_thread_mdelay(1);
  140. //模拟pwm比较
  141. //红色的pwm输出
  142. if (a_v==5) {
  143. if( count>=a){
  144.     rt_pin_write(pin_r, PIN_LOW);
  145. }
  146. else {
  147.     rt_pin_write(pin_r,PIN_HIGH);
  148. }
  149. } else {
  150.      rt_pin_write(pin_r,PIN_HIGH);
  151. }

  152. //绿色的PWM输出
  153. if (b_v==5) {
  154.   if(count>=b)
  155. {
  156.     rt_pin_write(pin_g, PIN_LOW);
  157. }
  158. else {
  159.     rt_pin_write(pin_g,PIN_HIGH);
  160. }
  161. } else {
  162.         rt_pin_write(pin_g,PIN_HIGH);
  163. }

  164. //蓝色的pwm输出
  165. if (c_v==5) {
  166.   if(count>=c)
  167. {
  168.     rt_pin_write(pin_b, PIN_LOW);
  169. }
  170. else {
  171.     rt_pin_write(pin_b,PIN_HIGH);
  172. }
  173. } else {
  174.         rt_pin_write(pin_b,PIN_HIGH);
  175. }
  176. }
  177. }

  178. void test_button(void* p)
  179. {

  180.     uint8_t pin_count = 0;
  181. while (1){

  182.   Button_Process();

  183.   rt_thread_mdelay(10);
  184.           pin_count++;
  185.   if ((pin_count%500)==19) {

  186. }
  187. }
  188. }


  189. void pwm_init(){
  190.     rt_thread_t tid1;
  191. //      rt_err_t ret= RT_EOK ;

  192.     /* 设置PIN脚模式为输出 */
  193.     rt_pin_mode(pin_r, PIN_MODE_OUTPUT);
  194.     rt_pin_mode(pin_g, PIN_MODE_OUTPUT);
  195.     rt_pin_mode(pin_b, PIN_MODE_OUTPUT);
  196.     rt_pin_write(pin_r, PIN_HIGH);
  197.     rt_pin_write(pin_g, PIN_HIGH);
  198.     rt_pin_write(pin_b,PIN_HIGH);
  199. //       rt_thread_t rt_thread_create( "pwm_rgb",//const char* name
  200. //            pwm_entry,//void (*entry)(void* parameter)
  201. //            RT_NULL,//void* parameter
  202. //                               512 ,//rt_uint32_t stack_size
  203. //                               3 ,//rt_uint8_t priority
  204. //                               2 );//rt_uint32_t tick
  205.     tid1 = rt_thread_create( "pwm_rgb", pwm_entry, RT_NULL,  512 ,
  206.                                3 ,
  207.                                2 );
  208.     /* 如果获得线程控制块,启动这个线程 */
  209.     if (tid1 != RT_NULL)
  210.         rt_thread_startup(tid1);
  211.                
  212.     //    _button_hw_init               
  213.     tid1 = rt_thread_create( "button_hw", test_button, RT_NULL,  1024 ,5 , 2 );
  214.     /* 如果获得线程控制块,启动这个线程 */
  215.     if (tid1 != RT_NULL)
  216.         rt_thread_startup(tid1);

  217.     //    _DF_A_hw_init
  218. //      tid_dfa = rt_thread_create( "DF_A", DF_A, RT_NULL,  1024 ,6 , 2 );
  219.     /* 如果获得线程控制块,启动这个线程 */
  220. //  if (tid_dfa != RT_NULL)
  221. //  rt_thread_startup(tid1);
  222. //    xTaskCreate(
  223. //                        DF_A,       /* Task function. */
  224. //                        "DF_A",     /* String with name of task. */
  225. //                        20000,             /* Stack size in words. */
  226. //                        NULL,              /* Parameter passed as input of the task */
  227. //                        2,                 /* Priority of the task. */
  228. //                        NULL);
  229. }



  230. //    Button
  231. uint8_t Read_Button2_Level(void)
  232. {
  233.   return rt_pin_read(Button_2);
  234. }

  235. uint8_t Read_Button3_Level(void)
  236. {
  237.     return rt_pin_read(Button_3);
  238. }


  239. uint8_t Read_Button4_Level(void)
  240. {
  241.     return rt_pin_read(Button_4);
  242. }


  243. void Btn2_Dowm_CallBack(void *btn)
  244. {
  245.   rt_kprintf("Button2 Dowm_!");
  246.   Button2_Dowm_v=1;
  247.   a++;
  248.   rt_kprintf("a=%d",a);
  249.   if (a>10) {a_v=5;
  250.       a=0;
  251. }else {a_v=5;

  252. }
  253. }

  254. void Btn2_Double_CallBack(void *btn)
  255. {
  256.   rt_kprintf("Button2 _Double_!");
  257. }

  258. void Btn2_Long_CallBack(void *btn)
  259. {
  260.   rt_kprintf("Button2_Long_!");

  261. //  Button_Delete(&Button2);
  262. //  rt_kprintf("删除Button2");
  263. //  Search_Button();
  264. }


  265. void Btn3_Dowm_CallBack(void *btn)
  266. {
  267.   rt_kprintf("Button3 _Dowm!");
  268.   b++;
  269.   rt_kprintf("b=%d",b);
  270.   if (b>10) {b_v=5;
  271.       b=0;
  272. }else {b_v=5;

  273. }
  274. }

  275. void Btn3_Double_CallBack(void *btn)
  276. {
  277.   rt_kprintf("Button3_Double_!");
  278. }

  279. void Btn3_Long_CallBack(void *btn)
  280. {
  281.   rt_kprintf("Button3 _Long_!");

  282. //  Button_Delete(&Button2);
  283. //  rt_kprintf("删除Button3");
  284. //  Search_Button();
  285. }

  286. void Btn4_Dowm_CallBack(void *btn)
  287. {
  288.   rt_kprintf("Button4 _Dowm!");
  289.   c++;if (c>10) {
  290.       c=0;
  291. }
  292. }

  293. void Btn4_Double_CallBack(void *btn)
  294. {
  295.   rt_kprintf("Button4 _Double_!");
  296. }

  297. void Btn4_Long_CallBack(void *btn)
  298. {
  299.   rt_kprintf("Button4 long_Dowm !");

  300. //  Button_Delete(&Button2);
  301. //  rt_kprintf("删除Button4");
  302. //  Search_Button();
  303. }


  304. uint16_t   adc_val_1;
  305. int main(void)
  306. {
  307. #define KEY_ON   0     //
  308.     //    Button
  309.     Button_2    = 34;//get_pin(34)//    22
  310.     Button_3    = 7;//get_pin(7);    21
  311.     Button_4    = 15;//get_pin(15);    2
  312.     rt_pin_mode(Button_2 , PIN_MODE_INPUT_PULLUP);
  313.     rt_pin_mode(Button_3 , PIN_MODE_INPUT_PULLUP);
  314.     rt_pin_mode(Button_4 , PIN_MODE_INPUT_PULLUP);

  315.     Button_Create("Button3",
  316.                 &Button3,
  317.                 Read_Button3_Level,
  318.                 KEY_ON);
  319.     Button_Attach(&Button3,BUTTON_DOWM,Btn3_Dowm_CallBack);                       //单击
  320.     Button_Attach(&Button3,BUTTON_DOUBLE,Btn3_Double_CallBack);                   //双击
  321. //    Button_Attach(&Button3,BUTTON_CONTINUOS,Btn3_Continuos_CallBack);             //连按
  322. //    Button_Attach(&Button3,BUTTON_CONTINUOS_FREE,Btn3_ContinuosFree_CallBack);    //连按释放
  323.     Button_Attach(&Button3,BUTTON_LONG,Btn3_Long_CallBack);                       //长按


  324.     Button_Create("Button2",
  325.                 &Button2,
  326.                 Read_Button2_Level,
  327.                 KEY_ON);
  328.     Button_Attach(&Button2,BUTTON_DOWM,Btn2_Dowm_CallBack);                     //单击
  329.     Button_Attach(&Button2,BUTTON_DOUBLE,Btn2_Double_CallBack);                 //双击
  330. //    Button_Attach(&Button2,BUTTON_CONTINUOS,Btn2_Continuos_CallBack);           //连按
  331. //    Button_Attach(&Button2,BUTTON_CONTINUOS_FREE,Btn2_ContinuosFree_CallBack);  //连按释放
  332.     Button_Attach(&Button2,BUTTON_LONG,Btn2_Long_CallBack);                     //长按



  333.     Button_Create("Button4",
  334.                 &Button4,
  335.                 Read_Button4_Level,
  336.                 KEY_ON);
  337.     Button_Attach(&Button4,BUTTON_DOWM,Btn4_Dowm_CallBack);                     //单击
  338.     Button_Attach(&Button4,BUTTON_DOUBLE,Btn4_Double_CallBack);                 //双击
  339. //    Button_Attach(&Button4,BUTTON_CONTINUOS,Btn4_Continuos_CallBack);           //连按
  340. //    Button_Attach(&Button4,BUTTON_CONTINUOS_FREE,Btn4_ContinuosFree_CallBack);  //连按释放
  341.     Button_Attach(&Button4,BUTTON_LONG,Btn4_Long_CallBack);                     //长按

  342.     Get_Button_Event(&Button4);
  343.     Get_Button_Event(&Button3);
  344.     Get_Button_Event(&Button2);





  345.      pin_r = 10;//rt_pin_get(10);
  346.      pin_g = 11;//rt_pin_get(11);
  347.      pin_b = 12;//rt_pin_get(12);
  348.                 pin_4= 13;//rt_pin_get(13);
  349.                 pinout_19= 19;//out_rt_pin_get(19);
  350.     rt_pin_mode(pin_r, PIN_MODE_OUTPUT);
  351.     rt_pin_mode(pin_g, PIN_MODE_OUTPUT);
  352.     rt_pin_mode(pin_b, PIN_MODE_OUTPUT);
  353.     rt_pin_mode(pin_4, PIN_MODE_OUTPUT);        
  354.     rt_pin_mode(pinout_19, PIN_MODE_OUTPUT);                        
  355.     rt_kprintf("Hello, worldn");
  356.     a=10;b=10;c=10;//pin_rgb不亮
  357.     a_v=5;b_v=5;c_v=5;//pin_rgb允许
  358. //    _irrx_hw_init();
  359.     pwm_init();
  360.     mind_n_key1 = 0;

  361. //    uint8_t pin_count = 0;
  362. //    while (1)
  363. //    {
  364. //
  365. //        rt_pin_write(pin_r, pin_count&0x1);
  366. //        rt_pin_write(pin_g,( pin_count>>1)&0x1);
  367. //        rt_pin_write(pin_b, ( pin_count>>2)&0x1);
  368. //        rt_thread_mdelay(50);
  369. //        rt_pin_write(pin_r, 0x1);
  370. //        rt_pin_write(pin_g, 0x1);
  371. //        rt_pin_write(pin_b,0x1);
  372. //        rt_thread_mdelay(500);
  373. //        pin_count++;
  374. //    }
  375. //adc
  376.     /* system clocks configuration */
  377.     rcu_config();
  378.     /* configure systick */
  379.     //systick_config();  
  380.     /* ADC configuration */
  381.     adc_config();
  382.     /* USART configuration */
  383.     //gd_eval_com_init(EVAL_COM1);
  384. //adc

  385. //adc
  386.         /* ADC software trigger enable */
  387.         adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
  388.         /* delay a time in milliseconds */
  389.         rt_thread_mdelay(2000);
  390.       
  391.         /* value convert  */
  392.         temperature = (1.43 - ADC_IDATA0(ADC0)*3.3/4096) * 1000 / 4.3 + 25;
  393.         vref_value = (ADC_IDATA1(ADC0) * 3.3 / 4096);
  394.       
  395.         /* value print */
  396.         printf(" the temperature data is %2.0f degrees Celsiusrn", temperature);
  397.         printf(" the reference voltage data is %5.3fV rn", vref_value);
  398.         printf(" rn");
  399.                                 //*!      disable the temperature sensor and Vrefint channel
  400.                                 adc_tempsensor_vrefint_disable();
  401.                                 adc_init();//pc3
  402. //adc

  403.     while (1){

  404.                                 /* ADC software trigger enable */
  405.         adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
  406.         /* delay a time in milliseconds */
  407.         rt_thread_mdelay(2000);
  408.       
  409.         /* value convert  */
  410.         adc_val_1 = ADC_IDATA0(ADC0); //pc3   IN13
  411. //        adc_val_2 = ADC_IDATA1(ADC0);//PC5   IN15
  412.       
  413.         /* value print */
  414.         printf(" adc val 1 is 0x%xrn", adc_val_1);
  415. //        printf(" adc val 2 is 0x%xrn", adc_val_2);
  416.         printf(" rn");

  417.                         
  418.       rt_thread_mdelay(500);




  419.     if(bootCount==0x0a55aa00){

  420.              bootCount=0x0a55aa01; //启动DeepSleep
  421. //     endProducing= clock_cpu_millisecond();
  422.         }
  423.     if(bootCount==0x0a55aa01){
  424.                               rt_thread_mdelay(90000);
  425. //             if(clock_cpu_millisecond() - endProducing > 90000){
  426. //          xTaskCreate(
  427. //                        DF_A,
  428. //                        "DF_A",     /* String with name of task. */
  429. //                        20000,             /* Stack size in words. */
  430. //                        NULL,              /* Parameter passed as input of the task */
  431. //                        2,                 /* Priority of the task. */
  432. //                        NULL);
  433.           tid_dfa = rt_thread_create( "DF_A", DF_A, RT_NULL,  1024 ,6 , 2 );
  434.           /* 如果获得线程控制块,启动这个线程 */
  435.           if (tid_dfa != RT_NULL)
  436.               rt_thread_startup(tid_dfa);
  437.           //    _DF_A_hw_init

  438. //         }

  439.           }
  440.       if(mind_n_key1==2) {
  441. //          digitalWrite(OUT_PWM, LOW);
  442.     rt_pin_write(pinout_19,PIN_LOW);
  443.       }
  444.           if(bootCount==1) {
  445.              rt_thread_mdelay(6); //delay(6);
  446.                   bootCount=0;
  447.        if (mind_n_key1==0) {

  448. //    xTaskCreate(
  449. //                        DF_A,
  450. //                        "DF_A",     /* String with name of task. */
  451. //                        20000,             /* Stack size in words. */
  452. //                        NULL,              /* Parameter passed as input of the task */
  453. //                        2,                 /* Priority of the task. */
  454. //                        NULL);
  455.            tid_dfa = rt_thread_create( "DF_A", DF_A, RT_NULL,  1024 ,6 , 2 );
  456.            /* 如果获得线程控制块,启动这个线程 */
  457.            if (tid_dfa != RT_NULL)
  458.                rt_thread_startup(tid_dfa);
  459.            //    _DF_A_hw_init

  460.     }
  461.       if (mind_n_key1==1) {


  462. //    xTaskCreate(
  463. //                        DF_B,
  464. //                        "DF_B",     /* String with name of task. */
  465. //                        10000,             /* Stack size in words. */
  466. //                        NULL,              /* Parameter passed as input of the task */
  467. //                        2,                 /* Priority of the task. */
  468. //                        NULL);
  469.           tid_dfb = rt_thread_create( "DF_B", DF_B, RT_NULL,  1024 ,6 , 2 );
  470.           /* 如果获得线程控制块,启动这个线程 */
  471.           if (tid_dfb!= RT_NULL)
  472.               rt_thread_startup(tid_dfb);
  473.           //    _DF_A_hw_init

  474.     }


  475.       }

  476.     }

  477. }


  478. // 自定义函数

  479. void DF_B(void * parameter) {
  480.   uint16_t  mind_a0_on, mind_a0_on1;
  481. //       Serial.print("Created task DF_b: Executing on core ");
  482. //   Serial.println(xPortGetCoreID());
  483.   while (mind_n_key1==1)  {
  484.     mind_a0_on=0;
  485.      for(b=0;b<8;b++){//检测双击 循环10次200ms

  486. //      startProducing = ESP.getCycleCount();//开始时间
  487. //           mind_a0_on1=analogRead(IN_0);
  488. //           mind_a0_on+=mind_a0_on1;
  489. //          endProducing = ESP.getCycleCount();//结束时间
  490.         //      Serial.print("mind_a0_on input A0 time all时间(CycleCount): ");
  491. //              Serial.println(( endProducing - startProducing));//写入耗费时间(CycleCount)
  492.         //      Serial.printf("DF_b(analogRead(IN_0))%d: %d   %d  n",b,mind_a0_on1,mind_a0_on);//
  493.   //      Serial.println("");
  494.      }

  495.     DF_onoff(mind_a0_on>>5, 1023, 1);



  496.   }


  497.    bootCount = 1;
  498.     //      Serial.println("删除DF_b");
  499. //    vTaskDelete( NULL );  //任务的删除
  500.     //      rt_thread_delete();//任务的删除
  501. }

  502. void DF_onoff(uint16_t  mind_n_on, uint16_t  mind_n_off, uint8_t  mind_n_key) {
  503.   //      Serial.printf("DF_onoff Cycle Count  mind_n_on:%d, uint16_t  mind_n_off:%d, uint8_t  mind_n_key:%dn",mind_n_on,mind_n_off, mind_n_key);//

  504.   while ((mind_n_off>0)) {
  505.     while ((mind_n_on>0)) {
  506.       //      digitalWrite(OUT_PWM, HIGH);
  507.       //      delay(6);
  508.       mind_n_off -= 1;
  509.       mind_n_on -= 1;
  510.       if (!(mind_n_key==mind_n_key1)) {

  511.         mind_n_off = 0;
  512.         mind_n_on = 0;
  513.    bootCount = 1;
  514.     }
  515.     }
  516.     if ((mind_n_off>0)) {
  517.       //      digitalWrite(OUT_PWM, LOW);
  518.       //      delay(6);
  519.       mind_n_off -= 1;
  520.       if (!(mind_n_key==mind_n_key1)) {

  521.         mind_n_off = 0;
  522.         mind_n_on = 0;
  523.    bootCount = 1;
  524.     }
  525.     }

  526.   }
  527. }
  528. void DF_A(void * parameter) {



  529.       //      Serial.print("Created task DF_A: Executing on core ");
  530.     //      Serial.println(xPortGetCoreID());
  531. //      startProducing = millis();//开始时间

  532. if(bootCount==0x0a55aa01){
  533.    bootCount = 0;
  534. uint8_t   mind_n_Asn = 80;


  535.         //      Serial.print("DF_A to 50% start Producing time 时间(ms): ");
  536.         //      Serial.println(( millis() - startProducing));//写入耗费时间(ms)
  537.   while (!(mind_n_Asn==0)) {
  538.     DF_onoff(490, 1023, 0);
  539.      if (mind_n_key1==0) {
  540.       mind_n_Asn -= 1;
  541.     }
  542.     else {
  543.    bootCount = 1;
  544.       mind_n_Asn = 0;
  545.     }

  546.   }
  547. }
  548. else{
  549.   mind_n_As1 = 10;
  550.   while (mind_n_As1>0) {
  551.     DF_onoff(1023, 1023, 0);


  552.     if (mind_n_key1==0) {

  553.       mind_n_As1 -= 1;

  554.     }
  555.     else {

  556.       mind_n_As1 = 0;
  557.    bootCount = 1;
  558.     }
  559. }
  560.      if (mind_n_key1==0) {
  561.         //      Serial.print("DF_A 启动1分 时间(ms): ");
  562.         //      Serial.println(( millis() - startProducing));//写入耗费时间(ms)
  563.        //      Serial.print("启动1.5分连续 :");
  564.        bootCount=0x0a55aa00; //启动DeepSleep

  565.     //      Serial.println(bootCount);
  566.   //      delay(1);
  567.     //      Serial.println("删除DF_A");
  568.     //      vTaskDelete( NULL );  //任务的删除

  569.      }

  570.   }

  571.     //      endProducing = millis();//结束时间
  572.         //      Serial.print("Producing time all时间(ms): ");
  573.         //      Serial.println(( endProducing - startProducing));//写入耗费时间(ms)
  574.   if (mind_n_key1==0) {
  575.     mind_n_key1 = 2;
  576.   }

  577.     //      Serial.println("删除DF_A");
  578.     //      vTaskDelete( NULL );  //任务的删除

  579. }



  580. /*!
  581.     brief      configure the different system clocks
  582.     param[in]  none
  583.     param[out] none
  584.     retval     none
  585. */
  586. void rcu_config(void)
  587. {
  588.     /* enable ADC clock */
  589.     rcu_periph_clock_enable(RCU_ADC0);
  590.     /* config ADC clock */
  591.     rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
  592. }

  593. /*!
  594.     brief      configure the ADC peripheral
  595.     param[in]  none
  596.     param[out] none
  597.     retval     none
  598. */
  599. void adc_config(void)
  600. {
  601.     /* ADC SCAN function enable */
  602.     adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);  
  603.     /* ADC trigger config */
  604.     adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE);
  605.     /* ADC data alignment config */
  606.     adc_data_alignment_config(ADC0,ADC_DATAALIGN_RIGHT);
  607.     /* ADC mode config */
  608.     adc_mode_config(ADC_MODE_FREE);  
  609.     /* ADC channel length config */
  610.     adc_channel_length_config(ADC0,ADC_INSERTED_CHANNEL,2);

  611.     /* ADC temperature sensor channel config */
  612.     adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5);
  613.     /* ADC internal reference voltage channel config */
  614.     adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5);

  615.     /* ADC external trigger enable */
  616.     adc_external_trigger_config(ADC0,ADC_INSERTED_CHANNEL,ENABLE);

  617.     /* ADC temperature and Vrefint enable */
  618.     adc_tempsensor_vrefint_enable();

  619.    
  620.     /* enable ADC interface */
  621.     adc_enable(ADC0);
  622.     rt_thread_mdelay(1);   
  623.     /* ADC calibration and reset calibration */
  624.     adc_calibration_enable(ADC0);
  625. }

  626. // 事件回调函数

  627. //宏: [RT_USING_CPUTIME]
  628. //使能此选项时,BSP 应提供 rt_clock_cputime_ops 为 CPU 时间,通过
  629. //const static struct rt_clock_cputime_ops _ops = {...};
  630. //clock_cpu_setops(&_ops);
  631. //
  632. //然后开发者能够使用高精度的时钟计数:
  633. //
  634. //ts1 = clock_cpu_gettime();
  635. //ts2 = clock_cpu_gettime();
  636. //
  637. ///* and get the ms of delta tick with API: */
  638. //ms_tick = clock_cpu_millisecond(t2 - t1);
  639. //us_tick = clock_cpu_microsecond(t2 - t1);

  640. void adc_init(void)
  641. {
  642.         //adc 引脚初始化
  643.         
  644.         /* 使能 GPIOC clock */
  645.     rcu_periph_clock_enable(RCU_GPIOC);
  646.         
  647.     /* 设置PC3  引脚为模拟输入 */  
  648.     gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_3 );
  649. //adc 引脚初始化
  650.         
  651.         /* 使能ADC0 时钟源 */
  652.     rcu_periph_clock_enable(RCU_ADC0);
  653.     /* 配置预分频 */
  654.     rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);

  655.         
  656.         /* ADC 扫描模式打开 */
  657.         adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);  
  658.         /* ADC 触发方式配置 不需要外部触发 */
  659.         adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE);
  660.         /* 数据右对齐 */
  661.         adc_data_alignment_config(ADC0,ADC_DATAALIGN_RIGHT);
  662.         /* ADC 模式配置,所有ADC独立工作 */
  663.         adc_mode_config(ADC_MODE_FREE);  
  664.         /* ADC 转换通道大小配置,2个通道 */
  665.         adc_channel_length_config(ADC0,ADC_INSERTED_CHANNEL,1);

  666.         /* ADC 通道配置 */
  667.         adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_13, ADC_SAMPLETIME_239POINT5);
  668.         /* ADC 通道配置 */
  669.         //adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_15, ADC_SAMPLETIME_239POINT5);

  670.         /* ADC 插入组使能 */
  671.         adc_external_trigger_config(ADC0,ADC_INSERTED_CHANNEL,ENABLE);
  672.         
  673.         /* 使能ADC */
  674.         adc_enable(ADC0);
  675.     rt_thread_mdelay(1);//        delay_1ms(1);         
  676.         /* ADC 校准 */
  677.         adc_calibration_enable(ADC0);
  678. }







Build started: Project: project
*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'g:Keil_v5ARMARMCCBin'
Build target 'rt-thread_gd32f30x'
compiling main.c...
applicationsmain.c(580): warning:  #177-D: variable "mind_a0_on1"  was declared but never referenced
    uint16_t  mind_a0_on, mind_a0_on1;
applicationsmain.c: 1 warning, 0 errors
linking...
Program Size: Code=107128 RO-data=11488 RW-data=1020 ZI-data=5676  
After Build - User command #1: fromelf --bin .buildrtthread-gd32f30x.axf --output rtthread.bin
".buildrtthread-gd32f30x.axf" - 0 Error(s), 1 Warning(s).
Build Time Elapsed:  00:00:07
Load "C:\rt-thread\bsp\gd32303e-eval\build\rtthread-gd32f30x.axf"
Dll Verison 1.06  Build date: 2019-06-23
Emulaor: 32-LINK, SN: 0023557, Hardware: A, Firmware: 1.23
Manufacturer: , Date: 2020-01-08
Appliaction: 32-LINK, Version: 1.51
Connect Mode SWD, Speed Auto
Out put voltage 3.34V, current 0.00mA, target voltage 3.34V
Found SWD-DP with ID 0x410FC241
TPIU fitted.
ETM fitted.
Found Cortex-M4 , Little endian
FPUnit: 6 code (BP) slots and 2 literal slots
Data Watchpoint: 4
Erase Done.
Programming Done.
Verify OK.
Flash Load finished at 14:36:30


com口
未标题-1 拷贝.jpg

更多回帖

发帖
×
20
完善资料,
赚取积分