单片机驱动数码管

数码管的原理?

数码管是分段控制点亮和熄灭的LED段,数码管分为共阳极以及共阴极,共阳极就是说数码管的LED段共一个电压正极,共阴极就是说所有的LED段共一个电压负极。一般方法,一个IO控制公共段,共阳极为例子,给公共段施加一个正电压,其余LED段由IO分开控制,想显示多少,只需点亮对应的LED段,即设置对应段的IO为低电平,不显示的LED段为高电平。

捕获

详细驱动方法见如下程序,项目合作交流,吹牛扯淡,交朋友,请联系18665321219

#ifndef file1_H
#define file1_H
#include <ny8.h>

#define dig1 PA2
#define dig2 PA3

#define sega PB0
#define segb PB1
#define segc PB2
#define segd PB3
#define sege PB4
#define segf PB5
#define segg PA0

void dpSeg(unsigned char num);

#endif /* end of include guard: file1_H */
#include "seg.h"

void dpSeg(unsigned char num)
{
    switch(num)
    {
        case 0:  sega = 1;segb = 1; segc = 1; segd = 1; sege = 1; segf = 1; segg = 0; break;    //0
        case 1:  sega = 0;segb = 1; segc = 1; segd = 0; sege = 0; segf = 0; segg = 0; break;    //1
        case 2:  sega = 1;segb = 1; segc = 0; segd = 1; sege = 1; segf = 0; segg = 1; break;    //2
        case 3:  sega = 1;segb = 1; segc = 1; segd = 1; sege = 0; segf = 0; segg = 1; break;    //3
        case 4:  sega = 0;segb = 1; segc = 1; segd = 0; sege = 0; segf = 1; segg = 1; break;    //4
        case 5:  sega = 1;segb = 0; segc = 1; segd = 1; sege = 0; segf = 1; segg = 1; break;    //5
        case 6:  sega = 1;segb = 0; segc = 1; segd = 1; sege = 1; segf = 1; segg = 1; break;    //6
        case 7:  sega = 1;segb = 1; segc = 1; segd = 0; sege = 0; segf = 0; segg = 0; break;    //7
        case 8:  sega = 1;segb = 1; segc = 1; segd = 1; sege = 1; segf = 1; segg = 1; break;    //8
        case 9:  sega = 1;segb = 1; segc = 1; segd = 1; sege = 0; segf = 1; segg = 1;  break;   //9
        case 0x0a: sega = 0;segb = 0; segc = 0; segd = 0; sege = 0; segf = 0; segg = 1; break;  //--
        case 0x0b: sega = 1;segb = 0; segc = 0; segd = 1; sege = 0; segf = 0; segg = 1; break;  //------
        case 0x0f: sega = 0;segb = 0; segc = 0; segd = 0; sege = 0; segf = 0; segg = 0; break;  //clean
        default: break; 
    }
}
if(dlpStp == 0)                //数码管显示
            {
                dpSeg((dispCode & 0xf0)>>4);
                
                IOSTA &= 0xfb;
                dig1 = 0;
                //dig2 = 1;  DIG2 set_high
                IOSTA |= 0x08;
                APHCON &= 0xf7;
                
                dlpStp = 1;
            }
            else if(dlpStp == 1)    
            {        
                dpSeg(dispCode & 0x0f);
                //dig1 = 1;
                IOSTA &= 0xf7;
                IOSTA |= 0x04;
                APHCON &= 0xfb;
                
                dig2 = 0;
                dlpStp = 2;
            }
            else if(dlpStp == 2)
            {
                IOSTA &= 0xf7;
                IOSTA &= 0xfb;
                if(led1En)
                {
                    dpSeg(0x0f);
                    dig1 = 0;
                    dig2 = 1;
                    //PA0 = 1;
                }
                else
                {
                    dpSeg(0x0f);
                    dig1 = 0;
                    dig2 = 0;    
                }
                dlpStp = 3;
            }
            else if(dlpStp == 3)
            {
                IOSTA &= 0xf7;
                IOSTA &= 0xfb;
                if(led2En)
                {
                    dpSeg(0x0f);                      
                    dig1 = 1;
                    dig2 = 0;
                    //PB5 = 1;
                }
                else
                {
                    dig1 = 0;
                    dig2 = 0;
                    dpSeg(0x0f);
                }
                dlpStp = 4;
            }
            else if(dlpStp == 4)
            {
                imTemp = culTemp(adcVal);
                if(imTemp <= 20)
                {
                    imTemp = imTemp + 2;
                }
                else if((imTemp < 69)&&(imTemp > 20))
                {
                    imTemp = imTemp + 1;
                }
                dlpStp = 0;
            }
posted @ 2022-01-12 18:32  steven_lg  阅读(630)  评论(0编辑  收藏  举报