cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER 1 channels in OC mode.

DebD21
Associate

0690X00000AQLE6QAP.jpg0690X00000AQLE1QAP.jpgHello! I'm learning to code an STMicroelectronics 8 bit micro-controller. I want to control a BLDC motor with this MCU for electric vehicle application. I'm using the STM8S DISCOVERY Board, based on the STM8S105C6T6 (48 pin) MCU. This MCU has 4 timers, with TIMER 1 being the advanced timer (complementary PWM channels, dead time capability etc. in output compare mode). However I'm facing difficulty in generating PWM signals from the TIMER 1, which has 4 channels. Channel 2 and Channel 2N produce complementary PWM outputs as expected. However every time I connect Channel 1 and Channel 1N pins respectively to my oscilloscope (or Channel 3 and Channel 3N pins), Channel 1 (or 3) produce nearly no output while their counterparts produce perfect PWM pulse train. Channel 4 also produces a correct output PWM, but it doesn't have a complementary pin. Given below are the codes which I've used (the one released by STMicroelectronics itself as well as the one I've written on my own) and the oscilloscope waveforms I'm getting with Channels 1 and 2 for both cases

/* My own code */
 
#include "STM8S.h"
 
 
void clock_setup(void);
void GPIO_setup(void);
void TIM1_setup(void);
 
 
void main(void)
{
	// signed int i = 0;
	
	clock_setup();
	GPIO_setup();
	TIM1_setup();
	
	/* Duty Cycle varied continuously to alter the blinking rate of LEDs
	while(TRUE)
	{
		for(i = 0; i < 1000; i += 1)
		{
			TIM1_SetCompare1(i);
			delay_ms(1);
		}
		for(i = 1000; i > 0; i -= 1)
		{
			TIM1_SetCompare1(i);
			delay_ms(1);
		}
	}; */
}
 
 
void clock_setup(void)
{
	CLK_DeInit();
	
	CLK_HSECmd(DISABLE);
	CLK_LSICmd(DISABLE);
	CLK_HSICmd(ENABLE);
	while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
	
	CLK_ClockSwitchCmd(ENABLE);
	CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
	CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
	
	CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, 
	DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
	
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);	
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
 
 
void GPIO_setup(void)
 
 /*TIM1_CH2 & CH2N configured in OC Mode. On executing the code, complementary PWM signals are seen on the scope. Other channels can be configured in the same way, however CH1 and CH3 pins produce no outputs */
{		
	GPIO_DeInit(GPIOB);
	GPIO_Init(GPIOB, GPIO_PIN_1, GPIO_MODE_OUT_PP_HIGH_FAST);  /* Port B Pin 1 or TIM1_CH2N gets configured in the OC mode */
	
	GPIO_DeInit(GPIOC);
	GPIO_Init(GPIOC, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST); /* Port C Pin 2 or TIM1_CH2 gets configured in the OC mode */
}
 
 
void TIM1_setup(void)
{
	TIM1_DeInit();
	
	TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_UP, 1000, 1);
	
	TIM1_OC2Init(TIM1_OCMODE_PWM1, 
							TIM1_OUTPUTSTATE_ENABLE, 
						        TIM1_OUTPUTNSTATE_ENABLE, 
							500, 
							TIM1_OCPOLARITY_LOW, 
							TIM1_OCNPOLARITY_LOW, 
							TIM1_OCIDLESTATE_RESET, 
							TIM1_OCNIDLESTATE_RESET);  /*TIM1_CH2 & CH2N setup. 
                                             PWM output of 1 kHz frequency and 50% duty cycle obtained */
	
	TIM1_CtrlPWMOutputs(ENABLE);
	TIM1_Cmd(ENABLE);
}

0 REPLIES 0