cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4DISCOVERY Register Level Programming

Yadav
Associate II
Posted on June 09, 2018 at 04:32

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 #stm32
23 REPLIES 23
Yadav
Associate II
Posted on June 14, 2018 at 03:51

   Sir, i have tried both the option. But still i could not get the output for ADC.

Please someone help me out to get this output. I have tried my best and still trying.

#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>=500)

{

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 pull

RCC->AHB1ENR |=(1<<0); // clock for gpioA

GPIOA->MODER |= (1<<3)|(1<<2); // Analog mode

GPIOA->PUPDR&=~(0X00000000);//No push pull

// GPIOA->AFR[0]|=(1<<0)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1

GPIOA->OTYPER&=~(1<<1);//pin 1 as push pull

GPIOA->OSPEEDR|=(1<<3);//HIGH speed

}

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<<24); //10BIT 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

ADC1->CR2&=~(1<<30);// STOP CONVERSION

}

return(adc_value1);

}
Posted on June 14, 2018 at 06:26

Maybe you can check here:

https://github.com/turboscrew/blue_pill_init

It's for a 'blue pill' (stm32F103-board), but the other STM32 uC:s should wotk pretty much the same way.

But please don't write programs the way I wrote that. The 'program' is rather an executable 'document' to myself.

(Everything in one file, easy to search for text.)

I have never tried the external ADC:s, but the internal temperature sensor (

ADC1_IN16

) works fine.

Some other peripherals are used as well.

The DHT-sensor handling is better done by a timer alone and not with GPIOs as it's done there.

The only code that I didn't write myself is 'stdint.h', so no libraries or frameworks.

Yadav
Associate II
Posted on June 16, 2018 at 13:51

Hi.. Now I am working on STM32F4Doscovery board timers. I want to make a timer interrupt for an application. It can be 1sec or 500ms interrupt. I have read the datasheet and written the program accordingly. But i dont know how to use interrupt in STM32. I have tried something, but nothing is happening. Can someone suggest me a solution??

#include 'stm32f4xx.h'

// Component selection

void GPIO_Init(void);

void tim10_init(void);

int main(void)

{

GPIO_Init();

tim10_init();

while (1)

{

GPIOD->ODR|=(1<<15);

}

}

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 15 is output

GPIOD->OTYPER&=~(1<<15);//pin 15 as push pull

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

}

void tim10_init(void)

{

RCC->APB2ENR|=(1<<17);

TIM10->CR1|=(1<<7);

TIM10->DIER|=(1<<1);

TIM10->SR|=(1<<1);

TIM10->EGR|=(1<<1);

TIM10->CCMR1|=(1<<3)|(0<<1)|(0<<0);

TIM10->CCER|=(1<<0);

//TIM10->CCR1=5;

TIM10->PSC|=4200;

TIM10->ARR|=1000;

NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);

}

void TIM10_IRQHandler(void)

{

GPIOD->ODR|=(1<<14);

}

Posted on June 16, 2018 at 14:13

https://community.st.com/0D50X00009XkWGnSAN