STM32初学之GPIO流水灯库函数版

#include "stm32f10x.h"
#include "led.h"

#define K1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)
#define K2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)

void delay(uint16_t c);

int main(void)
{
GPIO_InitTypeDef GPIO_Initkey;
GPIO_Initkey.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Initkey.GPIO_Pin=GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_Initkey);

GPIO_Initkey.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Initkey.GPIO_Pin=GPIO_Pin_1;
GPIO_Init(GPIOB,&GPIO_Initkey);

LED_Init();


while(1)
{
if(K1==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_0);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
}
if(K2==0)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
GPIO_SetBits(GPIOB,GPIO_Pin_0);
}
// GPIO_ResetBits(GPIOA,GPIO_Pin_0);
// GPIO_SetBits(GPIOB,GPIO_Pin_0);
// delay(10);
// GPIO_SetBits(GPIOA,GPIO_Pin_0);
// GPIO_ResetBits(GPIOB,GPIO_Pin_0);
// delay(10);
}
}

void delay(uint16_t c)
{
uint16_t a,b;
for(; c>0; c--)
for(a=1000; a>0; a--)
for(b=1000; b>0; b--);
}


// led.c

#include "led.h"
#include"stm32f10x.h"

void LED_Init(void)
{
GPIO_InitTypeDef GPIO_Initled;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);

GPIO_Initled.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Initled.GPIO_Pin=GPIO_Pin_0;
GPIO_Initled.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initled);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);

GPIO_Initled.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Initled.GPIO_Pin=GPIO_Pin_0;
GPIO_Initled.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_Initled);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
}

// led.h

#ifndef __LED_H__
#define __LED_H__

void LED_Init(void);

#endif

相关文档
最新文档