MENU

21 NE555脉冲发生器实验

July 21, 2020 • 51单片机

--- 当前时间: ---

21 NE555脉冲发生器实验

NE555芯片资料

21.1 NE555脉冲发生器模块电路

  • VR1 potentiometer:调节脉冲频率

21.2 编写NE555控制程序

将要采用数码管显示NE555(俗称3-5芯片(3个5))的频率

main.c

#include <reg52.h>

typedef unsigned char u8;    // 1 byte: 0 ~ 255
typedef unsigned int  u16;    // 2 byte: 0 ~ 65535

/** 74LS138/74HC138 */
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;

#define GPIO_DIG P0

/** 0 ~ F, common cathode, point: 0x80, none: 0x00 */
u8 code segDis_cat[16]  = {0x3f, 0x06, 0x5b, 0x4f,0x66, 0x6d, 0x7d, 0x07,
                           0x7f, 0x6f, 0x77, 0x7c,0x39, 0x5e, 0x79, 0x71};

unsigned long TimeCount = 0;// unsigned long int, int can be omitted
unsigned long Freq      = 0;

u8 DisplayData[8]       = {0x00};

void delay(u16 i)
{
    // Dynamic display
    while(i--);
}

void Timer_Config()
{
    // Timer T0, Counter T1
    
    TMOD |= 0x51;
    
    // 50ms
    TH0 = 0x3C;
    TL0 = 0xB0;
    ET0 = 1;
    ET1 = 1;
    EA  = 1;
    TR0 = 1;
    TR1 = 1;
}

void DigDisplay()
{
    u8 i = 0;
    for(i = 0; i < 8; i++)
    {
        switch(i)
        {
        case 0: LSA = 0; LSB = 0; LSC = 0; break;
        case 1: LSA = 1; LSB = 0; LSC = 0; break;
        case 2: LSA = 0; LSB = 1; LSC = 0; break;
        case 3: LSA = 1; LSB = 1; LSC = 0; break;
        case 4: LSA = 0; LSB = 0; LSC = 1; break;
        case 5: LSA = 1; LSB = 0; LSC = 1; break;
        case 6: LSA = 0; LSB = 1; LSC = 1; break;
        case 7: LSA = 1; LSB = 1; LSC = 1; break;
        }
        GPIO_DIG = DisplayData[i];
        delay(10);
        GPIO_DIG = 0x00;
    }
}

void main()
{
    Timer_Config();
    while(1)
    {
        if(TR1 == 0)
        {
            // 1s, than: 
            
            // L8-bit
            Freq += TL1;
            // H8-bit
            Freq += (TH1 * 256);
            
            // Max ~ x00,000 (x00K)
            DisplayData[2] = segDis_cat[Freq % 1000000 / 100000];
            DisplayData[3] = segDis_cat[Freq % 100000 / 10000];
            DisplayData[4] = segDis_cat[Freq % 10000 / 1000];
            DisplayData[5] = segDis_cat[Freq % 1000 / 100];
            DisplayData[6] = segDis_cat[Freq % 100 / 10];
            DisplayData[7] = segDis_cat[Freq % 10];
            
            // Clear - REMEMBER!
            Freq = 0;
            TH1  = 0;
            TL1  = 0;
            
            // Open them
            TR1 = 1;
            TR0 = 1;
        }
        
        DigDisplay();
    }
}

void Timer0() interrupt 1
{
    // 50ms
    TH0 = 0x3C;
    TL0 = 0xB0;

    // T = (20 * 50)ms = 1s
    if(++TimeCount == 20)
    {
        TimeCount = 0;
        TR1       = 0;
        TR0       = 0;
    }
}

void Timer1() interrupt 3
{
    // Overflow => save
    Freq += 65536;
}
Last Modified: August 21, 2020
Archives QR Code
QR Code for this page
Tipping QR Code