STM32 F103 基础实验

这篇博客详细介绍了基于STM32微控制器的一系列实验,包括LED灯控制、定时器计数、矩阵按键功能实现以及OLED显示屏的初始化和汉字、BMP图像显示。通过实验代码,展示了如何操作GPIO、定时器、I2C通信以及串口通信,帮助读者理解STM32的基础应用。
摘要由CSDN通过智能技术生成

索引 

        【1】led灯实验

        【2】定时器计时

        【3】矩阵按键

        【4】OLED显示实验

                       [4.1]oled i2c驱动程序

                        [4.2]oled 写数据命令

                        [4.3]SSH1106 初始化程序

                        [4.4]oled 汉字显示程序

                        [4.5]oled BMP图像显示程序

        【5】

                        [4.1]USART串口实验

【1】LED灯

功能:点亮PC13LED灯

​#include "stm32f10x.h"                  // Device header
void Led()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//APB2时钟
	GPIO_InitTypeDef led;
	led.GPIO_Mode=GPIO_Mode_Out_PP;
	led.GPIO_Pin=GPIO_Pin_13;             //PC13
	led.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOC,&led);
}

​int main()
{
   Led();
    while(1)
    {
         GPIO_ResetBits(GPIOC,GPIO_Pin_13);//led低电平有效
    }
}

 【2】定时器

功能:实现每秒计数加1


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

void Timer_Init(unsigned int arr,unsigned int psc)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//开启APB1定时器3的时钟
	TIM_TimeBaseInitTypeDef Timer;
	
	Timer.TIM_ClockDivision=TIM_CKD_DIV1;
	Timer.TIM_CounterMode=TIM_CounterMode_Up;
	Timer.TIM_Period=arr;
	Timer.TIM_Prescaler=psc;
	TIM_TimeBaseInit(TIM3,&Timer);
	TIM_Cmd(TIM3,ENABLE);//打开定时器
	
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能定时器更新中断
}


void TIM_IRQNHandle(void)//定时器服务函数
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update))
	{
	
		TIM_ClearFlag(TIM3,TIM_IT_Update);//清除定时器标志位
		sec++;  //此处为定时器服务函数,此处在oled显示秒++
	}
	
}

int main()
{

    Timer_Init(7199,9999);  //((arr+1)*(psc+1)/72M)
	OLED_Init(); 
	OLED_Clear();//清屏
    Timer_Init(7199,9999);//((arr+1)*(psc+1)/CLK)
    while(1)
    {
       
        TIM_IRQNHandle();
		OLED_ShowNum(1,7,sec,2);//OLED显示函数
    }
}

 【3】矩阵按键

功能:实现S1-S16键值录入

C1-C4为行,R1-R4为列

接线:C1-C4接单片机引脚PA4-PA7;R1-R4依次接单片机引脚PB0,PB1,PB10,PB11

#include "stm32f10x.h"                  // Device header
#include <delay.h>
#define C1_H GPIO_SetBits(GPIOA,GPIO_Pin_4)
#define C2_H GPIO_SetBits(GPIOA,GPIO_Pin_5)
#define C3_H GPIO_SetBits(GPIOA,GPIO_Pin_6)
#define C4_H GPIO_SetBits(GPIOA,GPIO_Pin_7)

#define C1_L GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define C2_L GPIO_ResetBits(GPIOA,GPIO_Pin_5)
#define C3_L GPIO_ResetBits(GPIOA,GPIO_Pin_6)
#define C4_L GPIO_ResetBits(GPIOA,GPIO_Pin_7)


#define R1_H GPIO_SetBits(GPIOB,GPIO_Pin_0)
#define R2_H GPIO_SetBits(GPIOB,GPIO_Pin_1)
#define R3_H GPIO_SetBits(GPIOB,GPIO_Pin_10)
#define R4_H GPIO_SetBits(GPIOB,GPIO_Pin_11)

#define R1_L GPIO_ResetBits(GPIOB,GPIO_Pin_0)
#define R2_L GPIO_ResetBits(GPIOB,GPIO_Pin_1)
#define R3_L GPIO_ResetBits(GPIOB,GPIO_Pin_10)
#define R4_L GPIO_ResetBits(GPIOB,GPIO_Pin_11) 

int KeyNum = 0;//键值

void Led()
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//APB2时钟
	GPIO_InitTypeDef led;
	led.GPIO_Mode=GPIO_Mode_Out_PP;
	led.GPIO_Pin=GPIO_Pin_13;             //PC13
	led.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOC,&led);
}

void GPIO_init()
{	

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIOA_InitStruct;//设置列R1-R4
	GPIOA_InitStruct.GPIO_Mode=GPIO_Mode_IPU;//行设置为上拉输入
	GPIOA_InitStruct.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_4;
	GPIOA_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIOA_InitStruct);
	
	GPIO_InitTypeDef GPIOB_InitStruct;//设置行C1-C4
	GPIOB_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//列设置为推挽输出
	GPIOB_InitStruct.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_10|GPIO_Pin_1|GPIO_Pin_0;
	GPIOB_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIOB_InitStruct);
	
}
void Key_
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃瓜的亚瑟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值