Sie sind auf Seite 1von 10

Lab 1 The CPU and General Purpose I/O

Objective: This laboratory is an introduction to the PSoC microcontroller (CPU) and its interface to the I/O ports. Please note that the lab is divided into three parts, Lab1A, Lab 1B, and Lab1C respectively. Successfully complete each lab before proceeding to the next. On completion of the exercise you will: Have used PSoC Designer to create a new project. Understand the basic operation and functionality of the CPU. Understand what system parameters affect CPU performance. Have declared global variables in assembly language Have calculated cycle time for a CPU control loop. Understand how to read and write an I/O port. Have configured I/O port pins for different drive modes. Used shadow registers to isolate input/output interaction. Have configured an I/O port pin to read a momentary switch. Have written a debounce routine to correctly read this switch. Assumptions: PSoC Designer and PSoC Programmer are installed on your computer. Required Materials: CY3210-PsoCEval1 Board Breadboard wire. Required Equipment Oscilloscope Related Reference Material: Cypress Technical Reference Manual Cypress Assembly Language User Guide Cypress Tele-Training Video Module 1: Introductory Module Cypress Tele-Training Video Module 2: Getting Started Designing

CPU Exercise
Step1. Start a New Project Open up Designer and start a new project. Select the base part to be a CY8C27443-24PXI. Name the Project Lab1A. (Unless explicitly expressed, all laboratory projects will use this base part) Select Assembler Option Go to the Device Editor. Switch over to the interconnect view. (It should look as follows.)

Step 2. Set Necessary Global Parameters. In the upper left of the PSoC Designer screen is the Global Resources window. Of the 20 parameters to be found there only CPU_Clock, and Supply Voltage affect CPU operation.

There are eight CPU clock selections. SysClk/1 (24MHz) should only be used when the supply voltage is guaranteed to be greater than 4.75 volts.

There are two voltage settings. For the 3.3V setting the fastest allowable CPU clock is SysClk/2 (12MHz).

Unless explicitly expressed, all laboratory projects will have these parameters set to 5.0V and 12MHz.

Step 3. Set the Drive Modes for Port 1 Pins In the lower left of the PSoC Designer screen is the Port Selection window. Set the port 1 pins to have a strong drive. Rename the port pins as shown below

Generate the application. (Always regenerate the application when changing anything in the interconnect view. It doesnt hurt to occasionally regenerate.) Step 4. Write the software Go to the Application Editor and open main.asm. It is shown below.

Add the following code.

Questions 1 What does this code do? This is a loop that: Reads the value of Port 1 data register Increments it Writes it back to the Port 1 data register. Each time through the loop, the Port 1 value increments. 2 What is the loop cycle time (in CPU cycles?) mov A,reg[PRTDR] ;6 CPU cycles inc A, ;4 CPU cycles mov reg[PRTDR],A ;5 CPU cycles jmp ;5 CPU cycles ;20 CPU cycles total 3 For a 12MHz CPU clock, what is each pins output frequency? Bit0 = _300kHz___ Bit1 = _150kHz___ Bit2 = __75kHz___ Bit3 = __37.5kHz__ Bit4 = _18.75kHz_ Bit5 = _9.375kHz_ Bit6 = _4.688kHz_ Bit7 = _2.344kHz_

Step 5. Download code and Run2 Build this project and verify that there are no errors. Download the program to the Eval board and run. Use an oscilloscope to view the waveform on each pin. Question 4 Do these measurements agree with your calculations? The loop cycles every 20CPU cycles. Bit0 goes high then low every 40 CPUs cycles or 12MHz/40 = 300kHz Bit1 is half the frequency of bit0 etc etc Go back to the interconnect view and change the CPU clock to 3MHz. Regenerate the application, rebuild the project, download to the board, and run. Questions 5 With the CPU clock now set to 3MHz, how would you expect the pin signals to change? With the CPU 4 times slower, the output at each pin should be 4 times slower.

6 Do your oscilloscope measurements verify this? It better!

Lab1A complete.

GPIO Output Exercise


Step 1. Make a copy of Lab1A Open project Lab1A. Go to File menu and select Save Project As. Set the New Project Name value to Lab1B. Step 2. Set the Drive Modes for Port 1 Pins In the lower left of the PSoC Designer screen is the Port Selection window. Change the port 1 pin drive selections from Strong to PullUp as shown below.

Writing to the port causes the port register to be set. The logic level at the pin is dependent on the drive register value, the drive mode, and external circuitry connected to the pin. Reading the port causes the data to be taken directly from the pins. When configured as pull-ups, the outputs have a strong impedance drive to logic low and a resistive drive to logic high. When driven high these pins can be pulled low externally. Step 3. Add jumpers to the Eval board Use jumper wire to short Bit0 to Bit6 on the Eval board. These two pins are wire -anded. Driving either pin low causes both pins to be pulled low. Question 1 Given this information, what should the waveform for each pin be? There should be no activity on the pins. Because Bit6 is low, Bit0 is low. Incrementing the Port value causes Bit0 in the data register to go high. but Bit6 pulls the output low. Step 4. Download code and Run Set the CPU_Clock global parameter to 12MHz. Regenerate the application, rebuild the project, download to the board, and run. Use an oscilloscope to view the waveform on each pin. Question 2 Do your observations agree with your predictions? Unfortunately yes.

Step 5. Use a shadow register Go to the Application Editor and open main.asm. It is shown below.

Add the following code.

There are a few things to note: - There is code added to allocate a byte of RAM for a variable. This particular variable is named bShadow. - The export statement makes bShadow a global variable. It is available to all other code files in this project. - The format for the contents of bShadow is [bShadow]. The state of the output register is now stored in a variable. Doing this breaks the I/O input output interaction. Questions 3 Now that the input output interaction has been broken, what should the waveform of each output look like? Bit6 will match Bit0. It is low half the time and toggles with a frequency of 272.7kHz (12MHz/2*22) the other half of the time.

4 What is the loop cycle time (in CPU cycles?) inc [bShadow] ;7 CPU cycles mov A,[bShadow] ;5 CPU cycles mov reg[PRTDR],A ;5 CPU cycles jmp ;5 CPU cycles ;22 CPU cycles total

Step 6. Download and Run Rebuild the project, download to the board, and run. Use the oscilloscope to view Bit6 and trigger on Bit7. Question 5 Do your observations agree with your predictions? It should.

Lab1B complete.

GPIO Input Exercise


Step1. Create New Project Name it Lab1C. Use standard laboratory settings for part type and settings. Step 2. Set the Drive Modes for Pins and Make External Connections Set five port pins with the setting shown below.
Name SwitchInput LedOut1 LedOut2 LedOut3 LedOut4 Port P1[0] P1[1] P1[2] P1[3] P1[4] Select StdCPU StdCPU StdCPU StdCPU StdCPU Drive PullDown Strong Strong Strong Strong

Step 3. Add jumpers to the Eval board Use jumper wire to make the following connections. - P10 to SW - P11 to LED1 - P12 to LED2 - P13 to LED3 - P14 to LED4 This configuration is shown in the schematic below.

PSoC
P10 P11 P12 P13 P14

Vdd SW

LED4 LED3 1K 1K

LED2 1K

LED1 1K

P10 (SwitchInput) is configured with a pull down drive mode. When this port pins data register is set to zero the output appears as a resistance to ground. When the external switch is pressed the pin is pulled high. Although configured as an output this pin is used as an input, as long as the data register bit remains zero. Generate the Application. Step 4. Write Control Code Write an assembly control program that does the following. - Declares five global single byte variables named, [bShadow], [bTemp2], [bTemp1], [bTemp0], and [bSwitchOn]. - Implement the following flow chart.

Initialize

[bShadow]=0 [bTemp2]=0 [bTemp1]=0 [bTemp]=0 [bSwitchOn]=0 reg[PRTDR=0]

Update Switch History


[bTemp2]=[bTemp1] [bTemp1]=[bTemp0] bTemp0=reg[PRT1DR]& 01h

[bTemp0]&[bTemp1] &[bTemp2] = 0?

no

[bSwitchOn] = 1?

no

Switch Just On
[bSwitchOn]=1

yes

yes

Increment LEDs
[bShadow]=[bShadow]+2 [reg[PRT1DR]=[bShadow]&1eh

Switch is Off
[bSwitchOn]=0

This algorithm contains a switch debounce routine. Three consecutive readings of the switch must be high before the switch is considered to be on. The LEDs are incremented, via the shadow register, only when the switch state goes from off to on (low to high). Step 5. Download code and run Rebuild the project, download to the Eval board, and run. Verify that the LEDs increment once for each switch press.

Lab1C complete

Das könnte Ihnen auch gefallen