Skip to main content
samimi.electronics
Associate
July 17, 2019
Solved

GPIO strange issue

  • July 17, 2019
  • 2 replies
  • 858 views

I have this code that works and i can turn an LED on:

#include "stm32f10x.h"
 
void RCC_Init(void){
 
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
}
 
void GPIO_Init(void){
GPIOC->CRH |= GPIO_CRH_CNF13_0 | GPIO_CRH_MODE13_0 | GPIO_CRH_MODE13_1;
}
 
int main(void){
RCC_Init();
GPIO_Init();
GPIOC->BSRR = GPIO_BSRR_BR13;
while(1){
 
}
return 0;
}

but when i change my port it doesn't work:

#include "stm32f10x.h"
 
void RCC_Init(void){
	
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
}
 
void GPIO_Init(void){
	GPIOA->CRL |= GPIO_CRL_CNF7_0 | GPIO_CRL_MODE7_0 | GPIO_CRL_MODE7_1;
}
 
int main(void){
	RCC_Init();
	GPIO_Init();
	GPIOA->BSRR = GPIO_BSRR_BS7;
	while(1){
		
	}
	return 0;
}

Based on the datasheet and the clock table shown, it should work. Can anyone point out the problem to me?

This topic has been closed for replies.
Best answer by samimi.electronics

Okay I found out what the problem was. The default value of the register is not 0x00000000 by default; It is 0x44444444. So, in other words, for me to implement this table:

What I was missing is shifting two zeros to CNF[1:0]. To do this I must write the following code:

void GPIO_Init(void){
	GPIOA->CRL |= GPIO_CRL_MODE7; // Output 50MHz
	GPIOA->CRL &= ~(GPIO_CRL_CNF7); // Set CNF[1:0] to 00
}

Instead of this:

void GPIO_Init(void){
	GPIOA->CRL |= GPIO_CRL_MODE7; // Output 50MHz
}

Thanks for your answer anyways mate.

2 replies

waclawek.jan
Super User
July 17, 2019

If I understand it correctly, you set the pin to General Purpose Output, Open Drain. Is this what you intended? Why do you then *set* the pin's output register? How are the LEDs connected, exactly?

I am not very familiar with the 'F1xx family.

JW

samimi.electronics
samimi.electronicsAuthorBest answer
Associate
July 17, 2019

Okay I found out what the problem was. The default value of the register is not 0x00000000 by default; It is 0x44444444. So, in other words, for me to implement this table:

What I was missing is shifting two zeros to CNF[1:0]. To do this I must write the following code:

void GPIO_Init(void){
	GPIOA->CRL |= GPIO_CRL_MODE7; // Output 50MHz
	GPIOA->CRL &= ~(GPIO_CRL_CNF7); // Set CNF[1:0] to 00
}

Instead of this:

void GPIO_Init(void){
	GPIOA->CRL |= GPIO_CRL_MODE7; // Output 50MHz
}

Thanks for your answer anyways mate.