This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TIDC-CC2650STK-SENSORTAG: PWM instability

Part Number: TIDC-CC2650STK-SENSORTAG

Hi everyone,

I need help with PWM configuration. I need to build a system of two CC2650STK-SENSORTAG's. First device should emit a stable frequency of 1 kHz.
Program source code is presented below:

PWM_Handle pwmH;

int main()
{
   Board_initGeneral();

   PWM_Params pwmParams;
   PWM_Params_init(&pwmParams);
   pwmParams.dutyUnits = PWM_DUTY_US;
   pwmParams.dutyValue = 450;
   pwmParams.idleLevel = PWM_IDLE_LOW;
   pwmParams.periodUnits = PWM_PERIOD_COUNTS;
   pwmParams.periodValue = 48000;
   PWM_Init();

   pwmH = PWM_open(Board_PWM0, &pwmParams);

   PWM_start(pwmH);
   
   while (1);
}


The second device should receive a signal from the first device and measure the frequency. To determine the frequency, I start the clock for one second and count how many pulses will come. 

The source code of the program of the second device is presented below:

#include <freq_meter_task.h>

int  pulse_count;
int  freq;

void int_cb(PIN_Handle handle, PIN_Id pinId)
{
    Event_post(EventHandle, IMPULSE_EVENT);
}

void clock_cb(UArg arg)
{
    Event_post(EventHandle, CLOCK_EVENT);
}

void freq_meter(UArg arg0, UArg arg1)
{


    static PIN_State PinState;
    PIN_Handle PinHandle;
    PIN_Config PinTable[] = {
        Board_RX | PIN_INPUT_EN | PIN_PULLDOWN, // Pin initialization
        PIN_TERMINATE
    };

    EventHandle = Event_create(NULL, NULL);

    PinHandle = PIN_open(&PinState, PinTable);
        if(!PinHandle) {
            System_abort("Error initializing board pins\n");
        }


    PIN_registerIntCb(PinHandle, (PIN_IntCb)&int_cb);

    Clock_Handle clockH;
    Clock_Params clockParams;
    Clock_Params_init(&clockParams);
    clockParams.startFlag = 0;
    clockH = Clock_create((Clock_FuncPtr) &clock_cb, 100000, &clockParams, NULL);
    Clock_start(clockH);
    PIN_setInterrupt(PinHandle, PinTable[0] | PIN_IRQ_NEGEDGE);

    while(true)
    {
        uint32_t events = Event_pend(EventHandle, Event_Id_NONE, 0xFFFFFFFF, BIOS_WAIT_FOREVER);

        switch (events)
        {
            case (CLOCK_EVENT) :

                PIN_setInterrupt(PinHandle, Board_RX | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_DIS);
                Clock_stop(clockH);
                freq = pulse_count;
                pulse_count = 0;
                Clock_start(clockH);
                PIN_setInterrupt(PinHandle, Board_RX | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_NEGEDGE);

            break;

            case (IMPULSE_EVENT) :

                pulse_count++;

            break;

         }
     }
}

Thus, the first device generates a signal with a frequency of 1 kHz, and the second device has 1010 ...1030 pulses per second, but 1000 pulses should be counted. What could be the problem? Is this my fault or the instability of the PWM?

Thanks in advance