我是靠谱客的博主 聪慧眼睛,最近开发中收集的这篇文章主要介绍GPIO LED灯实验,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

GPIO配置

 1 //conf_gpio.h
 2 #ifndef __CONF_GPIO_H
 3 #define __CONF_GPIO_H
 4 
 5 #include "stm32f10x_gpio.h"
 6 #include "stm32f10x_rcc.h"
 7 
 8 void GPIO_Config(void);
 9  
10 #endif
 1 //conf_gpio.c
 2 #include "conf_gpio.h"
 3 
 4 void GPIO_Config(void)
 5 {        
 6     /*定义一个GPIO_InitTypeDef类型的结构体*/
 7     GPIO_InitTypeDef GPIO_InitStructure;
 8 
 9     /*开启GPIOC的外设时钟*/
10     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE); 
11 
12     /*选择要控制的GPIOC引脚*/                                                               
13       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;    
14 
15     /*设置引脚模式为通用推挽输出*/
16       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
17 
18     /*设置引脚速率为50MHz */   
19       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
20 
21     /*调用库函数,初始化GPIOC*/
22       GPIO_Init(GPIOA, &GPIO_InitStructure);          
23 }

主函数

 1 //main.c
 2 #include "stm32f10x.h"
 3 #include "conf_gpio.h"
 4 
 5 #define CLOCK 72/8 //主频=72M
 6 //unsigned char tab[] = {GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};//stm32_gpio.h中的宏定义,与下列数字对应
 7 uint16_t tab[] = {0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080};//这是每组GPIO 0-7引脚的寄存器配置值,通过或操作可实现同时驱动多个引脚
 8 //uint16_t tab[] = {0x0081,0x0042,0x0024,0x0018,0x000C,0x0030,0x0003,0x00C0};
 9 void delay_us(unsigned int us);
10 void delay_ms(unsigned int ms);
11 
12 int main(void)
13 {
14     int i;
15     GPIO_Config();
16     
17     while(1)
18     {
19         for(i=0;i<8;i++)
20         {
21             GPIO_ResetBits(GPIOA,tab[i]);
22             delay_ms(100);
23         }
24         for(i=0;i<8;i++)
25         {
26             GPIO_SetBits(GPIOA,tab[i]);
27             delay_ms(100);
28         }
29         for(i=7;i>=0;i--)
30         {
31             GPIO_ResetBits(GPIOA,tab[i]);
32             delay_ms(100);
33         }
34         for(i=7;i>=0;i--)
35         {
36             GPIO_SetBits(GPIOA,tab[i]);
37             delay_ms(100);
38         }
39     }
40 }
41 
42 /*------------------------------------------------------------
43                          us延时
44 ------------------------------------------------------------*/
45 void delay_us(unsigned int us)
46 {
47     u8 n;            
48     while(us--)for(n=0;n<CLOCK;n++);      
49 }
50 
51 /*------------------------------------------------------------
52                          ms延时
53 ------------------------------------------------------------*/
54 void delay_ms(unsigned int ms)
55 {
56     while(ms--)delay_us(1000);     
57 }

 

转载于:https://www.cnblogs.com/lenxvp/p/5362674.html

最后

以上就是聪慧眼睛为你收集整理的GPIO LED灯实验的全部内容,希望文章能够帮你解决GPIO LED灯实验所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部