一个只有一个LED外设的板子能做多少实验呢?待我一个个地探索。这个系列可能只有三两篇名也可能会有十多篇。敬请期待
配置好时钟之后,先试试最简单的点灯。
看下板子,可以看见LED1接在PA1上边。管脚输出高电平时候LED点亮。 于是有了这个跟STM32几乎完全一样的点灯程序:int main(void)
{
/* 熟悉的时钟使能 */
RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOB |
RCC_AHBPERIPH_GPIOA , ENABLE );
{
GPIO_InitPara GPIO_InitStructure;
/* 配置LED管脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_Init( GPIOA , &GPIO_InitStructure );
}
GPIO_SetBits(GPIOA,GPIO_PIN_1); //高电平,灯亮
while(1);
}
然后试试按键。读取KEY的状态,用LED作为回馈。当按键按下去时候,KED熄灭否则LED点亮。
int main(void)
{
/* Enable the GPIO_LED Clock */
RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA , ENABLE );
{
GPIO_InitPara GPIO_InitStructure;
.............................
//配置Key引脚
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN;
GPIO_Init( GPIOA , &GPIO_InitStructure );
}
while(1)
{
if(GPIO_ReadInputBit(GPIOA,GPIO_PIN_0)) //读取Key引脚状态
GPIO_SetBits(GPIOA,GPIO_PIN_1); //设置LED脚输出
else
GPIO_ResetBits(GPIOA,GPIO_PIN_1);
}
}
下期预告:
高级点的,Key控制LED亮灭反转。这里试试外部中断,用中断方式检测Key输入。ext......
我要赚赏金
