最近弄LED段式数码管程序(采用扫描段码方式),但是编写后发现代码多,不知如何可以减少代码量。 //==================================================================== //=== 显示扫描函数 ===// void display_scan(void) { unsigned char i; P1 &= Bin(11111110); P1 |= Bin(11111000); P2 &= Bin(00000000); P3 &= Bin(01100111); P3 |= Bin(01100110); //关闭段码和位码,如共阳数码管(公共脚置低,段码置高) //======================================// for(i = 0;i < 4;i ++) { r_dis_data[i] = table_seg[r_dis_buff[i]]; //查LED段码表 } switch (r_dig_bit) //在公共端接限流电阻,在数码管位数少时布线时有优势 { case 1: p_seg_a = 0; //扫描a段 if(r_dis_data[0] & 0x01) p_dig_1 = 1; //如果r_dis_data[0]第0位为高,则需要点亮,反之则不点亮 if(r_dis_data[1] & 0x01) p_dig_2 = 1; if(r_dis_data[2] & 0x01) p_dig_3 = 1; if(r_dis_data[3] & 0x01) p_dig_4 = 1; if(r_dis_buff[4] & 0x01) p_dig_5 = 1; if(r_dis_buff[5] & 0x01) p_dig_6 = 1; break; case 2: p_seg_b = 0; if(r_dis_data[0] & 0x02) p_dig_1 = 1; if(r_dis_data[1] & 0x02) p_dig_2 = 1; if(r_dis_data[2] & 0x02) p_dig_3 = 1; if(r_dis_data[3] & 0x02) p_dig_4 = 1; if(r_dis_buff[4] & 0x02) p_dig_5 = 1; if(r_dis_buff[5] & 0x02) p_dig_6 = 1; break; case 3: p_seg_c = 0; if(r_dis_data[0] & 0x04) p_dig_1 = 1; if(r_dis_data[1] & 0x04) p_dig_2 = 1; if(r_dis_data[2] & 0x04) p_dig_3 = 1; if(r_dis_data[3] & 0x04) p_dig_4 = 1; if(r_dis_buff[4] & 0x04) p_dig_5 = 1; if(r_dis_buff[5] & 0x04) p_dig_6 = 1; break; case 4: p_seg_d = 0; if(r_dis_data[0] & 0x08) p_dig_1 = 1; if(r_dis_data[1] & 0x08) p_dig_2 = 1; if(r_dis_data[2] & 0x08) p_dig_3 = 1; if(r_dis_data[3] & 0x08) p_dig_4 = 1; if(r_dis_buff[4] & 0x08) p_dig_5 = 1; if(r_dis_buff[5] & 0x08) p_dig_6 = 1; break; case 5: p_seg_e = 0; if(r_dis_data[0] & 0x10) p_dig_1 = 1; if(r_dis_data[1] & 0x10) p_dig_2 = 1; if(r_dis_data[2] & 0x10) p_dig_3 = 1; if(r_dis_data[3] & 0x10) p_dig_4 = 1; if(r_dis_buff[4] & 0x10) p_dig_5 = 1; if(r_dis_buff[5] & 0x10) p_dig_6 = 1; break; case 6: p_seg_f = 0; if(r_dis_data[0] & 0x20) p_dig_1 = 1; if(r_dis_data[1] & 0x20) p_dig_2 = 1; if(r_dis_data[2] & 0x20) p_dig_3 = 1; if(r_dis_data[3] & 0x20) p_dig_4 = 1; if(r_dis_buff[4] & 0x20) p_dig_5 = 1; if(r_dis_buff[5] & 0x20) p_dig_6 = 1; break; case 7: p_seg_g = 0; if(r_dis_data[0] & 0x40) p_dig_1 = 1; if(r_dis_data[1] & 0x40) p_dig_2 = 1; if(r_dis_data[2] & 0x40) p_dig_3 = 1; if(r_dis_data[3] & 0x40) p_dig_4 = 1; if(r_dis_buff[4] & 0x40) p_dig_5 = 1; if(r_dis_buff[5] & 0x40) p_dig_6 = 1; break; case 8: p_seg_h = 0; if(r_dis_data[0] & 0x80) p_dig_1 = 1; if(r_dis_data[1] & 0x80) p_dig_2 = 1; if(r_dis_data[2] & 0x80) p_dig_3 = 1; if(r_dis_data[3] & 0x80) p_dig_4 = 1; if(r_dis_buff[4] & 0x80) p_dig_5 = 1; if(r_dis_buff[5] & 0x80) p_dig_6 = 1; break; default: keyscan_process(); //按键扫描 break; } r_dig_bit ++; if(r_dig_bit >= 9) //扫描时基 r_dig_bit = 0; } |
电路图如下: