unisqhzgok 发表于 2022-4-16 16:20

03.点亮LED及按键测试

<p>打开Demo包中02_GPIO_Key_Polling_mode文件夹下Keil程序 &nbsp;我们在原程序的基础上添加如下内容</p>

<p>int main(void)<br />
{<br />
&nbsp; &nbsp; uint8_t count;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;systick_config();</p>

<p>&nbsp; &nbsp; /* enable the LED2 GPIO clock */<br />
&nbsp; &nbsp; rcu_periph_clock_enable(RCU_GPIOA);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;rcu_periph_clock_enable(RCU_GPIOC);<br />
&nbsp; &nbsp; /* configure LED2 GPIO pin */<br />
&nbsp; &nbsp; gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7|GPIO_PIN_8);<br />
&nbsp; &nbsp; gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_8);<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7);<br />
&nbsp; &nbsp; gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7);<br />
&nbsp; &nbsp; /* reset LED2 GPIO pin */<br />
&nbsp; &nbsp; gpio_bit_reset(GPIOA,GPIO_PIN_7);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;gpio_bit_reset(GPIOA,GPIO_PIN_8);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;gpio_bit_reset(GPIOC,GPIO_PIN_7);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;gpio_bit_reset(GPIOC,GPIO_PIN_6);<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp; /* configure key pin as input */<br />
&nbsp; &nbsp; gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);</p>

<p>&nbsp; &nbsp; while(1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; /* check whether the key is pressed */<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_0)) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delay_1ms(100);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* check whether the key is pressed */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_0)) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch(count)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;case 0:gpio_bit_toggle(GPIOA, GPIO_PIN_7);break;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;case 1:gpio_bit_toggle(GPIOA, GPIO_PIN_8);break;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;case 2:gpio_bit_toggle(GPIOC, GPIO_PIN_6);break;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;case 3:gpio_bit_toggle(GPIOC, GPIO_PIN_7);break;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;count++;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(count == 4)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;count=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/* toggle LED2 GPIO pin */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_0)) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
}</p>

<p>&nbsp;编译完代码无误后,下载程序</p>

<p>这时我们每按下一次按键,LED1-LED4,依次点亮然后熄灭</p>

<p>&nbsp;Demo对应的例程有些少,接下来会围绕IIC和spi进行简单测试</p>

lugl4313820 发表于 2022-4-16 17:45

<p>看到按键的例程,我又想说几句。</p>

<p>对于这种在主程序中,用whil状态下检测按键,在现实中的项目没有实际意义。</p>

<p>希望检测按键是在定时器、中断的下的状态机实现,那才有实际意义。</p>

<p>对不起呀!</p>

wangerxian 发表于 2022-4-17 07:57

<p>在标题加上开发板的型号名称,这样会更好一些~</p>
页: [1]
查看完整版本: 03.点亮LED及按键测试