STM32 GPIO register level input

1

It's actually a basic code for controlling buzzer with leds. I did with HAL libraries.But the institution I was working with wanted to do this at the register level too. But I couldn't it. It's not too complicated however i don't understand where the problem is. My code in below.

    #include "stm32f10x.h"
void SystemCoreClockConfigure(void) 
{
    RCC->CR |= ((uint32_t)RCC_CR_HSION);                     // Enable HSI
  while ((RCC->CR & RCC_CR_HSIRDY) == 0);                  // Wait for HSI Ready

  RCC->CFGR = RCC_CFGR_SW_HSI;                             // HSI is system clock
  while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);  // Wait for HSI used as system clock

  FLASH->ACR  = FLASH_ACR_PRFTBE;                          // Enable Prefetch Buffer
  FLASH->ACR |= FLASH_ACR_LATENCY;                         // Flash 1 wait state

  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;                         // HCLK = SYSCLK
  RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;                        // APB1 = HCLK/2
  RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;                        // APB2 = HCLK

  RCC->CR &= ~RCC_CR_PLLON;                                // Disable PLL

 // PLL configuration:  = HSI/2 * 12 = 48 MHz
  RCC->CFGR &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL);
  RCC->CFGR |=  (RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLMULL12);

  RCC->CR |= RCC_CR_PLLON;                                 // Enable PLL
  while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP();           // Wait till PLL is ready

  RCC->CFGR &= ~RCC_CFGR_SW;                               // Select PLL as system clock source
  RCC->CFGR |=  RCC_CFGR_SW_PLL;
  while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);  // Wait till PLL is system clock src
}

This is the first oscillator activation part.I know that there is no problem until the main function. In main:

    int main(void)
    {

        SystemCoreClockUpdate();
        SysTick_Config(SystemCoreClock / 1000);     
        RCC->APB2ENR |= (1UL << 2);                /* Enable GPIOA clock            */
        RCC->APB2ENR |= (1UL << 4);                /* Enable GPIOC clock            */  

    //  Configure the GPIO Buttons 
    GPIOA->CRL   = 0x00000044;  //1SET    // GPIOA -> 0,1    
    GPIOC->CRL   = 0x00004400;  //3SET    // GPIOC -> 3,2   

    while (1)
  { 
    if(GPIOA->IDR == 0x0000003) // PA1 ON (Led4) //
    {
        GPIOC->CRH   = 0x00000002;  //  BUZZER SET 
        GPIOC->BSRR  = 0x00000100;  //  BUZZER ON
    }
  } 
 }

I'm using keil and I know that if i only set and on buzzer after GPIOC->CRL = 0x00004400; //3SET // GPIOC -> 3,2 it's working.But with the reading input to button with GPIO->IDR(Input Data Reg.) It's not toggle or whatever i don't know.I've been stuck here for a week and it's really annoying. Heelp me !?

arm
embedded
stm32
gpio
keil
asked on Stack Overflow Feb 10, 2018 by magnesian

1 Answer

0

the code inside your while loop is completely wrong!

while(1)
{
   if((GPIOA->IDR & 0x02) == 0x02) // 0x02 = 0b10 = PA1 (LED4)
   {
      GPIOC->BSRR = 0x100; // ON
   }
   else
   {
      GPIOC->BSRR = (0x100 << 16); // OFF
   }
}

and also set the GPIOC->CRH register only once before the while loop.

answered on Stack Overflow Jun 11, 2020 by Mahyar Shokraeian

User contributions licensed under CC BY-SA 3.0