cancel
Showing results for 
Search instead for 
Did you mean: 

PWM no ouput on PB6 using ide PlatformIO

paolo42
Visitor

I am not able to discover the error It should work but i do not have  any pulse on PB6

CPU  STM332F103C8T6  ide PlatformIO  

 

#include <Arduino.h>

void setup() {

	RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; // Enable GPIOB clock
	RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // Enable TIM4 clock
	RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // Enable Alternate Function clock
	GPIOB->CRL |= (0b0011 << (4 * 6)); // Set PB6 as Alternate function output Push-pull (TIM4_CH1)



  	TIM4->CR1 = TIM_CR1_CEN |TIM_CR1_ARPE;  // Enable counter and auto-reload preload
	TIM4->CR2 = 0;					
	TIM4->SMCR = 0;  						// No slave mode																		
	TIM4->DIER = 0;							// No interrupts
	TIM4->EGR = 0;							// No event generation
	TIM4->CCMR1 = (0b110 << 4 ) | TIM_CCMR1_OC1PE; // PWM mode 1, preload enable
	TIM4->CCMR2 = 0;
	TIM4->CCER = TIM_CCER_CC1E ;  // Enable output on channel 1
	TIM4->PSC = 71;				// Set prescaler to have 1MHz timer clock (72MHz/72=1MHz)
	TIM4->ARR = 0x5000;			// Set auto-reload value, period of the PWM signal
	TIM4->DCR = 0;				// No DMA
	TIM4->CCR1 = 1000;			// Set compare value for channel 1,  duty cycle	

}



void loop() {
	delay(10);
}
[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = arduino

 

1 REPLY 1
TDK
Super User

> GPIOB->CRL |= (0b0011 << (4 * 6));

This sets bits 0 and 1 of MODE6. It doesn't do anything to CNF6 which is 0b01 by default.

0b0111 is CNF6=0b01 and MODE6=0b11 which is open-drain output.

TDK_0-1763147752218.png

 

You want AF push pull, so 0b1011. You will need to clear bit 2.

If you feel a post has answered your question, please click "Accept as Solution".