• 单片机成长之路(51基础篇)


    写了一个方便的头文件:

      1 // 作者博客:https://www.cnblogs.com/jikexianfeng/
      2 // ---------------------------------------------------------------------------------------------------------------
      3 // 头文件功能简介:编写了一个驱动八段4位数码管的头文件:
      4 // 所有函数都是按位操作的:
      5 // led_Digital_Tube_Positioning_dynamic_display_data_int(value)        // 显示整形 value 的值(带符号)
      6 // led_Digital_Tube_Positioning_dynamic_display_data_float(value)    // 显示浮点 value 的值(无符号)
      7 // value 数据大于最大显示,显示 FFFF
      8 // ---------------------------------------------------------------------------------------------------------------
      9 # include "bsp.h"                    // 板卡的gpio宏定义,定义内容如下
     10 // ---------------------------------------------------------------------------------------------------------------
     11 //// lcd 数码管
     12 //sfr    LED_Digital_all   = 0x80;
     13 //sbit LED_Digital_A = P0^0;
     14 //sbit LED_Digital_B = P0^1;
     15 //sbit LED_Digital_C = P0^2;
     16 //sbit LED_Digital_D = P0^3;
     17 //sbit LED_Digital_E = P0^4;
     18 //sbit LED_Digital_F = P0^5;
     19 //sbit LED_Digital_G = P0^6;
     20 //sbit LED_Digital_DP = P0^7;
     21 
     22 //sbit LED_Digital_1 = P2^4;
     23 //sbit LED_Digital_2 = P2^5;
     24 //sbit LED_Digital_3 = P2^6;
     25 //sbit LED_Digital_4 = P2^7;
     26 
     27 
     28 void LED_Digital_delay_bps(int count){
     29     while(count--);
     30 }
     31 #define LED_Digital_delay()    LED_Digital_delay_bps(400)
     32 // ---------------------------------------------------------------------------------------------------------------
     33 // gpio 有效电平
     34 #define Low_level     0        // 低电平有效
     35 #define High_level    1        // 高电平有效
     36 #define led_data_Effective_level        High_level        // 配置数码管字符有效电平(我的板卡是高电平有效,配置为:High_level)
     37 #define led_position_Effective_level    High_level        // 配置数码管位选有效电平(我的板卡是高电平有效,配置为:High_level)
     38 // ---------------------------------------------------------------------------------------------------------------
     39 // 选中
     40 #define Selecteds_position_1    LED_Digital_1 = led_position_Effective_level
     41 #define Selecteds_position_2    LED_Digital_2 = led_position_Effective_level
     42 #define Selecteds_position_3    LED_Digital_3 = led_position_Effective_level
     43 #define Selecteds_position_4    LED_Digital_4 = led_position_Effective_level
     44 #define Selecteds_position_all     Selecteds_position_1;Selecteds_position_2;Selecteds_position_3;Selecteds_position_4
     45 // 放弃
     46 #define Abandon_position_1        LED_Digital_1 = (!led_position_Effective_level)
     47 #define Abandon_position_2        LED_Digital_2 = (!led_position_Effective_level)
     48 #define Abandon_position_3        LED_Digital_3 = (!led_position_Effective_level)
     49 #define Abandon_position_4        LED_Digital_4 = (!led_position_Effective_level)
     50 #define Abandon_position_all     Abandon_position_1;Abandon_position_2;Abandon_position_3;Abandon_position_4
     51 // ---------------------------------------------------------------------------------------------------------------
     52 // 借鉴 #include "led_device.h" 方法一: gpio 物理上连续 pin Program
     53 void led_Digital_Tube_all_gpio_status_group(uchar collection,char status){
     54     // 配置 LED GPIO
     55     int status_value;
     56     led_data_Effective_level? (status_value = LED_Digital_all):(status_value = ~LED_Digital_all);
     57     status?(status_value = status_value & (~collection)):(status_value = status_value | collection);
     58     led_data_Effective_level?(LED_Digital_all=status_value):(LED_Digital_all=~status_value);
     59 }
     60 // group
     61 #define set_led_Digital_Tube_all_gpio_status_group(collection)                 led_Digital_Tube_all_gpio_status_group(collection,1)
     62 #define clean_led_Digital_Tube_all_gpio_status_group(collection)             led_Digital_Tube_all_gpio_status_group(collection,0)
     63 // 消影
     64 #define led_Digital_Tube_all_Eliminate_shadow()            Selecteds_position_all;clean_led_Digital_Tube_all_gpio_status_group(0xff);Abandon_position_all
     65 // ---------------------------------------------------------------------------------------------------------------
     66 code uchar Digital_Tube_coding[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40,0x08};    
     67 code char Digital_Tube_Decimal_point = 0x80;
     68 // ---------------------------------------------------------------------------------------------------------------
     69 #define Calculation_int     0
     70 #define Calculation_float    1
     71 #define Calculation_status    Calculation_int
     72 // ---------------------------------------------------------------------------------------------------------------
     73 // 获取编码
     74 uchar git_Digital_Tube_value_coding(char Serial_number,char sign){
     75     uchar coding_value;
     76     if((0x00 <= Serial_number)&(Serial_number<0x10))coding_value = Digital_Tube_coding[Serial_number];
     77     else {
     78         switch(Serial_number){
     79             case ' ':{coding_value = Digital_Tube_coding[16];}break;
     80             case '-':coding_value = Digital_Tube_coding[17];break;
     81             case '_':coding_value = Digital_Tube_coding[18];break;
     82         }
     83     }
     84     if(sign)coding_value |=  Digital_Tube_Decimal_point;
     85     led_data_Effective_level?(coding_value = ~coding_value):(coding_value = coding_value);
     86     return coding_value;
     87 }
     88 // 静态输出字符
     89 void led_Digital_Tube_Positioning_Static_display_data(char Positioning,char value,char Decimal_point_status){
     90     led_Digital_Tube_all_Eliminate_shadow();
     91     switch(Positioning){
     92         case 0x00:{set_led_Digital_Tube_all_gpio_status_group(git_Digital_Tube_value_coding(value,Decimal_point_status));Selecteds_position_all;};break;
     93         case 0x01:{set_led_Digital_Tube_all_gpio_status_group(git_Digital_Tube_value_coding(value,Decimal_point_status));Selecteds_position_1;};break;
     94         case 0x02:{set_led_Digital_Tube_all_gpio_status_group(git_Digital_Tube_value_coding(value,Decimal_point_status));Selecteds_position_2;};break;
     95         case 0x03:{set_led_Digital_Tube_all_gpio_status_group(git_Digital_Tube_value_coding(value,Decimal_point_status));Selecteds_position_3;};break;
     96         case 0x04:{set_led_Digital_Tube_all_gpio_status_group(git_Digital_Tube_value_coding(value,Decimal_point_status));Selecteds_position_4;};break;
     97     }
     98 }
     99 #define led_Digital_Tube_Positioning_Static_display_data_int(Positioning,value) led_Digital_Tube_Positioning_Static_display_data(Positioning,value,0)
    100 #define led_Digital_Tube_Positioning_Static_display_data_float(Positioning,value,Decimal_point_status) led_Digital_Tube_Positioning_Static_display_data(Positioning,value,Decimal_point_status)
    101 // ---------------------------------------------------------------------------------------------------------------
    102 #include <math.h>
    103 #define git_bit_data(position) (0x01<<position)                // bit位获取
    104 #define git_Decimal_bit_data(p)        pow(10,p)
    105 // ---------------------------------------------------------------------------------------------------------------
    106 #define display_data_Digits_MAX        4
    107 char git_display_data_Digits(int value);
    108 char git_display_data_Digits(int value){
    109     int count = 0;
    110     if(value <= 0)return  count = 0;
    111     else {
    112         count = git_display_data_Digits((value/10)) + 1;
    113     }
    114     return count;
    115 }
    116 // 动态数据输出
    117 led_Digital_Tube_Positioning_dynamic_display_data_process(int value,char Reserved_digits){
    118     char count;
    119     for(count = 0;count <4;count++){
    120         if(count == (display_data_Digits_MAX - 1)){
    121             if(value < 0)led_Digital_Tube_Positioning_Static_display_data((count+1),'-',0);
    122             else led_Digital_Tube_Positioning_Static_display_data((count+1),(abs(value)/((int)git_Decimal_bit_data(count))%10),(count?(count == Reserved_digits):count));
    123         }
    124         else led_Digital_Tube_Positioning_Static_display_data((count+1),(((value<0)?(128+value):value)/((int)git_Decimal_bit_data(count))%10),(count?(count == Reserved_digits):count)); 
    125     }
    126 }
    127 #define led_Digital_Tube_Positioning_dynamic_display_data_error()    led_Digital_Tube_Positioning_Static_display_data(0x00,0x0f,0)
    128 
    129 // 动态输出整数(正负)
    130 void led_Digital_Tube_Positioning_dynamic_display_data_value(int value,char Reserved_digits){
    131     if(value > (git_Decimal_bit_data(display_data_Digits_MAX-1) - 1))led_Digital_Tube_Positioning_dynamic_display_data_error();
    132     else led_Digital_Tube_Positioning_dynamic_display_data_process(value,Reserved_digits);
    133     LED_Digital_delay();
    134 }
    135 
    136 #define led_Digital_Tube_Positioning_dynamic_display_data_int(value)                             led_Digital_Tube_Positioning_dynamic_display_data_value(value,0)
    137 #define led_Digital_Tube_Positioning_dynamic_display_data_float(value,Decimal_point_status)     led_Digital_Tube_Positioning_dynamic_display_data_value(((int)(value*git_Decimal_bit_data(Decimal_point_status))),Decimal_point_status)
    138 
    139  

    // --------------------------------------------------------------------------------------------------------

    默认stc51单片机举例,浮点支持不是很好,

    本代码默认支持4位的八段数码管,需要扩位的自行修改代码。

    创作不易,转载说明出处,谢谢!

     // --------------------------------------------------------------------------------------------------------

  • 相关阅读:
    记录MySQL中优化sql语句查询常用的30种方法
    记录分布式和集群的区别
    TCP的三次握手与四次挥手理解及面试题(很全面)
    记录Linux常用命令大全
    DNS解析流程
    dup和dup2用法小结
    c++多态的实现
    linux下常见的字符串处理
    ncurses库的一些函数
    用两个栈实现一个队列
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/13636591.html
Copyright © 2020-2023  润新知