嵌入式-点亮LED灯(基础版)

一、相关知识点

1、构造框架

void LED_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;       //构造函数框架
	//使能GPIO时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	//配置引脚
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;//0xffff
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    //输出模式为推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			//输出速率
	GPIO_Init(GPIOC,&GPIO_InitStructure);								//初始化引脚
}

2、初始化函数

2.1、RCC_APB2PeriphClockCmd()函数

RCC_APB2PeriphClockCmd()函数是GPIOx对应的外设时钟,若使能GPIOB和GPIOC时钟如下
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB,ENABLE);

2.2、GPIO_Init()函数

GPIO_Init()函数的功能是初始化(配置)GPIO的模式和速度,也就是设置相应的GPIO的CRL和CRH寄存器值
原型是:
void GPIO_Init(GPIO_TypeDef* GPIOx,GPIO_InitTypeDef* GPIO_InitStruct);
第一个参数:GPIO_TypeDef类型指针变量,用于确定是哪一个GPIO,GPIOx取值是GPIOA、GPIOB、GPIOC、GPIOD、GPIOE、GPIOF和GPIOG;
第一个参数:GPIO_InitTypeDef类型指针变量,用于确定GPIOx的对应引脚以及该引脚的模式和输出最大速度

3、输出函数

3.1、GPIO_ReadInputDataBit()函数

GPIO_ReadInputDataBit()函数是读取指定I/O口对应引脚值,也就是读取IDR寄存器的值。原型是:
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);
第一个参数同GPIO_Init()函数一样,第二个参数是读取,GPIOx的对应引脚值。如:
GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6); 

3.2、GPIO_ReadInputData()函数

GPIO_ReadInputData()函数读取指定的I/O口16个引脚的输入值,也是读取IDR寄存器的值,原型是:
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
如:temp = GPIO_ReadInputData(GPIOB)
IDR寄存器读取GPIO输入值的代码是:temp = GPIOx->IDR;

3.3、GPIO_ReadOutputDataBit()和GPIO_ReadOutputData()函数

GPIO_ReadOutputDataBit()函数是读取指定I/O口某个引脚的输出值,也就是读取寄存器ODR相应位的值。原型是:
uint8_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);
GPIO_ReadOutputData()函数是读取指定I/O口16个引脚的输出值,也就是读取寄存器ODR值。原型是:
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
如:读取某个引脚的输出值 GPIO_ReadOutputData(GPIOC,GPIO_Pin_5);
读取所有引脚的输出值 GPIO_ReadOutputData(GPIOC);

3.4、GPIO_SetBits()和GPIO_ResetBits()函数

GPIO_SetBits()和GPIO_ResetBits()函数用来设置指定引脚输出高电平和低电平,也就是设置寄存器BSRR、BRR的值,原型是:
void GPIO_SetBits(GPIO_TypeDef*GPIOx,uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef*GPIOx,uint16_t GPIO_Pin);
如: GPIO_SetBits(GPIOC, GPIO_Pin_5);          //点亮引脚
	GPIO_ResetBits(GPIOC, GPIO_Pin_5);        //熄灭引脚

3.5、GPIO_WriteBit()和GPIO_Write()函数

GPIO_WriteBit()函数向指定I/O口的引脚写0或者写1,也就是向寄存器ODR相应位写0或写1  原型是:
void GPIO_WriteBit(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin,BitAction BitVal);
GPIO_Write()函数向指定I/o口写数据,也就是像寄存器ODR写数据,原型是:
void GPIO_Write(GPIO_TypeDef*GPIOx,   uint16_t PortVal);
如: GPIO_WriteBit(GPIOC,GPIO_Pin_8,1);         //只能对一个引脚置0或置1
    GPIO_Write(GPIOC,0x0fffe);
	GPIO_Write(GPIOC,GPIO_Pin_8|GPIO_Pin_5);		//可以同时对多个引脚置1
#include "stm32f10x.h"
uint16_t temp,i;
GPIO_InitTypeDef GPIO_InitStructure;

void Delay(unsigned int count)
{
	unsigned int i;
	for(;count!=0;count--)
	{
		i=5000;
		while(i--);
	}
}
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_2 | GPIO_Pin_3|GPIO_Pin_4 | GPIO_Pin_5|GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_SetBits(GPIOC,GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_2 | GPIO_Pin_3|GPIO_Pin_4 | GPIO_Pin_5|GPIO_Pin_6 | GPIO_Pin_7);
	while(1)
	{
		for(i=0;i<3;i++)
		{
			//倒计时
			//9
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			//8
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			//7
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
			Delay(1000);	
			//6
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			//5
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_6);
			Delay(1000);
			//4
			GPIO_ResetBits(GPIOC,GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_1);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_1);
			Delay(1000);
			//3
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6);
			Delay(1000);
			//2
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_6|GPIO_Pin_1);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_6|GPIO_Pin_1);
			Delay(1000);
			//1
			GPIO_ResetBits(GPIOC,GPIO_Pin_1|GPIO_Pin_2);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_1|GPIO_Pin_2);
			Delay(1000);	
			//0
			GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
			Delay(1000);
			/*
			跑马灯
			GPIO_ResetBits(GPIOC,GPIO_Pin_0);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_1);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_2);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_3);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_4);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_5);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_6);
			Delay(100);
			GPIO_ResetBits(GPIOC,GPIO_Pin_7);
			Delay(100);
			
			GPIO_SetBits(GPIOC,GPIO_Pin_0);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_1);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_2);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_3);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_4);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_5);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_6);
			Delay(100);
			GPIO_SetBits(GPIOC,GPIO_Pin_7);
			Delay(100);
			*/
			
			/*GPIO_ResetBits(GPIOC,GPIO_Pin_2);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_2);
			Delay(1000);
			GPIO_ResetBits(GPIOC,GPIO_Pin_3);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_3);
			Delay(1000);
			GPIO_ResetBits(GPIOC,GPIO_Pin_4);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_4);
			Delay(1000);
			GPIO_ResetBits(GPIOC,GPIO_Pin_5);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_5);
			Delay(1000);
			GPIO_ResetBits(GPIOC,GPIO_Pin_6);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_6);
			Delay(1000);
			GPIO_ResetBits(GPIOC,GPIO_Pin_7);
			Delay(1000);
			GPIO_SetBits(GPIOC,GPIO_Pin_7);
			Delay(1000);*/
		}
	}
}
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值