AnsweredAssumed Answered

HC-05 & HC-06 Bluetooth receiving wrong data usart

Question asked by dragomir.michael on Sep 29, 2016
Latest reply on Sep 30, 2016 by dragomir.michael
Hi, 

I got two STM32 board which I'm trying to send data through one and receive it on the other board through bluetooth. I have configured the HC-05 as master and it connects to the HC-6 successfully.

The problem is the data i receive on the other board is not making any sense at all. When I use the same code and using a cable to send the data through the boards its working fine. I really appreciate if someone could help me with this problem, i have been stuck for over 2 days now and i have tried everything..

Here is my code for the TX board: 

void USART_Config(void);
void appInit(void){
    init_GPIO(GPIOE, GPIO_Pin_All, GPIO_Mode_OUT, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
    init_GPIO(GPIOC, GPIO_Pin_0, GPIO_Mode_AN, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
    init_GPIO(GPIOC, GPIO_Pin_1, GPIO_Mode_AN, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
}
 
uint16_t read_ADC_new(ADC_TypeDef* ADCx){
    ADC_SoftwareStartConv(ADCx);
    while(ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET)
    ;
    return ADC_GetConversionValue(ADCx);
}
 
void init_Any_ADC(ADC_TypeDef* ADCx,
    uint32_t ADC_Resolution,
    FunctionalState ADC_ScanConvMode,
    FunctionalState ADC_ContinuousConvMode,
    uint32_t ADC_ExternalTrigConvEdge,
    uint32_t ADC_ExternalTrigConv,
    uint32_t ADC_DataAlign,
    uint32_t ADC_NbrOfConversion){
    ADC_InitTypeDef ADC_InitStructure;
 
    ADC_DeInit();
 
    ADC_InitStructure.ADC_Resolution = ADC_Resolution;
    ADC_InitStructure.ADC_ScanConvMode = ADC_ScanConvMode;
    ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign;
    ADC_InitStructure.ADC_NbrOfConversion = ADC_NbrOfConversion;
 
    ADC_Init(ADCx, &ADC_InitStructure);
}
 
void ADC_Config(void){
    initCommon_ADC(ADC_Mode_Independent, ADC_Prescaler_Div2, ADC_DMAAccessMode_Disabled, ADC_TwoSamplingDelay_10Cycles);
    init_Any_ADC(ADC2, ADC_Resolution_6b, DISABLE, ENABLE, ADC_ExternalTrigConvEdge_None, ADC_ExternalTrigConv_T1_CC1,
        ADC_DataAlign_Right, 1);
    init_Any_ADC(ADC3, ADC_Resolution_6b, DISABLE, ENABLE, ADC_ExternalTrigConvEdge_None, ADC_ExternalTrigConv_T2_CC3,
        ADC_DataAlign_Right, 1);
 
    ADC_Cmd(ADC2, ENABLE);
    ADC_Cmd(ADC3, ENABLE);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 1, ADC_SampleTime_144Cycles);
    ADC_RegularChannelConfig(ADC3, ADC_Channel_11, 1, ADC_SampleTime_144Cycles);
}
 
void USART_Config(void){
    // Enable UART clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
    // Connect USART(3) to pin0 OUTPUT (TX)
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
     
    // Configure TX pin to alternative function with push-pull
    init_GPIO(GPIOA, GPIO_Pin_0, GPIO_Mode_AF, GPIO_Speed_2MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
    init_GPIO(GPIOA, GPIO_Pin_2, GPIO_Mode_AF, GPIO_Speed_2MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
 
     
    init_SCI(UART4, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Tx,
        USART_HardwareFlowControl_None);
    init_SCI(USART2, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Tx,
        USART_HardwareFlowControl_None);
    USART_Cmd(USART2, ENABLE);
}
 
 
void InitializeLEDs(){
   
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    init_GPIO(GPIOB, GPIO_Pin_4, GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
}
 
void sendString(char *s){
  while(*s){
    while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
    USART_SendData(UART4, 0x01);
  }
}
 
int main(int argc, char** argv){
 
 
    appInit();
    USART_Config();
    ADC_Config();
    while(1) {
 
       uint16_t power = read_ADC_new(ADC2);
       uint16_t steering = read_ADC_new(ADC3);
        power = (power >> 6) / 8; // 3 bitar gas
        steering = steering / 2; // 5 bitar styrning
        //uint8_t data = 0xAA;
        uint8_t data = ((power << 5) + steering);
         
        while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
            USART_SendData(USART2, data);
    }
}


And here is the code for RX board: 

void appInit(void)
{
    init_GPIO(GPIOE, GPIO_Pin_All, GPIO_Mode_OUT, GPIO_Speed_2MHz, GPIO_OType_PP, GPIO_PuPd_UP);
 
     
}
 
 
void USART_Config(void)
{   //Enable clock for GPIOC
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    // Enable UART clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    // Connect UaRT4 to pin11 INPUT (RX)
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
    // Configure RX pin to alternative function with push-pull
    init_GPIO(GPIOD, GPIO_Pin_6, GPIO_Mode_AF, GPIO_Speed_2MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
    //Configure UART4 with correct paramaters.
    init_SCI(USART2, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Rx,
        USART_HardwareFlowControl_None);
    //Enable UART4
    USART_Cmd(USART2, ENABLE);
}
 
//Function to init TIM
void Init_Timer(int period)
{
 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
 
    TIM_TimeBaseInitTypeDef TIM_Structure;
    TIM_Structure.TIM_Prescaler = 100;
    TIM_Structure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_Structure.TIM_Period = period;
    TIM_Structure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_Structure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_Structure);
    TIM_Cmd(TIM3, ENABLE);
}
 
//Function to set TIM to pwm mode.
void Init_PWM(int pulse)
{
    TIM_OCInitTypeDef OC_Init = {0,};
    OC_Init.TIM_OCMode = TIM_OCMode_PWM1;
    OC_Init.TIM_Pulse = pulse;
    OC_Init.TIM_OutputState = TIM_OutputState_Enable;
    OC_Init.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM3, &OC_Init);
    init_GPIO(GPIOB, GPIO_Pin_4, GPIO_Mode_AF, GPIO_Speed_100MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3);
    
}
 
void InitializeLEDs()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    init_GPIO(GPIOB, GPIO_Pin_4, GPIO_Mode_AF, GPIO_Speed_2MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
    // init_GPIO(GPIOB, GPIO_Pin_1, GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL);
}
 
 
int main(int argc, char** argv)
{
  //  InitializeLEDs();
   
  //11927
  // max höger 880
  // styrning idle 1200
   
  // körning idle 1000;
    Init_Timer(11700);
    Init_PWM(1500);
    appInit();
    USART_Config();
    unsigned char c;
    uint16_t t = 1116;
 
    while(1) {
        while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
        uint8_t val = USART_ReceiveData(USART2);
        write_GPIO_Byte(GPIOE, LOW_BYTE, val);
             
    }
}

Outcomes