中级会员
 
- 积分
- 242
- 金钱
- 242
- 注册时间
- 2015-12-28
- 在线时间
- 51 小时
|
10金钱
本帖最后由 新手驾到 于 2016-9-6 11:43 编辑
用stm32 A采集两路adc(霍尔摇杆),通过串口传递给另一个stm32 B,然后B借助串口调试工具打印在pc上。当霍尔摇杆不动,采集的adc不变化时候,pc上打印的数据正常,但是拨动摇杆adc采集数据变化时候pc上打印的串口数据就会变动、错位(第一个数据在第二个位置上打印出来,第二个在第三个位置上打印。。)。单独测试过adc采集,不会出现这样的情况,而且打印串口数据出错时候出现延时(单独测试adc不会有),原子哥,各位大神帮忙看看是个什么问题。图中后面两个数据为adc采样的数据24,25,前面两个数据100,2放着用于对比
代码都是用历程中的,然后修改对应的管脚等参数
以下是adc采集发送的代码:
int main(void)
{
float adcx1,adcx2;
u8 flag=100,speed=2;
delay_init();
NVIC_Configuration();
LED_Init();
uart1_init(9600);
Adc_Init();
while(1)
{
adcx1=Get_Adc_Average(ADC_Channel_6,10)*50/4096.0;
adcx2=Get_Adc_Average(ADC_Channel_7,10)*50/4096.0;
// printf("***%f %f \r\n",adcx1,adcx2);
delay_ms(5); USART_SendData(USART1,flag);
delay_ms(5);
USART_SendData(USART1,speed);
delay_ms(5);
USART_SendData(USART1,adcx1);
delay_ms(5);
USART_SendData(USART1,adcx2);
}
}
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB |RCC_APB2Periph_ADC1, ENABLE ); //adc1时钟使能
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置分频
//PA6,PA7设置为模拟通道
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
// delay_ms(5);
}
return temp_val/times;
}
void uart1_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
USART_DeInit(USART1);
//USART2_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
个人觉得应该是时序的问题,
USART_SendData(USART1,flag);
delay_ms(5);
在发送数据时候设定不同的延时时间5ms,8ms,会出现不同的数据错位情况。
|
-
正常情况
-
拨动摇杆数据错位情况
|