cancel
Showing results for 
Search instead for 
Did you mean: 

Data from ADC to USART

hospodar
Associate III
Posted on April 11, 2013 at 23:06

Hello, what changes must be performed in following code in order to send acquired data from ADC to USART and not to LCD (as it is now)?

/**
******************************************************************************
* @file ADC/Basic_example/main.c 
* @author MCD Application Team
* @version V1.0.0
* @date 18-May-2012
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the ''License'');
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an ''AS IS'' BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include ''main.h''
/** @addtogroup STM32F0xx_StdPeriph_Examples
* @{
*/
/** @addtogroup Basic_example
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define MESSAGE1 ''STM32F05x CortexM0 '' 
#define MESSAGE2 '' STM320518-EVAL '' 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
int
main(
void
)
{
/*!< At this stage the microcontroller clock setting is already configured, 
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
/* LCD Display init */
Display_Init();
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Configure ADC Channel11 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADCs DeInit */
ADC_DeInit(ADC1);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure); 
/* Convert the ADC1 Channel 11 with 5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles); 
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable ADCperipheral[PerIdx] */
ADC_Cmd(ADC1, ENABLE); 
/* Wait the ADCEN falg */
while
(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); 
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
/* Infinite loop */
while
(1)
{
/* Test EOC flag */
while
(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get ADC1 converted data */
ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
/* Compute the voltage */
ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
/* Display converted data on the LCD */
Display();
}
}
/**
* @brief Display ADC converted value on LCD
* @param None
* @retval None
*/
void
Display(
void
)
{
uint32_t v=0,mv=0;
uint8_t text[50];
v=(ADC1ConvertedVoltage)/1000;
mv = (ADC1ConvertedVoltage%1000)/100;
sprintf
((
char
*)text,
'' ADC = %d,%d V ''
,v,mv);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(LINE(6),text);
}
/**
* @brief Display Init (LCD)
* @param None
* @retval None
*/
void
Display_Init(
void
)
{
/* Initialize the LCD */
STM320518_LCD_Init();
/* Clear the LCD */
LCD_Clear(White);
/* Set the LCD Text size */
LCD_SetFont(&Font8x12);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
/* Display */
LCD_DisplayStringLine(LINE(0x13), 
'' ADC conversion example (Basic example)''
);
/* Set the LCD Text size */
LCD_SetFont(&Font16x24);
LCD_DisplayStringLine(LINE(0), 
''STM32F05x CortexM0 ''
);
LCD_DisplayStringLine(LINE(1), 
'' STM320518-EVAL ''
);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
/* Display */
LCD_DisplayStringLine(LINE(3),
'' Turn RV3(PC.01) ''
);
LCD_DisplayStringLine(LINE(4),
'' Potentiometer ''
);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

4 REPLIES 4
Posted on April 11, 2013 at 23:16

In Keil, I'd retarget STDIO to use the USART and use printf().

Alternatively you just initialize the USART and pins, and write a string output function.

void USART_OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, *s++);
}
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on April 11, 2013 at 23:20

Portable to STM32F2, adjust USART and pins as required.

// STM32 USART1 (Tx PA.9, Rx PA.10) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); // USART1_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // USART1_RX
}
/**************************************************************************************/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
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_Cmd(USART1, ENABLE);
}
/**************************************************************************************/
void USART1_OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, *s++);
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
while(1)
{
USART1_OutString(''Welcome to wherever you are!

'');
}
while(1); // Don't want to exit
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on April 12, 2013 at 02:42

I modified the code as follows. I added USART initialization and configuration, removed all command with LCD. I used printf in function ''

Display()''

,

which is retargered to the USART by the second code. However, it don´t working, I don´t receiving any data on the terminal. What is wrong?

#include ''main.h''
/** @addtogroup STM32F0xx_StdPeriph_Examples
* @{
*/
/** @addtogroup Basic_example
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define MESSAGE1 ''STM32F05x CortexM0 ''
#define MESSAGE2 '' STM320518-EVAL ''
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
int

main(

void

)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
/* GPIOC and A Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Povolenie hodin pre USART */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Premapovanie pinov PA9 a PA10 na USART */
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/* USART TX ako alternativna funkcia */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Nastavenie USART Rx pre prijem */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Nastavenie parametrov seriovej linky USART
pomocou inicializacnej struktury */
USART_InitStructure.USART_BaudRate = 115200;
// Bitova rychlost - 115200 baud
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// Dlzka slova - 8 bit
USART_InitStructure.USART_StopBits = USART_StopBits_1;
// Jeden stop-bit
USART_InitStructure.USART_Parity = USART_Parity_No;
// Bez parity
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
// Hardverove riadenie (RTS,CTS) vypnute
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// Prijimanie a vysielanie povolene
USART_Init(USART1, &USART_InitStructure);
// Inicializacia USART zo struktury
/* Configure ADC Channel11 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADCs DeInit */
ADC_DeInit(ADC1);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* Convert the ADC1 Channel 11 with 5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable ADCperipheral[PerIdx] */
ADC_Cmd(ADC1, ENABLE);
USART_Cmd(USART1, ENABLE);
/* Wait the ADCEN falg */
while

(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
/* Infinite loop */
while

(1)
{
/* Test EOC flag */
while

(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get ADC1 converted data */
ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
/* Compute the voltage */
ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
/* Display converted data on the LCD */
Display();
}
}
void

Display(

void

)
{
uint32_t v=0,mv=0;
uint8_t text[50];
v=(ADC1ConvertedVoltage)/1000;
mv = (ADC1ConvertedVoltage%1000)/100;
printf

((

char

*)text,

'' ADC = %d,%d V ''

,v,mv);
}

Hosting of stdio functionality through USART1:

#include <rt_misc.h>
#include ''stdio_through_usart.h''
#include ''stm32f0xx.h''
#include ''periph_init.h''
#include <stdio.h>
#pragma import(__use_no_semihosting_swi)
struct

__FILE {

int

handle;

/* Add whatever you need here */

};
FILE

__stdout;
FILE

__stdin;
int
fputc

(

int

ch,

FILE

*f)
{
while

(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
return

(ch);
}
int
fgetc

(

FILE

*f)
{
char

ch;
while

(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART1);
return

((

int

)ch);
}
int
ferror

(

FILE

*f)
{
/* Your implementation of ferror */
return

EOF;
}
void

_ttywrch(

int

ch)
{
while

(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
}
void

_sys_exit(

int

return_code)
{
label:

goto

label;

/* endless loop */

}
//******************************************************************************

Posted on April 12, 2013 at 03:10

Noticed it was an F0, sorry.

void Display(void)
{
uint32_t v=0,mv=0;
v=(ADC1ConvertedVoltage)/1000;
mv = (ADC1ConvertedVoltage%1000)/100;
printf('' ADC = %d,%d V 

'',v,mv);
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..