目前共有4篇帖子。 內容轉換:不轉換▼
 
點擊 回復
239 3
第一次使用138译码器控制数码管位选,出了点问题。。。
一派護法 十九級
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;
        }
    }
}
成功

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:239 回複數:3
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2015-11-9 20:15
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。