0

大家好,请帮帮我...我不知道为什么 void main() 函数在没有任何 while 循环的情况下不断重复。在 LCD 上首先显示“IN PROGRESS”(状态1),然后显示“IDLE MODE”(状态2),然后延迟 500 毫秒后“IN PROGRESS”(状态1),然后“IDLE MODE”(状态2)不断重复,它从不显示“SPI MODE (状态3)。我不知道发生了什么可能是微控制器自行复位。任何帮助将不胜感激。我正在使用带有C18编译器的MPLAB IDEv8.92。提前致谢。

#include <p18f4550.h>
#pragma config WDT = OFF
#define SCK LATBbits.LATB1              //Pin34 as Output
#define SDA PORTBbits.RB0               //Pin33 as Input
#define CS LATCbits.LATC2               //Pin17 as Output
#define SDO LATCbits.LATC7              //Pin26 as Output
#define LD LATD                         
#define RS LATEbits.LATE1
#define E LATEbits.LATE2
void cmdwrite(unsigned char);
void datawrite(unsigned char);
void strdis(unsigned char *str);
void initialize_SDC(void);
void ms_delay(void);
void msd(void);
void dis2digit(unsigned int var3);
void main()
    {
        unsigned char status1[20]="IN PROGRESS";
            SSPSTATbits.SMP=1;
            SSPSTATbits.CKE=0;
            SSPCON1=0x22;
            SSPCON1bits.SSPEN = 1;
            ms_delay();ms_delay();
            TRISBbits.TRISB1=0;                     //Pin34(SCK) as Output
            TRISBbits.TRISB0=1;                     //Pin33(SDA) as Input
            TRISCbits.TRISC2=0;                     //Pin17(CS) as Output
            TRISCbits.TRISC7=0;                     //Pin26(SDO) as Output
            TRISD=0;
            ADCON1=0x0F;
            TRISE=0;
            E=0;
            CS=1;
            cmdwrite(0x38);
            cmdwrite(0x38);
            cmdwrite(0x38);
            cmdwrite(0x0C);
            cmdwrite(0x82);
            strdis(status1);        //Displays IN PROGRESS
            initialize_SDC();
    }
void initialize_SDC(void)
    {
        unsigned char status2[20]="IDLE MODE";
        unsigned char status3[20]="SPI MODE";
        unsigned char i=0,j=0,r1=0xFF,high=0xFF,ar1=0xFF,spi_r1=0xFF;
        unsigned char cmd0[6]={0x40,0x00,0x00,0x00,0x00,0x95};      // CMD0
        unsigned char cmd1[6]={0x40,0x00,0x00,0x00,0x00,0x95};
        unsigned char cmd55[6]={0x77,0x00,0x00,0x00,0x00,0x01};
        unsigned char acmd41[6]={0x69,0x00,0x00,0x00,0x00,0x01};
        for(i=0;i<10;i++)
            {
                CS=1;
                SDO=high;       //Warming Up Card by sending 80 Clock Pulses
            }
            CS=0;
        if(r1==0xFF)
        {   
            for(j=0;j<6;j++)
            {
                SDO=cmd0[j];        // Sending CMD0
            }
                SDO=high;
                r1=SDA;             //Keep Sending Dummy Bytes until 0x01 is received
            CS=1;
            if(r1==0x01)
            {
            cmdwrite(0x01);
            cmdwrite(0x83);
            strdis(status2);        //Displays IDLE MODE if the response is valid
            msd();msd();
            }
        }   

        CS=1;
        CS=0;
        while(ar1==0xFF)
        {
        SDO=high;
        for(j=0;j<6;j++)
            {
                SDO=cmd1[j];        // Sending CMD0
            }
                SDO=high;
                ar1=SDA;                //Keep Sending Dummy Bytes until 0x01 is received
                CS=1;
            if(ar1==0x00)
            {
            cmdwrite(0x01);
            cmdwrite(0x83);
            strdis(status3);        //Displays IDLE MODE if the response is valid
            msd();msd();
            }
        }
        /*for(i=0;i<6;i++)  
        {
            SDO=cmd55[i];           //CMD55
        }
        SDO=high;                   //Send dummy byte
        ar1=SDA;                    //Get Response for CMD55
            if(ar1=0x00)            //Check if Response(R1) is valid
            {
                CS=1;
                CS=0;
                for(i=0;i<6;i++)
                {
                    SDO=acmd41[i];      //ACMD41
                }
            SDO=high;                   //Send dummy byte
            spi_r1=SDA;                 //Get Response for ACMD41
            if(spi_r1==0x00)            //Check if Response(R1) is valid
            {
                cmdwrite(0x01);
                cmdwrite(0x88);
                strdis(status3);        //Displays SPI MODE if the response is valid
                msd();msd();
            }
            CS=1;
            }*/
    }
void cmdwrite(unsigned char var1)
{
LD=var1;
RS=0;
/*RW is grounded */
E=1;
ms_delay();
E=0;
}
void datawrite(unsigned char var2)
{
LD=var2;
RS=1;
/*RW is grounded */
E=1;
ms_delay();
E=0;
}
void strdis(unsigned char *str)
{
        while(*str!='\0')
        {
        datawrite(*str);
        str++;
        }
}
void dis2digit(unsigned int var3)
{
     unsigned int hp,up,tp,x;
     x=var3/10;
     hp=x/10;
     tp=x%10;
     up=var3%10;
     datawrite(hp+48);
     datawrite(tp+48);
     datawrite(up+48);
}
void ms_delay(void)
{
T1CON = 0x30;
TMR1H=0xC2;
TMR1L=0xF7;
T1CONbits.TMR1ON=1;
while(PIR1bits.TMR1IF==0);
T1CONbits.TMR1ON=0;
PIR1bits.TMR1IF=0;
}
void msd(void)
{
T1CON = 0x30;
TMR1H=0x67;
TMR1L=0x6A;
T1CONbits.TMR1ON=1;
while(PIR1bits.TMR1IF==0);
T1CONbits.TMR1ON=0;
PIR1bits.TMR1IF=0;
}
4

1 回答 1

1

为了解决您的问题:

1) 在主函数结束之前,您的嵌入式代码中应该始终有一个“超级循环”或无限循环。我怀疑您的微控制器“重置”的原因是因为您在 main 结束之前没有这样的循环,并且程序返回到第一条指令行。在 main 结束之前插入此代码:

 while(1) {

 }

2)我怀疑您从不显示 SPI MODE(status 3) 的原因是因为您从未输入“if”语句:

if(ar1 == 0x00)

在您之前显示 SPI MODE(status 2) 的语句中,这个“if”语句是

if(r1 == 0x01).

尝试将语句更改为:

if(ar1 == 0x01)

甚至您的评论也表明您正在循环直到达到 0x01 值。希望这对你有用!

于 2014-10-31T17:41:03.227 回答