AnsweredAssumed Answered

STM32L, ADC, USART, INTERRUPT, PC COMMUNICATION

Question asked by triyadi.lukman on Aug 31, 2014
Latest reply on Aug 31, 2014 by triyadi.lukman
I used the ADC on STM32L, and usart to communicate with the PC. I need help with my code. My code is not working properly upon receiving instructions from the PC, such as receiving a character to tell the STM32 ADC to start converting and then sends the data to a PC to display the signal shape. I put some code, and i eliminate some part of code
 /*


//==Includes==
#include "stm32l1xx.h"
#include <stdio.h>


 




USART_InitTypeDef USART_InitStructure;
ADC_InitTypeDef ADC_InitStructure; 
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;




void RCC_conf(void)
{
RCC_DeInit();
RCC_HSICmd(ENABLE);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);




RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);


}


char USART_getchar(void) // fungsi menerima data//
{
  char ch;
  while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); 
  ch = USART_ReceiveData(USART1);      
  return ch;
}


void USART_putchar(uint8_t ch) // fungsi mengirim data//
{
  USART_SendData(USART1, ch);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);     
}
void USART_puts(char * ch ) // mengirim data berupa karakter dengan looping, tdk satu per satu//
{
     uint8_t i;
     USART_putchar(ch[i]);
}


void NVIC_Conf(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the USART Interrupt*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
     
     
/* Enable the ADC1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM1 Interrupt */


}
     
void USART1_IRQHandler(void)
{
     
     
     
}


void ADC1_IRQHandler (void)
{




}




void USART_Configuration(void)
{ /* USART parameters */ 
  USART_InitStructure.USART_BaudRate = 115200; 
  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;
  /* Configuring and enabling USART1 */ 
     USART_Init(USART1, &USART_InitStructure); 
  USART_Cmd(USART1, ENABLE);     
     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}




void GPIO_Conf(void)
{     
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_InitStructure.GPIO_Speed =  GPIO_Speed_40MHz;
     GPIO_Init(GPIOB, &GPIO_InitStructure);
     
     //==Configure ADC pins (PC.0) as analog inputs==
     GPIO_StructInit(&GPIO_InitStructure); // Reset init structure, if not it can cause issues...
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_InitStructure.GPIO_Speed =  GPIO_Speed_40MHz;
     GPIO_Init(GPIOC, &GPIO_InitStructure);
     
     /* Configure USART1 Tx (PA9) as alternate function push-pull */
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 ,GPIO_AF_USART1); //Tx 
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  /* Configure USART1 Rx (PA10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}




void ADC_Conf(void) 
{
     
     ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div1;
     ADC_CommonInit(&ADC_CommonInitStructure);
     //==Configure ADC1 - Channel 14 and Channel 15==
     ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
     ADC_InitStructure.ADC_ScanConvMode = DISABLE;
     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
     ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
     ADC_InitStructure.ADC_NbrOfConversion = 1; //We using one channels
     ADC_Init(ADC1, &ADC_InitStructure); //Initialise ADC1
     ADC_Cmd(ADC1, ENABLE); //Enable ADC1
     
}
uint16_t readADC1(uint8_t channel)
{ ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_384Cycles );
  // Start the conversion
  ADC_SoftwareStartConv(ADC1);
  // Wait until conversion completion
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  // Get the conversion value
  return ADC_GetConversionValue(ADC1);
}




/*
* Main program start
*/
int main(void) 
{
     
     
     char word [200]; // ADC DIGITAL OUTPUT FOR TRANSMITING PURPOSE
  int samples[200]; // ADC DIGITAL OUTPUT SAMPLE
  int m;
     char c_har [1];// CHAR FOR TO TELL PC
  char k;// CHAR FOR TO TELL STM32


  int delay;     //delay = 1 (default @ fs = 100%()
   
  
     
     RCC_conf();
     GPIO_Conf();
     ADC_Conf();
     USART_Configuration();




while (1) 
{
           k = USART_getchar(); //receive char '2' from PC to start 
           
     if (k == '2')
           {
        
                     k = USART_getchar(); // receive char 's' from PC to  start take sample ADC
                         
                                   
         if(k=='s')        
         {
                             
                             
                          for(m=0;m<200;m++)
                          {  
                               samples[m]=readADC1(10);          // ****** Taking Samples ******* //
                               
                                                                           
                          }
                            
                    
                                                          
                             /*
                           Send 200 Samples using Protocol "f" at the begining of the line
                           And Ending with "\n" indicating New Line.
                           
                           */
                          sprintf(c_har,"%c",'f'); //send char to tell PC that start to convert The 'String' to 'Double'
                          USART_puts(c_har);
                             
                          for(m=0;m<200;m++)
                          {
                               sprintf(word,"%d\n",samples[m]); //send the ADC output data
                               USART_puts(word);
                          }
                          
                              
        }                     
               }              
    }
          
     
     
}//end of main

Outcomes