Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/30/07 00:29
Read: times


 
#146365 - Need some help with timer 3 init
Hi! (My first time on this site).

This is my first experience with 8051, and I am trying to start by setting up and playing with some timers to blink some LEDs. I've got Timer 2 to blink correctly at 10Hz (basically the sample code from the SiLabs C8051F41x devkit). In the same ISR, I also blinked a second LED.

Now that I've added another timer (Timer 3) to blink the second LED, the second LED doesn't blink anymore. Plus, the 1st LED (controlled by Timer 2) is blinking at a slower rate - about 0.5Hz. The problem disappears as soon as I comment out init_timer3(). I've pored over the docs and still can't find what I'm doing wrong in setting up Timer 3.

So what am I missing? Thanks for your help!


#define TIMER2_LOW_USE_SYSCLK	0x10
#define TIMER2_HI_USE_SYSCLK	0x20

#define TIMER3_LOW_USE_SYSCLK	0x40
#define TIMER3_HI_USE_SYSCLK 	0x80

sbit LED1 = P2 ^ SBIT_P2_1;	/* LED of port 2.1 */
sbit LED2 = P2 ^ SBIT_P2_3;	/* LED of port 2.3 */

void init_timer2(const INT count_val)
{
    /* reset timer 2; clear TF2; use SYSCLK / 12 */
    TMR2CN = 0x00;					

    /* use SYSCLK for timer 2 */
    CKCON  = TIMER2_LOW_USE_SYSCLK | TIMER2_HI_USE_SYSCLK;	
	
    /* initialize reload value */
    TMR2RLL = count_val;
    TMR2L   = 0xFF;  /* set to overflow to reload right away */					
    ET2 = 1;  /* enable timer 2 interrupt */
    TR2	= 1;  /* enable timer 2 */
}

void init_timer3(const INT count_val)
{
    /* reset timer 3; clear TF3; use SYSCLK / 12 */
    TMR3CN &= ~(0x01 | 0x04 | 0x40 | 0x80);
    
    /* use SYSCLK for timer 3 */
    CKCON  = TIMER3_LOW_USE_SYSCLK | TIMER3_HI_USE_SYSCLK;	
	
    /* initialize reload value */
    TMR3RLL = count_val;
    TMR3L   = 0xFF;  /* set to overflow to reload right away */		
						
    EIE1   |= 0x80;
    TMR3CN |= 0x04;
}

void _isr_timer2(void) interrupt INTERRUPT_TIMER2
{
    TF2H = 0x00;  /* clear the interrupt flag */

    /* toggle the LED */
    LED1 = ~LED1;
}

void _isr_timer3(void) interrupt INTERRUPT_TIMER3
{
    /* clear the interrupt flag */
    TMR3CN &= ~0x80;

    /* toggle the LED */
    LED2 = ~LED2;
}


List of 5 messages in thread
TopicAuthorDate
Need some help with timer 3 init            01/01/70 00:00      
   Check CKCON initialization            01/01/70 00:00      
      it has to do with interrupt priorities            01/01/70 00:00      
      Yes, check what you're doing with CKCON.            01/01/70 00:00      
      Thanks!            01/01/70 00:00      

Back to Subject List