Bailey O.H.Embedded systems.Desktop integration.2005
.pdf
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:
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
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
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.
