Author Topic: Timer 3 on ch559  (Read 736 times)

0 Members and 1 Guest are viewing this topic.

Offline fabiodlTopic starter

  • Frequent Contributor
  • **
  • Posts: 282
Timer 3 on ch559
« on: April 10, 2021, 10:40:13 am »
I am trying to have timeout interrrupts (overflow) for timer 3 of a CH559.
As by the datasheet I set bT3_IE_END. I tried both setting/resetting  bT3_IE_ACT (according to the datasheet it should be reset), but the interrupt is never called for overflows. In the following code, cnt is always equal to ok.
To test I connect / disconnect  the capture pin (P4.2) to a pin with an alternating signal (P1.0).
If I set bT3_IE_ACT i can get cnt and ok increment together, but if I disconnect I get no increments of cnt (and not of ovf either, of course).
What am I doing wrong?
The maker's examples http://www.wch.cn/downloads/CH559EVT_ZIP.html for the timer do not provide any help for the overflow.

My code is the following,
Code: [Select]
#include <ch55x/delay.h>
#include <ch55x/CH559.h>
#include <ch55x/uart0.h>

#include <stdint.h>
#include <stdio.h>


void tmr3_setClk(uint16_t DIV)
{
    T3_SETUP |= bT3_EN_CK_SE;
    T3_CK_SE_L = DIV & 0xff;
    T3_CK_SE_H = (DIV >> 8) & 0xff;
    T3_SETUP &= ~bT3_EN_CK_SE; 
}


volatile uint8_t cnt,ovf,ok;

void mTimer3Interrupt( void ) __interrupt INT_NO_TMR3
{
 cnt++;
  if(T3_STAT & bT3_IF_ACT)                                               
    {
      ok++;
      T3_STAT |= bT3_IF_ACT;     
    }


    if(T3_STAT & bT3_IF_END)                                               
    {
      ovf++;
      T3_STAT |= bT3_IF_END;     
    }     
}

void tmr3_setPin(bool alt){
  if (alt){
    PIN_FUNC |= bTMR3_PIN_X; //When this bit is 0, PWM3/CAP3 uses P1.2. When this bit is 1, PWM3/CAP3 uses P4.2
  }else{
    PIN_FUNC &=~bTMR3_PIN_X; //When this bit is 0, PWM3/CAP3 uses P1.2. When this bit is 1, PWM3/CAP3 uses P4.2
  }

}


void tmr3_start(){
  T3_CTRL |= bT3_CNT_EN;
}

void tmr3_stop(){
  T3_CTRL &= ~bT3_CNT_EN;
}





void tmr3_captureInit(uint16_t timeout,uint8_t mode,bool edgeInterrupt,bool timeoutInterrupt){
  T3_CTRL = 0;
  T3_CTRL |= bT3_CLR_ALL;                                                   //Clear T3 related registers
  T3_CTRL &= ~bT3_CLR_ALL;
 

  if (edgeInterrupt)
    T3_SETUP |=  bT3_IE_ACT;
  else
    T3_SETUP &=  ~bT3_IE_ACT;
 
  if (timeoutInterrupt)
    T3_SETUP |= bT3_IE_END;
  else
    T3_SETUP &=  ~bT3_IE_END;
 
 
  T3_CTRL |= bT3_MOD_CAP ;//| bT3_CAP_WIDTH;
  T3_CTRL |= mode << 6;
  T3_END_L = timeout & 0xff;     
  T3_END_H = (timeout >> 8) & 0xff;
  T3_FIFO_L = 0;
  T3_FIFO_H = 0;
  T3_STAT = 0xF0;
}



int main(){
  delay_ms(5);   
  uart0_init(57600,false);
  printf("begin");
  tmr3_setPin(true);
  tmr3_setClk(12000);

 
  tmr3_captureInit(5000,1,false,true);
  EA = 1;
  IE_TMR3 = 1;
 
  P1_DIR|=0x01;


  while(true){ 
    P1^=0x01;
    delay_ms(1000);
    printf("cnt %d ok %d ovf %d\n",cnt,ok,ovf);   
  }
 
}

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf