要求
要求1:根据整数部分位数,动态调整小数部分精度,小数部分最大精确到小数点后4位(32位float型能保证的最大精度!),比如:总共5个位,可以显示
示例数据 | 小数精度 | 整数范围 |
---|
5.4321 | 0.0001 | 0~9 | 54.321 | 0.001 | 00~99 | 543.21 | 0.01 | 000~999 | 5432.1 | 0.1 | 0000~9999 |
要求2:能正确显示0.0和0.0001;
要求3:数要干净利落!不能有前缀和后缀0:比如3.14,不要加前缀000003.14,也不要加后缀 3.1400000;
测试数据集:{0.0001, 0, 0.0, 3.545, 3.124, 3.325, 4.1236, 99.9876, 43.125, 54. 4321, 0.1234, 54321.0}
1 Proteus 面向编程的模型搭建
2 代码实现
2.1. 实现数码管刷新
void LED_7SEG_Display_74HC245_Float(float num, uint8_t figure)
{
uint8_t de_num[NUM_DISPLAY];
uint8_t i;
uint8_t pos_data = 0x7f;
uint8_t seg_data = seg_code_cc[19];
num += 0.00005;
if(figure > NUM_DISPLAY){
figure = NUM_DISPLAY;
}
decomposeFloat(num, de_num, figure);
for(i = 0; i < figure; i++){
P1 = seg_code_cc[19];
P3 = pos_data;
pos_data = (pos_data >> 1 | 0x80);
seg_data = de_num[figure - i - 1];
P1 = seg_data;
delay_ms(2);
}
}
2.2 对小数进行分解的函数
#define NUM_DISPLAY 8
static uint8_t decomposeFloat(float f, uint8_t *decompose, uint8_t whole_figure)
{
uint8_t i = 0;
uint32_t int_part = (uint32_t)f;
uint8_t cnt_int_pos = 0;
uint8_t cnt_float_pos = 0;
uint8_t int_part_helper[8];
if(int_part == 0){
decompose[i] = seg_code_cc[0];
cnt_int_pos = 1;
i++;
}else{
while(int_part){
int_part_helper[i] = seg_code_cc[int_part % 10];
int_part = int_part / 10;
i++;
}
cnt_int_pos = i;
for(i = 0; i < cnt_int_pos; i++){
decompose[i] = int_part_helper[(cnt_int_pos - 1) - i];
}
}
decompose[i-1] |= seg_code_cc[17];
cnt_float_pos = whole_figure - i;
for(i = 0; i < cnt_float_pos; i++){
f *= 10;
}
int_part = (uint32_t)f;
for(i = 0; i < cnt_float_pos; i++){
decompose[whole_figure - i - 1] = seg_code_cc[int_part % 10];
int_part /= 10;
}
return cnt_float_pos;
}
3 测试用例
int main(void)
{
uint16_t i;
while(1) {
for(i = 0; i < 40; i++){
LED_7SEG_Display_74HC245_Float(0.0001, 5);
}
for(i = 0; i < 400; i++){
LED_7SEG_Display_74HC245_Float(0, 2);
}
for(i = 0; i < 200; i++){
LED_7SEG_Display_74HC245_Float(0.0, 2);
}
for(i = 0; i < 100; i++){
LED_7SEG_Display_74HC245_Float(3.545, 4);
}
for(i = 0; i < 100; i++){
LED_7SEG_Display_74HC245_Float(3.124, 4);
}
for(i = 0; i < 100; i++){
LED_7SEG_Display_74HC245_Float(3.325, 4);
}
for(i = 0; i < 40; i++){
LED_7SEG_Display_74HC245_Float(4.1236, 5);
}
for(i = 0; i < 60; i++){
LED_7SEG_Display_74HC245_Float(99.9876, 6);
}
for(i = 0; i < 100; i++){
LED_7SEG_Display_74HC245_Float(43.125, 5);
}
for(i = 0; i < 50; i++){
LED_7SEG_Display_74HC245_Float(54.4321, 6);
}
for(i = 0; i < 60; i++){
LED_7SEG_Display_74HC245_Float(0.1234, 5);
}
for(i = 0; i < 60; i++){
LED_7SEG_Display_74HC245_Float(54321.0, 6);
}
};
}
4 仿真结果
|