stm32下常用函数~基本外设

本文详细介绍了使用STM32进行基础硬件操作,包括GPIO配置、LED控制(点灯、闪烁)、流水灯、蜂鸣器响应、按键检测以及OLED屏幕显示的实例代码。
摘要由CSDN通过智能技术生成

学习江协科技stm32基于库函数开发记录一下
代码压缩包地址:code

1.点灯

RCC_APB2PeriphClockCmd()//时钟使能

GPIO_Init(GPIOx,&GPIO_InitStruct)//引脚初始化

GPIO_SetBits(GPIOC,GPIO_Pin_13);//设置高电平

GPIO_ResetBits(GPIOC,GPIO_Pin_13);//设置低电平

main.c

//P13脚点灯
#include "stm32f10x.h"                  // Device header
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//时钟使能
	
	GPIO_InitTypeDef GPIO_InitStruct;
	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);//高电平
	GPIO_ResetBits(GPIOC,GPIO_Pin_13);
	while(1)
	{
		;
	}
}

2.led闪烁

GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);//写引脚电平

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//时钟使能
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);//结构体定义
	
//	GPIO_SetBits(GPIOC,GPIO_Pin_13);//高电平
//	GPIO_ResetBits(GPIOC,GPIO_Pin_13);
	while(1)
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_0);
		Delay_ms(500);
		GPIO_SetBits(GPIOA,GPIO_Pin_0);
		Delay_ms(500);//gpio_setbits与gpio_resetbits
		
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
		Delay_ms(500);//gpio_write函数
		
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)0);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)1);
		Delay_ms(500);//强制类型转换
	}
}

3.流水灯

GPIO_Write(GPIOA, ~0x0001);//按字节写

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//时钟使能
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);//结构体定义

	while(1)
	{
		GPIO_Write(GPIOA, ~0x0001);//按字节写//0000 0000 0000 0001
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0002);//按字节写//0000 0000 0000 0010
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0004);//按字节写
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0008);//按字节写
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0010);//按字节写
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0020);//按字节写
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0040);//按字节写
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0080);//按字节写//0000 0000 1000 0000
		Delay_ms(100);
	}
}

4.蜂鸣器

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//时钟使能
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);//结构体定义

	while(1)
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB, GPIO_Pin_12);
		Delay_ms(100);
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(500);
	}
}

5.按键控制led

GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1)//读输出引脚状态

led.h

#ifndef __LED_H
#define __LED_H

void LED_Init(void);//引脚初始化
void LED1_ON(void);//led1点亮
void LED1_OFF(void);//led1熄灭
void LED1_Turn(void);//led1翻转
void LED2_ON(void);//led2点亮
void LED2_OFF(void);//led2熄灭
void LED2_Turn(void);//led2翻转

#endif

led.c

#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//时钟使能
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_2;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);//结构体定义
	
	GPIO_SetBits(GPIOA, GPIO_Pin_1 |GPIO_Pin_2);
}//led初始化~PA1与PA2脚
/
void LED1_ON(void)
{
	GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}//低电平点亮
void LED1_OFF(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_1);
}//高电平熄灭
void LED1_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)//读输出引脚状态
	{
		GPIO_SetBits(GPIOA , GPIO_Pin_1);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_1);
	}
}
//
void LED2_ON(void)
{
	GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}//低电平点亮
void LED2_OFF(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_2);
}//高电平熄灭
void LED2_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0)//读输出引脚状态
	{
		GPIO_SetBits(GPIOA , GPIO_Pin_2);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_2);
	}
}

GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)//读输入引脚状态

key.h

#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNum(void);
#endif

key.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
/*
按键初始化函数
功能:将PB1与PB11引脚初始化,上拉输入,按键按下低电平
形参:None
返回值:None
*/
void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//时钟使能
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_11;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);//结构体定义
}//PB1与PB11引脚接按键  
/*
获取按键状态函数
功能:获取按键状态并返回相应的参数
			1:P1引脚被拉低为低电平
			2:P11引脚被拉低为低电平
形参:None
返回值:数字
*/
uint8_t Key_GetNum(void)//接收按键状态
{
	uint8_t KeyNum = 0;
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)//读输入引脚状态
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)== 0);//读取引脚状态
		Delay_ms(20);//消抖
		KeyNum = 1;
	}
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11))
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11));
		Delay_ms(20);
		KeyNum = 2;
	}
	return KeyNum;
}

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "KEY.h"

uint8_t Key_num;//声明全局变量

int main()
{
	LED_Init();
	Key_Init();
	while(1)
	{
		Key_num = Key_GetNum();//获取按键状态赋值给Key_num
		if(Key_num == 1)
		{
			LED1_Turn();//声明时为高电平
		}
		if(Key_num == 2)
		{
			LED2_Turn();
		}
	}
}

6.光敏传感器与蜂鸣器

LightSensor.h

#ifndef __LIGHTSENSOR_H
#define __LIGHTSENSOR_H

void LightSensor_Init(void);
uint8_t LightSensor_Get(void);

#endif

LightSensor.c

#include "stm32f10x.h"                  // Device header

void LightSensor_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t LightSensor_Get(void)
{
	return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}

Buzzer.h

#ifndef __BUZZER_H
#define __BUZZER_H

void Buzzer_Init(void);
void Buzzer_On(void);
void Buzzer_Off(void);
void Buzzer_Turn(void);

#endif

Buzzer.c

#include "stm32f10x.h"                  // Device header

void Buzzer_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	GPIO_SetBits(GPIOB , GPIO_Pin_12);
}
void Buzzer_On(void)
{
	GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
void Buzzer_Off(void)
{
	GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
void Buzzer_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_12)== 0)
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_12);
	}
	else
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);
	}
}

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Buzzer.h"
#include "LightSensor.h"

uint8_t Key_num;//声明全局变量

int main()
{
	Buzzer_Init();
	LightSensor_Init();
	while(1)
	{
		if(LightSensor_Get() == 1)
		{
			Buzzer_On();
		}
		else
		{
			Buzzer_Off();
		}
	}
}

7.oled显示

直接添加文件

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "oled.h"

int main()
{
	OLED_Init();//oled初始化
	OLED_ShowChar(1, 1, 'c');//显示字符
	OLED_ShowString(1, 3 , "hello");//显示字符串
	OLED_ShowNum(2,1,123,3);//显示数字
	OLED_ShowSignedNum(2,7,-66,2);//显示有符号数字
	OLED_ShowHexNum(3,1,0xa5a5,4);//显示16进制数
	OLED_ShowBinNum(4,1,0x5a5a,16);//显示2进制数
	while(1)
	{
		
	}
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值