📅  最后修改于: 2023-12-03 15:06:56.255000             🧑  作者: Mango
在一些嵌入式系统开发中,常常需要在七段LED上显示程序结果。为了让结果更加清晰明了,我们可以使用底漆技术。
底漆技术是一种在数字管上显示数字的技术,通过给数字添加底色,使数字更加醒目。
底漆技术可以分为两种,一种是直接在数字管上添加底漆,另一种是通过控制RGB三个颜色的亮度,来显示出相应的底色。
直接添加底漆的实现原理是,在数字管上添加一个不透明的底漆层,将底漆层的数字位置切除,就可以在数字管上显示相应的底漆。
控制RGB三个颜色的亮度的实现原理是,通过控制红、绿、蓝三个颜色亮灭的时间比例,来生成所需的底漆颜色。
下面给出一个使用STM32开发板,在七段LED上实现底漆技术的代码示例,主要是通过控制RGB三个颜色的亮灭时间比例来实现底漆效果。
#include "stm32f10x.h"
#define SEG1 GPIO_Pin_0
#define SEG2 GPIO_Pin_1
#define SEG3 GPIO_Pin_2
#define SEG4 GPIO_Pin_3
#define SEG5 GPIO_Pin_4
#define SEG6 GPIO_Pin_5
#define SEG7 GPIO_Pin_6
#define SEG8 GPIO_Pin_7
#define RED TIM_OCMode_PWM2
#define GREEN TIM_OCMode_PWM1
#define BLUE TIM_OCMode_PWM1
#define MAX 9
int digit[MAX][8]={
{1,1,1,1,1,1,0,0},
{0,1,1,0,0,0,0,0},
{1,1,0,1,1,0,1,0},
{1,1,1,1,0,0,1,0},
{0,1,1,0,0,1,1,0},
{1,0,1,1,0,1,1,0},
{1,0,1,1,1,1,1,0},
{1,1,1,0,0,0,0,0},
{1,1,1,1,1,1,1,0}
};
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = SEG1 | SEG2 | SEG3 | SEG4 | SEG5 | SEG6 | SEG7 | SEG8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = RED;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = GREEN;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = BLUE;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
void Display(int num)
{
int i;
for(i=0; i<8; i++)
{
if(digit[num][i]==1)
{
GPIO_SetBits(GPIOA, SEG1<<i);
TIM_SetCompare3(TIM3, 2500); //设置红色占空比为25%
TIM_SetCompare2(TIM3, 1500); //设置绿色占空比为15%
TIM_SetCompare1(TIM3, 500); //设置蓝色占空比为5%
}
else
{
GPIO_ResetBits(GPIOA, SEG1<<i);
TIM_SetCompare3(TIM3, 0); //关闭红色
TIM_SetCompare2(TIM3, 0); //关闭绿色
TIM_SetCompare1(TIM3, 0); //关闭蓝色
}
delay(5); //延时5ms,使数字更清晰明了
}
}
int main(void)
{
SystemInit();
GPIO_Configuration();
TIM3_Configuration();
int i;
while(1)
{
for(i=0; i<MAX; i++)
{
Display(i);
}
}
}
底漆技术的优点是使得数字更加清晰明了,缺点是需要多个颜色的控制,增加了系统复杂度。在实际开发中,需要根据具体的场景和需求来选择是否使用底漆技术。