This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Converting 10kHz PWM into 2kHz PWM codes

Hi

Here is my pseduo-code:

// In my CAPCOM1 initalisation

CC1_T0REL=0xF830; // Generate 10kHz PWM

// Produce 50% duty cycle
Offset=(0xFFFF-CC1_T0REL)>>1;

// This will produce 50% duty cycle of 10kHz  PWM

CC1_CC2=CC1_T0REL+Offset;


Note: I use 100uS for Timer 0 Interrupt

Now I need to convert 10kHz PWM into 2kHz PWM but I am totally lost at the moment. However I tried to divide it by 5 but it doesn't work at all. I would be very very grateful to hear your suggestion or tip.

Kind regards

AJ

Parents
  • Yes, you can just use T3 to toggle pin P3.3 (T3OUT). Here is an example that uses the PEC to reload T3 (but only a 50% DC). If you want to use software then you can change the duty cycle for T3 and also use T2 and T4. However you would need to modify a port pin within the interrupt (meaning you would have to deal with the interrupt latency).

    unsigned int T3reload;
    
    void main(void) {
      /* timer 3 works in timer mode
         prescaler factor is 8
         timer counts down
         alternate output function T3OUT (P3.3) is enabled
         timer 3 output toggle latch (T3OTL) is set to 1
      */
      GPT12E_T3CON = 0x0680;
      T3reload     = 0x04E1;   /* 2KHz, 50% DC */
      GPT12E_T3    = T3reload;
    
      /* P3.3 is used for Timer 3 Toggle Output (T3OUT) */
      _bfld_(ALTSEL0P3,0x0008,0x0008); /* select alternate output */
      _bfld_(P3,0x0008,0x0008);        /* set data register */
      _bfld_(DP3,0x0008,0x0008);       /* set direction register */
    
      /* PEC0:(ILVL) = 14, (GLVL) = 0, (GPX) = 0 */
      GPT12E_T3IC = 0x0078;
      PECC0 = 0x00FF;             /* continuous transfer */
      SRCP0 = _sof_ (&T3reload);  /* source pointer */
      DSTP0 = _sof_(&GPT12E_T3);  /* destination pointer */
    
      GPT12E_T3CON_T3R = 1;       /* set timer 3 run bit */
    
      PSW_IEN = 1;
    
      for(;;) {
      }
    }

Reply
  • Yes, you can just use T3 to toggle pin P3.3 (T3OUT). Here is an example that uses the PEC to reload T3 (but only a 50% DC). If you want to use software then you can change the duty cycle for T3 and also use T2 and T4. However you would need to modify a port pin within the interrupt (meaning you would have to deal with the interrupt latency).

    unsigned int T3reload;
    
    void main(void) {
      /* timer 3 works in timer mode
         prescaler factor is 8
         timer counts down
         alternate output function T3OUT (P3.3) is enabled
         timer 3 output toggle latch (T3OTL) is set to 1
      */
      GPT12E_T3CON = 0x0680;
      T3reload     = 0x04E1;   /* 2KHz, 50% DC */
      GPT12E_T3    = T3reload;
    
      /* P3.3 is used for Timer 3 Toggle Output (T3OUT) */
      _bfld_(ALTSEL0P3,0x0008,0x0008); /* select alternate output */
      _bfld_(P3,0x0008,0x0008);        /* set data register */
      _bfld_(DP3,0x0008,0x0008);       /* set direction register */
    
      /* PEC0:(ILVL) = 14, (GLVL) = 0, (GPX) = 0 */
      GPT12E_T3IC = 0x0078;
      PECC0 = 0x00FF;             /* continuous transfer */
      SRCP0 = _sof_ (&T3reload);  /* source pointer */
      DSTP0 = _sof_(&GPT12E_T3);  /* destination pointer */
    
      GPT12E_T3CON_T3R = 1;       /* set timer 3 run bit */
    
      PSW_IEN = 1;
    
      for(;;) {
      }
    }

Children