PSoC Creator Tutorial: Design Example with PWM and Interrupt By Prawat Nagvajara

Electrical and Computer Engineering PSoC Creator Tutorial: Design Example with PWM and Interrupt By Prawat Nagvajara I. Introduction This tutorial i...
Author: Molly Watson
78 downloads 0 Views 600KB Size
Electrical and Computer Engineering

PSoC Creator Tutorial: Design Example with PWM and Interrupt By Prawat Nagvajara

I. Introduction This tutorial introduces PSoC Creator design and consists of: 1. Creating a system comprising PWM, clock, Interrupt (interrupt service routine interface), digital output port and LCD display. 2. Output pins assignment for PSoC 3 Dev. Kit CY8CKIT-030 3. Software - main.c 4. Programming the CY8CKIT-030 Design Example: An egg timer (a culinary term) is a device used for timing when T0 seconds has elapsed. This tutorial demonstrates its use for blinking LED with a 1-s on/off period. The design uses PWM terminal count interrupt to provide 200 ms interval interrupts. The interrupt service subroutine counts the number of times interrupt happens and sets a time-expired flag when the count corresponds to 1 s. II. Creating System 1. File -> New Project. Name the project “egg_timer”.

2. Workspace Explorer pane consists of the Project ‘egg_timer’ TopDesign.cysch (schematic), egg_timer.cydwr (hardaware), Header Files, Source Files: main.c and later the Generated_Source after

the system is built. On the top bar above the Workspace pace comprises buttons for build, compile, program the device and debug. 3. The steps in this section build the system below. Open TopDesign.cysch in the Workspace.

3.1 In the Component Catalog pane drag and drop PWM – in Digital/Functions Clock – in System Logic Low ‘0’ – in Digital/Logic Interrupt [v1.7-] – in System Digital Output Pins – in Ports and Pins Character LCD [v1.90] – in Display 3.2 Double click on each component and edit names according to the screenshot above. 3.3 Connect wires according to the screenshot using wiring tool the second button from the top of the TopDesign.cysch pane. 3.4 Clock parameters: Double click on the clock and set these parameters

3.5 PWM parameters: Double click on the component and set these parameters

3.6 PWM Advanced parameters

3.7 Interrupt parameters

3.8 Digital Output Port parameters

3.9 LCD parameters

3.10 Click the build button. 4. Main dot C: Open and edit main.c 4.1 Include header files and declare variables

4.2 Interrupt Service Routine (isr) code Before discussing the isr code, reader should know a method for declaring the address for an isr program. An excerpt from the ISR component datasheet on the function ISR_SetVector shows void ISR_SetVector(cyisraddress address) Description:

Parameters: Return Value: Side Effects:

Changes the ISR vector for the interrupt. Use this function to change the ISR vector to the address of a different interrupt service routine. Note that calling ISR_Start() overrides any effect this method would have had. To set the vector before the component has been started, use ISR_StartEx() instead. address: Address of the ISR to set in the interrupt vector table void Disable the interrupt before calling this function and re-enable it after.

The main program (see void main() below) calls this function PWM_isr_SetVector(my_PWM_TC_isr; which declares an address name my_PWM_TC_isr (PWM Terminal Count interrupt service routine) for the isr code. The isr code begins with CY_ISR with the address.

Upon an interrupt from the PWM terminal count, if Time_Expired flag not set increment count. If count reaches Max_Count set the flag and reset the count. LCD is used for debugging. Clear any pending on PWM interrupt flag. Read status register of the PWM to clear the register. (From the PWM datasheet “The status register is a clear-on-read register so the interrupt source is held until the PWM_ReadStatusRegister() function is called.”) 4.3 void main() 4.3.1 Between global interrupt disable and enable, start and initialize all the peripherals. The address for the interrupt service routine declared to be PWM_TC_isr. Disable the isr component initially.

4.3.2 Testing the Egg Timer by driving the digital output port (to be connected to an LED) high and low with 1 sec period, that is, the isr sets the Time_Expired flag once count = Max_Count.

5. Open egg_time.cydwr. Edit the pins for my_LCD to P2[6:0] and Digital_Out_LED to P6[3]

6. Hit the Build button III Program Device and Verification 1. Connect the USB JTAG to computer 2. Program the PSoC3 3. Verify for correctness.