分享一个基于九齐单片机NY8B062E做的数码管驱动,亲测显示OK,附源程序
单片机源程序如下:- // =========================================================================
- // Created by NYIDE.
- // User: ZHANG
- // Date: 2021/9/22
- // Description:
- // =========================================================================
- #include <at8.h>
- #include "at8_constant.h"
- __sbit PIN1 = PORTA:7;
- __sbit PIN2 = PORTA:6;
- __sbit PIN3 = PORTA:4;
- __sbit PIN4 = PORTA:3;
- __sbit PIN5 = PORTA:2;
-
- #define uint8_t (unsigned char) // 无符号8位整型变量
- #define uint16_t (unsigned short) // 无符号16位整型变量
-
- unsigned char SEG;
- unsigned char Data[3]={ 0,0,0};
- unsigned char ch[11]={ 0X3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0};//显示数字0~9
-
- void Display(unsigned char data0,unsigned char data1,unsigned char data2);
- void LED_Scan(void);
- void delay(int);
-
- void isr(void) __interrupt(0)//中断服务函数
- {
- if(INTFbits.T0IF)//1.02ms
- {
- LED_Scan();
- INTF= (unsigned char)~(C_INT_TMR0); // Clear T0IF flag bit
- }
- }
- void main(void)
- {
- IOSTA = 0;
- IOSTB = 0; //set PortB as output mode
-
- PORTA=0x00; //LED = off
- PORTB=0x00; //LED = off
- static unsigned int a = 0;
- while(1)
- {
- a++;
- switch(a/500)
- {
- case 0:
- Display(1,1,9);
- LED_Scan();
- break;
- case 1:
- Display(1,3,9);
- LED_Scan();
- break;
- case 2:
- Display(1,5,9);
- LED_Scan();
- break;
- case 3:
- Display(1,7,9);
- LED_Scan();
- break;
- case 4:
- Display(1,9,9);
- LED_Scan();
- break;
- default:
- Display(1,8,8);
- LED_Scan();
- break;
- }
- if(a>3000)
- a=0;
- }
- }
-
- //数码管对应位置显示数字
- void Display(unsigned char data0,unsigned char data1,unsigned char data2)
- {
- Data[0]=ch[data0];
- Data[1]=ch[data1];
- Data[2]=ch[data2];
- }
-
- //16颗LED逐一扫描
- //当用到其中任意两个IO口的时候,需要将剩余三个IO口设置为输入模式,防止互相干扰
- void LED_Scan(void)
- {
- SEG++;if(SEG>16)SEG=1;
- PIN1=PIN2=PIN3=PIN4=PIN5=0;
- if(SEG == 1){IOSTA=0xE7;if(Data[0]&0x02)PIN3=1;else PIN3=0;PIN4=0;}//B1
- if(SEG == 2){IOSTA=0xB7;if(Data[0]&0x04)PIN2=1;else PIN2=0;PIN4=0;}//C1
- if(SEG == 3){IOSTA=0xAF;if(Data[1]&0x01)PIN2=1;else PIN2=0;PIN3=0;}//A2
- if(SEG == 4){IOSTA=0xAF;if(Data[1]&0x02)PIN3=1;else PIN3=0;PIN2=0;}//B2
- if(SEG == 5){IOSTA=0xE7;if(Data[1]&0x04)PIN4=1;else PIN4=0;PIN3=0;}//C2
- if(SEG == 6){IOSTA=0xB7;if(Data[1]&0x08)PIN4=1;else PIN4=0;PIN2=0;}//D2
- if(SEG == 7){IOSTA=0xBB;if(Data[1]&0x10)PIN5=1;else PIN5=0;PIN2=0;}//E2
- if(SEG == 8){IOSTA=0xEB;if(Data[1]&0x20)PIN5=1;else PIN5=0;PIN3=0;}//F2
- if(SEG == 9){IOSTA=0xF3;if(Data[1]&0x40)PIN5=1;else PIN5=0;PIN4=0;}//G2
- if(SEG == 10){IOSTA=0x3F;if(Data[2]&0x01)PIN1=1;else PIN1=0;PIN2=0;}//A3
- if(SEG == 11){IOSTA=0x3F;if(Data[2]&0x02)PIN2=1;else PIN2=0;PIN1=0;}//B3
- if(SEG == 12){IOSTA=0x6F;if(Data[2]&0x04)PIN1=1;else PIN1=0;PIN3=0;}//C3
- if(SEG == 13){IOSTA=0x6F;if(Data[2]&0x08)PIN3=1;else PIN3=0;PIN1=0;}//D3
- if(SEG == 14){IOSTA=0x77;if(Data[2]&0x10)PIN1=1;else PIN1=0;PIN4=0;}//E3
- if(SEG == 15){IOSTA=0x77;if(Data[2]&0x20)PIN4=1;else PIN4=0;PIN1=0;}//F3
- if(SEG == 16){IOSTA=0x7B;if(Data[2]&0x40)PIN5=1;else PIN5=0;PIN1=0;}//G3
- }
复制代码
|