STM32入门(1)

STM32入门(1)

项目1:实现LED灯闪烁

// 项目:实现LED灯闪烁

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

int main()
{
	// 开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	
	// 配置GPIOC_13
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	// 最小系统板 PC_13口LED灯低电平点亮
	//GPIO_SetBits(GPIOC,GPIO_Pin_13);      // 设置为高电平  LED熄灭
	//GPIO_ResetBits(GPIOC,GPIO_Pin_13);		// 设置为低电平  LED点亮
	
	
	while(1)
	{
		GPIO_ResetBits(GPIOC,GPIO_Pin_13);		// 设置为低电平  LED点亮
		Delay_ms(500);												// 延时
		GPIO_SetBits(GPIOC,GPIO_Pin_13);      // 设置为高电平  LED熄灭
		Delay_ms(500);									      // 延时
	}
}

项目2:实现LED流水灯

// 项目:实现LED流水灯

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

int main()
{
	// 开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	// 配置GPIOA
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	// GPIO_Init(); 函数初始化后默认引脚为低电平
	// 硬件电路设计的是低电平点亮LED,所以上电后会出现所有LED被点亮
	
	// 将 GPIOA 端口的全部16个引脚都设为高电平
	// 0xFFFF 的二进制是 1111 1111 1111 1111,所有位都是1
	GPIO_Write(GPIOA, 0xFFFF);
	
	// GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);   // 低电平点亮PA0端口LED
	
	while(1)
	{
		// 实现流水灯
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
		GPIO_WriteBit(GPIOA,GPIO_Pin_1,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_1,Bit_SET);
		GPIO_WriteBit(GPIOA,GPIO_Pin_2,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_2,Bit_SET);
		GPIO_WriteBit(GPIOA,GPIO_Pin_3,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_3,Bit_SET);
		GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_SET);
	}
}	

项目3:蜂鸣器(低电触发)

// 项目:蜂鸣器(低电触发)

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

#define BEEP_ON()   GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);    // 蜂鸣器鸣响
#define BEEP_OFF()  GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);      // 蜂鸣器关闭

int main() 
{
	// 开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	// 配置GPIOB_3端口
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	// GPIO_WriteBit(GPIOB,GPIO_Pin_3,Bit_RESET);
	// PB13 是JTDO接口,如果没有配置不能直接使用,换成PB12
	
	// GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);    	 // 蜂鸣器鸣响
	
	while(1)
	{
		//GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);    // 蜂鸣器鸣响
		//GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);    	 // 蜂鸣器关闭
		
		BEEP_ON();
		Delay_ms(100);
		BEEP_OFF();
		Delay_ms(100);
		BEEP_ON();
		Delay_ms(100);
		BEEP_OFF();
		Delay_ms(500);
	}
}

项目4:按键控制LED

// 项目:按键控制LED

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

uint8_t KeyNum = 0;        // 设置键码值变量

int main()
{
	// 初始化 LED模块、按键模块
	LED_Init();
	Key_Init();
	
	
	while(1)
	{
		KeyNum = Key_GetNum();
		if(KeyNum == 1)
		{
			LED1_turn();
		}
		if(KeyNum == 2)
		{
			LED2_turn();
		}
	}
	
}
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

/**
  * 函    数:按键初始化
  * 参    数:无
  * 返 回 值:无
  */
void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);      // 开启时钟
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;             // 设置为下拉模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

/**
  * 函    数:按键获取键码
  * 参    数:无
  * 返 回 值:按下按键的键码值,范围:0~2,返回0代表没有按键按下
	* 注意事项:此函数是阻塞式操作,当按键按住不放时,函数会卡住,直到按键松手
  */
uint8_t Key_GetNum(void)
{
	uint8_t KeyNum = 0;   // 定义变量,默认键码值为0
	
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == SET)
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == SET);
		Delay_ms(20);
		KeyNum = 1;
	}
	
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == SET)
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == SET);
		Delay_ms(20);
		KeyNum = 2;
	}
	
	return KeyNum;
}

#ifndef __KEY_H_
#define __KEY_H_

void Key_Init(void);
uint8_t Key_GetNum(void);

#endif

#include "stm32f10x.h"                  // Device header

/**
  * 函    数:LED初始化
  * 参    数:无
  * 返 回 值:无
  */
void LED_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);   // 开启GPIOA时钟
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
}

/**
  * 函    数:LED1 (高电平)点亮
  * 参    数:无
  * 返 回 值:无
  */
void LED1_ON(void)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
}

/**
  * 函    数:LED1 (低电平)熄灭
  * 参    数:无
  * 返 回 值:无
  */
void LED1_OFF(void)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
}

/**
  * 函    数:LED1 电平反转
  * 参    数:无
  * 返 回 值:无
  */
void LED1_turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0) == SET)    // 读取输出寄存器,如果为高电平
	{
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);					 // 则修改为低电平
	}
	else
	{
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);						 // 否则修改为高电平
	}
}


/**
  * 函    数:LED2 (高电平)点亮
  * 参    数:无
  * 返 回 值:无
  */
void LED2_ON(void)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_1,Bit_SET);
}

/**
  * 函    数:LED2 (低电平)熄灭
  * 参    数:无
  * 返 回 值:无
  */
void LED2_OFF(void)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_1,Bit_RESET);
}

/**
  * 函    数:LED1 电平反转
  * 参    数:无
  * 返 回 值:无
  */
void LED2_turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1) == SET)    // 读取输出寄存器,如果为高电平
	{
		GPIO_WriteBit(GPIOA,GPIO_Pin_1,Bit_RESET);					 // 则修改为低电平
	}
	else
	{
		GPIO_WriteBit(GPIOA,GPIO_Pin_1,Bit_SET);						 // 否则修改为高电平
	}
}


#ifndef __LED_H_
#define __LED_H_

void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED1_turn(void);

void LED2_ON(void);
void LED2_OFF(void);
void LED2_turn(void);


#endif

#include "stm32f10x.h"

/**
  * @brief  微秒级延时
  * @param  xus 延时时长,范围:0~233015
  * @retval 无
  */
void Delay_us(uint32_t xus)
{
	SysTick->LOAD = 72 * xus;				//设置定时器重装值
	SysTick->VAL = 0x00;					//清空当前计数值
	SysTick->CTRL = 0x00000005;				//设置时钟源为HCLK,启动定时器
	while(!(SysTick->CTRL & 0x00010000));	//等待计数到0
	SysTick->CTRL = 0x00000004;				//关闭定时器
}

/**
  * @brief  毫秒级延时
  * @param  xms 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_ms(uint32_t xms)
{
	while(xms--)
	{
		Delay_us(1000);
	}
}
 
/**
  * @brief  秒级延时
  * @param  xs 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_s(uint32_t xs)
{
	while(xs--)
	{
		Delay_ms(1000);
	}
} 

#ifndef __DELAY_H
#define __DELAY_H

void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);

#endif

项目5:光敏电阻控制蜂鸣器

// 项目:光敏传感器控制蜂鸣器

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

uint8_t LightS_Num = 0;        // 设置光敏传感器状态变量

int main()
{
	// 初始化 光敏传感器模块、蜂鸣器模块
	LightSensor_Init();
	Buzzer_Init();
	
	while(1)
	{
		LightS_Num = LightSensor_Get();
		if(LightS_Num == 1)
		{
			Buzzer_ON();
		}
		else
		{
			Buzzer_OFF();
		}
	}
}


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

#define BEEP_ON()   GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);    // 蜂鸣器鸣响
#define BEEP_OFF()  GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);      // 蜂鸣器关闭


/**
  * 函    数:蜂鸣器初始化
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_Init(void)
{
	// 开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	// 配置GPIOB_3端口
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	BEEP_OFF();    // 把GPIOB_12设置为高电平,防止蜂鸣器上电鸣响
}
	

/**
  * 函    数:开启蜂鸣器
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_ON(void)
{
	 BEEP_ON();
}


/**
  * 函    数:关闭蜂鸣器
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_OFF(void)
{
	 BEEP_OFF();
}

#ifndef __BUZZER_H_
#define __BUZZER_H_

void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);

#endif

#include "stm32f10x.h"                  // Device header

/**
  * 函    数:光敏传感器初始化
  * 参    数:无
  * 返 回 值:无
  */
void LightSensor_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;     // 设置为下拉模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

/**
  * 函    数:获取当前光敏传感器输出的高低电平
  * 参    数:无
  * 返 回 值:光敏传感器输出的高低电平,范围:0/1
  */
uint8_t LightSensor_Get(void)
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);     // 返回GPIOB_13端口的输入值
}

#ifndef __LIGHT_SENSOR_H_
#define __LIGHT_SENSOR_H_

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

#endif

posted @ 2026-03-07 23:46  Q&25  阅读(0)  评论(0)    收藏  举报