proteus仿真数码管失败。。。。。

2020-02-10 08:35发布

在proteus中仿真数码管,在12m晶振时,数码管的第四个不显示,  4m晶振时,数码管都显示,但是闪烁。。。。。。
下周板子回来后,用板子实验。。。。。。
感觉程序应该没什么问题。。。。。
就是每10ms在定时中断中调用显示函数Dis();

#include <pic.h>
#include "delay.h"
unsigned char tick;
unsigned char sec;
const unsigned char num_code[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xC6};
unsigned char dis_buf[4]={1,2,3,5};
unsigned int ad_result[8];


__CONFIG( HS & WDTDIS& PWRTEN& BORDIS& UNPROTECT );

interrupt ISR()
{
        if(TMR1IF&&TMR1IE)
        {
                TMR1IF=0;
                TMR1H=(65526-83000/3)/256;
                TMR1L=(65526-83000/3)%256;//11.0592M   10.017MS
                tick++;
                if(tick>=100)
                        {
                                tick=0;
                                sec++;
                                dis_buf[2]=sec%10;
                                dis_buf[1]=(sec%100)/10;       
                                dis_buf[0]=sec/100;
                        }
                Dis();//显示
        }
}

Init_Tmr1()
{
        PEIE=1;
        TMR1IF=0;
        TMR1IE=1;
        T1CKPS1=0;
        T1CKPS0=0;
        TMR1CS=0;
        TMR1H=(65526-83000/3)/256;
        TMR1L=(65526-83000/3)%256;//11.0592M   10.017MS
        TMR1ON=1;
}
Init_Dis()
{
        TRISC0=0;
        TRISC1=0;
        TRISC2=0;
        TRISC3=0;//BIT

        TRISB=0X0;       
        PORTB=0XFF;
}
Dis()
{
        static unsigned char step=3;
        switch(step){
        case 0:
        RC0=0;
        RC1=1;
        RC2=1;
        RC3=1;
        PORTB=num_code[dis_buf[0]];
        break;

        case 1:
        RC0=1;
        RC1=0;
        RC2=1;
        RC3=1;
        PORTB=num_code[dis_buf[1]];
        break;

        case 2:
        RC0=1;
        RC1=1;
        RC2=0;
        RC3=1;
        PORTB=num_code[dis_buf[2]];
        break;
        case 3:
        RC0=1;
        RC1=1;
        RC2=1;
        RC3=0;
        PORTB=num_code[dis_buf[3]];
        break;
        default: break;
        }
        step++;
        if(step>=4)
                step=0;

}

void Ad_Init(void)
{
        ADCON1=0X80;//right ,all pin analog
    ADCS1=1; ADCS0=0;//F_OSC/32
        TRISA0=1;
        TRISA1=1;
        TRISA2=1;
        TRISA3=1;
        TRISA5=1;
        TRISE|=0X07;
    ADON=1;//AD MODULE WORK
}
unsigned int Ad_Convert(unsigned  char ch)
{
    unsigned  int ad_result;
    Ad_Init();
        if(ch<=7)
        {
                ADCON0&=~(7<<3);
            ADCON0|=ch<<3;
        }
        DelayUs(20);
    ADGO=1;
    for(ad_result=0;ad_result<1000;ad_result++)
                {
                        if(ADGO==0)
                                break;
                }
       
    DelayMs(1);
        ad_result=ADRESH&0XFF;
        ad_result=ad_result<<8;
        ad_result=ad_result+ADRESL;
        return ad_result;       
}

main()
{
unsigned char i=0;
Ad_Init();
Init_Tmr1();
Init_Dis();
GIE=1;
while(1)
        {
                for (i=0;i<8;i++)
                        ad_result=Ad_Convert(i);
        }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
millwood0
2020-02-10 19:45
you should try to isolate the problem to certain modules and try to make sure that each module works before putting them together.

as to dis(), you can set up a software flag in the isr() so that every 10ms, that flag is set by the isr() and in the main loop, you run dis() which checks the flag. if that flag is up, it clears that flag and then does its thing. aka create a "software" interrupt of your own for dis() but outside of the real isr().

一周热门 更多>