AnsweredAssumed Answered

TIM3 Problem

Question asked by ctechguy on Nov 4, 2011
Latest reply on Nov 5, 2011 by Clive One

I’m having a lot of problems with timer 3. What I’m trying to simply do now is remap TIM3 to the leds and have the blue led stay one for 1sec and the green to stay on for 1.5sec. Now when I tried to do this the leds didn’t turn off. I saw if I did the operation twice it appeared to worked the second time. So now I turn the timer on wait 2sec and turn it off and it seems to work except when it overflows it turns the leds on then stops. Is there a warmup required for the timers, this doesn’t seem like the correct solution. In other tasks I’ve been working on I keep having issues with TIM3, especially not working the first time. I have attached my current code with delay and I would like to remove the delay if possible. I think the second part of the overflow and staying on is doing what I said. Is there a way to stop the counter before turning the leds back on?

Thanks so much Chris.

#include "stm32f10x.h"
#define eint() asm volatile("cpsie i")
#define dint() asm volatile("cpsid i")
#define PCDDR_HIGH      GPIOC->CRH


int tick=0;
int main(void) {
     dint();
     initializeHW();
     initializeTimersAndInterrupts();


     SysTick->CTRL = 3;
     SysTick->LOAD = 1000;


     eint();
     int curr=tick;
     while(tick<curr+2000){}//2sec delay, warmup????
     TIM3->CR1=0;//Turn off timer

     TIM3->CCR3=1000;//Blue led 1sec
     TIM3->CCR4=1500;//Green led 1.5sec


     TIM3->CCMR2=0x6060;//Set compare 3 and 4 to pwm
     TIM3->CNT=0;//Set counter to 0
     TIM3->CR1=9;//Run once!


     while(1){}
    return 0;
}


void initializeHW(){
     RCC->APB2ENR = 0x74A3D;     // enable the GPIO devices and timers
     RCC -> APB1ENR = 0x1ff;
     AFIO -> MAPR = 0x02008000;
     AFIO->MAPR |= 0x00000C00;// remap tim3

     PCDDR_HIGH=0x11111199;//Set Leds to timer
}

void initializeTimersAndInterrupts(void){
     TIM3->PSC=7999;
     TIM3->CCER=0x1100; // enable compare 3 and 4
     TIM3->CCMR2=0x4040;//Force off
     TIM3->CR1=1;//Start timer
}

void systick_isr(void) {
     tick++;
}




#define STACK_TOP 0x20002000
extern void _start(void);
unsigned int *myvectors[] __attribute__((section(".vectors"))) = {
    //(unsigned int*) STACK_TOP,
    //(unsigned int*) _start
     (void(*)(void)) STACK_TOP,     // stack pointer
     _start,        // code entry point
     0,   // handle non-maskable interrupts
     0, // handle hard faults
     0,0,0,0,                /* 10...1f */
     0,0,0,0,                /* 20...2f */
     0,0,0,systick_isr, /* 30...3f */
};

Outcomes