cancel
Showing results for 
Search instead for 
Did you mean: 

I am working on PWM signal and the following code is not responding properly. Can someone explain where I am making a mistake. I need to generate pwm signal on PB0 (timer ch1)

VSrin
Associate II

/* MAIN.C file

 * 

 * Copyright (c) 2002-2005 STMicroelectronics

 */

#include <iostm8l.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <stdint.h>

#include "defs.h"

unsigned int count = 0;

unsigned int pwm_duty = 0x0000;

void TIM2_SetCompare1(uint16_t Compare)

{

 TIM2_CCR1H = (uint8_t)(Compare >> 8);

 TIM2_CCR1L = (uint8_t)(Compare);

}

void delay(unsigned long dd)

{

  unsigned long i;

  for (i=0;i<dd;i++) nop(); 

}

@svlreg @interrupt void TIM2(void)

{

PC_ODR ^= 0x80;

TIM2_SR1 &= ~2;

}

main()

{

CLK_DIVR = 0x00; // Set the frequency to 16Mhz

CLK_PCKENR1 = 0x01; // clock for timer2

PC_DDR = 0x80; // direction output for led

PC_CR1 = 0x80; // fast push pull mode for led

PB_DDR = 0x01;

PB_CR1 = 0x01;

PB_CR2 = 0x00;

PB_ODR = 0x01;

TIM2_PSCR = 0x07;// 125000

TIM2_ARRH = 0x03;// 50000

TIM2_ARRL = 0xe8;

 TIM2_CCMR1 = 0x60;

 TIM2_CCER1 = 0x01; 

TIM2_CR1 = 0x01;

while(1)

{

for(pwm_duty = 0; pwm_duty <500 ; pwm_duty += 25)

{

 TIM2_SetCompare1(pwm_duty);

delay(pwm_duty);

}

for(pwm_duty =500; pwm_duty > 0; pwm_duty -= 25)

{

TIM2_SetCompare1(pwm_duty);

  delay(pwm_duty);

}

}

}

0 REPLIES 0