STM32标准库基础知识-2

简介: STM32标准库基础知识

STM32标准库基础知识-1

https://developer.aliyun.com/article/1508378


3.按键和传感器示例

硬件原理图:

由原理图可知左边两种电路引脚要设为上拉或下拉输入模式,右边两种电路引脚可设为浮空输入模式,因为已经外置了上下拉电阻,一般都用上面两种接法

(1)按键控制LED亮灭

key.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
 
/**
  * 函    数:按键初始化
  * 参    数:无
  * 返 回 值:无
  */
void Key_Init(void)
{
  /*开启时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);   //开启GPIOB的时钟
  
  /*GPIO初始化*/
  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);            //将PB1和PB11引脚初始化为上拉输入
}
 
/**
  * 函    数:按键获取键码
  * 参    数:无
  * 返 回 值:按下按键的键码值,范围:0~2,返回0代表没有按键按下
  * 注意事项:此函数是阻塞式操作,当按键按住不放时,函数会卡住,直到按键松手
  */
uint8_t Key_GetNum(void)
{
  uint8_t KeyNum = 0;   //定义变量,默认键码值为0
  
  if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)      //读PB1输入寄存器的状态,如果为0,则代表按键1按下
  {
    Delay_ms(20);                     //延时消抖
    while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);  //等待按键松手
    Delay_ms(20);                     //延时消抖
    KeyNum = 1;                       //置键码为1
  }
  
  if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)     //读PB11输入寄存器的状态,如果为0,则代表按键2按下
  {
    Delay_ms(20);                     //延时消抖
    while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0); //等待按键松手
    Delay_ms(20);                     //延时消抖
    KeyNum = 2;                       //置键码为2
  }
  
  return KeyNum;      //返回键码值,如果没有按键按下,所有if都不成立,则键码为默认值0
}

key.h

#ifndef __KEY_H
#define __KEY_H
 
void Key_Init(void);
uint8_t Key_GetNum(void);
 
#endif

led.c

#include "stm32f10x.h"                  // Device header
 
/**
  * 函    数:LED初始化
  * 参    数:无
  * 返 回 值:无
  */
void LED_Init(void)
{
  /*开启时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);   //开启GPIOA的时钟
  
  /*GPIO初始化*/
  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);            //将PA1和PA2引脚初始化为推挽输出
  
  /*设置GPIO初始化后的默认电平*/
  GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);       //设置PA1和PA2引脚为高电平
}
 
/**
  * 函    数:LED1开启
  * 参    数:无
  * 返 回 值:无
  */
void LED1_ON(void)
{
  GPIO_ResetBits(GPIOA, GPIO_Pin_1);    //设置PA1引脚为低电平
}
 
/**
  * 函    数:LED1关闭
  * 参    数:无
  * 返 回 值:无
  */
void LED1_OFF(void)
{
  GPIO_SetBits(GPIOA, GPIO_Pin_1);    //设置PA1引脚为高电平
}
 
/**
  * 函    数:LED1状态翻转
  * 参    数:无
  * 返 回 值:无
  */
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引脚为低电平
  }
}
 
/**
  * 函    数:LED2开启
  * 参    数:无
  * 返 回 值:无
  */
void LED2_ON(void)
{
  GPIO_ResetBits(GPIOA, GPIO_Pin_2);    //设置PA2引脚为低电平
}
 
/**
  * 函    数:LED2关闭
  * 参    数:无
  * 返 回 值:无
  */
void LED2_OFF(void)
{
  GPIO_SetBits(GPIOA, GPIO_Pin_2);    //设置PA2引脚为高电平
}
 
/**
  * 函    数:LED2状态翻转
  * 参    数:无
  * 返 回 值:无
  */
void LED2_Turn(void)
{
  if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0)   //获取输出寄存器的状态,如果当前引脚输出低电平
  {                                                  
    GPIO_SetBits(GPIOA, GPIO_Pin_2);                  //则设置PA2引脚为高电平
  }                                                  
  else                                                  //否则,即当前引脚输出高电平
  {                                                  
    GPIO_ResetBits(GPIOA, GPIO_Pin_2);                //则设置PA2引脚为低电平
  }
}

led.h

#ifndef __LED_H
#define __LED_H
 
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED1_Turn(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED2_Turn(void);
 
#endif

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
 
uint8_t KeyNum;   //定义用于接收按键键码的变量
 
int main(void)
{
  /*模块初始化*/
  LED_Init();   //LED初始化
  Key_Init();   //按键初始化
  
  while (1)
  {
    KeyNum = Key_GetNum();    //获取按键键码
    
    if (KeyNum == 1)      //按键1按下
    {
      LED1_Turn();      //LED1翻转
    }
    
    if (KeyNum == 2)      //按键2按下
    {
      LED2_Turn();      //LED2翻转
    }
  }
}

(2)光敏传感器控制蜂鸣器

buzzer.c

#include "stm32f10x.h"                  // Device header
 
/**
  * 函    数:蜂鸣器初始化
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_Init(void)
{
  /*开启时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);   //开启GPIOB的时钟
  
  /*GPIO初始化*/
  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);            //将PB12引脚初始化为推挽输出
  
  /*设置GPIO初始化后的默认电平*/
  GPIO_SetBits(GPIOB, GPIO_Pin_12);             //设置PB12引脚为高电平
}
 
/**
  * 函    数:蜂鸣器开启
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_ON(void)
{
  GPIO_ResetBits(GPIOB, GPIO_Pin_12);   //设置PB12引脚为低电平
}
 
/**
  * 函    数:蜂鸣器关闭
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_OFF(void)
{
  GPIO_SetBits(GPIOB, GPIO_Pin_12);   //设置PB12引脚为高电平
}
 
/**
  * 函    数:蜂鸣器状态翻转
  * 参    数:无
  * 返 回 值:无
  */
void Buzzer_Turn(void)
{
  if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0)    //获取输出寄存器的状态,如果当前引脚输出低电平
  {
    GPIO_SetBits(GPIOB, GPIO_Pin_12);           //则设置PB12引脚为高电平
  }
  else                            //否则,即当前引脚输出高电平
  {
    GPIO_ResetBits(GPIOB, GPIO_Pin_12);           //则设置PB12引脚为低电平
  }
}

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

lightsensor.c

#include "stm32f10x.h"                  // Device header
 
/**
  * 函    数:光敏传感器初始化
  * 参    数:无
  * 返 回 值:无
  */
void LightSensor_Init(void)
{
  /*开启时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);   //开启GPIOB的时钟
  
  /*GPIO初始化*/
  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);            //将PB13引脚初始化为上拉输入
}
 
/**
  * 函    数:获取当前光敏传感器输出的高低电平
  * 参    数:无
  * 返 回 值:光敏传感器输出的高低电平,范围:0/1
  */
uint8_t LightSensor_Get(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);     //返回PB13输入寄存器的状态
}

lightsensor.h

#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H
 
void LightSensor_Init(void);
uint8_t LightSensor_Get(void);
 
#endif

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Buzzer.h"
#include "LightSensor.h"
 
int main(void)
{
  /*模块初始化*/
  Buzzer_Init();      //蜂鸣器初始化
  LightSensor_Init();   //光敏传感器初始化
  
  while (1)
  {
    if (LightSensor_Get() == 1)   //如果当前光敏输出1
    {
      Buzzer_ON();        //蜂鸣器开启
    }
    else              //否则
    {
      Buzzer_OFF();       //蜂鸣器关闭
    }
  }
}

三、OLED调试工具

常用三种调试方式如下:


  • 串口调试:通过串口通信,将调试信息发送到电脑端,电脑使用串口助手显示调试信息
  • 显示屏调试:直接将显示屏连接到单片机,将调试信息打印在显示屏上
  • Keil调试模式:借助Keil软件的调试模式,可使用单步运行、设置断点、查看寄存器及变量等功能


1.OLED介绍

硬件电路:四针脚用I2C协议,七针脚用SPI协议

在这里我们使用OLED驱动函数进行调试,如下图:


2.OLED驱动代码

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);
}
 
/**
  * @brief  I2C开始
  * @param  无
  * @retval 无
  */
void OLED_I2C_Start(void)
{
  OLED_W_SDA(1);
  OLED_W_SCL(1);
  OLED_W_SDA(0);
  OLED_W_SCL(0);
}
 
/**
  * @brief  I2C停止
  * @param  无
  * @retval 无
  */
void OLED_I2C_Stop(void)
{
  OLED_W_SDA(0);
  OLED_W_SCL(1);
  OLED_W_SDA(1);
}
 
/**
  * @brief  I2C发送一个字节
  * @param  Byte 要发送的一个字节
  * @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);
}
 
/**
  * @brief  OLED写命令
  * @param  Command 要写入的命令
  * @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();
}
 
/**
  * @brief  OLED写数据
  * @param  Data 要写入的数据
  * @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();
}
 
/**
  * @brief  OLED设置光标位置
  * @param  Y 以左上角为原点,向下方向的坐标,范围:0~7
  * @param  X 以左上角为原点,向右方向的坐标,范围: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位
}
 
/**
  * @brief  OLED清屏
  * @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);
    }
  }
}
 
/**
  * @brief  OLED显示一个字符
  * @param  Line 行位置,范围:1~4
  * @param  Column 列位置,范围:1~16
  * @param  Char 要显示的一个字符,范围: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[Char - ' '][i]);      //显示上半部分内容
  }
  OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
  for (i = 0; i < 8; i++)
  {
    OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]);    //显示下半部分内容
  }
}
 
/**
  * @brief  OLED显示字符串
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  String 要显示的字符串,范围:ASCII可见字符
  * @retval 无
  */
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
  uint8_t i;
  for (i = 0; String[i] != '\0'; i++)
  {
    OLED_ShowChar(Line, Column + i, String[i]);
  }
}
 
/**
  * @brief  OLED次方函数
  * @retval 返回值等于X的Y次方
  */
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
  uint32_t Result = 1;
  while (Y--)
  {
    Result *= X;
  }
  return Result;
}
 
/**
  * @brief  OLED显示数字(十进制,正数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~4294967295
  * @param  Length 要显示数字的长度,范围: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');
  }
}
 
/**
  * @brief  OLED显示数字(十进制,带符号数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:-2147483648~2147483647
  * @param  Length 要显示数字的长度,范围: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');
  }
}
 
/**
  * @brief  OLED显示数字(十六进制,正数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~0xFFFFFFFF
  * @param  Length 要显示数字的长度,范围: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');
    }
  }
}
 
/**
  * @brief  OLED显示数字(二进制,正数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  * @param  Length 要显示数字的长度,范围: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');
  }
}
 
/**
  * @brief  OLED初始化
  * @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.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.font.h

#ifndef __OLED_FONT_H
#define __OLED_FONT_H
 
/*OLED字模库,宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16]=
{
  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初始化
  
  /*OLED显示*/
  OLED_ShowChar(1, 1, 'A');       //1行1列显示字符A
  
  OLED_ShowString(1, 3, "HelloWorld!"); //1行3列显示字符串HelloWorld!
  
  OLED_ShowNum(2, 1, 12345, 5);     //2行1列显示十进制数字12345,长度为5
  
  OLED_ShowSignedNum(2, 7, -66, 2);   //2行7列显示有符号十进制数字-66,长度为2
  
  OLED_ShowHexNum(3, 1, 0xAA55, 4);   //3行1列显示十六进制数字0xA5A5,长度为4
  
  OLED_ShowBinNum(4, 1, 0xAA55, 16);    //4行1列显示二进制数字0xA5A5,长度为16
                      //C语言无法直接写出二进制数字,故需要用十六进制表示
  
  while (1)
  {
    
  }
}
相关文章
|
5月前
使用STM32F103标准库实现定时器控制LED点亮和关闭
通过这篇博客,我们学习了如何使用STM32F103标准库,通过定时器来控制LED的点亮和关闭。我们配置了定时器中断,并在中断处理函数中实现了LED状态的切换。这是一个基础且实用的例子,适合初学者了解STM32定时器和中断的使用。 希望这篇博客对你有所帮助。如果有任何问题或建议,欢迎在评论区留言。
433 2
|
4月前
stm32f407探索者开发板(十七)——串口寄存器库函数配置方法
stm32f407探索者开发板(十七)——串口寄存器库函数配置方法
703 0
|
5月前
|
IDE 开发工具
使用STM32F103标准库实现自定义键盘
通过本文,我们学习了如何使用STM32F103标准库实现一个简单的自定义键盘。我们首先初始化了GPIO引脚,然后实现了一个扫描函数来检测按键状态。这个项目不仅能够帮助我们理解STM32的GPIO配置和按键扫描原理,还可以作为进一步学习中断处理和低功耗设计的基础。希望本文对你有所帮助,祝你在嵌入式开发的道路上不断进步!
506 4
|
5月前
|
传感器
【经典案例】STM32F407使用HAL库配置I2C详解
STM32F407是一个强大的微控制器,广泛应用于嵌入式系统中。在许多应用中,我们需要使用I2C总线来与传感器、EEPROM、显示屏等外设进行通信。本文将详细介绍如何使用STM32 HAL库来配置和使用I2C接口。
676 2
|
5月前
|
存储 数据采集 数据安全/隐私保护
使用STM32F103读取TF卡并模拟U盘:使用标准库实现
通过以上步骤,你可以实现用STM32F103将TF卡内容变成U盘进行读取。这种功能在数据采集、便携式存储设备等应用中非常有用。如果你有更多的需求,可以进一步扩展此项目,例如添加文件管理功能、加密存储等。希望这篇博客能帮到你,如果有任何问题,欢迎在评论区留言讨论!
230 1
|
5月前
|
开发者
【经典案例】使用HAL库配置STM32F407的SPI外设
在嵌入式系统开发中,STM32F407是一款广泛应用的微控制器,而SPI(Serial Peripheral Interface)是一种常用的通信接口。本文将详细介绍如何使用STM32的硬件抽象层(HAL)库配置STM32F407的SPI外设,并提供完整的代码示例。
556 1
|
4月前
|
传感器 编解码 API
【STM32开发入门】温湿度监测系统实战:SPI LCD显示、HAL库应用、GPIO配置、UART中断接收、ADC采集与串口通信全解析
SPI(Serial Peripheral Interface)是一种同步串行通信接口,常用于微控制器与外围设备间的数据传输。SPI LCD是指使用SPI接口与微控制器通信的液晶显示屏。这类LCD通常具有较少的引脚(通常4个:MISO、MOSI、SCK和SS),因此在引脚资源有限的系统中非常有用。通过SPI协议,微控制器可以向LCD发送命令和数据,控制显示内容和模式。
157 0
|
5月前
|
芯片
【STM32】STM32简述中断的基础知识
【STM32】STM32简述中断的基础知识
|
6月前
|
传感器 存储 缓存