找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1747|回復(fù): 5
收起左側(cè)

有沒有大佬幫忙改一下這個單片機程序

[復(fù)制鏈接]
ID:999824 發(fā)表于 2022-4-4 20:56 | 顯示全部樓層 |閱讀模式
要求是倒計時30秒顯示這是自己的源程序

#include<reg51.h>
#define uchar unsigned char

sbit S1=P2^0;
sbit S2=P2^1;

sbit K1=P3^2;

uchar code dispcode[]= {0x3f,0x06,0x5b,
                        0x4f,0x66,0x6d,
                        0x7d,0x07,0x7f,0x6f90
                       } ;

void delay(void)
{
        uchar i,j,k;
        for(i=5; i>0; i--)
        {
                for(j=200; j>0; j--)
                {
                        for(k=250; k>0; k--)
                        {
                                ;
                        }
                }
        }
}

void delay10ms(void)
{
        unsigned char i,k;
        for(i=40; i<0; i--)
                for(k=250; k>0; k--);
}

void daojishi(void)
{
        uchar i;
        while(1)
        {
                for(i=0; i<10; i++)
                {
                        P1=dispcode[ i];
                        delay();
                        delay();
                }
        }
}

void main()
{
        TMOD=0x06;
        TH0=0x00;
        TL0=0x00;
        TR0=1 ;

            P1=0X7f;

        while(1)
        {

                if(K1==0)
                {
                        delay10ms();
                        if(K1==0);
                }
                if(S1==0xf9)//30,20
                {
                        S1=0x6d;
                        daojishi();
                        S1=0x6d;
                }
                if(S1==0x6d)//20,10
                {
                        S1=0x6d;
                        daojishi();
                        S1=0x30;
                }
                if(S1==0x30)//10
                {
                        S1=0x30;
                        daojishi();
                        S1=0x7e;
                }
                daojishi();
        }

}

回復(fù)

使用道具 舉報

ID:195446 發(fā)表于 2022-4-5 10:27 | 顯示全部樓層
這程序問題多。1. S1是位變量,S1=0x6d;賦值等同于S1=1 。2. 定時器啟動了,但程序里沒有用到。3. 顯示函數(shù)里的死循環(huán)導(dǎo)致出不來了。
回復(fù)

使用道具 舉報

ID:979299 發(fā)表于 2022-4-5 16:27 | 顯示全部樓層
改動有點大哦
#include <reg52.h>
#define uchar unsigned char
sbit S1=P2^0;
sbit S2=P2^1;
sbit k1=P3^2;
uchar a=30,b,time;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void delay_ms(unsigned int ms)  
{
        while(ms--)
        for(b=123;b>0;b--);
}
void disp();
void t0intinit()
{
        TMOD=0x11;
        TH0=0x3c;
        TL0=0xb0;
        TH1=0xfe;
        TL1=0x0c;
        EA=1;
        ET0=1;
        ET1=1;
        TR0=1;
}
void key()
{
        if(k1==0)                     
        {                             
                delay_ms(10);        
                if(k1==0)                 
                {
                        t0intinit();
                       
                }
                while(!k1);            
        }
}
void main()
{
       
        while(1)
        {
                disp();
                key();

        }
}

void disp( )
{
        P1=tab[a%10];
        S2=0;
        delay_ms(1);
        S2=1;
        P1=tab[a/10];
        S1=0;
        delay_ms(1);
        S1=1;
}
void t0int(void) interrupt 1
{
        TH0=0x3c;
        TL0=0xb0;
        time++;
        if(time==20)
        {
                time=0;
                if(a!=0)                    
                a--;

        }
}
回復(fù)

使用道具 舉報

ID:999824 發(fā)表于 2022-4-5 19:11 | 顯示全部樓層
mffhxa 發(fā)表于 2022-4-5 10:27
這程序問題多。1. S1是位變量,S1=0x6d;賦值等同于S1=1 。2. 定時器啟動了,但程序里沒有用到。3. 顯示函數(shù) ...

嗯嗯好嘞哥
回復(fù)

使用道具 舉報

ID:999824 發(fā)表于 2022-4-5 19:12 | 顯示全部樓層
興安xa 發(fā)表于 2022-4-5 16:27
改動有點大哦
#include
#define uchar unsigned char

TMOD=0x11;
        TH0=0x3c;
        TL0=0xb0;
        TH1=0xfe;
        TL1=0x0c;
哥,這一快地方咋理解
回復(fù)

使用道具 舉報

ID:609524 發(fā)表于 2022-4-7 14:54 | 顯示全部樓層
XCYYLX 發(fā)表于 2022-4-5 19:12
TMOD=0x11;
        TH0=0x3c;
        TL0=0xb0;

TMOD=0x11; //設(shè)置定時器工作模式
TH0=0x3c;   //定時器0初值的高8位
TL0=0xb0;  //定時器0初值的低8位
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表