WDT (Watchdog Timer) sample program4 for PSoC4

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Kenshow
Level 8
Level 8
Distributor - Marubun (Japan)
50 solutions authored 25 solutions authored 10 solutions authored

Goal: It starts after a certain period of time and blinks the LED. Reset with a switch.

Hi,

I have to take medicine every morning because I had been over weight for years. However, as I get used to taking medicine every day, I sometime forget to take it. In order to prevent this, I am thinking of programming to make it known by lighting the LED at a certain fixed time. I studied the Deep Sleep function to make it battery-powered and have a long life. I will introduce the program created at that time. This program will be the minimum template that can realize what I want to do. Maybe....

 

The environment used is as follows:

・PSoC Creator 4.4

・CY8CKIT-042-BLE-A

 

Program behavior:

 After Power On, it does not start up immediately, but after a certain period of time, it starts up and turns on the LED. The LED counts the blinking of the blue LED and changes to red after blinking 4 times. Reset with SW and repeat the same operation.

 In my case, I usually take the medicine once a day. If you can adjust the WDT to reset after drinking, the blue LED flashes after 23 hours, the red LED flashes after another hour, and a warning is issued as a two-step alarm, the desired program is completed. This sample program If you set this, it will be a wait-and-see for one day, so I have not set it and confirmed the feasibility in a short time.

1.png

 

 After Power On or reset release, enter Deep Sleep after initial setting. The first WDT setting is to cascade WDT0-1-2 and wake up the CPU with a WDT2 interrupt (path ①). The WDT2 interrupt changes to a cascade of WDT0-1 only (path ②). WDT1 interrupt creates LED blinking timing. It is set so that Software can be reset by GPIO interrupt of SW.

 

The circuit is simply the setting of the SW/GPIO and LED part as shown below.

 2.png

 

Pin map for CY8CKIT-042-BLE-A is as below.

 3.png

 

 The clock source of WDT is set on the program.

#include "project.h"

 

#define LED_ON                  (0u)

#define LED_OFF                 (1u)

#define LED_ON_TIME             (100u)

 

#include "project.h"

 

volatile int Cnt = 0;

void wdt1Callback()

{

    Cnt++;

    CySysWdtClearInterrupt(CY_SYS_WDT_COUNTER1);

}

 

/* Enable modification of the WDT Timers for LED twinkle */

void wdt2Callback()

{

    // Turn off the WDTs (all three of them)

    CySysWdtDisable(CY_SYS_WDT_COUNTER0_MASK);

    CySysWdtDisable(CY_SYS_WDT_COUNTER1_MASK);

    CySysWdtDisable(CY_SYS_WDT_COUNTER2_MASK);

   

    // Make Timer 0 & 1 run with no interrupt, and 2 cause an interrupt

    CySysWdtSetMode(CY_SYS_WDT_COUNTER0,CY_SYS_WDT_MODE_NONE);

    CySysWdtSetMode(CY_SYS_WDT_COUNTER1,CY_SYS_WDT_MODE_INT);

    CySysWdtSetMode(CY_SYS_WDT_COUNTER2,CY_SYS_WDT_MODE_NONE);

 

    CySysWdtSetCascade(CY_SYS_WDT_CASCADE_01 ); // Cascade 0-1, not to use WDT2

    CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK | CY_SYS_WDT_COUNTER1_MASK);

   

    CySysWdtClearInterrupt(CY_SYS_WDT_COUNTER2);

}

 

CY_ISR(GPIOIsrHandler)

{

    /*Software Reset */

//   CySoftwareReset(void);

    CY_SYS_AIRCR_REG = (CY_SYS_AIRCR_REG & (uint32)(~CY_SYS_AIRCR_VECTKEY_MASK)) |

                            CY_SYS_AIRCR_VECTKEY | CY_SYS_AIRCR_SYSRESETREQ;

}

 

int main(void)

{

    CySysWdtSetInterruptCallback(CY_SYS_WDT_COUNTER1,wdt1Callback);

    CySysWdtSetInterruptCallback(CY_SYS_WDT_COUNTER2,wdt2Callback);

 

    //    SW_int_StartEx(SW_isr) ;

    isr_GPIO_StartEx(GPIOIsrHandler);

 

    CySysWdtUnlock(); // Enable modification of the WDT Timers

   

    // Turn off the WDTs (all three of them)

    CySysWdtDisable(CY_SYS_WDT_COUNTER0_MASK);

    CySysWdtDisable(CY_SYS_WDT_COUNTER1_MASK);

    CySysWdtDisable(CY_SYS_WDT_COUNTER2_MASK);

    

    // Make Timer 0 & 1 run with no interrupt, and 2 cause an interrupt

    CySysWdtSetMode(CY_SYS_WDT_COUNTER0,CY_SYS_WDT_MODE_NONE);

    CySysWdtSetMode(CY_SYS_WDT_COUNTER1,CY_SYS_WDT_MODE_NONE);

    CySysWdtSetMode(CY_SYS_WDT_COUNTER2,CY_SYS_WDT_MODE_INT);

   

    // Set the time to 32KHz/(128*64*2^4) = 0.244 Hz = 4.1S

    CySysWdtSetMatch(CY_SYS_WDT_COUNTER0, 128);

    CySysWdtSetMatch(CY_SYS_WDT_COUNTER1, 64 );

    CySysWdtSetToggleBit(4);

   

    CySysWdtSetClearOnMatch(CY_SYS_WDT_COUNTER0,1); // When counter this period reset counter

    CySysWdtSetClearOnMatch(CY_SYS_WDT_COUNTER1,1);

 

    CySysWdtSetCascade(CY_SYS_WDT_CASCADE_01 | CY_SYS_WDT_CASCADE_12); // Cascade 0-1-2

    CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK | CY_SYS_WDT_COUNTER1_MASK | CY_SYS_WDT_COUNTER2_MASK );

 

    LED_B_Write(LED_OFF); //LED OFF

    LED_R_Write(LED_OFF); //LED OFF

 

    CyGlobalIntEnable; /* Enable global interrupts. */

   

    for(;;)

    {

        CySysPmDeepSleep();

       

        if(Cnt<4){

            LED_B_Write(LED_ON); //LED ON   

            CyDelay(LED_ON_TIME);       

            LED_B_Write(LED_OFF); //LED OFF

        }else{

            LED_R_Write(LED_ON); //LED ON   

            CyDelay(LED_ON_TIME);       

            LED_R_Write(LED_OFF); //LED OFF

        }

    }

}

 

Thanks,
Kenshow

1 Reply
lock attach
Attachments are accessible only for community members.
Kenshow
Level 8
Level 8
Distributor - Marubun (Japan)
50 solutions authored 25 solutions authored 10 solutions authored

Sorry, I forgot a project file.

Thanks,
Kenshow

0 Likes