cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to vary duty cycle and frequency of pwm

Ajadh.1
Associate II

Hi,

I am using STM8S105c6 , I have used timer1 software interrupt, to generate PWM. but I am not able yo control it, not duty cycle not frequency plz check code give and correct me.

Thanks in Advance

/*

 * BMSBattery S series motor controllers firmware

 *

 * Copyright (C) Casainho, 2017.

 *

 * Released under the GPL License, Version 3

 */

#include <stdint.h>

#include <stdio.h>

#include "stm8s.h"

#include "gpio.h"

#include "stm8s_itc.h"

#include "stm8s_gpio.h"

#include "interrupts.h"

#include "stm8s_tim1.h"

#include "main.h"

#include "interrupts.h"

unsigned int ms_count=0;//Millisecond count

unsigned int flag = 0;

void InitLED();

void InitTIM1();

//void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void) __interrupt(TIM1_UPD_OVF_TRG_BRK_IRQHANDLER);

int main(void)

{

 CLK_DeInit();

 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

 //CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

 disableInterrupts();     //First close the total interrupt

 InitLED();     //Various initializations

 InitTIM1();

 enableInterrupts();   //Open total interrupt

 while(1);      //Enter the dead cycle and wait for the timer cycle to interrupt

}

/*********************************************************

Timer 1 updates interrupt processing function

 Interrupt vectors can also be replaced by decimal system, which is used here for convenience.

Because the macro definition of the header file is in hexadecimal.

Function names can be written freely, just be happy.

*****************************************************/

void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)

{

 TIM1->SR1 &= ~(1<<0);//Clear interruption mark

 /*ms_count++;

 if(ms_count< 1000)//1ms*1000=1s

 {

  ms_count=0;

  GPIO_WriteReverse(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_4);

  //PE_ODR ^= 1<<5;//LED lamp flipped once in 1s

 }*/

/*if(flag == 0){

  GPIO_WriteHigh(GPIOB, GPIO_PIN_4);

  flag = 1;

}

else

 {

  GPIO_WriteLow(GPIOB, GPIO_PIN_4);

  flag = 0;

 }*/

 GPIO_WriteReverse(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_4);

  //PE_ODR ^= 1<<5;//LED lamp flipped once in 1s

}

void InitTIM1()

{

 TIM1->PSCRH=0;//Be sure to write high eight first

 TIM1->PSCRL=0;//1 frequency division, timer clock equals system clock = 16MHz

 TIM2->ARRH = 0x00;    // High byte of 50,000.

 TIM2->ARRL = 0x00;    // Low byte of 50,000 //20Hz signal

 TIM1->CNTRH= 0xFF;

 TIM1->CNTRL= 0xFF;//It is necessary to clear down the counter

 TIM1->IER |= 1<<0;//Enable tim1 update interrupt

 TIM1->SR1 |= 1<<0;//Clear tim1 update interrupt flag

 TIM1->CR1 |= 1<<7;//Allow reassembly to enable timer

 TIM1->CR1 |= (1<<5);

  TIM1->CR1 |= (1<<6);

  TIM1->CR1 |= (1<<4);//Select Downward Counting Mode

 TIM1->CR1 |= 1<<0;//Enabling counter

}

void InitLED()

{

 GPIO_DeInit(GPIOB);

 GPIO_Init(GPIOB, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);

}

0 REPLIES 0