学习目的:本设计是基于STM32F103C8T6单片机完成入门点灯设计。根据STM32F103C8T6的原理图,可知LED2与CPU的PC13相连,当PC13=1(高电平)时,LED2呈熄灭状态;当PC13=0(低电平)时,LED2呈点亮状态。
参考代码如下:- <font face="微软雅黑" size="3">//main.c
- #include "stm32f10x.h"
- #include "led.h"
- #include "delay.h"
- int main(void)
- {
- LED_Init();
- while(1)
- {
- GPIO_ResetBits(GPIOC,GPIO_Pin_13);
- delay(200);
- GPIO_SetBits(GPIOC,GPIO_Pin_13);
- delay(200);
- }
- }</font>
复制代码- <font face="微软雅黑" size="3">//led.c
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStruct);
- GPIO_SetBits(GPIOC,GPIO_Pin_13);
- }</font>
复制代码- <font face="微软雅黑" size="3">//led.h
- #ifndef _LED_H
- #define _LED_H
- void LED_Init(void);
- #endif</font>
复制代码- <font face="微软雅黑" size="3">//delay.c
- #include "stdint.h"
- void delay(uint16_t timers)
- {
- uint16_t i,j;
- for(i=0;i<timers ;i++)
- {
- for(j=0;j<0xffff;j++)
- {}
- }
- }</font>
复制代码- <font face="微软雅黑" size="3">//delay.h
- #ifndef _DELAY_H
- #define _DELAY_H
- #include "stdint.h"
- void delay(uint16_t timers);
- #endif</font>
复制代码 |