我用NRF24L01进行上位机和下位机的通信,用systick定时器给程序计时,得到下位机发送40个数据包的时间是400ms,上位机接收40个数据包的时间也是400多ms。时间过长,这个该怎么办呢?
我的上位机接收数据用的是查询模式,先查询是否接收到数据,若接收到数据就把数据包里的数据搬运到另一个数组。如果在搬运的过程中下位机有数据发送会出现什么后果呢?时间过长是不是和这个有关呢?
这是上位机的程序:
[mw_shl_code=c,true]
/*使用systick对程序某部分计时*/
SysTick->LOAD = 0xFFFFFF; /* Time load (SysTick-> LOAD is 24bit). */
SysTick->VAL = 0xFFFFFF; /* Empty the counter value. */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* 计时开始 */
for(t=1;t<41;t++)
{
switch(NRF24L01_RxPacket(receive))
{
case 0x00:
{
if((receive[0]=='1')&&(receive[1]=='L'))
{
movnums(receive,A1L,counterA1L);
counterA1L++;
sprintf(test_counter,"%d",counterA1L);
LCD_ShowString(30,150,200,16,16,test_counter);
}
if((receive[0]=='1')&&(receive[1]=='H'))
{
movnums(receive,A1H,counterA1H);
counterA1H++;
sprintf(test_counter,"%d",counterA1H);
LCD_ShowString(50,150,200,16,16,test_counter);
}
/*
if((receive[0]=='2')&&(receive[1]=='L'))
{
movnums(receive,A2L,counterA2L);
counterA2L++;
sprintf(test_counter,"%d",counterA2L);
LCD_ShowString(110,150,200,16,16,test_counter);
}
if((receive[0]=='2')&&(receive[1]=='H'))
{
movnums(receive,A2H,counterA2H);
counterA2H++;
sprintf(test_counter,"%d",counterA2H);
LCD_ShowString(150,150,200,16,16,test_counter);
}
*/
} break;
case 0x02:
{
if((receive[0]=='1')&&(receive[1]=='L'))
{
movnums(receive,B1L,counterB1L);
counterB1L++;
sprintf(test_counter,"%d",counterB1L);
LCD_ShowString(30,190,200,16,16,test_counter);
}
if((receive[0]=='1')&&(receive[1]=='H'))
{
movnums(receive,B1H,counterB1H);
counterB1H++;
sprintf(test_counter,"%d",counterB1H);
LCD_ShowString(70,190,200,16,16,test_counter);
}
if((receive[0]=='2')&&(receive[1]=='L'))
{
movnums(receive,B2L,counterB2L);
counterB2L++;
sprintf(test_counter,"%d",counterB2L);
LCD_ShowString(110,190,200,16,16,test_counter);
}
if((receive[0]=='2')&&(receive[1]=='H'))
{
movnums(receive,B2H,counterB2H);
counterB2H++;
sprintf(test_counter,"%d",counterB2H);
LCD_ShowString(150,190,200,16,16,test_counter);
}
} break;
case 0x04:
{
if((receive[0]=='1')&&(receive[1]=='L'))
{
movnums(receive,C1L,counterC1L);
counterC1L++;
sprintf(test_counter,"%d",counterC1L);
LCD_ShowString(30,230,200,16,16,test_counter);
}
if((receive[0]=='1')&&(receive[1]=='H'))
{
movnums(receive,C1H,counterC1H);
counterC1H++;
sprintf(test_counter,"%d",counterC1H);
LCD_ShowString(70,230,200,16,16,test_counter);
}
if((receive[0]=='2')&&(receive[1]=='L'))
{
movnums(receive,C2L,counterC2L);
counterC2L++;
sprintf(test_counter,"%d",counterC2L);
LCD_ShowString(110,230,200,16,16,test_counter);
}
if((receive[0]=='2')&&(receive[1]=='H'))
{
movnums(receive,C2H,counterC2H);
counterC2H++;
sprintf(test_counter,"%d",counterC2H);
LCD_ShowString(150,230,200,16,16,test_counter);
}
} break;
default: t--;
}
}
runtime = MeasureTimeStop(21);
sprintf(time_show_on_screen,"%f",runtime);
LCD_ShowString(100,150,200,16,16,time_show_on_screen);
/*使用systick对程序某部分计时*/
SysTick->LOAD = 0xFFFFFF; /* Time load (SysTick-> LOAD is 24bit). */
SysTick->VAL = 0xFFFFFF; /* Empty the counter value. */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* 计时开始 */
while(SD_Init())//检测不到SD卡
{
LCD_ShowString(30,150,200,16,16,"SD Card Error!");
delay_ms(500);
LCD_ShowString(30,150,200,16,16,"Please Check! ");
delay_ms(500);
LED0=!LED0;//DS0闪烁
}
exfuns_init(); //为fatfs相关变量申请内存
f_mount(fs[0],"0:",1); //挂载SD卡
res=f_open (&fil,"0:/data_A.txt", FA_OPEN_ALWAYS|FA_WRITE);
f_write (&fil,"data_A1", 7, &bww);
f_write (&fil, "
", 2, &bww);
for(i=0;i<1200;i++)
{
voltage=(A1H*256+A1L)*3.3/4096;
sprintf(voltage_number,"%f",voltage);
f_write (&fil, voltage_number, 8, &bww);
f_write (&fil, "
", 2, &bww);
}
f_close(&fil);
LCD_ShowString(50,30,200,16,16,"A");
runtime = MeasureTimeStop(21);
sprintf(time_show_on_screen,"%f",runtime);
LCD_ShowString(100,170,200,16,16,time_show_on_screen);
/*
res=f_open (&fil,"0:/data_B.txt", FA_CREATE_ALWAYS|FA_WRITE);
f_write (&fil,"data_B1", 7, &bww);
f_write (&fil, "
", 2, &bww);
for(i=0;i<120;i++)
{
voltage=(B1H*256+B1L)*3.3/4096;
sprintf(voltage_number,"%f",voltage);
f_write (&fil, voltage_number, 8, &bww);
f_write (&fil, "
", 2, &bww);
}
f_close(&fil);
LCD_ShowString(50,60,200,16,16,"B");
res=f_open (&fil,"0:/data_C.txt", FA_CREATE_ALWAYS|FA_WRITE);
f_write (&fil,"data_C1", 7, &bww);
f_write (&fil, "
", 2, &bww);
for(i=0;i<120;i++)
{
voltage=(C1H*256+C1L)*3.3/4096;
sprintf(voltage_number,"%f",voltage);
f_write (&fil, voltage_number, 8, &bww);
f_write (&fil, "
", 2, &bww);
}
f_close(&fil);
LCD_ShowString(50,90,200,16,16,"C");
*/
//LCD_ShowString(50,120,200,16,16,"complete");
}[/mw_shl_code]
这是下位机的程序:
[mw_shl_code=c,true] while(1)
{
if(DMA_Flag)
{
if(DMA_GetCurrentMemoryTarget(DMA2_Stream0) == 1) //当前是内存1,那么先处理内存0
{
/*使用systick对程序某部分计时*/
SysTick->LOAD = 0xFFFFFF; /* Time load (SysTick-> LOAD is 24bit). */
SysTick->VAL = 0xFFFFFF; /* Empty the counter value. */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown. */
LCD_ShowString(30,150,200,16,16,"memory0");
for(packet_number=0;packet_number<40;packet_number++)
{
for(i=0;i<30;i++)
{
txpacket_A1L[i+2]=(ADCValue0[packet_number*30+i]&0x0000FFFF)&0x00FF;
txpacket_A1H[i+2]=((ADCValue0[packet_number*30+i]&0x0000FFFF)&0xFF00)>>8;
}
while(NRF24L01_TxPacket(txpacket_A1L)!=TX_OK);
while(NRF24L01_TxPacket(txpacket_A1H)!=TX_OK);
}
runtime = MeasureTimeStop(21);
sprintf(time_show_on_screen,"%f",runtime);
LCD_ShowString(150,150,200,16,16,time_show_on_screen);
}
else //处理内存1
{
LCD_ShowString(30,190,200,16,16,"memory1");
/*使用systick对程序某部分计时*/
SysTick->LOAD = 0xFFFFFF; /* Time load (SysTick-> LOAD is 24bit). */
SysTick->VAL = 0xFFFFFF; /* Empty the counter value. */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown. */
for(packet_number=0;packet_number<40;packet_number++)
{
for(i=0;i<30;i++)
{
txpacket_A1L[i+2]=(ADCValue1[packet_number*30+i]&0x0000FFFF)&0x00FF;
txpacket_A1H[i+2]=((ADCValue1[packet_number*30+i]&0x0000FFFF)&0xFF00)>>8;
}
while(NRF24L01_TxPacket(txpacket_A1L)!=TX_OK);
while(NRF24L01_TxPacket(txpacket_A1H)!=TX_OK);
}
runtime = MeasureTimeStop(21);
sprintf(time_show_on_screen,"%f",runtime);
LCD_ShowString(150,190,200,16,16,time_show_on_screen);
}
DMA_Flag=0;
}
}
}
void DMA2_Stream0_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream0,DMA_IT_TCIF0) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
DMA_Flag = 1; //传输完成标志位
}
}[/mw_shl_code]
一周热门 更多>