cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO strange issue

samimi.electronics
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions
samimi.electronics
Associate II

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.

View solution in original post

2 REPLIES 2

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
Associate II

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.