Hi, I have stm32f4 discovery. I am reading a voltage from a pin through the ADC1. My question is: how can I visualize the read voltage on the Computer (with Keil)? Which connection I have to do to visualize that? I need an UART-USB converter or the MiniB-USB cable is sufficient? Here is my code:
#include "stm32f4_discovery.h" #include "stdio.h" #include "stdlib.h" #include "stdint.h"
__IO uint16_t ADC1ConvertedValue = 0; __IO uint16_t ADC1ConvertedVoltage = 0; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure2; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; NVIC_InitTypeDef NVIC_InitStruct; NVIC_InitTypeDef NVIC_adc;
void GPIOInitialize(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable clock for GPIOB
/* USART1 Tx on PA9 | Rx on PA10 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);//Connect PA9 to USART1_Tx GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);//Connect PA10 to USART1_Rx }
void UART_Initialize(void) { /* Enable peripheral clock for USART1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* USART1 configured as follow: * BaudRate 9600 baud * Word Length 8 Bits * 1 Stop Bit * No parity * Hardware flow control disabled * 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 configuration USART_Cmd(USART1, ENABLE); // Enable USART
USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART1, &USART_ClockInitStructure);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0xFF; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0xFF; NVIC_Init(&NVIC_InitStruct); //USART_ITConfig(USART1, USART_IT_TC, ENABLE); }
void Delay(__IO uint32_t nCount) {
while(nCount--) { } } void ADC_Configuration(void) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure2); GPIO_Init(GPIOD, &GPIO_InitStructure);
NVIC_adc.NVIC_IRQChannel = ADC_IRQn; NVIC_adc.NVIC_IRQChannelCmd = ENABLE; NVIC_adc.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_adc.NVIC_IRQChannelSubPriority = 0x01; NVIC_Init(&NVIC_adc);
ADC_DeInit(); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1,ADC_SampleTime_480Cycles); ADC_ITConfig(ADC1,ADC_IT_AWD,ENABLE);
ADC_AnalogWatchdogSingleChannelConfig(ADC1,ADC_Channel_12); ADC_AnalogWatchdogThresholdsConfig(ADC1,0xFFF,0xF00); ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);
ADC_Cmd(ADC1 , ENABLE);
}
void ADC_IRQHandler(void) { Delay(0xFFFFFF); ADC_ClearFlag(ADC1,ADC_FLAG_AWD); ADC_ClearITPendingBit(ADC1, ADC_IT_AWD); GPIO_ToggleBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); ADC1ConvertedValue=ADC_GetConversionValue(ADC1); USART_SendData(USART1,ADC1ConvertedValue); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); Delay(0xFFFFFF); GPIO_ToggleBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13); Delay(0xFFFFFF); }
int main(){ Delay(0xFFFFFF); GPIOInitialize(); UART_Initialize(); ADC_Configuration(); ADC_SoftwareStartConv(ADC1); while(1){ }
Code unreadable, read the instructions.
USART1 PA9/PA10 probably not viable here. Try USART6 PC6/PC7. Then add some string output functions and form data with sprintf(), or add retargeting code so that printf(), putchar() output via the chosen serial port.
The SendData function sends single byte ASCII/binary values, if you want it to send decimal digits you can read you'll need to convert them. You should wait for TXE before outputting to the buffer.
#include <rt_misc.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(USART6, USART_FLAG_TXE) == RESET); USART_SendData(USART6, ch); return(ch); } int fgetc(FILE *f) { char ch; while(USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == RESET); ch = USART_ReceiveData(USART6); return((int)ch); } int ferror(FILE *f) { /* Your implementation of ferror */ return EOF; } void _ttywrch(int ch) { while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); USART_SendData(USART6, ch); } void _sys_exit(int return_code) { label: goto label; /* endless loop */ }
void USART6_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* GPIOC clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* USART6 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); /*-------------------------- GPIO Configuration ----------------------------*/ 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; /* Configure USART RX and TX pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect USART pins to AF */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); // USART6_RX GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); // USART6_TX /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 115200 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 = 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; USART_Init(USART6, &USART_InitStructure); USART_Cmd(USART6, ENABLE); }