We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
HI…I am using adc and then usart to send data to pc at timer interrupts.My code was working properly. checked it by sampling a signal from signal generator and displaying it on matlab GUI.But suddenly it stopped working.now it correctly samples DC signal but any varying signal is sampled by just continuous 0's and 1's.I checked everything independently. Timer and usart are working accurately. The problem is with ADC. can anyone help me to resolve this? #include "stm32f4xx_adc.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" #include <misc.h> #include <stm32f4xx_usart.h> #include <stdint.h> ADC_CommonInitTypeDef ADC_CommonInitStructure; volatile unsigned int ConvertedValue = 0; volatile unsigned int sample_data; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; GPIO_InitTypeDef GPIO_InitStructure; void INTTIM_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ RCC->CFGR |=0X1400; //setting AHB1 prescalar eqaul to 4 TIM_TimeBaseStructure.TIM_Period = 1000-1; // 1 MHz down to 1 KHz (1 ms) TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock) TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); } void init_USART1(uint32_t baudrate){ GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller) RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC->CFGR |=0xE000; /* enable the peripheral clock for the pins used by * USART1, PB6 for TX and PB7 for RX */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate! GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain) GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // this activates the pullup resistors on the IO pins GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStruct); /* The RX and TX pins are now connected to their AF * so that the USART1 can take over control of the * pins */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStruct); /* Here the USART1 receive interrupt is enabled * and the interrupt controller is configured * to jump to the USART1_IRQHandler() function * if the USART1 receive interrupt occurs */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // finally this enables the complete USART1 peripheral USART_Cmd(USART1, ENABLE); } void USART_puts(USART_TypeDef* USARTx,volatile int s){ //while(*s){ // wait until data register is empty while( !(USARTx->SR & 0x00000040) ); USART_SendData(USARTx, s); //*s++; //} }
void TIM2_IRQHandler(void) //Timer Interupt for sending data { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); GPIO_ToggleBits(GPIOA,GPIO_Pin_0); sample_data = (uint8_t) (ConvertedValue>>4); //convert 12 bit data to 8 bits USART_puts(USART1,sample_data );
} }
void adc_configure(){ ADC_InitTypeDef ADC_init_structure; //Structure for adc confguration GPIO_InitTypeDef GPIO_initStructre; //Structure for analog input pin //Clock configuration RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE); //Analog pin configuration GPIO_initStructre.GPIO_Pin = GPIO_Pin_0;//The channel 10 is connected to PC0 GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN; //The PC0 pin is configured in analog mode GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down GPIO_Init(GPIOC,&GPIO_initStructre);//Affecting the port with the initialization structure configuration //ADC structure configuration ADC_DeInit(); ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right; ADC_init_structure.ADC_Resolution = ADC_Resolution_12b; ADC_init_structure.ADC_ContinuousConvMode = ENABLE; ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_init_structure.ADC_NbrOfConversion = 1;//I think this one is clear :p ADC_init_structure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel ADC_Init(ADC1,&ADC_init_structure); ADC_Cmd(ADC1,ENABLE); //Select the channel to be read from ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_144Cycles); } int adc_convert(){ ADC_SoftwareStartConv(ADC1);//Start the conversion while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion return ADC_GetConversionValue(ADC1); //Return the converted data }
int main(void){
init_USART1(19200); INTTIM_Config(); adc_configure();
while(1){ ConvertedValue = adc_convert();//Read the ADC converted value } }
I am using adc and then usart to send data to pc at timer interrupts.My code was working properly. checked it by sampling a signal from signal generator and displaying it on matlab GUI.But suddenly it stopped working.now it correctly samples DC signal but any varying signal is sampled by just continuous 0's and 1's.I checked everything independently. Timer and usart are working accurately. The problem is with ADC. can anyone help me to resolve this
Programs seldom magically stops working.
That is a reason why it is important to try to use some form of version control. Even if that version control is regularly making a copy of the source files to directories bu1/, bu2/, bu3/, ...
Being able to compare what has changed between working/non-working is often the fastest way to spot errors that has managed to work themselves into the code or project settings.
Anyway - back to debugging. Get the ADC module to function as an independent box" of functionality.