cancel
Showing results for 
Search instead for 
Did you mean: 

Digital pin status read on STM32f103C8T6

Dmitrij Newcomer
Associate
Posted on December 29, 2017 at 20:43

Dear community,

I just started to learn stm32 trying to forgive about AVR.

The problem I faced with is I can not force my STM32F103C8T6 to read pin PB0. I`ve read both Datasheet and RM0008, I saw the examples of another guys but I can`t understand what I do in the wrong way. I do believe some one with a big experience in STM32 will dedicate a few seconds of his time to show me the right way.

The task is so simple that probably there is no reason to describe it, it should be clear from the code. In the loop the programm should read PB0 and if it is 1 - envoke led2 function, otherwise - led3 function.

But led2 function never heppens. Initial part of this code works ok, the board blinks with LED as expected. But when I am trying to change PB0 stage - there is no reaction. Either I set the pin not corectly or I read the pin in wrong way...

I tried to set pin in two ways: writing 1000   and 1010 in the 3-0 bits of GPIOB_CRL register. No reaction in both cases.

Will anyone help me? Thank you in advance!

#include unsigned char statusLed1 = 0, statusLed2 = 0, statusLed3 = 0, counter = 0; void RCC_init (void); void GPIO_init(void); void soft_delay(unsigned long time); void led1(void); void led2(void); void led3(void); int main (void){ RCC_init(); GPIO_init(); GPIOB->CRL |= 0x8; // SET PB0 as a pull-up input //GPIOB->ODR |= 0x1; led2(); soft_delay(5000000); led2(); soft_delay(5000000); led3(); soft_delay(5000000); led3(); while(1){ if((GPIOB->IDR)&0x1)led2(); else led3(); } } void RCC_init(){ RCC->CR = 0x83;//reset CR register for (unsigned short i = 0; i<1280; i++){}//the same as HSEStartUp_TimeOut RCC->CR = 0x10000;// HSE ON for (unsigned short i = 0; i<1280; i++){}//the same as HSEStartUp_TimeOut while(!(RCC->CR & RCC_CR_HSERDY)){}; //FLASH->ACR |= FLASH_ACR_PRFTBE; //FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); //FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; RCC->CFGR = 0x41D0400;// main config RCC->CR |= 0x1000000;//PLL ON while(!(RCC->CR&0x2000000)){}// all bad RCC->CFGR |= 0x2;// PLL set as a system clock while(!(RCC->CFGR & 0x8)){ }//all is bad RCC->CFGR |= 0x8000;// set ADC prescaler 6 } void GPIO_init(){ RCC->APB2ENR |= 0x1C; // port A, B, C eneble GPIOB->CRL &= ~GPIO_CRL_CNF5; GPIOA->CRL &= ~GPIO_CRL_CNF3; GPIOC->CRH &= ~GPIO_CRH_CNF13; GPIOA->CRL |= GPIO_CRL_MODE3_0; //set A3 as a general purporse push-pull output GPIOB->CRL |= GPIO_CRL_MODE5_0; //set B5 as a general purporse push-pull output GPIOC->CRH |= GPIO_CRH_MODE13_0; //set C13 as a general purporse push-pull output GPIOC->BSRR |= GPIO_BSRR_BR13; // set C13 0 GPIOA->BSRR |= GPIO_BSRR_BS3; // set A3 1 GPIOB->BSRR |= GPIO_BSRR_BS5; // set B5 1 soft_delay(5000000); GPIOC->BSRR |= GPIO_BSRR_BS13; GPIOA->BSRR |= GPIO_BSRR_BR3; GPIOB->BSRR |= GPIO_BSRR_BR5; soft_delay(5000000); } void led2(void){ if(statusLed2)GPIOB->BSRR |= GPIO_BSRR_BR5; else GPIOB->BSRR |= GPIO_BSRR_BS5; statusLed2 = !statusLed2; } void led3(void){ if(statusLed3)GPIOA->BSRR |= GPIO_BSRR_BR3; else GPIOA->BSRR |= GPIO_BSRR_BS3; statusLed3 = !statusLed3; } void led1(void){ if(statusLed1)GPIOC->BSRR |= GPIO_BSRR_BS13; else GPIOC->BSRR |= GPIO_BSRR_BR13; statusLed1 = !statusLed1; } void soft_delay(unsigned long time){while(time)time--;}

1 REPLY 1
Dmitrij Newcomer
Associate
Posted on December 29, 2017 at 20:45

Sorry, have no idea how to format the code

#include <stm32f10x.h>

unsigned char statusLed1 = 0, statusLed2 = 0, statusLed3 = 0, counter = 0;

void RCC_init (void);

void GPIO_init(void);

void soft_delay(unsigned long time);

void led1(void);

void led2(void);

void led3(void);

int main (void){

RCC_init();

GPIO_init();

GPIOB->CRL |= 0x8; // SET PB0 as a pull-up input

//GPIOB->ODR |= 0x1;

led2();

soft_delay(5000000);

led2();

soft_delay(5000000);

led3();

soft_delay(5000000);

led3();

while(1){

if((GPIOB->IDR)&0x1)led2();

else led3();

}

}

void RCC_init(){

RCC->CR = 0x83;//reset CR register

for (unsigned short i = 0; i<1280; i++){}//the same as HSEStartUp_TimeOut

RCC->CR = 0x10000;// HSE ON

for (unsigned short i = 0; i<1280; i++){}//the same as HSEStartUp_TimeOut

while(!(RCC->CR & RCC_CR_HSERDY)){};

//FLASH->ACR |= FLASH_ACR_PRFTBE;

//FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);

//FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;

RCC->CFGR = 0x41D0400;// main config

RCC->CR |= 0x1000000;//PLL ON

while(!(RCC->CR&0x2000000)){}// all bad

RCC->CFGR |= 0x2;// PLL set as a system clock

while(!(RCC->CFGR & 0x8)){ }//all is bad

RCC->CFGR |= 0x8000;// set ADC prescaler 6

}

void GPIO_init(){

RCC->APB2ENR |= 0x1C; // port A, B, C eneble

GPIOB->CRL &= ~GPIO_CRL_CNF5;

GPIOA->CRL &= ~GPIO_CRL_CNF3;

GPIOC->CRH &= ~GPIO_CRH_CNF13;

GPIOA->CRL |= GPIO_CRL_MODE3_0; //set A3 as a general purporse push-pull output

GPIOB->CRL |= GPIO_CRL_MODE5_0; //set B5 as a general purporse push-pull output

GPIOC->CRH |= GPIO_CRH_MODE13_0; //set C13 as a general purporse push-pull output

GPIOC->BSRR |= GPIO_BSRR_BR13; // set C13 0

GPIOA->BSRR |= GPIO_BSRR_BS3; // set A3 1

GPIOB->BSRR |= GPIO_BSRR_BS5; // set B5 1

soft_delay(5000000);

GPIOC->BSRR |= GPIO_BSRR_BS13;

GPIOA->BSRR |= GPIO_BSRR_BR3;

GPIOB->BSRR |= GPIO_BSRR_BR5;

soft_delay(5000000);

}

void led2(void){

if(statusLed2)GPIOB->BSRR |= GPIO_BSRR_BR5;

else GPIOB->BSRR |= GPIO_BSRR_BS5;

statusLed2 = !statusLed2;

}

void led3(void){

if(statusLed3)GPIOA->BSRR |= GPIO_BSRR_BR3;

else GPIOA->BSRR |= GPIO_BSRR_BS3;

statusLed3 = !statusLed3;

}

void led1(void){

if(statusLed1)GPIOC->BSRR |= GPIO_BSRR_BS13;

else GPIOC->BSRR |= GPIO_BSRR_BR13;

statusLed1 = !statusLed1;

}

void soft_delay(unsigned long time){while(time)time--;}