2018-06-08 07:32 PM
Hi Everyone,
I am new to stm32f4 series. I have done GPIO example using libraries. But i wanted to do register level programming. I have tried it but i could not get output for this. Can anyone help me out?
&sharpinclude 'main.h'
&sharpinclude 'stm32f4xx.h'&sharpinclude 'stm32f4xx_hal.h' // Device header
void SystemClock_Config(void);
void GPIO_Init(void);int main(void)
{// HAL_Init();
//unsigned int delay=0;SystemClock_Config();
GPIO_Init();while (1)
{ GPIOD->ODR|=0X00004000; //for(delay=0;delay<=16666667;delay++); HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15); GPIOD->ODR&=~0X00004000; // for(delay=0;delay<=16666667;delay++);HAL_Delay(500);}
/* USER CODE END 3 */}
/**
* @brief System Clock Configuration * @retval None */void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;/**Configure the main internal regulator output voltage
*/ __HAL_RCC_PWR_CLK_ENABLE();__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }/**Initializes the CPU, AHB and APB busses clocks
*/ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{ _Error_Handler(__FILE__, __LINE__); }/**Configure the Systick interrupt time
*/ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);/**Configure the Systick
*/ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}/** Configure pins as
* Analog * Input * Output * EVENT_OUT * EXTI*/void GPIO_Init(void){ RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN; GPIOD->MODER|=0X1000000;// PIN 14 as output GPIOD->OTYPER|=0X00004000;//Output push pull GPIOD->OSPEEDR|=0X00000000;/ low speed GPIOD->PUPDR&=0X00000000;//no pull up and pull down}
void _Error_Handler(char *file, int line){ /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ while(1) { } /* USER CODE END Error_Handler_Debug */}&sharpifdef USE_FULL_ASSERT
/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */void assert_failed(uint8_t* file, uint32_t line){ /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */ /* USER CODE END 6 */}&sharpendif /* USE_FULL_ASSERT *//**
* @} *//**
* @} *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#stm32f4discovery #stm32f4 #stm322018-06-09 11:14 PM
,
,
We need to provide active high to the led.
Problem was with speed register. i had set it high and now its working fine. ,
,
But my question is, why it is not working in low speed?
,
here is my working code.
,
♯ include 'stm32f4xx.h'
,
void GPIO_Init(void),int main(void)
,
{,
unsigned int delay=0,,
GPIO_Init(),while (1)
,
{,
GPIOD->,ODR|=(1<,<,14),,
for(delay=0,delay<,=16666667,delay++),GPIOD->,ODR&,=~(1<,<,14),
,
for(delay=0,delay<,=16666667,delay++),,
}
}
,
void GPIO_Init(void)
,
{,
RCC->,AHB1ENR|=RCC_AHB1ENR_GPIODEN,GPIOD->,MODER|=(1<,<,28), // pin 14 is output
,
GPIOD->,OTYPER&,=~(1<,<,14),//pin 14 as push pull,
GPIOD->,OSPEEDR|=(1<,<,29)|(1<,<,28),//LOW speed,
GPIOD->,PUPDR&,=~(1<,<,29)|(1<,<,28),//No push pull,
}
2018-06-09 11:15 PM
I got the output. But i wanna learn how to debug and inspect my code. Can u give any document or link for this ?
2018-06-09 11:39 PM
Use atolic trustudio debugger with st link and do step by step execution.
On Sun, Jun 10, 2018, 9:46 AM Yadavaprasath Gopi <
2018-06-10 02:09 AM
Problem was with speed register. i had set it high and now its working fine.
It most probably was not. Try to comment out that one line, compile and run again.
Your delay may also be the culprit. With any complier optimisation switched on, it will eliminate those loops completely. Make the loop variable volatile.
i wanna learn how to debug and inspect my code.
There is no single advice for this, and I know of no good single source reading. You need to develop skills in many different ways - including understanding your own errors, reading other's code, utilizing various debugging methods including and excluding using debuggers, reading and understanding disassembled output from the compiler, observing register content in the debugger and simultaneously understanding how that may be intrusive for the code execution etc.
JW
2018-06-10 04:22 AM
The topics of debugging including what you have listed plus other topics (racing, multi-threaded environments, distributed processing) is really interesting.
It's worth separate blog post;) I know this is the area where the experience and talent come to play.
I believe some technics are common for many computer related fields (I spent myself 25 years as networking engineer, mostly troubleshooting IP-based systems).
2018-06-10 11:53 PM
There was another difference too, that, I think, matters more...
In the original:
GPIOD->OTYPER|=0X00004000;//Output push pull
In the new one:
GPIOD->OTYPER&=~(1<<14);//pin 14 as push pull
2018-06-12 08:34 PM
Hi Everyone,
I have successfully got the output for GPIO. Now i am started working on ADC interface. But i couldn't get the output for my code. I had given 3V to PA1 pin but still i couldn't get the proportional value in DR register. Kindly help me out to solve this issue.
#include 'stm32f4xx.h'
void GPIO_Init(void);void adc_init(void);
uint16_t adc_read();
uint16_t adc_value1=0,adc_value2=0;int main(void)
{GPIO_Init();adc_init();while (1)
{adc_value2=adc_read();
if(adc_value2>=0X000C)
{GPIOD->ODR|=(1<<14);}else{GPIOD->ODR|=(1<<15);}//
// GPIOD->ODR&=~(1<<14);// for(delay=0;delay<=4166666;delay++);// GPIOD->ODR|=(1<<15);
// for(delay=0;delay<=4166666;delay++);//// GPIOD->ODR&=~(1<<15);// for(delay=0;delay<=4166666;delay++);}
}
void GPIO_Init(void)
{RCC->AHB1ENR |=(1<<1); // clock for gpioA
GPIOA->MODER |= 0X03; // PA1 analog inputs}void adc_init(void)
{RCC->APB2ENR|=(1<<8);ADC1->CR1|=(1<<24)|(1<<0); // 1st CHANNEL // 10BIT RESOLUTION
ADC1->CR2|=(1<<10);// ADC1->CR2ADC1->SMPR2 |= 1; //15 cyclesADC1->SQR1 &= ~(1<<20); // 1 conversionADC1->SQR3 = 0x00;}uint16_t adc_read()
{ADC1->CR2|=(1<<0);// ADC ON
ADC1->CR2|=(1<<30);// START CONVERSION// if((ADC1->SR&(1<<1))) // wait untill EOC bit clear//{ADC1->SR = 0X00;adc_value1=ADC1->DR;//}ADC1->CR2&=~(1<<0);// ADC OFFreturn(adc_value1);}2018-06-12 10:13 PM
Check the documents. I recall there needs to be a delay ('Tstab' in data sheets) between ADC_ON and START_CONVERSION to get valid values. And you should wait until the conversion is done (the out-commented part). In my document (407 reference) the conversion is done when EOC gets set, not cleared.
2018-06-13 09:49 AM
I have made few changes in my code. Now i am getting random values in Data register. Kindly help me out to get exact values.
#include 'stm32f4xx.h'
void GPIO_Init(void);void adc_init(void);
uint16_t adc_read();
uint16_t adc_value1=0,adc_value2=0;int main(void)
{ GPIO_Init(); adc_init();while (1)
{ adc_value2=adc_read(); if(adc_value2>=1000) { GPIOD->ODR|=(1<<14); } else { GPIOD->ODR|=(1<<15); } // // GPIOD->ODR&=~(1<<14);// for(delay=0;delay<=4166666;delay++);// GPIOD->ODR|=(1<<15);
// for(delay=0;delay<=4166666;delay++);// // GPIOD->ODR&=~(1<<15);// for(delay=0;delay<=4166666;delay++);}
}
void GPIO_Init(void)
{ RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN; GPIOD->MODER|=(1<<28); // pin 14 is output GPIOD->OTYPER&=~(1<<14);//pin 14 as push pull GPIOD->OSPEEDR|=(1<<29)|(1<<28);//LOW speed GPIOD->PUPDR&=~(1<<29)|(1<<28);//No push pull GPIOD->MODER|=(1<<30); // pin 14 is output GPIOD->OTYPER&=~(1<<15);//pin 14 as push pull GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pullRCC->AHB1ENR |=(1<<0); // clock for gpioA
//GPIOA->AFR|=(1<<0); GPIOD->OSPEEDR|=(1<<3); GPIOA->MODER |= (1<<3); // alternate function GPIOA->PUPDR&=~(0X00000000); GPIOA->AFR[0]|=(0<<1)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1 GPIOA->OTYPER&=~(1<<1);//pin 1 as push pull }void adc_init(void)
{ RCC->APB2ENR|=(1<<8); ADC1->CR1|=(1<<0)|(1<<23); // 1st CHANNEL // WATCHDOG ENABLE ADC1->CR1&=~(1<<24);// ADC1->CR1&=~(1<<25); //12BIT RESOLUTION ADC1->CR2|=(1<<10);// EOCs is set ADC1->SMPR2 |= (1<<3); //15 cycles ADC1->SQR1 &= ~(1<<20); // 1 conversion ADC1->SQR3 = 0x00; }uint16_t adc_read()
{ ADC1->CR2|=(1<<0);// ADC ON ADC1->CR2|=(1<<30);// START CONVERSION while(ADC1->SR&(1<<1)) // wait untill EOC bit set { ADC1->SR = 0X00; adc_value1=ADC1->DR; } // ADC1->CR2&=~(1<<0);// ADC OFF return(adc_value1);}2018-06-13 04:08 PM
Should this: GPIOA->AFR[0]|=(0<<1)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1
be: GPIOA->AFR[0]|=(1<<0)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1 ?
I also remember vaguely, that ADC was not an alternate function.
I think the pin should be configured as analog input.
Not sure about the pin configuration.