https://bbs.elecfans.com/jishu_2183442_1_1.html
接上稿,进行 ADC程序测试
在之前基础上加入ADC程序(温度传感器和内部参考电压 VREFINT ), ADC(PC3)模拟输入。
程序:
- /*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2006-03-23 Bernard the first version
- * 2010-11-10 Bernard add cleanup callback function in thread exit.
- * 2012-12-29 Bernard fix compiling warning.
- * 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no
- * dead thread.
- * 2016-08-09 ArdaFu add method to get the handler of the idle thread.
- * 2018-02-07 Bernard lock scheduler to protect tid->cleanup.
- * 2018-07-14 ARMink add idle hook list
- * 2018-11-22 Jesven add per cpu idle task
- * combine the code of primary and secondary cpu
- */
- #include
- #include
- #ifdef RT_USING_MODULE
- #include
- #endif
- #if defined (RT_USING_HOOK)
- #ifndef RT_USING_IDLE_HOOK
- #define RT_USING_IDLE_HOOK
- #endif
- #endif
- #ifndef IDLE_THREAD_STACK_SIZE
- #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
- #define IDLE_THREAD_STACK_SIZE 256
- #else
- #define IDLE_THREAD_STACK_SIZE 128
- #endif
- #endif
- #ifdef RT_USING_SMP
- #define _CPUS_NR RT_CPUS_NR
- #else
- #define _CPUS_NR 1
- #endif
- extern rt_list_t rt_thread_defunct;
- static struct rt_thread idle[_CPUS_NR];
- ALIGN(RT_ALIGN_SIZE)
- static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];
- #ifdef RT_USING_IDLE_HOOK
- #ifndef RT_IDLE_HOOK_LIST_SIZE
- #define RT_IDLE_HOOK_LIST_SIZE 4
- #endif
- static void (*idle_hook_list[RT_IDLE_HOOK_LIST_SIZE])(void);
- /**
- * @ingroup Hook
- * This function sets a hook function to idle thread loop. When the system peRForms
- * idle loop, this hook function should be invoked.
- *
- * [url=home.php?mod=space&uid=3142012]@param[/url] hook the specified hook function
- *
- * [url=home.php?mod=space&uid=1141835]@Return[/url] RT_EOK: set OK
- * -RT_EFULL: hook list is full
- *
- * [url=home.php?mod=space&uid=1902110]@NOTE[/url] the hook function must be simple and never be blocked or suspend.
- */
- rt_err_t rt_thread_idle_sethook(void (*hook)(void))
- {
- rt_size_t i;
- rt_base_t level;
- rt_err_t ret = -RT_EFULL;
- /* disable interrupt */
- level = rt_hw_interrupt_disable();
- for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
- {
- if (idle_hook_list[i] == RT_NULL)
- {
- idle_hook_list[i] = hook;
- ret = RT_EOK;
- break;
- }
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- return ret;
- }
- /**
- * delete the idle hook on hook list
- *
- * [url=home.php?mod=space&uid=3142012]@param[/url] hook the specified hook function
- *
- * [url=home.php?mod=space&uid=1141835]@Return[/url] RT_EOK: delete OK
- * -RT_ENOSYS: hook was not found
- */
- rt_err_t rt_thread_idle_delhook(void (*hook)(void))
- {
- rt_size_t i;
- rt_base_t level;
- rt_err_t ret = -RT_ENOSYS;
- /* disable interrupt */
- level = rt_hw_interrupt_disable();
- for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
- {
- if (idle_hook_list[i] == hook)
- {
- idle_hook_list[i] = RT_NULL;
- ret = RT_EOK;
- break;
- }
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- return ret;
- }
- #endif
- /* Return whether there is defunctional thread to be deleted. */
- rt_inline int _has_defunct_thread(void)
- {
- /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
- * So the compiler has a good reason that the rt_thread_defunct list does
- * not change within rt_thread_idle_excute thus optimize the "while" loop
- * into a "if".
- *
- * So add the volatile qualifier here. */
- const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;
- return l->next != l;
- }
- /**
- * @ingroup Thread
- *
- * This function will perform system background job when system idle.
- */
- void rt_thread_idle_excute(void)
- {
- /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
- * will do all the cleanups. */
- while (_has_defunct_thread())
- {
- rt_base_t lock;
- rt_thread_t thread;
- #ifdef RT_USING_MODULE
- struct rt_dlmodule *module = RT_NULL;
- #endif
- RT_debug_NOT_IN_INTERRUPT;
- /* disable interrupt */
- lock = rt_hw_interrupt_disable();
- /* re-check whether list is empty */
- if (_has_defunct_thread())
- {
- /* get defunct thread */
- thread = rt_list_entry(rt_thread_defunct.next,
- struct rt_thread,
- tlist);
- #ifdef RT_USING_MODULE
- module = (struct rt_dlmodule*)thread->module_id;
- if (module)
- {
- dlmodule_destroy(module);
- }
- #endif
- /* remove defunct thread */
- rt_list_remove(&(thread->tlist));
- /* lock scheduler to prevent scheduling in cleanup function. */
- rt_enter_critical();
- /* invoke thread cleanup */
- if (thread->cleanup != RT_NULL)
- thread->cleanup(thread);
- #ifdef RT_USING_SIGNALS
- rt_thread_free_sig(thread);
- #endif
- /* if it's a system object, not delete it */
- if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
- {
- /* detach this object */
- rt_object_detach((rt_object_t)thread);
- /* unlock scheduler */
- rt_exit_critical();
- /* enable interrupt */
- rt_hw_interrupt_enable(lock);
- return;
- }
- /* unlock scheduler */
- rt_exit_critical();
- }
- else
- {
- /* enable interrupt */
- rt_hw_interrupt_enable(lock);
- /* may the defunct thread list is removed by others, just return */
- return;
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(lock);
- #ifdef RT_USING_HEAP
- /* release thread's stack */
- RT_KERNEL_FREE(thread->stack_addr);
- /* delete thread object */
- rt_object_delete((rt_object_t)thread);
- #endif
- }
- }
- extern void rt_system_power_manager(void);
- static void rt_thread_idle_entry(void *parameter)
- {
- #ifdef RT_USING_SMP
- if (rt_hw_cpu_id() != 0)
- {
- while (1)
- {
- rt_hw_secondary_cpu_idle_exec();
- }
- }
- #endif
- while (1)
- {
- #ifdef RT_USING_IDLE_HOOK
- rt_size_t i;
- for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
- {
- if (idle_hook_list[i] != RT_NULL)
- {
- idle_hook_list[i]();
- }
- }
- #endif
- rt_thread_idle_excute();
- #ifdef RT_USING_PM
- rt_system_power_manager();
- #endif
- }
- }
- /**
- * @ingroup SystemInit
- *
- * This function will initialize idle thread, then start it.
- *
- * [url=home.php?mod=space&uid=1902110]@NOTE[/url] this function must be invoked when system init.
- */
- void rt_thread_idle_init(void)
- {
- rt_ubase_t i;
- char tidle_name[RT_NAME_MAX];
- for (i = 0; i < _CPUS_NR; i++)
- {
- rt_sprintf(tidle_name, "tidle%d", i);
- rt_thread_init(&idle[i],
- tidle_name,
- rt_thread_idle_entry,
- RT_NULL,
- &rt_thread_stack[i][0],
- sizeof(rt_thread_stack[i]),
- RT_THREAD_PRIORITY_MAX - 1,
- 32);
- #ifdef RT_USING_SMP
- rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
- #endif
- /* startup */
- rt_thread_startup(&idle[i]);
- }
- }
- /**
- * @ingroup Thread
- *
- * This function will get the handler of the idle thread.
- *
- */
- rt_thread_t rt_thread_idle_gethandler(void)
- {
- #ifdef RT_USING_SMP
- register int id = rt_hw_cpu_id();
- #else
- register int id = 0;
- #endif
- return (rt_thread_t)(&idle[id]);
- }
复制代码
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: -LINK, SN: 0023557, Hardware: A, Firmware: 1.23
Manufacturer: , Date: 2020-01-08
Appliaction: -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
键盘显示及内部两路ADC测试正常。
加入(PC3)模拟输入程序
- /*
- * File : main.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *想用星空派gd32F303开发板资源 (P F 7)输出(19),(PC3)模拟输入(29),板(P F0~3)LED,板(PA 0 、PC13 、 P F 5 、 P F 4)button,来通过光耦中间继电器控制加热设备。
- * Change Logs:
- * Date Author Notes
- * 2021-10-29 first implementation
- */
- #include
- #include
- #include "stdio.h"
- #include
- #include
- #include "gd32f303e_eval.h"
- //#include "systick.h"
- #include "button.h"
- //adc
- float temperature;
- float vref_value;
- void rcu_config(void);
- void adc_config(void);
- void adc_init(void);
- /*!
- brief main function
- param[in] none
- param[out] none
- retval none
- */
- //adc
- uint8_t pin_count = 0;
- volatile long bootCount = 0; //DF-A进行 1分,1.5分,10分状态。
- volatile uint8_t mind_n_As1; //
- unsigned long startProducing, endProducing;
- volatile float mind_n_key1, mind_n_all, mind_n_oninall;
- // 函数声明
- void DF_B(void*);
- void DF_onoff(uint16_t mind_n_on, uint16_t mind_n_off, uint8_t mind_n_key);
- void DF_A(void*);
- static rt_thread_t tid_dfa,tid_dfb;
-
- void key_init (void)//(PA 0 、PC13 、 P F 5 、 P F 4)
- {
-
- /* 使能 GPIOC 时钟源 */
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOC);
- rcu_periph_clock_enable(RCU_GPIOF);
- /* 配置按键为 浮空输入 */
- gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_0); //34
- gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_13); //7
- gpio_init(GPIOF, GPIO_MODE_IN_FLOATING , GPIO_OSPEED_50MHZ,GPIO_PIN_4|GPIO_PIN_5); //14,15
-
- }
- void key_exit_mode_init (void)
- {
- //复用功能时钟源
- rcu_periph_clock_enable(RCU_AF);
-
- /* 配置中断优先级 */
- nvic_irq_enable(EXTI10_15_IRQn, 2U, 0U);
- /* 把对应的引脚连接到 exit外部中断 */
- gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_13);
- /* 配置外部中断13 */
- exti_init(EXTI_13, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
- exti_interrupt_flag_clear(EXTI_13);
- }
- uint8_t key_state_get(void)
- {
- return gpio_input_bit_get(GPIOC, GPIO_PIN_13);
- }
- void led_init (void)
- {
- /* 使能对应得时钟源 */
- rcu_periph_clock_enable(RCU_GPIOF);
- /* 配置引脚 */
- gpio_init(GPIOF, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);//10~13
- }
- void led_on(int lednum)
- {
- switch(lednum)
- {
- case 0:
- gpio_bit_set(GPIOF,GPIO_PIN_0);
- break;
-
- case 1:
- gpio_bit_set(GPIOF,GPIO_PIN_1);
- break;
-
- case 2:
- gpio_bit_set(GPIOF,GPIO_PIN_2);
- break;
- case 3:
- gpio_bit_set(GPIOF,GPIO_PIN_3);
- break;
- }
- }
- void led_off(int lednum)
- {
- switch(lednum)
- {
- case 0:
- gpio_bit_reset(GPIOF,GPIO_PIN_0);
- break;
-
- case 1:
- gpio_bit_reset(GPIOF,GPIO_PIN_1);
- break;
-
- case 2:
- gpio_bit_reset(GPIOF,GPIO_PIN_2);
- break;
- case 3:
- gpio_bit_reset(GPIOF,GPIO_PIN_3);
- break;
- }
- }
- uint8_t Button_2 ,Button_3 ,Button_4 ;
- Button_t Button2;
- Button_t Button3;
- Button_t Button4;
- uint8_t a,b,c;
- uint8_t a_v,b_v,c_v;
- uint8_t Button2_Dowm_v=0,Button2_Dowm_rgbv;
- //返回句柄
- uint8_t pin_r ,pin_g, pin_b , pin_4 , pinout_19;
- //GPIO模拟PWM的实体
- void pwm_entry(void* p){
- uint8_t count;
- while(1) {
- //设定脉宽200ms
- count++;
- count %=10;
- rt_thread_mdelay(1);
- //模拟pwm比较
- //红色的pwm输出
- if (a_v==5) {
- if( count>=a){
- rt_pin_write(pin_r, PIN_LOW);
- }
- else {
- rt_pin_write(pin_r,PIN_HIGH);
- }
- } else {
- rt_pin_write(pin_r,PIN_HIGH);
- }
- //绿色的PWM输出
- if (b_v==5) {
- if(count>=b)
- {
- rt_pin_write(pin_g, PIN_LOW);
- }
- else {
- rt_pin_write(pin_g,PIN_HIGH);
- }
- } else {
- rt_pin_write(pin_g,PIN_HIGH);
- }
- //蓝色的pwm输出
- if (c_v==5) {
- if(count>=c)
- {
- rt_pin_write(pin_b, PIN_LOW);
- }
- else {
- rt_pin_write(pin_b,PIN_HIGH);
- }
- } else {
- rt_pin_write(pin_b,PIN_HIGH);
- }
- }
- }
- void test_button(void* p)
- {
- uint8_t pin_count = 0;
- while (1){
- Button_Process();
- rt_thread_mdelay(10);
- pin_count++;
- if ((pin_count%500)==19) {
- }
- }
- }
- void pwm_init(){
- rt_thread_t tid1;
- // rt_err_t ret= RT_EOK ;
- /* 设置PIN脚模式为输出 */
- rt_pin_mode(pin_r, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_g, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_b, PIN_MODE_OUTPUT);
- rt_pin_write(pin_r, PIN_HIGH);
- rt_pin_write(pin_g, PIN_HIGH);
- rt_pin_write(pin_b,PIN_HIGH);
- // rt_thread_t rt_thread_create( "pwm_rgb",//const char* name
- // pwm_entry,//void (*entry)(void* parameter)
- // RT_NULL,//void* parameter
- // 512 ,//rt_uint32_t stack_size
- // 3 ,//rt_uint8_t priority
- // 2 );//rt_uint32_t tick
- tid1 = rt_thread_create( "pwm_rgb", pwm_entry, RT_NULL, 512 ,
- 3 ,
- 2 );
- /* 如果获得线程控制块,启动这个线程 */
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
-
- // _button_hw_init
- tid1 = rt_thread_create( "button_hw", test_button, RT_NULL, 1024 ,5 , 2 );
- /* 如果获得线程控制块,启动这个线程 */
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
- // _DF_A_hw_init
- // tid_dfa = rt_thread_create( "DF_A", DF_A, RT_NULL, 1024 ,6 , 2 );
- /* 如果获得线程控制块,启动这个线程 */
- // if (tid_dfa != RT_NULL)
- // rt_thread_startup(tid1);
- // xTaskCreate(
- // DF_A, /* Task function. */
- // "DF_A", /* String with name of task. */
- // 20000, /* Stack size in words. */
- // NULL, /* Parameter passed as input of the task */
- // 2, /* Priority of the task. */
- // NULL);
- }
- // Button
- uint8_t Read_Button2_Level(void)
- {
- return rt_pin_read(Button_2);
- }
- uint8_t Read_Button3_Level(void)
- {
- return rt_pin_read(Button_3);
- }
- uint8_t Read_Button4_Level(void)
- {
- return rt_pin_read(Button_4);
- }
- void Btn2_Dowm_CallBack(void *btn)
- {
- rt_kprintf("Button2 Dowm_!");
- Button2_Dowm_v=1;
- a++;
- rt_kprintf("a=%d",a);
- if (a>10) {a_v=5;
- a=0;
- }else {a_v=5;
- }
- }
- void Btn2_Double_CallBack(void *btn)
- {
- rt_kprintf("Button2 _Double_!");
- }
- void Btn2_Long_CallBack(void *btn)
- {
- rt_kprintf("Button2_Long_!");
- // Button_Delete(&Button2);
- // rt_kprintf("删除Button2");
- // Search_Button();
- }
- void Btn3_Dowm_CallBack(void *btn)
- {
- rt_kprintf("Button3 _Dowm!");
- b++;
- rt_kprintf("b=%d",b);
- if (b>10) {b_v=5;
- b=0;
- }else {b_v=5;
- }
- }
- void Btn3_Double_CallBack(void *btn)
- {
- rt_kprintf("Button3_Double_!");
- }
- void Btn3_Long_CallBack(void *btn)
- {
- rt_kprintf("Button3 _Long_!");
- // Button_Delete(&Button2);
- // rt_kprintf("删除Button3");
- // Search_Button();
- }
- void Btn4_Dowm_CallBack(void *btn)
- {
- rt_kprintf("Button4 _Dowm!");
- c++;if (c>10) {
- c=0;
- }
- }
- void Btn4_Double_CallBack(void *btn)
- {
- rt_kprintf("Button4 _Double_!");
- }
- void Btn4_Long_CallBack(void *btn)
- {
- rt_kprintf("Button4 long_Dowm !");
- // Button_Delete(&Button2);
- // rt_kprintf("删除Button4");
- // Search_Button();
- }
- uint16_t adc_val_1;
- int main(void)
- {
- #define KEY_ON 0 //
- // Button
- Button_2 = 34;//get_pin(34)// 22
- Button_3 = 7;//get_pin(7); 21
- Button_4 = 15;//get_pin(15); 2
- rt_pin_mode(Button_2 , PIN_MODE_INPUT_PULLUP);
- rt_pin_mode(Button_3 , PIN_MODE_INPUT_PULLUP);
- rt_pin_mode(Button_4 , PIN_MODE_INPUT_PULLUP);
- Button_Create("Button3",
- &Button3,
- Read_Button3_Level,
- KEY_ON);
- Button_Attach(&Button3,BUTTON_DOWM,Btn3_Dowm_CallBack); //单击
- Button_Attach(&Button3,BUTTON_DOUBLE,Btn3_Double_CallBack); //双击
- // Button_Attach(&Button3,BUTTON_CONTINUOS,Btn3_Continuos_CallBack); //连按
- // Button_Attach(&Button3,BUTTON_CONTINUOS_FREE,Btn3_ContinuosFree_CallBack); //连按释放
- Button_Attach(&Button3,BUTTON_LONG,Btn3_Long_CallBack); //长按
- Button_Create("Button2",
- &Button2,
- Read_Button2_Level,
- KEY_ON);
- Button_Attach(&Button2,BUTTON_DOWM,Btn2_Dowm_CallBack); //单击
- Button_Attach(&Button2,BUTTON_DOUBLE,Btn2_Double_CallBack); //双击
- // Button_Attach(&Button2,BUTTON_CONTINUOS,Btn2_Continuos_CallBack); //连按
- // Button_Attach(&Button2,BUTTON_CONTINUOS_FREE,Btn2_ContinuosFree_CallBack); //连按释放
- Button_Attach(&Button2,BUTTON_LONG,Btn2_Long_CallBack); //长按
- Button_Create("Button4",
- &Button4,
- Read_Button4_Level,
- KEY_ON);
- Button_Attach(&Button4,BUTTON_DOWM,Btn4_Dowm_CallBack); //单击
- Button_Attach(&Button4,BUTTON_DOUBLE,Btn4_Double_CallBack); //双击
- // Button_Attach(&Button4,BUTTON_CONTINUOS,Btn4_Continuos_CallBack); //连按
- // Button_Attach(&Button4,BUTTON_CONTINUOS_FREE,Btn4_ContinuosFree_CallBack); //连按释放
- Button_Attach(&Button4,BUTTON_LONG,Btn4_Long_CallBack); //长按
- Get_Button_Event(&Button4);
- Get_Button_Event(&Button3);
- Get_Button_Event(&Button2);
- pin_r = 10;//rt_pin_get(10);
- pin_g = 11;//rt_pin_get(11);
- pin_b = 12;//rt_pin_get(12);
- pin_4= 13;//rt_pin_get(13);
- pinout_19= 19;//out_rt_pin_get(19);
- rt_pin_mode(pin_r, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_g, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_b, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_4, PIN_MODE_OUTPUT);
- rt_pin_mode(pinout_19, PIN_MODE_OUTPUT);
- rt_kprintf("Hello, worldn");
- a=10;b=10;c=10;//pin_rgb不亮
- a_v=5;b_v=5;c_v=5;//pin_rgb允许
- // _irrx_hw_init();
- pwm_init();
- mind_n_key1 = 0;
- // uint8_t pin_count = 0;
- // while (1)
- // {
- //
- // rt_pin_write(pin_r, pin_count&0x1);
- // rt_pin_write(pin_g,( pin_count>>1)&0x1);
- // rt_pin_write(pin_b, ( pin_count>>2)&0x1);
- // rt_thread_mdelay(50);
- // rt_pin_write(pin_r, 0x1);
- // rt_pin_write(pin_g, 0x1);
- // rt_pin_write(pin_b,0x1);
- // rt_thread_mdelay(500);
- // pin_count++;
- // }
- //adc
- /* system clocks configuration */
- rcu_config();
- /* configure systick */
- //systick_config();
- /* ADC configuration */
- adc_config();
- /* USART configuration */
- //gd_eval_com_init(EVAL_COM1);
- //adc
- //adc
- /* ADC software trigger enable */
- adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
- /* delay a time in milliseconds */
- rt_thread_mdelay(2000);
-
- /* value convert */
- temperature = (1.43 - ADC_IDATA0(ADC0)*3.3/4096) * 1000 / 4.3 + 25;
- vref_value = (ADC_IDATA1(ADC0) * 3.3 / 4096);
-
- /* value print */
- printf(" the temperature data is %2.0f degrees Celsiusrn", temperature);
- printf(" the reference voltage data is %5.3fV rn", vref_value);
- printf(" rn");
- //*! disable the temperature sensor and Vrefint channel
- adc_tempsensor_vrefint_disable();
- adc_init();//pc3
- //adc
- while (1){
- /* ADC software trigger enable */
- adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
- /* delay a time in milliseconds */
- rt_thread_mdelay(2000);
-
- /* value convert */
- adc_val_1 = ADC_IDATA0(ADC0); //pc3 IN13
- // adc_val_2 = ADC_IDATA1(ADC0);//PC5 IN15
-
- /* value print */
- printf(" adc val 1 is 0x%xrn", adc_val_1);
- // printf(" adc val 2 is 0x%xrn", adc_val_2);
- printf(" rn");
-
- rt_thread_mdelay(500);
- if(bootCount==0x0a55aa00){
- bootCount=0x0a55aa01; //启动DeepSleep
- // endProducing= clock_cpu_millisecond();
- }
- if(bootCount==0x0a55aa01){
- rt_thread_mdelay(90000);
- // if(clock_cpu_millisecond() - endProducing > 90000){
- // xTaskCreate(
- // DF_A,
- // "DF_A", /* String with name of task. */
- // 20000, /* Stack size in words. */
- // NULL, /* Parameter passed as input of the task */
- // 2, /* Priority of the task. */
- // NULL);
- tid_dfa = rt_thread_create( "DF_A", DF_A, RT_NULL, 1024 ,6 , 2 );
- /* 如果获得线程控制块,启动这个线程 */
- if (tid_dfa != RT_NULL)
- rt_thread_startup(tid_dfa);
- // _DF_A_hw_init
- // }
- }
- if(mind_n_key1==2) {
- // digitalWrite(OUT_PWM, LOW);
- rt_pin_write(pinout_19,PIN_LOW);
- }
- if(bootCount==1) {
- rt_thread_mdelay(6); //delay(6);
- bootCount=0;
- if (mind_n_key1==0) {
- // xTaskCreate(
- // DF_A,
- // "DF_A", /* String with name of task. */
- // 20000, /* Stack size in words. */
- // NULL, /* Parameter passed as input of the task */
- // 2, /* Priority of the task. */
- // NULL);
- tid_dfa = rt_thread_create( "DF_A", DF_A, RT_NULL, 1024 ,6 , 2 );
- /* 如果获得线程控制块,启动这个线程 */
- if (tid_dfa != RT_NULL)
- rt_thread_startup(tid_dfa);
- // _DF_A_hw_init
- }
- if (mind_n_key1==1) {
- // xTaskCreate(
- // DF_B,
- // "DF_B", /* String with name of task. */
- // 10000, /* Stack size in words. */
- // NULL, /* Parameter passed as input of the task */
- // 2, /* Priority of the task. */
- // NULL);
- tid_dfb = rt_thread_create( "DF_B", DF_B, RT_NULL, 1024 ,6 , 2 );
- /* 如果获得线程控制块,启动这个线程 */
- if (tid_dfb!= RT_NULL)
- rt_thread_startup(tid_dfb);
- // _DF_A_hw_init
- }
- }
- }
- }
- // 自定义函数
- void DF_B(void * parameter) {
- uint16_t mind_a0_on, mind_a0_on1;
- // Serial.print("Created task DF_b: Executing on core ");
- // Serial.println(xPortGetCoreID());
- while (mind_n_key1==1) {
- mind_a0_on=0;
- for(b=0;b<8;b++){//检测双击 循环10次200ms
- // startProducing = ESP.getCycleCount();//开始时间
- // mind_a0_on1=analogRead(IN_0);
- // mind_a0_on+=mind_a0_on1;
- // endProducing = ESP.getCycleCount();//结束时间
- // Serial.print("mind_a0_on input A0 time all时间(CycleCount): ");
- // Serial.println(( endProducing - startProducing));//写入耗费时间(CycleCount)
- // Serial.printf("DF_b(analogRead(IN_0))%d: %d %d n",b,mind_a0_on1,mind_a0_on);//
- // Serial.println("");
- }
- DF_onoff(mind_a0_on>>5, 1023, 1);
- }
- bootCount = 1;
- // Serial.println("删除DF_b");
- // vTaskDelete( NULL ); //任务的删除
- // rt_thread_delete();//任务的删除
- }
- void DF_onoff(uint16_t mind_n_on, uint16_t mind_n_off, uint8_t mind_n_key) {
- // 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);//
- while ((mind_n_off>0)) {
- while ((mind_n_on>0)) {
- // digitalWrite(OUT_PWM, HIGH);
- // delay(6);
- mind_n_off -= 1;
- mind_n_on -= 1;
- if (!(mind_n_key==mind_n_key1)) {
- mind_n_off = 0;
- mind_n_on = 0;
- bootCount = 1;
- }
- }
- if ((mind_n_off>0)) {
- // digitalWrite(OUT_PWM, LOW);
- // delay(6);
- mind_n_off -= 1;
- if (!(mind_n_key==mind_n_key1)) {
- mind_n_off = 0;
- mind_n_on = 0;
- bootCount = 1;
- }
- }
- }
- }
- void DF_A(void * parameter) {
- // Serial.print("Created task DF_A: Executing on core ");
- // Serial.println(xPortGetCoreID());
- // startProducing = millis();//开始时间
- if(bootCount==0x0a55aa01){
- bootCount = 0;
- uint8_t mind_n_Asn = 80;
- // Serial.print("DF_A to 50% start Producing time 时间(ms): ");
- // Serial.println(( millis() - startProducing));//写入耗费时间(ms)
- while (!(mind_n_Asn==0)) {
- DF_onoff(490, 1023, 0);
- if (mind_n_key1==0) {
- mind_n_Asn -= 1;
- }
- else {
- bootCount = 1;
- mind_n_Asn = 0;
- }
- }
- }
- else{
- mind_n_As1 = 10;
- while (mind_n_As1>0) {
- DF_onoff(1023, 1023, 0);
- if (mind_n_key1==0) {
- mind_n_As1 -= 1;
- }
- else {
- mind_n_As1 = 0;
- bootCount = 1;
- }
- }
- if (mind_n_key1==0) {
- // Serial.print("DF_A 启动1分 时间(ms): ");
- // Serial.println(( millis() - startProducing));//写入耗费时间(ms)
- // Serial.print("启动1.5分连续 :");
- bootCount=0x0a55aa00; //启动DeepSleep
- // Serial.println(bootCount);
- // delay(1);
- // Serial.println("删除DF_A");
- // vTaskDelete( NULL ); //任务的删除
- }
- }
- // endProducing = millis();//结束时间
- // Serial.print("Producing time all时间(ms): ");
- // Serial.println(( endProducing - startProducing));//写入耗费时间(ms)
- if (mind_n_key1==0) {
- mind_n_key1 = 2;
- }
- // Serial.println("删除DF_A");
- // vTaskDelete( NULL ); //任务的删除
- }
- /*!
- brief configure the different system clocks
- param[in] none
- param[out] none
- retval none
- */
- void rcu_config(void)
- {
- /* enable ADC clock */
- rcu_periph_clock_enable(RCU_ADC0);
- /* config ADC clock */
- rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
- }
- /*!
- brief configure the ADC peripheral
- param[in] none
- param[out] none
- retval none
- */
- void adc_config(void)
- {
- /* ADC SCAN function enable */
- adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);
- /* ADC trigger config */
- adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE);
- /* ADC data alignment config */
- adc_data_alignment_config(ADC0,ADC_DATAALIGN_RIGHT);
- /* ADC mode config */
- adc_mode_config(ADC_MODE_FREE);
- /* ADC channel length config */
- adc_channel_length_config(ADC0,ADC_INSERTED_CHANNEL,2);
- /* ADC temperature sensor channel config */
- adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5);
- /* ADC internal reference voltage channel config */
- adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5);
- /* ADC external trigger enable */
- adc_external_trigger_config(ADC0,ADC_INSERTED_CHANNEL,ENABLE);
- /* ADC temperature and Vrefint enable */
- adc_tempsensor_vrefint_enable();
-
- /* enable ADC interface */
- adc_enable(ADC0);
- rt_thread_mdelay(1);
- /* ADC calibration and reset calibration */
- adc_calibration_enable(ADC0);
- }
- // 事件回调函数
- //宏: [RT_USING_CPUTIME]
- //使能此选项时,BSP 应提供 rt_clock_cputime_ops 为 CPU 时间,通过
- //const static struct rt_clock_cputime_ops _ops = {...};
- //clock_cpu_setops(&_ops);
- //
- //然后开发者能够使用高精度的时钟计数:
- //
- //ts1 = clock_cpu_gettime();
- //ts2 = clock_cpu_gettime();
- //
- ///* and get the ms of delta tick with API: */
- //ms_tick = clock_cpu_millisecond(t2 - t1);
- //us_tick = clock_cpu_microsecond(t2 - t1);
- void adc_init(void)
- {
- //adc 引脚初始化
-
- /* 使能 GPIOC clock */
- rcu_periph_clock_enable(RCU_GPIOC);
-
- /* 设置PC3 引脚为模拟输入 */
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_3 );
- //adc 引脚初始化
-
- /* 使能ADC0 时钟源 */
- rcu_periph_clock_enable(RCU_ADC0);
- /* 配置预分频 */
- rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
-
- /* ADC 扫描模式打开 */
- adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);
- /* ADC 触发方式配置 不需要外部触发 */
- adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE);
- /* 数据右对齐 */
- adc_data_alignment_config(ADC0,ADC_DATAALIGN_RIGHT);
- /* ADC 模式配置,所有ADC独立工作 */
- adc_mode_config(ADC_MODE_FREE);
- /* ADC 转换通道大小配置,2个通道 */
- adc_channel_length_config(ADC0,ADC_INSERTED_CHANNEL,1);
- /* ADC 通道配置 */
- adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_13, ADC_SAMPLETIME_239POINT5);
- /* ADC 通道配置 */
- //adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_15, ADC_SAMPLETIME_239POINT5);
- /* ADC 插入组使能 */
- adc_external_trigger_config(ADC0,ADC_INSERTED_CHANNEL,ENABLE);
-
- /* 使能ADC */
- adc_enable(ADC0);
- rt_thread_mdelay(1);// delay_1ms(1);
- /* ADC 校准 */
- adc_calibration_enable(ADC0);
- }
复制代码
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口信息:
达到预期。
0
|
|
|
|