大家好,请帮帮我...我不知道为什么 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;
}