同样是对天平操作的代码,读取天平的重量和其他部分的运行,因为数量很多所以采用了递归的方法
void ProcessSamples(int samplePositions[],u16 num,int totalSamples,u32 *Weightbuffer)
{
if (num >= totalSamples)
{
return;
}
ProcessSample(samplePositions[num],Weightbuffer);
ProcessSamples(samplePositions, num + 1, totalSamples,Weightbuffer);
}
void ProcessSample(u16 Samplenum,u32 *Weightbuffer)
{
if(StartWorkFlag==false)
return;
SamplefromTurnTable(Samplenum);
ArriveBalance();
delay_ms(100);
u32 Recordweight=Weightbuffer[Samplenum-1];
while(readBalancebyUart(&Weightbuffer[Samplenum-1])==false)
{
delay_sec(2);
}
TaskParabuffer.CurSampleWeight=Weightbuffer[Samplenum-1];
ReturnTurntable(Samplenum);
u32 differnum = abs(Weightbuffer[Samplenum-1]-Recordweight);
if(differnum<=PCSetTaskPara.Balanceerronum)
{
Recordweight=0;
PCSetTaskPara.bufferTask[Samplenum-1]=0;
Weightbuffer[Samplenum-1]=0;
}
else{
Recordweight=0;
}
}
bool readBalancebyUart(u32 *weight)
{
float data=0;
u8 buffer[3] = {0};
buffer[0]=0x53;
buffer[1]=0x0D;
buffer[2]=0x0A;
for(int i=0;i<3;i++)
{
while(USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET){};
USART_SendData(UART5,buffer[i]);
}
while(USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET){};
delay_ms(50);
u8 bufferRec[30] = {0};
int recLen = 0;
getUart5RecData(bufferRec,&recLen);
if((bufferRec[0] == 0x53)&&(bufferRec[2] == 0x53)&&(bufferRec[8] == 0x2E)&& recLen == 0x12)
{
u8 dataAddr[9]= {0};
for(int i = 0;i<9;i++)
{
dataAddr[i] = bufferRec[5+i];
}
data = atof((char*)dataAddr);
if(data>150)
{
return false;
}
*weight=(u32)floor(data*100000);
}
else
{
return false;
}
return true;
}
但是运行到ProcessSample的
while(readBalancebyUart(&Weightbuffer[Samplenum-1])==false)
{
delay_sec(2);
}时程序又进入到HardFault_Handler,然后通过调用堆栈窗口发现在程序运行到 ProcessSample函数的 u32 Recordweight=Weightbuffer[Samplenum-1];之前Samplenum和Weightbuffer的数值和地址是正常的,进入到while(readBalancebyUart(&Weightbuffer[Samplenum-1])==false)
{
delay_sec(2);
}后,Samplenum和Weightbuffer的地址就全不对了,原本Samplenum的值为3变为0x2036,Weightbuffer的地址从0x200043E0变为0x2000430A