设计说明:
依次实现以下显示模式:
(1)全亮并闪烁4次。
(2)点亮一个发光管,右循环。
(3)点亮一个发光管,左循环。
(4)单数LED与双数LED交替点亮4次。
代码如下:
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
void Delay(unsigned count)//延时函数
{
int i,j;
for(i=0;i<50000;i++)
{
for(j=0;j<count;j++);
}
}
void LED_GPIO_Config(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void LED_GPIO_A(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
}
int main(void)
{
int i;
LED_GPIO_Config();
LED_GPIO_A();
while(1)
{ for(i=0;i<4;i++)//第一个模式
{
GPIO_ResetBits(GPIOC,GPIO_Pin_10);
GPIO_ResetBits(GPIOC,GPIO_Pin_11);
GPIO_ResetBits(GPIOC,GPIO_Pin_12);
GPIO_ResetBits(GPIOA,GPIO_Pin_15);
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_10) ;
GPIO_SetBits(GPIOC,GPIO_Pin_11) ;
GPIO_SetBits(GPIOC,GPIO_Pin_12);
GPIO_SetBits(GPIOA,GPIO_Pin_15) ;
Delay(100);
}
for(i=0;i<2;i++)//第二个模式
{
GPIO_ResetBits(GPIOC,GPIO_Pin_12) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
GPIO_ResetBits(GPIOC,GPIO_Pin_11) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_11);
GPIO_ResetBits(GPIOC,GPIO_Pin_10);
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_10);
GPIO_ResetBits(GPIOA,GPIO_Pin_15) ;
Delay(100);
GPIO_SetBits(GPIOA,GPIO_Pin_15);
Delay(100);
}
for(i=0;i<2;i++)//第三个模式
{
GPIO_ResetBits(GPIOA,GPIO_Pin_15);
Delay(100);
GPIO_SetBits(GPIOA,GPIO_Pin_15);
GPIO_ResetBits(GPIOC,GPIO_Pin_10) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_10);
GPIO_ResetBits(GPIOC,GPIO_Pin_11) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_11);
GPIO_ResetBits(GPIOC,GPIO_Pin_12) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
Delay(100);
}
for(i=0;i<5;i++)//第四个模式
{
GPIO_ResetBits(GPIOC,GPIO_Pin_11);
GPIO_ResetBits(GPIOA,GPIO_Pin_15) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_11);
GPIO_SetBits(GPIOA,GPIO_Pin_15);
GPIO_ResetBits(GPIOC,GPIO_Pin_12);
GPIO_ResetBits(GPIOC,GPIO_Pin_10) ;
Delay(100);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
GPIO_SetBits(GPIOC,GPIO_Pin_10);
Delay(100);
}
Delay(100);
}
}
这样就可以实现所有的显示模式!