单片机成长之路(51基础篇) - 027 lcd排灯驱动文件

写了一个方法比较全面的头文件:

  1 // 作者博客:https://www.cnblogs.com/jikexianfeng/
  2 // ---------------------------------------------------------------------------------------------------------------
  3 // 头文件功能简介:编写了一个驱动led灯的头文件:
  4 // 所有函数都是按位操作的:
  5 // set_xxx_xxx_xxxx_xxxx_xxx(collection)        // 设置选中位为有效状态
  6 //    set_xxx_xxx_xxxx_xxxx_xxx(0x05)                    // 设置gpio 0,3 引脚为有效状态
  7 // clean_xxx_xxx_xxxx_xxxx_xxx(collection)        // 清除选中位为有效状态
  8 //     clean_xxx_xxx_xxxx_xxxx_xxx(0x05)                // 清除gpio 0,3 引脚为有效状态
  9 // ---------------------------------------------------------------------------------------------------------------
 10 # include <intrins.h>                // 左右移位头文件
 11 # include "bsp.h"                    // 板卡的gpio宏定义,定义内容如下
 12 // ---------------------------------------------------------------------------------------------------------------
 13 //sfr    LED_all   = 0x90;
 14 //sbit LED_0 = P1^0;
 15 //sbit LED_1 = P1^1;
 16 //sbit LED_2 = P1^2;
 17 //sbit LED_3 = P1^3;
 18 //sbit LED_4 = P1^4;
 19 //sbit LED_5 = P1^5;
 20 //sbit LED_6 = P1^6;
 21 //sbit LED_7 = P1^7;
 22 // ---------------------------------------------------------------------------------------------------------------
 23 // 真值表
 24 // led_Effective_level    status     result
 25 //     0                    0        1
 26 //    0                    1        0
 27 //    1                    0        0
 28 //    1                    1        1
 29 // 公式:result=(led_Effective_level == status)
 30 // ---------------------------------------------------------------------------------------------------------------
 31 #define git_bit_data(position) (0x01<<position)                // bit位获取
 32 // ---------------------------------------------------------------------------------------------------------------
 33 // gpio 有效电平
 34 #define Low_level     0        // 低电平有效
 35 #define High_level    1        // 高电平有效
 36 #define led_Effective_level    Low_level        // 配置板卡有效电平(我的板卡是低电平有效,配置为:Low_level)
 37 // ---------------------------------------------------------------------------------------------------------------
 38 // gpio 物理上连续 pin Program
 39 void led_all_gpio_status_group(uchar collection,char status){
 40     // 配置 LED GPIO
 41     int status_value;
 42     led_Effective_level? (status_value = LED_all):(status_value = ~LED_all);
 43     status?(status_value = status_value | collection):(status_value = status_value & (~collection));
 44     led_Effective_level?(LED_all=status_value):(LED_all=~status_value);
 45 }
 46 // gpio 物理上不连续 直接 com IO 方法
 47 void led_all_gpio_status_IO(uchar collection,char status){
 48     int i;
 49     for(i=0;i<8;i++){
 50         switch((collection&git_bit_data(i))){
 51             case 0x00:{;}break;
 52             case 0x01:{(led_Effective_level==status) ? (LED_0 |= (led_Effective_level==status)):(LED_0 &= (led_Effective_level==status));}break;
 53             case 0x02:{(led_Effective_level==status) ? (LED_1 |= (led_Effective_level==status)):(LED_1 &= (led_Effective_level==status));}break;
 54             case 0x04:{(led_Effective_level==status) ? (LED_2 |= (led_Effective_level==status)):(LED_2 &= (led_Effective_level==status));}break;
 55             case 0x08:{(led_Effective_level==status) ? (LED_3 |= (led_Effective_level==status)):(LED_3 &= (led_Effective_level==status));}break;
 56             case 0x10:{(led_Effective_level==status) ? (LED_4 |= (led_Effective_level==status)):(LED_4 &= (led_Effective_level==status));}break;
 57             case 0x20:{(led_Effective_level==status) ? (LED_5 |= (led_Effective_level==status)):(LED_5 &= (led_Effective_level==status));}break;
 58             case 0x40:{(led_Effective_level==status) ? (LED_6 |= (led_Effective_level==status)):(LED_6 &= (led_Effective_level==status));}break;
 59             case 0x80:{(led_Effective_level==status) ? (LED_7 |= (led_Effective_level==status)):(LED_7 &= (led_Effective_level==status));}break;
 60             default:{;};break;
 61         }
 62     }
 63 }
 64 // gpio 物理上不连续,通过结构体进行连续
 65 struct gpio_Physical_continuity_Splicing{
 66     uchar gpio_0:1;
 67     uchar gpio_1:1;
 68     uchar gpio_2:1;
 69     uchar gpio_3:1;
 70     uchar gpio_4:1;
 71     uchar gpio_5:1;
 72     uchar gpio_6:1;
 73     uchar gpio_7:1;
 74 };
 75 // 获取gpio虚拟组值
 76 uchar git_Discontinuous_gpio_group(void){
 77     struct gpio_Physical_continuity_Splicing lcd_gpio_Splicing;
 78     uchar * ptr = (uchar *)(&lcd_gpio_Splicing);
 79     lcd_gpio_Splicing.gpio_0 = LED_0;
 80     lcd_gpio_Splicing.gpio_1 = LED_1;
 81     lcd_gpio_Splicing.gpio_2 = LED_2;
 82     lcd_gpio_Splicing.gpio_3 = LED_3;
 83     lcd_gpio_Splicing.gpio_4 = LED_4;
 84     lcd_gpio_Splicing.gpio_5 = LED_5;
 85     lcd_gpio_Splicing.gpio_6 = LED_6;
 86     lcd_gpio_Splicing.gpio_7 = LED_7;
 87     return *ptr;
 88 }
 89 // 虚拟组设置 gpio 状态值
 90 void set_Discontinuous_gpio_group(uchar gpio_status_value){
 91     struct gpio_Physical_continuity_Splicing * lcd_gpio_Splicing = &gpio_status_value;
 92     LED_0 = lcd_gpio_Splicing->gpio_0;
 93     LED_1 = lcd_gpio_Splicing->gpio_1;
 94     LED_2 = lcd_gpio_Splicing->gpio_2;
 95     LED_3 = lcd_gpio_Splicing->gpio_3;
 96     LED_4 = lcd_gpio_Splicing->gpio_4;
 97     LED_5 = lcd_gpio_Splicing->gpio_5;
 98     LED_6 = lcd_gpio_Splicing->gpio_6;
 99     LED_7 = lcd_gpio_Splicing->gpio_7;
100 }
101 // gpio虚拟组 group 方法
102 void led_Discontinuous_gpio_group_group(uchar collection,char status){
103     uchar status_value;
104     uchar git_gpio_status_value = git_Discontinuous_gpio_group();
105     led_Effective_level? (status_value = git_gpio_status_value):(status_value = ~git_gpio_status_value);
106     status?(status_value = status_value | collection):(status_value = status_value & (~collection));
107     led_Effective_level?(git_gpio_status_value=status_value):(git_gpio_status_value=~status_value);
108     set_Discontinuous_gpio_group(git_gpio_status_value);
109 }
110 // gpio虚拟组 IO 方法
111 void led_Discontinuous_gpio_group_IO(uchar collection,char status){
112     int i;
113     uchar LED_status = git_Discontinuous_gpio_group();
114     for(i=0;i<8;i++){
115         if(collection&git_bit_data(i)){
116             (led_Effective_level==status) ? (LED_status |= git_bit_data(i)):(LED_status &= (~git_bit_data(i)));
117         }
118     }
119     set_Discontinuous_gpio_group(LED_status);
120 }
121 // ---------------------------------------------------------------------------------------------------------------
122 // group
123 #define set_led_all_gpio_status_group(collection)         led_all_gpio_status_group(collection,1)
124 #define clean_led_all_gpio_status_group(collection)     led_all_gpio_status_group(collection,0)
125 // IO
126 #define set_led_all_gpio_status_IO(collection)            led_all_gpio_status_IO(collection,1)
127 #define clean_led_all_gpio_status_IO(collection)        led_all_gpio_status_IO(collection,0)
128 // gpio虚拟组 group 方法
129 #define set_led_Discontinuous_gpio_group_group(collection)        led_Discontinuous_gpio_group_group(collection,1)
130 #define clean_led_Discontinuous_gpio_group_group(collection)    led_Discontinuous_gpio_group_group(collection,0)
131 // gpio虚拟组 IO 方法
132 #define set_led_Discontinuous_gpio_group_IO(collection)            led_Discontinuous_gpio_group_IO(collection,1)
133 #define clean_led_Discontinuous_gpio_group_IO(collection)        led_Discontinuous_gpio_group_IO(collection,0)

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

默认stc51单片机举例,代码方法比较全面,选择适合的自己的就行。

本代码默认支持8位的lcd排灯,需要扩列的自行修改代码。

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

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

posted @ 2020-09-08 01:24  极客先锋  阅读(298)  评论(0编辑  收藏  举报