本项目基于STM32F103C8T6单片机,这是一款价格低廉,功能强大,有较多开源资料的芯片,切引脚较少,便于手工焊接非常适合作为毕业设计的材料。
本项目包含电压,电流,电阻的测量,暂时采用硬件开关控制功能,有需求的读者可以搜索“模拟开关”词条,用模拟开关芯片替代硬件开关实现纯粹用软件控制电路,也可以考虑MOS管。由于本文主要面向为大学毕设头疼的怨种朋友们,主要采用较为简单的方式实现功能。
本产品采用嘉立创EDA设计,手工焊接,细节出有不完美之处请谅解。
实物图:
上图中电阻为%1精度的51K金属膜电阻,下图为正规充电宝的输出,可见,该万用表精度可达%1.
以下是设计原理图:
STM32核心板:
测量扩展板:
PCB板设计:
对测量要求较高的项目可以用运算放大器将电压信号放大后再测量。推荐使用运算放大器虚短的特性降低测量对原电路的干扰。
源代码如下
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"
#define NEXT GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)
#define ENTER GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)
#define RETURN GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)
#define PAUSE GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)
#define V_6V6 ADC_Channel_0
#define V_36V ADC_Channel_1
#define V_73V ADC_Channel_2
#define A_330MA ADC_Channel_3
#define A_3_3A ADC_Channel_4
#define A_6_6A ADC_Channel_5
#define R_10R ADC_Channel_7
#define R_1K ADC_Channel_6
#define R_100K ADC_Channel_8
void menu_V();
void volt_measure_6V6();
void volt_measure_36V();
void volt_measure_73V();
void menu_A();
void current_measure_330mA();
void current_measure_3_3A();
void current_measure_6_6A();
void menu_R();
void resisstance_measure_10R();
void resistance_measure_1K();
void reasisance_measure_100K();
void keys_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //开启GPIOB时钟
/*GPIO初始化*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*模块初始化*/
}
void cursor(int n)
{
OLED_ShowString(1,1," ");
OLED_ShowString(2,1," ");
OLED_ShowString(3,1," ");
OLED_ShowString(4,1," ");
OLED_ShowString(n,1,"->");
}
/*---------------------------------------------主菜单-------------------------------------------------------------*/
void menu_1(void)
{
OLED_Init(); //OLED初始化
/*OLED显示*/
OLED_ShowString(1, 3, "Voltage");
OLED_ShowString(2,3,"Current");
OLED_ShowString(3,3,"Resistance");
OLED_ShowString(4,3,"Timer");
Delay_ms(200);
int n=1;
cursor(n);
while(1)
{
while((!NEXT)&&(!ENTER)&&(!RETURN)&&(!PAUSE));
if (NEXT)
{
n++;
if(n>4){n=1;}
cursor(n);
Delay_ms(200);
}
if(ENTER)
{
switch(n)
{
case(1):menu_V();break;
case(2):menu_A();break;
case(3):menu_R();break;
}
Delay_ms(200);
}
}
}
/*--------------------------------------------电压测量--------------------------------------------------------------*/
void menu_V()
{
OLED_Init(); //OLED初始化
OLED_ShowString(1, 3, " 6.6V");
OLED_ShowString(2,3," 36.3V");
OLED_ShowString(3,3," 73.5V");
Delay_ms(200);
int i=1;
cursor(i);
while(1)
{
while((!ENTER)&&(!NEXT)&&(!RETURN)&&(!PAUSE));
if(ENTER)
{
switch(i)
{
case(1):volt_measure_6V6();break;
case(2):volt_measure_36V();break;
case(3):volt_measure_73V();break;
}
}
if (NEXT)
{
i++;
if(i>3){i=1;}
cursor(i);
Delay_ms(200);
}
if(RETURN)
{
Delay_ms(200);
menu_1();
}
}
}
void volt_measure_6V6()
{
uint16_t ADValue; //定义AD值变量
float Voltage; //定义电压变量
OLED_Init(); //OLED初始化
AD_Init(); //AD初始化
OLED_ShowString(1,2,"set key.1 to 1.");
OLED_ShowString(2,2,"set key.2 to 1.");
while(1)
{
while (!RETURN)
{
ADValue= AD_GetValue(V_6V6); //获取AD转换的值
Voltage = (float)ADValue / 4095 * 3.3*2*0.971; //将AD值线性变换到0~3.3的范围,表示电压
OLED_ShowNum(3, 5, Voltage, 2); //显示电压值的整数部分
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3, 8, (uint16_t)(Voltage * 1000) % 1000, 3); //显示电压值的小数部分
OLED_ShowChar(3,11,'V');
Delay_ms(200);
}
if(RETURN){menu_V();}
}
}
void volt_measure_36V()
{
uint16_t ADValue; //定义AD值变量
float Voltage; //定义电压变量
OLED_Init(); //OLED初始化
AD_Init(); //AD初始化
OLED_ShowString(1,2,"set key.1 to 1.");
OLED_ShowString(2,2,"set key.2 to 2.");
while (1)
{
if(RETURN){menu_V();}
ADValue= AD_GetValue(V_36V); //获取AD转换的值
Voltage = (float)ADValue / 4095 * 3.3*11; //将AD值线性变换到0~3.3的范围,表示电压
OLED_ShowNum(3, 5, Voltage, 2); //显示电压值的整数部分
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3, 8, (uint16_t)(Voltage * 1000) % 1000, 3); //显示电压值的小数部分
OLED_ShowChar(3,11,'V');
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
void volt_measure_73V()
{
uint16_t ADValue; //定义AD值变量
float Voltage; //定义电压变量
OLED_Init(); //OLED初始化
AD_Init(); //AD初始化
OLED_ShowString(1,2,"set key.1 to 1.");
OLED_ShowString(2,2,"set key.2 to 3.");
while (1)
{
if(RETURN){menu_V();}
ADValue= AD_GetValue(V_73V); //获取AD转换的值
Voltage = (float)ADValue / 4095 * 3.3*(104.7/4.7); //将AD值线性变换到0~3.3的范围,表示电压
OLED_ShowNum(3, 5, Voltage, 2); //显示电压值的整数部分
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3, 8, (uint16_t)(Voltage * 1000) % 1000, 3); //显示电压值的小数部分
OLED_ShowChar(3,11,'V');
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
/*---------------------------------------电流测量-----------------------------------------------------------------*/
void menu_A()
{
OLED_Init(); //OLED初始化
OLED_ShowString(1, 3, " 330mA");
OLED_ShowString(2,3," 3.3A");
OLED_ShowString(3,3," 6.6A");
Delay_ms(200);
int i=1;
cursor(i);
while(1)
{
while((!ENTER)&&(!NEXT)&&(!RETURN)&&(!PAUSE));
if(ENTER)
{
switch(i)
{
case(1):current_measure_330mA();break;
case(2):current_measure_3_3A();break;
case(3):current_measure_6_6A();break;
}
}
if (NEXT)
{
i++;
if(i>3){i=1;}
cursor(i);
Delay_ms(200);
}
if(RETURN)
{
Delay_ms(200);
menu_1();
}
}
}
void current_measure_330mA()
{
uint16_t ADValue; //定义AD值变量
float current; //定义电压变量
OLED_Init(); //OLED初始化
AD_Init(); //AD初始化
OLED_ShowString(1,2,"set key.1 to 2");
OLED_ShowString(2,2,"set key.3 to 1");
while (1)
{
if(RETURN){menu_A();}
ADValue= AD_GetValue(A_330MA); //获取AD转换的值
current = (float)ADValue / 4095 * 3.3*100; //将AD值线性变换到0~3.3的范围,表示电压,然后求电流
OLED_ShowNum(3, 4, (int)current, 3); //显示电压值的整数部分
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3, 8, (uint16_t)(current * 1000) % 1000, 3); //显示电压值的小数部分
OLED_ShowString(3,9,"mA");
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
void current_measure_3_3A()
{
uint16_t ADValue; //定义AD值变量
float current; //定义电压变量
OLED_Init(); //OLED初始化
AD_Init(); //AD初始化
OLED_ShowString(1,2,"set key.1 to 2.");
OLED_ShowString(2,2,"set key.3 to 2.");
while (1)
{
if(RETURN){menu_A();}
ADValue= AD_GetValue(A_3_3A); //获取AD转换的值
current = (float)ADValue / 4095 * 3.3; //将AD值线性变换到0~3.3的范围,表示电压,然后求电流
OLED_ShowNum(3, 5, (int)current, 1); //显示电压值的整数部分
OLED_ShowChar(3,6,'.');
OLED_ShowNum(3, 7, (uint16_t)(current * 1000) % 1000, 3); //显示电压值的小数部分
OLED_ShowString(3,10," A");
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
void current_measure_6_6A()
{
uint16_t ADValue; //定义AD值变量
float current; //定义电压变量
OLED_Init(); //OLED初始化
AD_Init(); //AD初始化
OLED_ShowString(1,2,"set key.1 to 2.");
OLED_ShowString(2,2,"set key.3 to 3.");
while (1)
{
if(RETURN){menu_A();}
ADValue= AD_GetValue(A_6_6A); //获取AD转换的值
current = (float)ADValue / 4095 * 3.3*2; //将AD值线性变换到0~3.3的范围,表示电压,然后求电流
OLED_ShowNum(3, 5, (int)current, 1); //显示电压值的整数部分
OLED_ShowChar(3,6,'.');
OLED_ShowNum(3, 7, (uint16_t)(current * 1000) % 1000, 3); //显示电压值的小数部分
OLED_ShowString(3,10," A");
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
/*--------------------------------------------电阻测量------------------------------------------------------------*/
void menu_R()
{
OLED_Init(); //OLED初始化
OLED_ShowString(1, 3, " 1k ohm");
OLED_ShowString(2,3," 10 ohm");
OLED_ShowString(3,3," 100k ohm");
Delay_ms(200);
int i=1;
cursor(i);
while(1)
{
while((!ENTER)&&(!NEXT)&&(!RETURN)&&(!PAUSE));
if(ENTER)
{
switch(i)
{
case(1):resistance_measure_1K();break;
case(2):resisstance_measure_10R();break;
case(3):reasisance_measure_100K();break;
}
}
if (NEXT)
{
i++;
if(i>3){i=1;}
cursor(i);
Delay_ms(200);
}
if(RETURN)
{
Delay_ms(200);
menu_1();
}
}
}
void resisstance_measure_10R()
{
int ADValue;float Voltage,Resist;
OLED_Init();
AD_Init();
OLED_ShowString(1,2,"set key.1 to 3");
OLED_ShowString(2,2,"set key.4 to 2");
while(1)
{
if(RETURN){menu_R();}
ADValue= AD_GetValue(R_10R); //获取AD转换的值
Voltage = (float)ADValue / 4095 * 3.3; //将AD值线性变换到0~3.3的范围,表示电压
Resist=10*Voltage/(3.3-Voltage);
if(Resist>=1000){OLED_ShowString(3,4,"overload ");}
else
{
OLED_ShowNum(3,4,(int)Resist,3);
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3,8,(uint16_t)(Resist * 1000) % 1000,3);
OLED_ShowString(3,11," ohm");
}
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
void resistance_measure_1K()
{
int ADValue;float Voltage,Resist;
OLED_Init();
AD_Init();
OLED_ShowString(1,2,"set key.1 to 3");
OLED_ShowString(2,2,"set key.4 to 1");
while(1)
{
if(RETURN){menu_R();}
ADValue= AD_GetValue(R_1K); //获取AD转换的值
Voltage = (float)ADValue / 4095 * 3.3; //将AD值线性变换到0~3.3的范围,表示电压
Resist=Voltage/(3.3-Voltage);
if(Resist>=1000){OLED_ShowString(3,4,"overload ");}
else
{
OLED_ShowNum(3,4,(int)Resist,3);
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3,8,(uint16_t)(Resist * 1000) % 1000,3);
OLED_ShowString(3,11,"k ohm");
}
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
void reasisance_measure_100K()
{
int ADValue;float Voltage,Resist;
OLED_Init();
AD_Init();
OLED_ShowString(1,2,"set key.1 to 3");
OLED_ShowString(2,2,"set key.4 to 3");
while(1)
{
if(RETURN){menu_R();}
ADValue= AD_GetValue(R_100K); //获取AD转换的值
Voltage = (float)ADValue / 4095 * 3.3; //将AD值线性变换到0~3.3的范围,表示电压
Resist=100*Voltage/(3.3-Voltage);
if(Resist>=1e6){OLED_ShowString(3,4,"overload ");}
else
{
OLED_ShowNum(3,4,(int)Resist,3);
OLED_ShowChar(3,7,'.');
OLED_ShowNum(3,8,(uint16_t)(Resist * 1000) % 1000,3);
OLED_ShowString(3,11,"k ohm");
}
Delay_ms(200); //延时100ms,手动增加一些转换的间隔时间
}
}
/*-----------------------------------------------------------------------------------------------------------------*/
int main()
{
menu_1();
return 0;
}
#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_PP;
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清屏
}
#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
#include "stm32f10x.h" // Device header
/**
* 函 数:AD初始化
* 参 数:无
* 返 回 值:无
*/
void AD_Init(void)
{
/*开启时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //开启ADC1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟
/*设置ADC时钟*/
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //选择时钟6分频,ADCCLK = 72MHz / 6 = 12MHz
/*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_Pin_4|
GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA0、PA1、PA2和PA3引脚初始化为模拟输
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*不在此处配置规则组序列,而是在每次AD转换前配置,这样可以灵活更改AD转换的通道*/
/*ADC初始化*/
ADC_InitTypeDef ADC_InitStructure; //定义结构体变量
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //模式,选择独立模式,即单独使用ADC1
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据对齐,选择右对齐
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发,使用软件触发,不需要外部触发
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //连续转换,失能,每转换一次规则组序列后停止
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描模式,失能,只转换规则组的序列1这一个位置
ADC_InitStructure.ADC_NbrOfChannel = 1; //通道数,为1,仅在扫描模式下,才需要指定大于1的数,在非扫描模式下,只能是1
ADC_Init(ADC1, &ADC_InitStructure); //将结构体变量交给ADC_Init,配置ADC1
/*ADC使能*/
ADC_Cmd(ADC1, ENABLE); //使能ADC1,ADC开始运行
/*ADC校准*/
ADC_ResetCalibration(ADC1); //固定流程,内部有电路会自动执行校准
while (ADC_GetResetCalibrationStatus(ADC1) == SET);
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1) == SET);
}
/**
* 函 数:获取AD转换的值
* 参 数:ADC_Channel 指定AD转换的通道,范围:ADC_Channel_x,其中x可以是0/1/2/3
* 返 回 值:AD转换的值,范围:0~4095
*/
uint16_t AD_GetValue(uint8_t ADC_Channel)
{
ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_55Cycles5); //在每次转换前,根据函数形参灵活更改规则组的通道1
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //软件触发AD转换一次
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //等待EOC标志位,即等待AD转换结束
return ADC_GetConversionValue(ADC1); //读数据寄存器,得到AD转换的结果
}