使用89c51的螺旋桨显示代码 - 需要帮助

时间:2016-05-26 04:44:59

标签: c microcontroller 8051

以下是我为螺旋桨显示器编写的代码,当在电机上运行时显示“HELLO”。用于LED的逻辑低电平有效,连接到端口1,端口1被声明为输出端口。端口3.2被声明为由IR传感器触发的输入端口。 我已经使用Keil来编译这个程序并生成它的hex文件但是当我在我的微控制器上烧它并运行它时,引脚3.2不会触发端口1输出的任何变化。如果你有任何问题,你会怀疑在评论中帮助我。谢谢。

#include <reg52.h>

void delay_us(long num){ //function to delay 1 microsecond using tic
    long i;
    for(i=0;i<=num;i++){    
        TMOD = 0x01;
        TH0 = 0xFF;
        TL0 = 0xFE;
        TR0 =  1;
        while(TF0 == 0);
        TF0 = 0;
        TR0 = 0;
    }
}

void print_char(char input, long time) //function to print HELLO!
{
    if (input == 'H')
    {
        P1 = 0x3E;
        delay_us(time);
                P1 = 0x00;
        delay_us(time);
                P1 = 0x36;
        delay_us(time);
                P1 = 0x00;
        delay_us(time);
                P1 = 0x3E;
        delay_us(time);
    }
    else if (input == 'E')
    {
        P1 = 0x3E;
        delay_us(time);
                P1 = 0x00;
        delay_us(time);
                P1 = 0x14;
        delay_us(time);
                P1 = 0x1C;
        delay_us(time);
                P1 = 0x3E;
        delay_us(time);
    }
        if (input == 'L')
    {
        P1 = 0x3E;
        delay_us(time);
                P1 = 0x3E;
        delay_us(time);
                P1 = 0x00;
        delay_us(time);
                P1 = 0x3E;
        delay_us(time);
                P1 = 0x3E;
        delay_us(time);
    }
        if (input == 'O')
    {
        P1 = 0x3E;
        delay_us(time);
                P1 = 0x22;
        delay_us(time);
                P1 = 0x1C;
        delay_us(time);
                P1 = 0x22;
        delay_us(time);
                P1 = 0x3E;
        delay_us(time);
    }
}

//Declare Prototypes
int get_rpu();
sbit LED1 = P1^0;
sbit LED2 = P1^1;
sbit LED3 = P1^2;
sbit LED4 = P1^3;
sbit LED5 = P1^4;
sbit LED6 = P1^5;
sbit LED7 = P1^6;
sbit IR = P3^2;

void main()
{
    long delay_time; //degrees per microsecond

    P1 = 0x10; 
    P3 = 0x00; 

    //Initialize all LED's to be zero
    LED1 = 1;
    LED2 = 1;
    LED3 = 1;
    LED4 = 1;
    LED5 = 1;
    LED6 = 1;
    LED7 = 1;

    //get the amount of delay
   delay_us(1000);

    delay_time = (get_rpu() * 100) / 360;

    while (1) {
        //Wait for the sensor to start at reference point
        while (!IR);

        print_char('H',delay_time);
        delay_us(delay_time);
        print_char('E',delay_time);
        delay_us(delay_time);
        print_char('L',delay_time);
        delay_us(delay_time);
        print_char('L',delay_time);
        delay_us(delay_time);
        print_char('O',delay_time);

        //recalculate the spin rate
    delay_time = (get_rpu() * 100) / 360;
    }
}

int get_rpu() //function to calculate time per revolution
{
    int count = 0;

    while (!IR); //wait for first pass of the rate snesor

    //begin counting how many microseconds are there before the next pass
    while(IR)
    {
        delay_us(1);
        count++;
    }
    return count;
}

0 个答案:

没有答案