何小豆儿在此 发表于 2025-3-24 17:57:59

STM32初学条记

一、STM32工程模板

Library文件夹放置各种库,固定不修改。 
 https://i-blog.csdnimg.cn/direct/f2225c8c24014057a7aa0582d5922237.png
//main.c
#include "stm32f10x.h"                  // Device header

int main(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
        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);
        GPIO_SetBits(GPIOC, GPIO_Pin_13);
//        GPIO_ResetBits(GPIOC, GPIO_Pin_13);
        while(1)
        {
       
        }
}   stlink和stm32连线:
SWDIO->SWIO
GND->GND
SWCLK->SWCLK
3.3V->3V3 
二、GPIO输出与输入

   GPIO输出 
2.1 LED闪烁

添加Delay函数和main.c 
https://i-blog.csdnimg.cn/direct/c2678085c4cd46d3a162721e5fc80c39.png
//Delay.h
#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



//Delay.c
#include "stm32f10x.h"

/**
* @brief微秒级延时
* @paramxus 延时时长,范围: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毫秒级延时
* @paramxms 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_ms(uint32_t xms)
{
        while(xms--)
        {
                Delay_us(1000);
        }
}

/**
* @brief秒级延时
* @paramxs 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_s(uint32_t xs)
{
        while(xs--)
        {
                Delay_ms(1000);
        }
} //main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        while(1)
        {
                GPIO_ResetBits(GPIOA, GPIO_Pin_0);
                Delay_ms(500);
                GPIO_SetBits(GPIOA, GPIO_Pin_0);
                Delay_ms(500);
               
                GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_RESET);
                Delay_ms(500);
                GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_SET);
                Delay_ms(500);
               
                GPIO_WriteBit(GPIOA, GPIO_Pin_0,(BitAction)0);
                Delay_ms(500);
                GPIO_WriteBit(GPIOA, GPIO_Pin_0,(BitAction)1);
                Delay_ms(500);
        }
} 2.2  LED流水灯

https://i-blog.csdnimg.cn/direct/b44af70c355946a8a2f4328e1d171b23.png
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        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); //0000 0000 0000 0100
                Delay_ms(100);
                GPIO_Write(GPIOA, ~0x0008); //0000 0000 0000 1000
                Delay_ms(100);
                GPIO_Write(GPIOA, ~0x0010); //0000 0000 0001 0000
                Delay_ms(100);
                GPIO_Write(GPIOA, ~0x0020); //0000 0000 0010 0000
                Delay_ms(100);
                GPIO_Write(GPIOA, ~0x0040); //0000 0000 0100 0000
                Delay_ms(100);
                GPIO_Write(GPIOA, ~0x0080); //0000 0000 1000 0000
                Delay_ms(100);
        }
} 2.3  蜂鸣器

https://i-blog.csdnimg.cn/direct/417c82e22ffc46bea3a21ec5b99d8ae9.png
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
       
        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);
       
        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(700);
        }
}
   GPIO输入 
2.4 按键控制LED

 https://i-blog.csdnimg.cn/direct/445b310a27b74c4fa47102cd9f6b9a17.png
利用到两个LED和两个按键,将这两部门模块化放入Hardware文件夹下。
实现:按键1和2分别控制LED1和2开关。 
引脚:LED1 -> PA1,LED2 -> PA2,按键1 -> PB1,按键2 -> PB11<。
//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"

void Key_Init(void)
{
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        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;
        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) == 0)
        {
                Delay_ms(20);
                while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0);
                Delay_ms(20);
                KeyNum = 2;
        }
        return KeyNum;
} //LED.h
#ifndef __LED_H__
#define __LED_H__

void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_Turn(void);
void LED2_Turn(void);

#endif


//LED.c
#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}

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);                                        //则设置PA1引脚为高电平
        }
        else                                                                                                //否则,即当前引脚输出高电平
        {                                                                                               
                GPIO_ResetBits(GPIOA,GPIO_Pin_1);                                //则设置PA1引脚为低电平
        }
}

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);
        }
} //main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.H"

uint8_t KeyNum;

int main(void)
{
        LED_Init();
        Key_Init();
       
        while(1)
        {
                KeyNum = Key_GetNum();
                if(KeyNum == 1)
                {
                        LED1_Turn();
                }
                if(KeyNum == 2)
                {
                        LED2_Turn();
                }
        }
}
2.5 光敏传感器控制蜂鸣器 

https://i-blog.csdnimg.cn/direct/bde93196a68f4f6abed375738c80f26f.png
利用到光敏传感器和蜂鸣器,将这两部门模块化放入Hardware文件夹下。
实现:遮住光敏传感器,蜂鸣器响。 
引脚:蜂鸣器PB12,光敏传感器PB13。
//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_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_SetBits(GPIOA,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(GPIOA,GPIO_Pin_12) == 0)        //获取输出寄存器的状态,如果当前引脚输出低电平
        {
                GPIO_SetBits(GPIOB,GPIO_Pin_12);                                        //则设置PA1引脚为高电平
        }
        else                                                                                                //否则,即当前引脚输出高电平
        {                                                                                               
                GPIO_ResetBits(GPIOB,GPIO_Pin_12);                                //则设置PA1引脚为低电平
        }
} //LightSensor.h
#ifndef __LIGHT_SENSOR_H__
#define __LIGHT_SENSOR_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_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
}

uint8_t LightSensor_Get(void)
{
        return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
} //main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.H"
#include "Buzzer.H"
#include "LightSensor.H"

int main(void)
{
        Buzzer_Init();
        LightSensor_Init();
        while(1)
        {
                if(LightSensor_Get() == 1)
                {
                        Buzzer_ON();
                }
                else
                {
                        Buzzer_OFF();
                }
        }
} 三、OLED 

3.1 OLED显示屏 

 https://i-blog.csdnimg.cn/direct/acb1abb30fba4eb6adb2b35a5529e6b6.png
实现:OLED的基本显示。
引脚:SCL -> PB8,SDA -> PB9。(4针OLED)
//OLED.h
#ifndef __OLED_H
#define __OLED_H

void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);

#endif



//OLED.c
#include "stm32f10x.h"
#include "OLED_Font.h"

/*引脚配置*/
#define OLED_W_SCL(x)                GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x)                GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))

/*引脚初始化*/
void OLED_I2C_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        OLED_W_SCL(1);
        OLED_W_SDA(1);
}

/**
* @briefI2C开始
* @param无
* @retval 无
*/
void OLED_I2C_Start(void)
{
        OLED_W_SDA(1);
        OLED_W_SCL(1);
        OLED_W_SDA(0);
        OLED_W_SCL(0);
}

/**
* @briefI2C停止
* @param无
* @retval 无
*/
void OLED_I2C_Stop(void)
{
        OLED_W_SDA(0);
        OLED_W_SCL(1);
        OLED_W_SDA(1);
}

/**
* @briefI2C发送一个字节
* @paramByte 要发送的一个字节
* @retval 无
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
        uint8_t i;
        for (i = 0; i < 8; i++)
        {
                OLED_W_SDA(!!(Byte & (0x80 >> i)));
                OLED_W_SCL(1);
                OLED_W_SCL(0);
        }
        OLED_W_SCL(1);        //额外的一个时钟,不处理应答信号
        OLED_W_SCL(0);
}

/**
* @briefOLED写命令
* @paramCommand 要写入的命令
* @retval 无
*/
void OLED_WriteCommand(uint8_t Command)
{
        OLED_I2C_Start();
        OLED_I2C_SendByte(0x78);                //从机地址
        OLED_I2C_SendByte(0x00);                //写命令
        OLED_I2C_SendByte(Command);
        OLED_I2C_Stop();
}

/**
* @briefOLED写数据
* @paramData 要写入的数据
* @retval 无
*/
void OLED_WriteData(uint8_t Data)
{
        OLED_I2C_Start();
        OLED_I2C_SendByte(0x78);                //从机地址
        OLED_I2C_SendByte(0x40);                //写数据
        OLED_I2C_SendByte(Data);
        OLED_I2C_Stop();
}

/**
* @briefOLED设置光标位置
* @paramY 以左上角为原点,向下方向的坐标,范围:0~7
* @paramX 以左上角为原点,向右方向的坐标,范围:0~127
* @retval 无
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
        OLED_WriteCommand(0xB0 | Y);                                        //设置Y位置
        OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));        //设置X位置高4位
        OLED_WriteCommand(0x00 | (X & 0x0F));                        //设置X位置低4位
}

/**
* @briefOLED清屏
* @param无
* @retval 无
*/
void OLED_Clear(void)
{
        uint8_t i, j;
        for (j = 0; j < 8; j++)
        {
                OLED_SetCursor(j, 0);
                for(i = 0; i < 128; i++)
                {
                        OLED_WriteData(0x00);
                }
        }
}

/**
* @briefOLED显示一个字符
* @paramLine 行位置,范围:1~4
* @paramColumn 列位置,范围:1~16
* @paramChar 要显示的一个字符,范围:ASCII可见字符
* @retval 无
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{             
        uint8_t i;
        OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);                //设置光标位置在上半部分
        for (i = 0; i < 8; i++)
        {
                OLED_WriteData(OLED_F8x16);                        //显示上半部分内容
        }
        OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);        //设置光标位置在下半部分
        for (i = 0; i < 8; i++)
        {
                OLED_WriteData(OLED_F8x16);                //显示下半部分内容
        }
}

/**
* @briefOLED显示字符串
* @paramLine 起始行位置,范围:1~4
* @paramColumn 起始列位置,范围:1~16
* @paramString 要显示的字符串,范围:ASCII可见字符
* @retval 无
*/
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
        uint8_t i;
        for (i = 0; String != '\0'; i++)
        {
                OLED_ShowChar(Line, Column + i, String);
        }
}

/**
* @briefOLED次方函数
* @retval 返回值等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
        uint32_t Result = 1;
        while (Y--)
        {
                Result *= X;
        }
        return Result;
}

/**
* @briefOLED显示数字(十进制,正数)
* @paramLine 起始行位置,范围:1~4
* @paramColumn 起始列位置,范围:1~16
* @paramNumber 要显示的数字,范围:0~4294967295
* @paramLength 要显示数字的长度,范围:1~10
* @retval 无
*/
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
        uint8_t i;
        for (i = 0; i < Length; i++)                                                       
        {
                OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
        }
}

/**
* @briefOLED显示数字(十进制,带符号数)
* @paramLine 起始行位置,范围:1~4
* @paramColumn 起始列位置,范围:1~16
* @paramNumber 要显示的数字,范围:-2147483648~2147483647
* @paramLength 要显示数字的长度,范围:1~10
* @retval 无
*/
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
        uint8_t i;
        uint32_t Number1;
        if (Number >= 0)
        {
                OLED_ShowChar(Line, Column, '+');
                Number1 = Number;
        }
        else
        {
                OLED_ShowChar(Line, Column, '-');
                Number1 = -Number;
        }
        for (i = 0; i < Length; i++)                                                       
        {
                OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
        }
}

/**
* @briefOLED显示数字(十六进制,正数)
* @paramLine 起始行位置,范围:1~4
* @paramColumn 起始列位置,范围:1~16
* @paramNumber 要显示的数字,范围:0~0xFFFFFFFF
* @paramLength 要显示数字的长度,范围:1~8
* @retval 无
*/
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
        uint8_t i, SingleNumber;
        for (i = 0; i < Length; i++)                                                       
        {
                SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
                if (SingleNumber < 10)
                {
                        OLED_ShowChar(Line, Column + i, SingleNumber + '0');
                }
                else
                {
                        OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
                }
        }
}

/**
* @briefOLED显示数字(二进制,正数)
* @paramLine 起始行位置,范围:1~4
* @paramColumn 起始列位置,范围:1~16
* @paramNumber 要显示的数字,范围:0~1111 1111 1111 1111
* @paramLength 要显示数字的长度,范围:1~16
* @retval 无
*/
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
        uint8_t i;
        for (i = 0; i < Length; i++)                                                       
        {
                OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
        }
}

/**
* @briefOLED初始化
* @param无
* @retval 无
*/
void OLED_Init(void)
{
        uint32_t i, j;
       
        for (i = 0; i < 1000; i++)                        //上电延时
        {
                for (j = 0; j < 1000; j++);
        }
       
        OLED_I2C_Init();                        //端口初始化
       
        OLED_WriteCommand(0xAE);        //关闭显示
       
        OLED_WriteCommand(0xD5);        //设置显示时钟分频比/振荡器频率
        OLED_WriteCommand(0x80);
       
        OLED_WriteCommand(0xA8);        //设置多路复用率
        OLED_WriteCommand(0x3F);
       
        OLED_WriteCommand(0xD3);        //设置显示偏移
        OLED_WriteCommand(0x00);
       
        OLED_WriteCommand(0x40);        //设置显示开始行
       
        OLED_WriteCommand(0xA1);        //设置左右方向,0xA1正常 0xA0左右反置
       
        OLED_WriteCommand(0xC8);        //设置上下方向,0xC8正常 0xC0上下反置

        OLED_WriteCommand(0xDA);        //设置COM引脚硬件配置
        OLED_WriteCommand(0x12);
       
        OLED_WriteCommand(0x81);        //设置对比度控制
        OLED_WriteCommand(0xCF);

        OLED_WriteCommand(0xD9);        //设置预充电周期
        OLED_WriteCommand(0xF1);

        OLED_WriteCommand(0xDB);        //设置VCOMH取消选择级别
        OLED_WriteCommand(0x30);

        OLED_WriteCommand(0xA4);        //设置整个显示打开/关闭

        OLED_WriteCommand(0xA6);        //设置正常/倒转显示

        OLED_WriteCommand(0x8D);        //设置充电泵
        OLED_WriteCommand(0x14);

        OLED_WriteCommand(0xAF);        //开启显示
               
        OLED_Clear();                                //OLED清屏
}
字库:
//OLED_Font.h
#ifndef __OLED_FONT_H
#define __OLED_FONT_H

/*OLED字模库,宽8像素,高16像素*/
const uint8_t OLED_F8x16[]=
{
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//0
       
        0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
       
        0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
       
        0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
        0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
       
        0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
        0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
       
        0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
        0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
       
        0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
        0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
       
        0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
       
        0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
        0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
       
        0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
        0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
       
        0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
        0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
       
        0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
        0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
       
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
       
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
       
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
       
        0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
        0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
       
        0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
        0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
       
        0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
        0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
       
        0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
        0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
       
        0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
        0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
       
        0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
        0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
       
        0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
        0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
       
        0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
        0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
       
        0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
        0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
       
        0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
        0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
       
        0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
        0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
       
        0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
        0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
       
        0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
        0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
       
        0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
        0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
       
        0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
       
        0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
        0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
       
        0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
        0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
       
        0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
        0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
       
        0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
        0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
       
        0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
        0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
       
        0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
        0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
       
        0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
        0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
       
        0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
        0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
       
        0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
        0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
       
        0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
        0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
       
        0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
        0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
       
        0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
        0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
       
        0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
        0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
       
        0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
        0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
       
        0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
        0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
       
        0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
        0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
       
        0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
        0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
       
        0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
        0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
       
        0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
        0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
       
        0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
        0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
       
        0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
        0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
       
        0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
        0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
       
        0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
        0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
       
        0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
        0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
       
        0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
        0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
       
        0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
        0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
       
        0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
        0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
       
        0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
        0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
       
        0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
        0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
       
        0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
        0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
       
        0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
       
        0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
        0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
       
        0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
       
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
       
        0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
       
        0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
        0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
       
        0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
        0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
       
        0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
        0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
       
        0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
        0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
       
        0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
        0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
       
        0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
        0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
       
        0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
        0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
       
        0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
        0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
       
        0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
        0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
       
        0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
        0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
       
        0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
        0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
       
        0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
        0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
       
        0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
        0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
       
        0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
        0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
       
        0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
        0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
       
        0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
        0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
       
        0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
        0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
       
        0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
        0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
       
        0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
        0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
       
        0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
        0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
       
        0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
        0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
       
        0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
        0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
       
        0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
        0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
       
        0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
        0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
       
        0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
        0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
       
        0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
        0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
       
        0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
        0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
       
        0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
       
        0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
        0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
       
        0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};

#endif
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"

int main(void)
{
        OLED_Init();
        OLED_ShowChar(1,1,'A');
        OLED_ShowString(1,3,"Hello!");
        OLED_ShowNum(2,1,12345,5);
        OLED_ShowSignedNum(2,7,-66,2);
        OLED_ShowHexNum(3,1,0xAA55,4);
        OLED_ShowBinNum(4,1,0xAA55,16);
       
        while(1)
        {
               
        }
}
四、EXIT外部停止

4.1 对射式红外传感器计次

https://i-blog.csdnimg.cn/direct/35b002c34d0342fc8e6e8ab8d054c033.png
实现:利用对射式红外传感器进行计次 。下降沿触发,每当遮挡物离开红外,则计次1。
关键文件:模块化CounterSensor、main.c
引脚:DO -> PB14。
//CounterSensor.h
#ifndef __CountSensor_H__
#define __CountSensor_H__

void CountSensor_Init(void);
uint16_t CountSensor_Get(void);

#endif


//CounterSensor.c
#include "stm32f10x.h"                  // Device header

uint16_t CountSensor_Count;

void CountSensor_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);//数据选择器
       
        EXTI_InitTypeDef EXTI_InitStructure;
        EXTI_InitStructure.EXTI_Line = EXTI_Line14;
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
        EXTI_Init(&EXTI_InitStructure);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
}
uint16_t CountSensor_Get(void)
{
        return CountSensor_Count;
}

void EXTI15_10_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line14) == SET)
        {
                CountSensor_Count ++;
                EXTI_ClearITPendingBit(EXTI_Line14);
        }
} //main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.H"

int main(void)
{
        OLED_Init();
        CountSensor_Init();
        OLED_ShowString(1,1,"Count:");
       
        while(1)
        {
                OLED_ShowNum(1,7,CountSensor_Get(),5);
        }
}
4.2  旋转编码器计次

 https://i-blog.csdnimg.cn/direct/005896c8cefa4d2b9b64a3be781effb9.png
实现:利用旋转编码器进行计次 。向左旋转数值递减,向右旋转递增。
关键文件:模块化Encoder、main.c
引脚:A -> PB0,B -> PB1。
//Encoder.h
#ifndef __ENCODER_H__
#define __ENCODER_H__

void Encoder_Init(void);
int16_t Encode_Get(void);

#endif




//Encoder.c
#include "stm32f10x.h"                  // Device header

int16_t Encoder_Count;

void Encoder_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);//数据选择器
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
       
        EXTI_InitTypeDef EXTI_InitStructure;
        EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
        EXTI_Init(&EXTI_InitStructure);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
       
        NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
        NVIC_Init(&NVIC_InitStructure);
}

int16_t Encode_Get(void)
{
        int16_t Temp;
        Temp = Encoder_Count;
        Encoder_Count = 0;
        return Temp;
}

void EXTI0_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line0) == SET)
        {
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
                {
                        Encoder_Count--;
                }
                EXTI_ClearITPendingBit(EXTI_Line0);
        }
}

void EXTI1_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line1) == SET)
        {
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
                {
                        Encoder_Count++;
                }
                EXTI_ClearITPendingBit(EXTI_Line1);
        }
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Encoder.h"

int16_t Num;

int main(void)
{
        OLED_Init();
        Encoder_Init();
        OLED_ShowString(1,1,"Num:");
       
        while(1)
        {
                Num+= Encode_Get();
                OLED_ShowSignedNum(1,5,Num,5);
        }
}
 五、TIM定时器

5.1 定时器定时停止

https://i-blog.csdnimg.cn/direct/0147abae0cde4b4e941e231837fa3203.png
在System文件夹进行Timer模块化。
实现:定时器计时,在OLED上显示,每一秒数字增一。
//Timer.h
#ifndef __TIMER_H__
#define __TIMER_H__

void Timer_Init(void);

#endif



//Timer.c
#include "stm32f10x.h"                  // Device header

void Timer_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
       
        TIM_InternalClockConfig(TIM2);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 10000-1;
        TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1;
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
       
        TIM_ClearFlag(TIM2,TIM_FLAG_Update);
        TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
       
        TIM_Cmd(TIM2,ENABLE);
}

/*
void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
        {
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }
}
*/
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Timer.h"

uint16_t Num;

int main(void)
{
        OLED_Init();
        Timer_Init();
        OLED_ShowString(1,1,"Num:");
       
        while(1)
        {
                OLED_ShowNum(1,5,Num,5);
        }
}

void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
        {
                Num++;
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }
}
5.2 定时器外部时钟

 https://i-blog.csdnimg.cn/direct/7de6344fea83403c8685be27ae30ae59.png
实现:外接红外对射式传感器,PA0引脚就是TIM2的ETR引脚,在这个引脚输入一个外部时钟。挡光片每遮挡一次红外对射式传感器,CNT加1,加到9,CNT自动清零,同时申请停止,NUM++。CNT和NUM的值在OLED上显示。
引脚:红外对射式传感器DO -> PA0。OLED接法同上。
//Timer.h
#ifndef __TIMER_H__
#define __TIMER_H__

void Timer_Init(void);
uint16_t Timer_GetCounter(void);

#endif




//Timer.c
#include "stm32f10x.h"                  // Device header

void Timer_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x00);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 10-1;
        TIM_TimeBaseInitStructure.TIM_Prescaler = 1-1;
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
       
        TIM_ClearFlag(TIM2,TIM_FLAG_Update);
        TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
       
        TIM_Cmd(TIM2,ENABLE);
}

uint16_t Timer_GetCounter(void)
{
        return TIM_GetCounter(TIM2);
}

/*
void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
        {
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }
}
*/
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Timer.h"

uint16_t Num;

int main(void)
{
        OLED_Init();
        Timer_Init();
        OLED_ShowString(1,1,"Num:");
        OLED_ShowString(2,1,"CNT:");
       
        while(1)
        {
                OLED_ShowNum(1,5,Num,5);
                OLED_ShowNum(2,5,Timer_GetCounter(),5);
        }
}

void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
        {
                Num++;
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }
}

5.3 PWM驱动LED呼吸灯

 https://i-blog.csdnimg.cn/direct/1004d5962b3f4fa69246bd8641de6755.png
这里模块化PWM。
实现:LED呼吸灯效果。
引脚:LED接到PA0。
//PWM.h
#ifndef __PWM_H__
#define __PWM_H__

void PWM_Init(void);
void PWM_SetCompare1(uint16_t Compare);

#endif




//PWM.c
#include "stm32f10x.h"                  // Device header

void PWM_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//重映射,将PA0转移到PA15
//        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//        GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);
//        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;        //GPIO_Pin_15
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_InternalClockConfig(TIM2);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 100-1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 720-1;        //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
       
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_OCStructInit(&TIM_OCInitStructure);
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 50;                //CCR
        TIM_OC1Init(TIM2, &TIM_OCInitStructure);
       
        TIM_Cmd(TIM2,ENABLE);
}

//设置CCR寄存器的值
void PWM_SetCompare1(uint16_t Compare)
{
        TIM_SetCompare1(TIM2, Compare);
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"

uint8_t i;

int main(void)
{
        OLED_Init();
        PWM_Init();
       
        while(1)
        {
                for(i=0;i<=100;i++)
                {
                        PWM_SetCompare1(i);
                        Delay_ms(10);
                }
                for(i=0;i<=100;i++)
                {
                        PWM_SetCompare1(100 - i);
                        Delay_ms(10);
                }
        }
}
5.4 PWM驱动舵机

https://i-blog.csdnimg.cn/direct/31b4b51a7b71488394d1b95425608ebe.png

增加PWM、Servo的模块化文件,放入Hardware文件夹内。
实现:通过按键,控制舵机旋转不同的角度(从0°-180°,按键每按一次旋转30°),并在OLED上显示当前角度。
引脚:舵机vcc -> STlink的5V,舵机信号引脚(橙色线)-> PA1。按键接在PB1。OLED的SCL->PB8,SDA->PB9。
//PWM.h
#ifndef __PWM_H__
#define __PWM_H__

void PWM_Init(void);
void PWM_SetCompare2(uint16_t Compare);

#endif



//PWM.c
#include "stm32f10x.h"                  // Device header

void PWM_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_InternalClockConfig(TIM2);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 20000-1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1;        //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
       
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_OCStructInit(&TIM_OCInitStructure);
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0;                //CCR
        TIM_OC2Init(TIM2, &TIM_OCInitStructure);
       
        TIM_Cmd(TIM2,ENABLE);
}

//设置CCR寄存器的值
void PWM_SetCompare2(uint16_t Compare)
{
        TIM_SetCompare2(TIM2, Compare);
}
//Servo.h
#ifndef __SERVO_H__
#define __SERVO_H__

void Servo_Init(void);
void Servo_SetAngle(float Angle);

#endif



//Servo.c
#include "stm32f10x.h"                  // Device header
#include "PWM.H"

void Servo_Init(void)
{
        PWM_Init();
}

void Servo_SetAngle(float Angle)
{
        PWM_SetCompare2(Angle / 180 * 2000 + 500);
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Servo.h"
#include "Key.h"

uint8_t KeyNum;
float Angle;

int main(void)
{
        OLED_Init();
        Servo_Init();
        Key_Init();
       
        OLED_ShowString(1,1,"Angle:");
       
        while(1)
        {
                KeyNum = Key_GetNum();
                if(KeyNum == 1)
                {
                        Angle += 30;
                        if(Angle > 180)
                        {
                                Angle = 0;
                        }
                }
                Servo_SetAngle(Angle);
                OLED_ShowNum(1,7,Angle,3);
        }
}
 5.5 PWM驱动直流电机

https://i-blog.csdnimg.cn/direct/b0db19bc3684422b83ef6a09f4d19e5b.png
模块化Motor。 
实现:按键控制直流电机的转动速率与方向。正向从0转到100,然后反向从-100到0。OLED上显示速率的多少。
引脚:
TB6612电机驱动模块:VM -> STLink的5V引脚,VCC -> 电源正,GND -> 电源负,AO1和AO2分别接直流电机的两根线,PWMA -> PA2,AN2 -> PA5,AN1-> PA4,STBY -> 电源正。
按键:PB1。OLED接法同上。
//PWM.h
#ifndef __PWM_H__
#define __PWM_H__

void PWM_Init(void);
void PWM_SetCompare3(uint16_t Compare);

#endif






//PWM.c
#include "stm32f10x.h"                  // Device header

void PWM_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_InternalClockConfig(TIM2);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 100-1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 36-1;        //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
       
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_OCStructInit(&TIM_OCInitStructure);
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 50;                //CCR
        TIM_OC3Init(TIM2, &TIM_OCInitStructure);
       
        TIM_Cmd(TIM2,ENABLE);
}

//设置CCR寄存器的值
void PWM_SetCompare3(uint16_t Compare)
{
        TIM_SetCompare3(TIM2, Compare);
}
//Motor.h
#ifndef __MOTOR_H__
#define __MOTOR_H__

void Motor_Init(void);
void Motor_SetSpeed(int8_t Speed);

#endif






//Motor.c
#include "stm32f10x.h"                  // Device header
#include "PWM.H"

void Motor_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        PWM_Init();
}

void Motor_SetSpeed(int8_t Speed)
{
        if(Speed >= 0)
        {
                GPIO_SetBits(GPIOA, GPIO_Pin_4);
                GPIO_ResetBits(GPIOA, GPIO_Pin_5);
                PWM_SetCompare3(Speed);
        }
        else
        {
                GPIO_ResetBits(GPIOA, GPIO_Pin_4);
                GPIO_SetBits(GPIOA, GPIO_Pin_5);
                PWM_SetCompare3(-Speed);
        }
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Motor.h"
#include "Key.h"

uint8_t KeyNum;
int8_t Speed;

int main(void)
{
        OLED_Init();
        Motor_Init();
        Key_Init();
       
        OLED_ShowString(1,1,"Speed:");
       
        while(1)
        {
                KeyNum=Key_GetNum();
                if(KeyNum == 1)
                {
                        Speed += 20;
                        if(Speed >100)
                        {
                                Speed = -100;
                        }
                }
                Motor_SetSpeed(Speed);
                OLED_ShowSignedNum(1,7,Speed,3);
        }
}
5.6 输入捕获模式测频率

https://i-blog.csdnimg.cn/direct/e90a7b2deeb74524a416989d55342807.png
创建新的文件模块化IC输入捕获到Hardware文件夹。 
实现:PWM模块将待测信号输出到PA0,PA0又通过导线,输入到PA6。PA6是TIM3的通道1,通道1通过输入捕获模块,测量得到频率。OLED不断刷新显示频率。
连线:利用一根飞线PA0 -> PA6
//PWM.h
#ifndef __PWM_H__
#define __PWM_H__

void PWM_Init(void);
void PWM_SetCompare1(uint16_t Compare);
void PWM_SetPrescaler(uint16_t Prescaler);

#endif



//PWM.c
#include "stm32f10x.h"                  // Device header

void PWM_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_InternalClockConfig(TIM2);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 100-1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 720-1;        //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
       
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_OCStructInit(&TIM_OCInitStructure);
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 50;                //CCR
        TIM_OC1Init(TIM2, &TIM_OCInitStructure);
       
        TIM_Cmd(TIM2,ENABLE);
}

//设置CCR寄存器的值,改变通道1的占空比
void PWM_SetCompare1(uint16_t Compare)
{
        TIM_SetCompare1(TIM2, Compare);
}
//改变频率PSC
void PWM_SetPrescaler(uint16_t Prescaler)
{
        TIM_PrescalerConfig(TIM2, Prescaler,TIM_PSCReloadMode_Immediate);
}
//IC.h
#ifndef __IC_H__
#define __IC_H__

void IC_Init(void);
uint16_t IC_GetFreq(void);

#endif



//IC.c
#include "stm32f10x.h"                  // Device header

void IC_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_InternalClockConfig(TIM3);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;                //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
       
        TIM_ICInitTypeDef TIM_ICInitStructure;
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
        TIM_ICInitStructure.TIM_ICFilter = 0xF;
        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
        TIM_ICInit(TIM3,&TIM_ICInitStructure);
       
        TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
        TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
       
        TIM_Cmd(TIM3,ENABLE);
}

uint16_t IC_GetFreq(void)
{
        return 1000000 / (TIM_GetCapture1(TIM3) + 1);
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"

int main(void)
{
        OLED_Init();
        PWM_Init();
        IC_Init();
       
        OLED_ShowString(1,1,"Freq:00000Hz");
       
        PWM_SetPrescaler(720 -1);                //Freq = 72M / (PSC + 1) / 100
        PWM_SetCompare1(50);                        //Duty = CCR / 100
       
        while(1)
        {
                OLED_ShowNum(1,6,IC_GetFreq(),5);
        }
}
 5.7 PWMI模式测频率占空比

本小节在 5.6 的基础上修改 IC 代码,除此外无变更。
实现:测占空比,与频率一起在OLED上刷新显示。 
//IC.h
#ifndef __IC_H__
#define __IC_H__

void IC_Init(void);
uint16_t IC_GetFreq(void);
uint16_t IC_GetDuty(void);

#endif




//IC.c
#include "stm32f10x.h"                  // Device header

void IC_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_InternalClockConfig(TIM3);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;                //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
       
        TIM_ICInitTypeDef TIM_ICInitStructure;
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
        TIM_ICInitStructure.TIM_ICFilter = 0xF;
        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
        TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);
       
        TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
        TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
       
        TIM_Cmd(TIM3,ENABLE);
}

//获取频率
uint16_t IC_GetFreq(void)
{
        return 1000000 / (TIM_GetCapture1(TIM3) + 1);
}

//获取占空比
uint16_t IC_GetDuty(void)
{
        return (TIM_GetCapture2(TIM3) + 1) * 100 / (TIM_GetCapture1(TIM3) + 1);
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"

int main(void)
{
        OLED_Init();
        PWM_Init();
        IC_Init();
       
        OLED_ShowString(1,1,"Freq:00000Hz");
        OLED_ShowString(2,1,"Duty:00%");
       
        PWM_SetPrescaler(720 -1);                //Freq = 72M / (PSC + 1) / 100
        PWM_SetCompare1(50);                        //Duty = CCR / 100
       
        while(1)
        {
                OLED_ShowNum(1,6,IC_GetFreq(),5);
                OLED_ShowNum(2,6,IC_GetDuty(),2);
        }
}
 5.8 编码器接口测速

https://i-blog.csdnimg.cn/direct/65a0a8b73e9d4934b9936cad1ade8292.png
延用 5.1 的代码 ,新增 Encoder 的模块化代码文件。
实现:利用旋转编码器,对旋转速率进行测速。向左旋转为负,反之为正。(着实就是 4.2 的计次,但是添加了清零,每次计次完成都清零,这样就是速率了。)每次在OLED上刷新显示速率,利用main的停止函数而不是Delay。
引脚: 旋转编码器的A -> PA6,B -> PA7。

//Encoder.h
#ifndef __ENCODER_H__
#define __ENCODER_H__

void Encoder_Init(void);
int16_t Encoder_Get(void);

#endif



//Encoder.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Timer.h"
#include "Encoder.h"

int16_t Speed;

int main(void)
{
        OLED_Init();
        Timer_Init();
        Encoder_Init();
       
        OLED_ShowString(1,1,"Speed:");
       
        while(1)
        {
                OLED_ShowSignedNum(1, 7, Speed, 5);
        }
}

void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
        {
                Speed = Encoder_Get();
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Timer.h"
#include "Encoder.h"

int16_t Speed;

int main(void)
{
        OLED_Init();
        Timer_Init();
        Encoder_Init();
       
        OLED_ShowString(1,1,"Speed:");
       
        while(1)
        {
                OLED_ShowSignedNum(1, 7, Speed, 5);
        }
}

void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
        {
                Speed = Encoder_Get();
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }
}
六、ADC数模转换器

6.1 AD单通道 

https://i-blog.csdnimg.cn/direct/6bafe878f76849cf9775c54ed1eb0ec9.png
延用 3.1 的代码,新增 AD 模块。
实现:AD模数转换。利用电位器,向左拧电压减小,向右宁电压增大。 
引脚:电位器三个引脚在上呈三角排列。左边引脚接电源负,右边引脚接电源正,中间引脚接PA0。 
//AD.h
#ifndef __AD_H__
#define __AD_H__

void AD_Init(void);
uint16_t AD_GetValue(void);

#endif




//AD.c
#include "stm32f10x.h"                  // Device header

void AD_Init(void)
{
        //配置时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

        RCC_ADCCLKConfig(RCC_PCLK2_Div6);
       
        //配置GPIO口
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        //选择输入通道
        ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
       
        //ADC初始化(单次转换、非扫描模式)
        ADC_InitTypeDef ADC_InitStructure;
        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
        ADC_InitStructure.ADC_ScanConvMode = DISABLE;
        ADC_InitStructure.ADC_NbrOfChannel = 1;
        ADC_Init(ADC1,&ADC_InitStructure);
       
        //开启电源
        ADC_Cmd(ADC1,ENABLE);
       
        //ADC校准
        ADC_ResetCalibration(ADC1);
        while(ADC_GetResetCalibrationStatus(ADC1) == SET);
        ADC_StartCalibration(ADC1);
        while(ADC_GetCalibrationStatus(ADC1) == SET);
}

uint16_t AD_GetValue(void)
{
        ADC_SoftwareStartConvCmd(ADC1,ENABLE);                                        //启动
        while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);        //等待
        return ADC_GetConversionValue(ADC1);                                        //读取
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"

uint16_t ADValue;
float Voltage;

int main(void)
{
        OLED_Init();
        AD_Init();
       
        OLED_ShowString(1,1,"ADValue:");
        OLED_ShowString(2,1,"Voltage:0.00V");
       
        while(1)
        {
                ADValue = AD_GetValue();
                Voltage = (float)ADValue / 4095 * 3.3;
               
                OLED_ShowNum(1, 9, ADValue, 4);
                OLED_ShowNum(2, 9, Voltage, 1);
                OLED_ShowNum(2, 11, (uint16_t)(Voltage * 100) % 100, 2);
               
                Delay_ms(100);
        }
}
 6.2 AD多通道

实物图: 
https://i-blog.csdnimg.cn/direct/f9fb4d7c030f43d19e8a8c7d9db2f8f4.jpeg
复制 6.1 文件,电位器不变,新增光敏传感器、热敏传感器、对射式红外传感器。修改AD模块与main函数。
引脚:光敏传感器、热敏传感器、对射式红外传感器的AO引脚分别接PA1、PA2、PA3。VCC都接电源正,GND都接电源负。
//AD.h
#ifndef __AD_H__
#define __AD_H__

void AD_Init(void);
uint16_t AD_GetValue(uint8_t ADC_Channel);

#endif



//AD.c
#include "stm32f10x.h"                  // Device header

void AD_Init(void)
{
        //配置时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

        RCC_ADCCLKConfig(RCC_PCLK2_Div6);
       
        //配置GPIO口
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        //ADC初始化(单次转换、非扫描模式)
        ADC_InitTypeDef ADC_InitStructure;
        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
        ADC_InitStructure.ADC_ScanConvMode = DISABLE;
        ADC_InitStructure.ADC_NbrOfChannel = 1;
        ADC_Init(ADC1,&ADC_InitStructure);
       
        //开启电源
        ADC_Cmd(ADC1,ENABLE);
       
        //ADC校准
        ADC_ResetCalibration(ADC1);
        while(ADC_GetResetCalibrationStatus(ADC1) == SET);
        ADC_StartCalibration(ADC1);
        while(ADC_GetCalibrationStatus(ADC1) == SET);
}

uint16_t AD_GetValue(uint8_t ADC_Channel)
{
        //选择输入通道
        ADC_RegularChannelConfig(ADC1,ADC_Channel,1,ADC_SampleTime_55Cycles5);
        ADC_SoftwareStartConvCmd(ADC1,ENABLE);                                        //启动
        while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);        //等待
        return ADC_GetConversionValue(ADC1);                                        //读取
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"

uint16_t AD0, AD1, AD2, AD3;

int main(void)
{
        OLED_Init();
        AD_Init();
       
        OLED_ShowString(1,1,"AD0:");
        OLED_ShowString(2,1,"AD1:");
        OLED_ShowString(3,1,"AD2:");
        OLED_ShowString(4,1,"AD3:");
       
        while(1)
        {
                AD0 = AD_GetValue(ADC_Channel_0);
                AD1 = AD_GetValue(ADC_Channel_1);
                AD2 = AD_GetValue(ADC_Channel_2);
                AD3 = AD_GetValue(ADC_Channel_3);
               
                OLED_ShowNum(1, 5, AD0, 4);
                OLED_ShowNum(2, 5, AD1, 4);
                OLED_ShowNum(3, 5, AD2, 4);
                OLED_ShowNum(4, 5, AD3, 4);
               
                Delay_ms(100);
        }
}
七、DMA数据转运

7.1 DMA数据转运 单通道

https://i-blog.csdnimg.cn/direct/f762a34e02cc402f9c66f9d26a7a9a2a.png
复制 3.1 代码,新建 MyDMA 至System文件夹。
和 6.1 的接线相同。
实现:DataA每隔一秒向DataB传送数据,并在OLED更新显示。 
//MyDMA.h
#ifndef __MYDMA_H__
#define __MYDMA_H__

void MyDMA_Init(uint32_t AddrA, uint32_t AddrB, uint16_t Size);
void MyDMA_Transfer(void);

#endif



//MyDMA.c
#include "stm32f10x.h"                  // Device header

uint16_t MyDMA_Size;

void MyDMA_Init(uint32_t AddrA, uint32_t AddrB, uint16_t Size)
{
        MyDMA_Size = Size;
       
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
       
        DMA_InitTypeDef DMA_InitStructure;
        DMA_InitStructure.DMA_PeripheralBaseAddr = AddrA;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
        DMA_InitStructure.DMA_MemoryBaseAddr = AddrB;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
        DMA_InitStructure.DMA_BufferSize = Size;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
        DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
        DMA_Init(DMA1_Channel1, &DMA_InitStructure);
       
        DMA_Cmd(DMA1_Channel1,DISABLE);
}

void MyDMA_Transfer(void)
{
        DMA_Cmd(DMA1_Channel1,DISABLE);
        DMA_SetCurrDataCounter(DMA1_Channel1,MyDMA_Size);
        DMA_Cmd(DMA1_Channel1,ENABLE);
       
        while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);
        DMA_ClearFlag(DMA1_FLAG_TC1);
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "MyDMA.h"

uint8_t DataA[] = {0x01, 0x02, 0x03, 0x04};
uint8_t DataB[] = {0, 0, 0, 0};

int main(void)
{
        OLED_Init();
       
        MyDMA_Init((uint32_t)DataA,(uint32_t)DataB,4);
       
        OLED_ShowString(1,1,"DataA");
        OLED_ShowString(3,1,"DataB");
        OLED_ShowHexNum(1,8,(uint32_t)DataA,8);
        OLED_ShowHexNum(3,8,(uint32_t)DataB,8);
       
        OLED_ShowHexNum(2,1,DataA,2);
        OLED_ShowHexNum(2,4,DataA,2);
        OLED_ShowHexNum(2,7,DataA,2);
        OLED_ShowHexNum(2,10,DataA,2);
        OLED_ShowHexNum(4,1,DataB,2);
        OLED_ShowHexNum(4,4,DataB,2);
        OLED_ShowHexNum(4,7,DataB,2);
        OLED_ShowHexNum(4,10,DataB,2);
       
        while(1)
        {
                DataA ++;
                DataA ++;
                DataA ++;
                DataA ++;
               
                OLED_ShowHexNum(2,1,DataA,2);
                OLED_ShowHexNum(2,4,DataA,2);
                OLED_ShowHexNum(2,7,DataA,2);
                OLED_ShowHexNum(2,10,DataA,2);
                OLED_ShowHexNum(4,1,DataB,2);
                OLED_ShowHexNum(4,4,DataB,2);
                OLED_ShowHexNum(4,7,DataB,2);
                OLED_ShowHexNum(4,10,DataB,2);
               
                Delay_ms(1000);
               
                MyDMA_Transfer();
               
                OLED_ShowHexNum(2,1,DataA,2);
                OLED_ShowHexNum(2,4,DataA,2);
                OLED_ShowHexNum(2,7,DataA,2);
                OLED_ShowHexNum(2,10,DataA,2);
                OLED_ShowHexNum(4,1,DataB,2);
                OLED_ShowHexNum(4,4,DataB,2);
                OLED_ShowHexNum(4,7,DataB,2);
                OLED_ShowHexNum(4,10,DataB,2);
               
                Delay_ms(1000);
        }
}

7.2 DMA+AD多通道

https://i-blog.csdnimg.cn/direct/772f98fbb71b42f4b1241d06d7faa8c5.png
复制 6.2 代码。 
与 6.2 实现结果相同,但利用了DMA数据转运。
//AD.h
#ifndef __AD_H__
#define __AD_H__

extern uint16_t AD_Value;
void AD_Init(void);

#endif



//AD.c
#include "stm32f10x.h"                  // Device header

uint16_t AD_Value;

void AD_Init(void)
{
        //配置时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
       
        RCC_ADCCLKConfig(RCC_PCLK2_Div6);
       
        //配置GPIO口
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
        ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_55Cycles5);
        ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_55Cycles5);
        ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_55Cycles5);

       
        //ADC初始化(单次转换、非扫描模式)
        ADC_InitTypeDef ADC_InitStructure;
        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        ADC_InitStructure.ADC_NbrOfChannel = 4;
        ADC_Init(ADC1,&ADC_InitStructure);
       
        DMA_InitTypeDef DMA_InitStructure;
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AD_Value;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
        DMA_InitStructure.DMA_BufferSize = 4;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
        DMA_Init(DMA1_Channel1, &DMA_InitStructure);
       
        DMA_Cmd(DMA1_Channel1,ENABLE);
        ADC_DMACmd(ADC1,ENABLE);
        ADC_Cmd(ADC1,ENABLE);
       
        //ADC校准
        ADC_ResetCalibration(ADC1);
        while(ADC_GetResetCalibrationStatus(ADC1) == SET);
        ADC_StartCalibration(ADC1);
        while(ADC_GetCalibrationStatus(ADC1) == SET);
       
        ADC_SoftwareStartConvCmd(ADC1,ENABLE);                //启动
}

//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"

int main(void)
{
        OLED_Init();
        AD_Init();
       
        OLED_ShowString(1,1,"AD0:");
        OLED_ShowString(2,1,"AD1:");
        OLED_ShowString(3,1,"AD2:");
        OLED_ShowString(4,1,"AD3:");
       
        while(1)
        {
                OLED_ShowNum(1, 5, AD_Value, 4);
                OLED_ShowNum(2, 5, AD_Value, 4);
                OLED_ShowNum(3, 5, AD_Value, 4);
                OLED_ShowNum(4, 5, AD_Value, 4);
               
                Delay_ms(100);
        }
}
 八、USART串口协议

8.1 串口发送

https://i-blog.csdnimg.cn/direct/d85c5f0074a14f4dbdf864d25d11fe9b.png
复制 3.1 代码,加上Serial文件。 
实现:串口发送数据。 每复位一次,串口发送一次数据。
接线:STLink和USB转TTL同时插在电脑USB上 。
USB转TTL:TX -> PA10, RX -> PA9。STLink接线同上。
在串口助手中的最终显示结果如下:
https://i-blog.csdnimg.cn/direct/f1c782b8ef734361b42673459bf56e0d.png
//Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <stdio.h>

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);

#endif




//Serial.c
#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

void Serial_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        USART_InitTypeDef USART_InitStructure;
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Tx;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART1,&USART_InitStructure);
       
        USART_Cmd(USART1,ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
        USART_SendData(USART1,Byte);
        while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
        uint16_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Array);
        }
}

void Serial_SendString(char *String)
{
        uint8_t i;
        for(i = 0;String != '\0'; i ++)
        {
                Serial_SendByte(String);
        }
}       

uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
        uint32_t Result = 1;
        while(Y --)
        {
                Result *= X;
        }
        return Result;
}

void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
        uint8_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) % 10 + '0');
        }
}       

int fputc(int ch,FILE *f)
{
        Serial_SendByte(ch);
        return ch;
}

//可变参数
void Serial_Printf(char *format,...)
{
        char String;
        va_list arg;
        va_start(arg, format);
        vsprintf(String, format, arg);
        va_end(arg);
        Serial_SendString(String);
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"

int main(void)
{
        OLED_Init();

        Serial_Init();
       
        Serial_SendByte(0x41);
       
        uint8_t MyArray[] = {0x42, 0x43, 0x44, 0x45};
        Serial_SendArray(MyArray,4);
       
        Serial_SendString("\r\nNum1=");
       
        Serial_SendNumber(111,3);
       
        printf("\r\nNum2=%d",222);
       
        char String;
        sprintf(String,"\r\nNum3=%d",333);
        Serial_SendString(String);
       
        Serial_Printf("\r\nNum4=%d",444);
        Serial_Printf("\r\n");
       
        while(1)
        {
               
        }
}
 8.2 串口发送+接收

 复制 8.1 串口发送代码,接线相同不更改。
实现:停止接收。在串口助手发送数据,成功接收,并在OLED上也显示。目前只支持一个字节的接收。
https://i-blog.csdnimg.cn/direct/04d8400ff8074d1ab05d45bca5b9bee9.png
//Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <stdio.h>

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);

uint8_t Serial_GetRxFlag(void);
uint8_t Serial_GetRxData(void);

#endif




//Serial.c
#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_RxData;
uint8_t Serial_RxFlag;

void Serial_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        USART_InitTypeDef USART_InitStructure;
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART1,&USART_InitStructure);
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
       
        USART_Cmd(USART1,ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
        USART_SendData(USART1,Byte);
        while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
        uint16_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Array);
        }
}

void Serial_SendString(char *String)
{
        uint8_t i;
        for(i = 0;String != '\0'; i ++)
        {
                Serial_SendByte(String);
        }
}       

uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
        uint32_t Result = 1;
        while(Y --)
        {
                Result *= X;
        }
        return Result;
}

void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
        uint8_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) % 10 + '0');
        }
}       

int fputc(int ch,FILE *f)
{
        Serial_SendByte(ch);
        return ch;
}

//可变参数
void Serial_Printf(char *format,...)
{
        char String;
        va_list arg;
        va_start(arg, format);
        vsprintf(String, format, arg);
        va_end(arg);
        Serial_SendString(String);
}

uint8_t Serial_GetRxFlag(void)
{
        if(Serial_RxFlag == 1)
        {
                Serial_RxFlag = 0;
                return 1;
        }
        return 0;
}

uint8_t Serial_GetRxData(void)
{
        return Serial_RxData;
}

void USART1_IRQHandler(void)
{
        if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
        {
                Serial_RxData = USART_ReceiveData(USART1);
                Serial_RxFlag = 1;
                USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        }
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"

uint8_t RxData;

int main(void)
{
        OLED_Init();
        OLED_ShowString(1, 1, "RxData:");
       
        Serial_Init();
       
        while(1)
        {
                if(Serial_GetRxFlag() == 1)
                {
                        RxData = Serial_GetRxData();
                        Serial_SendByte(RxData);
                        OLED_ShowHexNum(1, 8, RxData, 2);
                }
        }
}
8.3 串口收发HEX数据包

https://i-blog.csdnimg.cn/direct/a46a55e69ce84413b831f6ce92389ec6.png
复制 8.2 的代码。 
接线:STLink和USB转TTL照常接。PB1接按键。LED长脚接电源正,短脚接PA1。
实现:串口收发Hex数据包。按下按键发送,串口接受区显示接收的数据包TxPacket。串口助手发送数据包,会在OLED上显示接收的数据包RxPacket。
https://i-blog.csdnimg.cn/direct/9a9b7dce1add4e78b8b434a1e971dc8c.jpeg

https://i-blog.csdnimg.cn/direct/6ddfa4ee35244831ae5925f383a95fc4.png
//Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <stdio.h>

extern uint8_t Serial_TxPacket[];
extern uint8_t Serial_RxPacket[];
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);

void Serial_SendPacket(void);

uint8_t Serial_GetRxFlag(void);

#endif




//Serial.c
#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_TxPacket;
uint8_t Serial_RxPacket;
uint8_t Serial_RxFlag;

void Serial_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        USART_InitTypeDef USART_InitStructure;
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART1,&USART_InitStructure);
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
       
        USART_Cmd(USART1,ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
        USART_SendData(USART1,Byte);
        while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
        uint16_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Array);
        }
}

void Serial_SendString(char *String)
{
        uint8_t i;
        for(i = 0;String != '\0'; i ++)
        {
                Serial_SendByte(String);
        }
}       

uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
        uint32_t Result = 1;
        while(Y --)
        {
                Result *= X;
        }
        return Result;
}

void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
        uint8_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) % 10 + '0');
        }
}       

int fputc(int ch,FILE *f)
{
        Serial_SendByte(ch);
        return ch;
}

//可变参数
void Serial_Printf(char *format,...)
{
        char String;
        va_list arg;
        va_start(arg, format);
        vsprintf(String, format, arg);
        va_end(arg);
        Serial_SendString(String);
}

void Serial_SendPacket(void)
{
        Serial_SendByte(0xFF);
        Serial_SendArray(Serial_TxPacket,4);
        Serial_SendByte(0xFE);
}

uint8_t Serial_GetRxFlag(void)
{
        if(Serial_RxFlag == 1)
        {
                Serial_RxFlag = 0;
                return 1;
        }
        return 0;
}


void USART1_IRQHandler(void)
{
        static uint8_t RxState = 0;
        static uint8_t pRxPacket = 0;
        if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
        {
                uint8_t RxData = USART_ReceiveData(USART1);
               
                if(RxState == 0)
                {
                        if(RxData == 0xFF)
                        {
                                RxState = 1;
                                pRxPacket = 0;
                        }
                }
                else if(RxState == 1)
                {
                        Serial_RxPacket = RxData;
                        pRxPacket ++;
                        if(pRxPacket >= 4)
                        {
                                RxState =2;
                        }
                }
                else if(RxState == 2)
                {
                        if(RxData == 0xFE)
                        {
                                RxState = 0;
                                Serial_RxFlag = 1;
                        }
                }
                USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        }
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Key.h"

uint8_t KeyNum;

int main(void)
{
        OLED_Init();
        Key_Init();
        Serial_Init();
       
        OLED_ShowString(1, 1, "TxPacket");
        OLED_ShowString(3, 1, "RxPacket");
       
        Serial_TxPacket = 0x01;
        Serial_TxPacket = 0x02;
        Serial_TxPacket = 0x03;
        Serial_TxPacket = 0x04;
       
        while(1)
        {
                KeyNum = Key_GetNum();
                if(KeyNum == 1)
                {
                        Serial_TxPacket ++;
                        Serial_TxPacket ++;
                        Serial_TxPacket ++;
                        Serial_TxPacket ++;
               
                        Serial_SendPacket();
                       
                        OLED_ShowHexNum(2, 1, Serial_TxPacket, 2);
                        OLED_ShowHexNum(2, 4, Serial_TxPacket, 2);
                        OLED_ShowHexNum(2, 7, Serial_TxPacket, 2);
                        OLED_ShowHexNum(2, 10, Serial_TxPacket, 2);
                }
                if(Serial_GetRxFlag() == 1)
                {
                        OLED_ShowHexNum(4, 1, Serial_RxPacket, 2);
                        OLED_ShowHexNum(4, 4, Serial_RxPacket, 2);
                        OLED_ShowHexNum(4, 7, Serial_RxPacket, 2);
                        OLED_ShowHexNum(4, 10, Serial_RxPacket, 2);
                }
        }
}
8.4 串口收发文本数据包

复制 8.3 代码。
实现:串口收发文本数据包。在串口助手发送“@LED_ON(回车)”,LED亮,STM32发送“LED_ON_OK\r\n”。发送“@LED_OFF(回车)”,LED灭,STM32发送“LED_OFF_OK\r\n”。发送其他则显示ERROR_COMMAND
https://i-blog.csdnimg.cn/direct/02b42579cdb84feeb7257cda26b6bb6b.png
https://i-blog.csdnimg.cn/direct/29f33de3fe2c499886dff0b116a0e1bd.jpeg
//Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <stdio.h>

extern char Serial_RxPacket[];
extern uint8_t Serial_RxFlag;
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);

#endif




//Serial.c
#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

char Serial_RxPacket;
uint8_t Serial_RxFlag;

void Serial_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        USART_InitTypeDef USART_InitStructure;
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART1,&USART_InitStructure);
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStructure);
       
        USART_Cmd(USART1,ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
        USART_SendData(USART1,Byte);
        while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
        uint16_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Array);
        }
}

void Serial_SendString(char *String)
{
        uint8_t i;
        for(i = 0;String != '\0'; i ++)
        {
                Serial_SendByte(String);
        }
}       

uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
        uint32_t Result = 1;
        while(Y --)
        {
                Result *= X;
        }
        return Result;
}

void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
        uint8_t i;
        for(i = 0;i < Length; i ++)
        {
                Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) % 10 + '0');
        }
}       

int fputc(int ch,FILE *f)
{
        Serial_SendByte(ch);
        return ch;
}

//可变参数
void Serial_Printf(char *format,...)
{
        char String;
        va_list arg;
        va_start(arg, format);
        vsprintf(String, format, arg);
        va_end(arg);
        Serial_SendString(String);
}

void USART1_IRQHandler(void)
{
        static uint8_t RxState = 0;
        static uint8_t pRxPacket = 0;
        if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
        {
                uint8_t RxData = USART_ReceiveData(USART1);
               
                if(RxState == 0)
                {
                        if(RxData == '@' && Serial_RxFlag == 0)
                        {
                                RxState = 1;
                                pRxPacket = 0;
                        }
                }
                else if(RxState == 1)
                {
                        if(RxData == '\r')
                        {
                                RxState =2;
                        }
                        else
                        {
                                Serial_RxPacket = RxData;
                                pRxPacket ++;
                        }
                }
                else if(RxState == 2)
                {
                        if(RxData == '\n')
                        {
                                RxState = 0;
                                Serial_RxPacket = '\0';
                                Serial_RxFlag = 1;
                        }
                }
                USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        }
}
//main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "LED.h"
#include <string.h>

int main(void)
{
        OLED_Init();
        LED_Init();
        Serial_Init();
       
        OLED_ShowString(1, 1, "TxPacket");
        OLED_ShowString(3, 1, "RxPacket");
       
        while(1)
        {
                if(Serial_RxFlag == 1)
                {
                        OLED_ShowString(4, 1, "                ");
                        OLED_ShowString(4, 1, Serial_RxPacket);
                       
                        if(strcmp(Serial_RxPacket,"LED_ON") == 0)
                        {
                                LED1_ON();
                                Serial_SendString("LED_ON_OK\r\n");
                                OLED_ShowString(2, 1, "                ");
                                OLED_ShowString(2, 1, "LED_ON_OK");
                        }
                        else if(strcmp(Serial_RxPacket,"LED_OFF") == 0)
                        {
                                LED1_OFF();
                                Serial_SendString("LED_OFF_OK\r\n");
                                OLED_ShowString(2, 1, "                ");
                                OLED_ShowString(2, 1, "LED_OFF_OK");
                        }
                        else{
                                Serial_SendString("ERROR_COMMAND\r\n");
                                OLED_ShowString(2, 1, "                ");
                                OLED_ShowString(2, 1, "ERROR_COMMAND");
                        }
                        Serial_RxFlag = 0;
                }
        }
}







免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: STM32初学条记