This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF24LE1 how to set PWM frequency?

To what values should I set the PWMCON register to so that the PWM0 and PWM1 frequency is output at 20Khz with duty cycle varying between 1 to 2ms. Now specifically I am asking this after reading the data sheet because the tables mentioned are not clearing giving information. How do I decipher the table given here:

Please clearly disclose information in the data sheets. Also give an example to show how to set to particular frequency and pulse width.

Let me tell you how I decipher this as a newbie. Now CCLK is 16,000,000 for 16Mhz clock. I chose PWM0 as output so bit 0 is 1 and bit 1 is 0. Now I need to find out what PWMCON is so the formula for PWMCON for 20Khz PWM freq will be. (16000000/(255 x 20000))-1 = 160/51 -1 which is approximately 2. Hence PWMCON[5:2] will be 0010. There fore PWMCON will be 11001001 which is 0xC9. But this dint work when I put in this value for PWMCON as the servo did not rotate. Here is my code.

#include "reg24le1.h" // I/O header file for NRF24LE1
#include "hal_delay.h" // header file containing delay functions
 
// main function
void main()
{
int i = 0, j = 0; // loop variable
P0DIR = 0; // Port 0 as output
PWMCON = 0xc9; // enable PWM1
 
// infinte loop
while(1)
{
for(i= 180; i--; i > 0)
{
PWMDC0 = i ; // change duty cycle
delay_ms(10); // delay of 10 ms
}
 
 
}
 
}

Parents Reply
  • I wrote this code but it does not work,

    void t0_interrupt(void) interrupt 1
    {       
      // Timer increments every 2*12 CPU clock cycles
      // Timer starts counting down from 65535
      // 666 * 1,5 us = 999 us
      static bit timer_state;
      uint16_t timer_val;
    
    	if(timer_state == 2)
      {
        // Need to adjust for the prior timer event
        timer_val = 0xFFFF - 13333 + 4*666;
    		P03 = 0;
    		P06 = 0;
        timer_state = 0;
      }
    	
    	if(timer_state == 1)
    	{
    		timer_val = 0xFFFF-666;
    		P03 = 0;
    		P06 = 1;
    		timer_state = 2;
    	}
      if (timer_state == 0)
      {
        timer_val = 0xFFFF - 666;
        P03 = 1;
    		P06 = 0;
    		timer_state = 1;
      }
      // Wait a given time, in this case 19 ms, before pulsing the pin again.
      
      TL0 = (uint8_t) (timer_val & 0xFF);
      TH0 = (uint8_t) ((timer_val >> 8) & 0xFF);
    }

    I pulse one pin high for 1ms then I turn it low and pulse  second pin high and then update timer values to 20- 1-1-1-1. Since I am pulsing two pins and 2 ms for each pin.

Children
Related