Hi, I am just a beginner with Keil and STM32, so I apologize for stupid questions. I would like to read the conversion of the ADC1 (using the Analog Wathcdog timer) present on the STM32F4 discovery board and send it through USART1 to the PC (and visualize that). I am using FTDI232 USB UART converter, and I have attached the pins as follows (FTDI232-STM32F4):
RX-PA10 TX-PA9 GND-GND
My code is the following:
#include "stm32f4_discovery.h" #include "stdio.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;
int main(){ Delay(0xFFFFFF); GPIOInitialize(); ADC_Configuration(); UART_Initialize(); ADC_SoftwareStartConv(ADC1); //GPIO_SetBits(GPIOC, GPIO_Pin_2); while(1); }
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) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
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_InitTypeDef NVIC_InitStruct; 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_InitTypeDef NVIC_adc; 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); }
I do the debugging and run the program (on Keil), it works, but I can't see any value in the windows UART#1,2,3...
Am I connecting it wrong? How can I visualize the data exchanged by the USART1? Which are the settings to do (Options for target) to use the specified hardware?
Use a Terminal program, and select the COMx port you're device is physically connected too. Look in Device Manager under Ports if you are confused. The only thing that knows about "USART1" is the STM32, no other implied connectivity is established by that.
RealTerm, TeraTerm, or whatever.
I have installed Tera Term,connected the ftdi232 (COM port 6) and opened the terminal. The previous program is running on the discovery board; so it sends the convertedvoltage through the uart1. I connected the pin of ftdi232 as before. How can I visualize then the uart messages on the terminal? And for sending messages through the Tera Term terminal?
The classic STM32F4-DISCO (F407, no LCD) can't use USART1 PA9/PA10 because of other circuitry on the board. USART6 PC6/PC7 is usable.
Provided the baud rates are compatible, you should see data on the terminal, and be able to type back. It's important that HSE_VALUE, and the PLL settings, are configured for the 8 MHz used on the board.
Check the signals and rates with a scope.
I can't read the code as you failed to format it, see instructions.