|  | 
          1楼
          巨大八爪鱼
          2015-11-9 18:11
          
          
            程序代码:(编译器sdcc,单片机是STC的)#include <at89x51.h>
 
 unsigned char SEG7_CODE[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
 
 /*
 CBA234(7~0): 04261357
 111 000
 110 100
 101 010
 */
 void select(unsigned char n)
 {
 /*n &= 0x07;
 n <<= 2;
 n = ~n;*/
 n = ~(n << 2);
 P2 = n;
 }
 
 void delay(int n)
 {
 unsigned char i;
 while (n--)
 for (i = 0; i < 90; i++);
 }
 
 void main()
 {
 unsigned char n = 7;
 while (1)
 {
 select(n);
 P0 = ~SEG7_CODE[n];
 if (n == 0)
 n = 7;
 else
 n--;
 delay(1);
 P0 = 0x00;
 }
 }
 | 
    
      |  | 
          2楼
          巨大八爪鱼
          2015-11-9 18:12
          
          
            數碼管顯示的數字是73516240正確的應該是76543210,所以除了兩端顯示正確,中間的數位全部錯位了。
 
 | 
    
      |  | 
          3楼
          巨大八爪鱼
          2015-11-9 19:38
          
          
            // 7segdebug.cpp : Defines the entry point for the console application.//
 
 #include "stdafx.h"
 #include <stdio.h>
 #include <conio.h>
 
 #define _BV(n) 1<<n
 
 void select(unsigned char n)
 {
 n &= 0x07;
 n <<= 2;
 n = ~n;
 printf("%#2x, CBA", n);
 
 char bin[] = "000";
 unsigned char num = 0;
 if (n & _BV(4))
 {
 bin[2] = '1';
 num++;
 }
 if (n & _BV(3))
 {
 bin[1] = '1';
 num += 2;
 }
 if (n & _BV(2))
 {
 bin[0] = '1';
 num += 4;
 }
 printf("%s(%d)\n", bin, num);
 }
 
 int _tmain(int argc, _TCHAR* argv[])
 {
 unsigned char n;
 for (n = 0; n < 8; n++)
 select(n);
 _getch();
 return 0;
 }
 
 輸出結果:
 0xff, CBA111(7)
 0xfb, CBA011(3)
 0xf7, CBA101(5)
 0xf3, CBA001(1)
 0xef, CBA110(6)
 0xeb, CBA010(2)
 0xe7, CBA100(4)
 0xe3, CBA000(0)
 組成的數字剛好就是73516240
 
 | 
    
      |  | 
          4楼
          巨大八爪鱼
          2015-11-9 20:15
          
          
            #include <at89x51.h>#define N 4
 
 unsigned char SEG7_CODE[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
 unsigned char SEG7_PLACE[] = {0x1c, 0x0c, 0x14, 0x04, 0x18, 0x08, 0x10, 0x00};
 
 unsigned int number = 1970;
 
 void delay(int n)
 {
 unsigned char i;
 while (n--)
 for (i = 0; i < 100; i++);
 }
 
 void Seg7_Scan()
 {
 unsigned char i;
 unsigned int u = 1;
 for (i = 0; i < N; i++)
 {
 P2 = SEG7_PLACE[i];
 if (i > 0 && number / u == 0)
 P0 = 0x00;
 else
 P0 = ~SEG7_CODE[number % (u * 10) / u];
 delay(1);
 P0 = 0x00;
 u *= 10;
 }
 }
 
 void main()
 {
 unsigned char counter = 0;
 while (1)
 {
 Seg7_Scan();
 counter++;
 if (counter > 100)
 {
 counter = 0;
 number++;
 if (number > 2100)
 number = 0;
 }
 }
 }
 成功
 
 |