嗨,大家好请帮帮我...我不知道为什么void main()函数在没有任何while循环的情况下继续重复。在LCD上首先显示“IN PROGRESS”(status1),然后显示“IDLE MODE”(状态2),然后在500ms延迟“IN PROGRESS”(状态1)后,“空闲模式”(状态2)不断重复,它从不显示“SPI MODE” (状态3)。我不知道发生了什么可能是微控制器自身重置。任何帮助都会受到赞赏。我正在使用MPLAB IDEv8.92和C18编译器。提前谢谢。
#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;
}
答案 0 :(得分:1)
解决您的问题:
1)在主函数结束之前,嵌入式代码中应始终有“超级循环”或无限循环。我怀疑你的微控制器“重置”的原因是因为你在main结束之前没有这样的循环,程序返回到第一条指令行。在主要结束之前插入此代码:
while(1) {
}
2)我怀疑你从不显示SPI MODE(状态3)的原因是因为你从未输入“if”语句:
if(ar1 == 0x00)
在上一个显示SPI MODE(状态2)的语句中,此“if”语句为
if(r1 == 0x01).
尝试将语句更改为:
if(ar1 == 0x01)
即使您的评论也表明您正在循环,直到达到值0x01。希望这适合你!