Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Bailey O.H.Embedded systems.Desktop integration.2005

.pdf
Скачиваний:
73
Добавлен:
23.08.2013
Размер:
9.53 Mб
Скачать

420

Chapter 10 / The PSoC Prototype

 

 

**********************************************************************************/

I2Cm_1_Start();

// Start I2C Processing

//Output sizeof(txBuf) characters to I2C SlaveID 0x50 in a complete

//Data Transfer

I2Cm_1_bWriteCBytes(0x50, txBuf, sizeof(txBuf), I2Cm_1_CompleteXfer);

}

Two function calls are all that is required to send data via I2C. Next we will examine the 1-Wire interface used for collecting the temperature. Before we do, be aware that the 1-Wire user module is a third-party module. It can be downloaded from www.psocdeveloper.com and is included with the source files for this book, available at www.wordware.com/files/embsys and www.time-lines.com.

Adding User Modules to the PSoC Designer

To add the 1-Wire module simply copy the entire data directory to the C:\Program Files\Cypress MicroSystems\PSoC Designer\ Data folder. If you have the PSoC Designer open, close it and restart it; you should now see the 1-Wire user modules under the Digital Comm button.

The PSoC 1-Wire Interface

Next we have the 1-Wire interface to implement. Much of the work in implementation has been done in the Designer, so we need to add the code to address the 1-Wire devices. There are more functions available for the 1-Wire, so instead of going through them all we will only list those that are needed for our device. They are:

Chapter 10 / The PSoC Prototype

421

 

 

OW_Start — Initializes the 1-Wire I/O pin

OW_Reset — Resets any 1-Wire devices on bus and detects presence

OW_WriteByte — Writes a byte to the 1-Wire bus

OW_ReadByte — Reads a byte from the 1-Wire bus

OW_Delay10mTimes — Provides number of 10 millisecond loops to delay

Reading and converting a temperature requires the following code:

Listing 10-2

/********************************************************************************* Do Temperature conversion for Dallas 1820 Chip

**********************************************************************************/

OneWireSW_1_Start();

// Initialize 1-Wire Bus

OneWireSW_1_Reset();

// Reset Bus, Check for Devices

OneWireSW_1_WriteByte(0xCC);

// Skip ROM Command

OneWireSW_1_WriteByte(0x44);

// Convert Temperature Command

OneWireSW_1_Delay10mTimes(75);

// Wait 750 milliseconds for completion

OneWireSW_1_Reset();

// Reset the bus again

OneWireSW_1_WriteByte(0xCC);

// Skip ROM Command

OneWireSW_1_WriteByte(0xBE);

// Read Scratchpad Command

OWTemp = OneWireSW_1_ReadByte();

// Read Converted Temperature

The PSoC SPI User Module

This brings us to the SPI user module definitions. In past chapters when we have implemented the SPI interface we have not designated it as a “master.” There are two reasons for this. First, we have not connected multiple slaves to the SPI interface; it has been a one-to-one relationship with a single slave. The second reason is because we have not had a need to explore using multiple slaves simply because this is not a part of the specification for

Chapter 10

422

Chapter 10 / The PSoC Prototype

 

 

our prototype for the design. Having said that we will now explore the SPI master user module as it relates to our needs.

There are just a few API calls available in the SPIM user module. They are:

SPIM_Start — Configures the SPI mode and sets the proper bits in the control registers

SPIM_Start — Disables the SPI interface by clearing the enable bit in the control register

SPIM_EnableInt — Enables the SP interrupt on an SPI done condition

SPIM_DisableInt — Disables the SPI interrupt on an SPI done condition

SPIM_SendTxData — Transmits a byte to slave device

SPIM_bReadRxData — Returns a received byte from a slave device

SPIM_bReadStatus — Returns the contents of the control/status register

As always, the slave’s device signal must be asserted to a low state just before sending data. The RX Buffer full flag should be checked prior to a call to SPIM_bReadRxData to verify data has been received. The following listing sends the message “Hello from PSoC” to an SPI slave device.

Listing 10-3

/********************************************************************************* Send "Hello from PSoC" to SPI Device

**********************************************************************************/

//Start the SPI interface with control mode 0 ans MSB First SPIM_1_Start(SPIM_1_SPIM_MODE_0 || SPIM_1_SPIM_MSB_FIRST);

//While there are still valid characters, loop

while (*ptrSPI_MSG !=0) {

// Check RX Buffer and TX Data Buffer

while(! (SPIM_1_bReadStatus() & SPIM_1_SPIM_TX_BUFFER_EMPTY)){;} // Send next byte

Chapter 10 / The PSoC Prototype

423

 

 

SPIM_1_SendTxData(*ptrSPI_MSG);

// Increment Message Pointer

ptrSPI_MSG++;

}

The PSoC RS-232 Interface

We’ve reached the fourth and final interface to the outside world, the RS-232 interface. The PSoC UART is very flexible, which also means there is a level of complexity associated with it. You won’t see any API calls available to set baud rates. Instead, the clock source is used with a divider to set the baud rate. Having said this, the UART user module conforms to RS-232 data transmit and receive standards without handshaking. As we did with the dsPIC, we will support handshaking though the use of general I/O pins on the PSoC. Because RS-232 communications are more complex than the other communications methods used, the user module has two APIs available — a high-level and low-level API.

The low-level API allows fine control over interrupt configurations and buffer status controls, whereas the high-level API provides more control over transmit and receive functionality. For the purpose of explaining how to handle data I/O we will forego the discussion on interrupts; however, the associated project and source does both explain and use interrupts. Let’s take a look at the high-level UART user module API. The routines are:

UART_IntCntl — Selects RX/TX interrupt control

UART_PutString — Transmits NULL-terminated string

UART_CPutString — Transmits NULL-terminated constants

UART_PutChar — Transmits single character when TX register is empty

Chapter 10

424Chapter 10 / The PSoC Prototype

UART_cGetChar — Receives character from RX data register when available

UART_Write — Transmits multiple characters to TX port

UART_CWrite — Transmits multiple constant characters to TX port

UART_cReadChar — Does immediate receive from RX port; returns either valid ASCII character or zero if no character is available

UART_iReadChar — Same as above but returns 2 bytes, one with ASCII character and other with any error code or zero if no error detected

UART_PutCRLF — Sends carriage return and line feed to TX port

There are several other high-level commands that deal with command buffer status and the transmission of hex data we will not be using. In addition, there are several low-level API functions we will be using. They are:

UART_Start — Enables UART user module

UART_Stop — Disables UART user module

Again, there are several other commands that deal with interrupts and register status that we will not be discussing at this time. Let’s take a look at what is required to send the infamous “Hello from PSoC” message via the UART.

The UART divides the clock source by 8, which means we need a clock that is eight times the desired baud rate. The simplest method of implementing a clock is to use a Counter8 user module as a source. This, however, is not the only way to attain a clock source, but for the purpose of our listing we will assume a Counter8 clock is being used for our UART with a device clock speed of 24 mHz. The Counter8 parameters would be set as follows:

Chapter 10 / The PSoC Prototype

425

 

 

Clock — 24 mHz

Enable — High

Output — None

Period — 155

CompareValue — 78

CompareType — Less than or equal

InterruptType — Terminal count

Using the above parameters we would calculate the UART baud rate as:

BaudRate = 24,000,000/((155 + 1) * 8) = 19.2K baud

Pay close attention to variables in the preceding equation. If you change the clock speed you will also change the baud rate, so keep this in mind when determining the PSoC main clock rate.

Using the above calculations we would set the UART user module parameters as follows:

 

Clock — Counter8 Broadcast

 

 

 

RX Input — UART Assigned Global Input

10

 

 

 

TX Output — UART Assigned Global Output

Chapter

 

 

TX InterruptMode — TXComplete

 

 

RxCmdBuffer — Enable

 

 

 

RxBufferSize — 16 bytes

 

 

Command Terminator — CR (Carriage Return)

 

 

Param_Delimiter — 32 (Space)

 

 

IgnoreCharsBelow — 32 (Ignore all control characters below

 

 

a space)

 

We are using the broadcast line from the Counter8 user module as the clock source for the UART user module. The RX in is attached to the UART global input and the TX out is attached to the UART global output line. We are using an interrupt triggered on a transmit or receive complete. In order to use this mode the

426

Chapter 10 / The PSoC Prototype

 

 

RxCmdBuffer must be enabled. We have assigned a 16-byte buffer to the RxBufferSize. Since this is for sending text messages a carriage return signals the end of a transmission as the message terminator. The ASCII space is the delimiter between parameters and we ignore characters below decimal 32, which is the space. It is worthy to note that software handshaking will be masked out from the remote system because those characters fall below decimal 32. The following code will send the string “Hello from PSoC” to the TX port.

Listing 10-4

/********************************************************************************* Send "Hello from PSoC" from the UART

**********************************************************************************/

UART_1_CmdReset();

// Initialize UART 1 command/receive buffer.

UART_1_IntCntl(UART_1_ENABLE_RX_INT);

// Enable Receive Interrupt

// The following is a method of configuring the Counter8 user module in code. /*********************************************************************************

Counter8_1_WritePeriod(155);

// Set Write Period

Counter8_1_WriteCompareValue(77);

// Set Compare Value

Counter8_1_Start();

// Start Counter

*********************************************************************************/

UART_1_Start(UART_1_PARITY_NONE);

//

Start UART 1 with No Parity bit

M8C_EnableGInt;

//

Enable PSoC Interrupts

// Send string

UART_1_CPutString("\r\nHello from PSoCb\r\n");

The completed listing for the PSoC follows:

Listing 10-5

#include

<m8c.h>

//

part-specific constants and macros

#include

"PSoCAPI.h"

//

PSoC API definitions for all user modules

BYTE txBuf[] = "Hello World";

// Create Storage for String

 

Chapter 10 / The PSoC Prototype

427

 

 

 

BYTE OWTemp;

// Create storage byte for Temperature

 

CHAR SPI_MSG[]= "Hello from PSoC";

// Static SPI Message

 

CHAR *ptrSPI_MSG;

// Pointer to SPI Message

 

CHAR * uartMsg;

 

 

void main()

{

/********************************************************************************** Send "Hello World" to the LK202-25

**********************************************************************************/

I2Cm_1_Start();

// Start I2C Processing

//Output sizeof(txBuf) characters to I2C SlaveID 0x50 in a complete

//Data Transfer

I2Cm_1_bWriteCBytes(0x50, txBuf, sizeof(txBuf), I2Cm_1_CompleteXfer);

/********************************************************************************* Do Temperature conversion for Dallas 1820 Chip

**********************************************************************************/

OneWireSW_1_Start();

// Initialize 1-Wire Bus

OneWireSW_1_Reset();

// Reset Bus, Check for Devices

OneWireSW_1_WriteByte(0xCC);

// Skip ROM Command

OneWireSW_1_WriteByte(0x44);

// Convert Temperature Command

OneWireSW_1_Delay10mTimes(75);

// Wait 750 milliseconds for completion

OneWireSW_1_Reset();

// Reset the bus again

OneWireSW_1_WriteByte(0xCC);

// Skip ROM Command

OneWireSW_1_WriteByte(0xBE);

// Read Scratchpad Command

OWTemp = OneWireSW_1_ReadByte();

// Read Converted Temperature

/********************************************************************************* Send "Hello from PSoC" to SPI Device

**********************************************************************************/

//Start the SPI interface with control mode 0 and MSB First SPIM_1_Start(SPIM_1_SPIM_MODE_0 || SPIM_1_SPIM_MSB_FIRST);

//While there are still valid characters, loop

while (*ptrSPI_MSG !=0) {

// Check RX Buffer and TX Data Buffer

while(! (SPIM_1_bReadStatus() & SPIM_1_SPIM_TX_BUFFER_EMPTY)){;} // Send next byte

SPIM_1_SendTxData(*ptrSPI_MSG); // Increment Message Pointer

Chapter 10

428 Chapter 10 / The PSoC Prototype

ptrSPI_MSG++;

}

/********************************************************************************* Send "Hello from PSoC" from the UART

**********************************************************************************/

UART_1_CmdReset();

// Initialize UART 1 command/receive buffer.

UART_1_IntCntl(UART_1_ENABLE_RX_INT);

// Enable Receive Interrupt

// The following is a method of configuring the Counter8 user module in code. /*********************************************************************************

Counter8_1_WritePeriod(155);

// Set Write Period

Counter8_1_WriteCompareValue(77);

// Set Compare Value

Counter8_1_Start();

// Start Counter

*********************************************************************************/

UART_1_Start(UART_1_PARITY_NONE);

//

Start UART 1 with No Parity bit

M8C_EnableGInt;

//

Enable PSoC Interrupts

// Send string

UART_1_CPutString("\r\nHello from PSoCb\r\n");

}

Before we conclude with the PSoC device, please spend some time getting familiar with how this device works and the procedures to configure and program it. The PSoC really represents a convergence of software and hardware in developing a hardware component. There is no way possible to cover the capabilities of this product in the space of this book. The idea is to provide you with the information needed to understand and start using the device in an intelligent fashion. If you’re new to electronics I do not recommend using this device until you’ve built several circuits using the BASIC Stamp or PIC components.

Chapter 10 / The PSoC Prototype

429

 

 

The Cypress EZ-USB Chip

Earlier we briefly discussed some of the advantages of the EZ-USB chip. Now it’s time to take a serious look at this interesting and useful device. As mentioned earlier the EZ-USB boots twice, once as a generic device and a second time as a specific device type. The EZ-USB is not just a USB chip but a full microcontroller as well. The PSoC differs from the PIC 16C745/16C765 through the use of flash memory and a different instruction set. It is also a full speed device, whereas the PIC counterparts are slow speed devices.

The EZ-USB development kit contains a development board as shown in Figure 10-17 and a snap-on prototype board for custom development. The prototype board snaps onto the development board via five headers as shown in Figure 10-18.

Chapter 10

Figure 10-17

Соседние файлы в предмете Электротехника