PSoC Timer Example

Created by Scott Schmit, last modified on Nov 13, 2013

image

Purpose

Timers are one of the most commonly used peripherals in embedded designs. The purpose of this page is to provide some simple Timer examples for PSoC devices. PSoC Creator, which is a free development tool from Cypress for PSoC 3, 4, 5, and 5LP devices, was used for firmware development for this project. The PSoC4 Pioneer Kit was used as the platform for this demo. However, any PSoC device can use the code for this project.

Project Goals

The following goals will be accomplished in this project. If the user simply wants to browse through the application code for this project, feel free to skip ahead.

  • Start a project from scratch in PSoC Creator
  • Add/Configure a Timer component to the design
  • Add an Interrupt component to the design
  • Add a Digital Output pin to the design
  • Select which pins are used to control LED
  • Provide/explain application code

Project Requirements

The following tools were used for this project:

  • PSoC Creator - Free firmware development software provided by Cypress. Version 3.0 was used for this project.
  • PSoC4 Pioneer Kit - Development platform for PSoC4 devices.

Procedure

Start a PSoC Creator Project
  1. Open PSoC Creator
  2. Navigate to File → New → Project
  3. Select “Empty PSoC 4 Design”
  4. Name the project “Timer_Example”, select a location to save the project, and click “Ok”
  5. This will bring you to the schematic view of the project where you can click and drag desired components into your design. If you do not see a blank schematic page, double click “TopDesign.cysch” from the Workspace Explorer window
Add/Configure a Timer Component
  1. In the Component Catalog window, in the Digital → Functions folder, click and drag the “Timer” component onto the page. By default, a 24MHz clock is attached to the component.
  2. Double click on the component to bring up the Configuration window. For this project, the following timer settings were used:
  • Resolution: 16-bit (counter value can be 0~65535).
  • Period: 24000 (with 24MHz clock, period = 1ms)
  • Trigger Mode: None
  • Capture Mode: None
  • Enable Mode: Software Only
  • Run Mode: Continuous
  • Interrupts: None
  1. Click “Apply” to apply any changes and click “Ok” to close the Configuration window.
Add Interrupt Component
  1. In the Component Catalog window, in the System folder, click and drag the “Interrupt” component onto the page.
  2. For this project, the default name (isr_1) was used.
  3. Hit “w” on the keyboard and connect the Interrupt component to the “tc” output of the Timer.
Add a Digital Output Pin
  1. In the Component Catalog window, in the Ports and Pins folder, click and drag the “Digital Output” component onto the page.
  2. Double click on the component to bring up the Configuration window.
  3. Rename the component “LED”. Ensure “Digital Output” is checked and uncheck “HW Connection”.
  4. Under the “General” tab, select “Strong Drive” as the Drive Mode and “High (1)” as the Initial State.
  5. Click “Apply” to apply any changes and click “Ok” to close the Configuration window.
Select LED Pin
  1. In the Workspace Explorer window, click on “Timer_Example.cydwr”. This is where you can define which pin of the PSoC device will be used for LED control.
  2. You should see a signal called “LED” with a drop-down arrow next to it. The drop-down arrow can be used to select a Port/Pin for the LED signal. Select P0[3] as the LED control pin. P0[3] is connected to the Blue LED on the Pioneer Board.
  3. Save and close the “Timer_Example.cydwr” tab.
Build the Project
  1. Build the project. This will auto-generate functions for each component which you can execute from application code.

Example Code

Timer with Interrupt Example

The following code block demonstrates how to implement a timer using interrupts.

#include <project.h>
 
uint16 ms_count = 0;
 
CY_ISR(MY_ISR) {
    ms_count++;
     
    if(ms_count == 1000) { // 1 second
        LED_Write(!LED_Read()); // Toggle LED
        ms_count = 0; // reset ms counter
    }
}
 
int main()
{
    Timer_1_Start(); // Configure and enable timer
    isr_1_StartEx(MY_ISR); // Point to MY_ISR to carry out the interrupt sub-routine
    CyGlobalIntEnable; // Enable global interrupts
     
    for(;;)
    {
        // Infinite loop for timer/interrupt to keep executing
    }
}
Timer Delay Example

The following code block demonstrates how a timer can be used to create delays. For this example code, a second LED was added (P1[6]) and the timer was changed to 32-bit. All other settings were the same as what’s stated in the above sections.

#include <project.h>
 
#define ON 0
#define OFF 1
#define TIMER_CLOCK 24000000 // This value should match the clock input to the Timer
 
uint8 timer_flag = 0;
void ms_delay (uint32 ms);
void us_delay (uint32 us);
 
CY_ISR(MY_ISR) {
    timer_flag = 1;
}
int main()
{
    Blue_LED_Write(ON);
    Red_LED_Write(OFF);
     
    Timer_1_Init(); // config but don't start the timeout counter
    isr_1_StartEx(MY_ISR); // setup timer interrupt sub-routine
    CyGlobalIntEnable; // enable global interrupts
     
    for(;;)
    {
        ms_delay(500); // delay 1/2 second
        // Toggle LEDs
        Blue_LED_Write(!Blue_LED_Read());
        Red_LED_Write(!Red_LED_Read());
         
        us_delay(500000); // delay 1/2 second
        // Toggle LEDs
        Blue_LED_Write(!Blue_LED_Read());
        Red_LED_Write(!Red_LED_Read());
    }
}
 
// This function can be used for delays in units of ms.
// Valid value of ms can be in the range [0 ~ 4,294,967,295].
void ms_delay (uint32 ms) {
    uint32 period = TIMER_CLOCK/1000*ms;
    Timer_1_WritePeriod(period);
    Timer_1_Enable(); // start the timeout counter
    while(!timer_flag);
    Timer_1_Stop();
    timer_flag = 0;
}
 
// This function can be used for delays in units of us.
// Valid value of us can be in the range [0 ~ 4,294,967,295].
void us_delay (uint32 us) {
    uint32 period = TIMER_CLOCK/1000/1000*us;
    Timer_1_WritePeriod(period);
    Timer_1_Enable(); // start the timeout counter
    while(!timer_flag);
    Timer_1_Stop();
    timer_flag = 0;
}

Downloadable Projects

  1. Timer.zip (986.5 KB) - This project toggles the blue LED on the Pioneer Board every second using a 16-bit timer with interrupt.
  2. Timer_Delay.zip (1.0 MB) - This project toggles the red and blue LED on the Pioneer Board every 1/2 second using a 32-bit timer and 2 different delay functions.

Questions/Comments

Timers are one of the most commonly used peripherals in embedded applications. I hope this example was helpful for you. For questions or feedback about information on this or any other page, please go to the TechForum: TechForum . Thanks for visiting the eewiki!

  • Scott