Bailey O.H.Embedded systems.Desktop integration.2005
.pdf
370 |
|
Chapter 9 / The PIC Prototype |
|
|
|
|
|
|
|
|
|
|
I2C_GCALL_DIS & |
// Disable General Call |
|
|
I2C_STR_DIS & |
// Disable SCL Clock Stretch |
|
|
I2C_NACK & |
// Enable Acknowledge Bit |
|
|
I2C_ACK_DIS & |
// Disable ACK |
|
|
I2C_RCV_DIS & |
// Disable Receive |
|
|
I2C_STOP_DIS & |
// Disable Stop Condition Enable |
|
|
I2C_RESTART_DIS & |
// Disable Repeat Start Condition |
|
|
I2C_START_DIS; |
// Disable Start Condition Bit |
|
StopI2C(); |
// Stop Interface just to be safe |
||
CloseI2C(); |
// Close if already open |
||
OpenI2C(setup, baudrate); |
// Open with new configuration settings |
||
IdleI2C(); |
// Wait until bus is idle |
||
StartI2C(); |
// Start I2C bus |
||
while(I2CCONbits.SEN); |
// Loop until Start Sequence is complete |
||
MasterWriteI2C(0x50); |
// Wait up the slave device |
||
while(I2CSTATbits.TBF); |
// Wait until Transmit Buffer is Empty |
||
while(I2CSTATbits.ACKSTAT); |
// Wait for slave acknowledgement |
||
MasterputsI2C(msgPtr); |
// Send message string |
||
StopI2C(); |
// Stop I2C Bus |
||
while(I2CCONbits.PEN); |
// Wait for Stop condition to complete |
||
CloseI2C(); |
// Close device |
||
}; |
|
|
|
As you can see there are a couple of vague similarities between RS-232 and I2C I/O. The first is baud rate. Just as we declare a baud rate in RS-232 we also declare a transfer speed in I2C, but it is not the same. We are declaring the bus speed or clock for which the data is sent. I know this may sound just like a baud rate generator but there is one major difference: The clock pulse is sent along with the data in I2C, whereas in RS-232 the clock is physically separate at each end. In the I2C definition we only need to worry about the maximum speed at which the slave can receive data, and make sure we stay within that speed range.
The second similarity is the number of data bits defined. Again, this is different from RS-232 in that the 7-bit definition applies only to the slave device ID length. Remember that the
374 |
Chapter 9 / The PIC Prototype |
|
|
well documented and cost nothing to view and use for testing. Most are very simple to understand and use as a starting point for other projects. While none of these routines are written for the dsPIC, several can be easily converted. For this project download and review the Dallas ds1821 thermometer, found in the source code folder. It consists of three files: delay.c, delay.h, and 1wire00x.c. These three files are easy to understand and can easily be modified to work with the dsPIC and HI-TECH dsPICC compiler. If you not up to writing your own C files, then download the source files for this book at www.wordware.com/ files/embsys or www.time-lines.com. You’ll find the dsPIC 1-Wire source in the chapter 9\dsPIC\1-Wire folder.
Implementing USB and Ethernet
Communications
This completes the implementation of the requirements for the dsPIC. Next, we will complete the USB communications controller. The TTL serial interface we developed earlier will handle all of our host hardware interfaces. The same four wires will allow the dsPIC to communicate with Ethernet, USB, or RS-232 communications interfaces. The same source code will also work on the dsPIC, so the communications hardware is actually transparent to the dsPIC.
Now you may be asking how this is possible since there are big differences in how RS-232, USB, and Ethernet interfaces work. The answer to that question lies in our design. We have designed in the ability to have one microcontroller that manages the environment by monitoring and controlling the temperature and air circulation for our thermostat. We have also chosen to implement a standard 4-Wire TTL interface to an external communications processor. As we’ve moved through this chapter we have kept in mind the fact that we will need to use a DIP switch or jumper for selecting the USB or Ethernet interface. Or do we?
378 |
Chapter 9 / The PIC Prototype |
|
|
Figure 9-11
The PICDEM USB is driven by the 16C765, which is a larger chip with more memory and I/O. In Figure 9-11 the 16C765 is the large chip on the left. Note the empty socket that can be used for the 16C745 device.
The 16C745 has 8 K of program space, 256 bytes of RAM, and 8-bit A/D resolution with five A/D channels. It conforms to the USB 1.1 specification and has 64 bytes of dual-port RAM, 22 I/O pins, and eight available interrupts.
The Microchip USB Engine
The 16C7x5 USB series has a built-in USB engine that you can use or modify to your own needs. The software interface is available for Assembler, C, or Basic. There are both an interrupt and polled versions of the engines. There are five functions supported by the Microchip USB engine: InitUSB, PutEP1, GetEP1, PutEP2, and GetEP2. The Put functions send data to the host while the Get routines receive data from the host system. These are the application layer functions. Beneath these functions are the protocol layer functions where all the background work is
Chapter 9 / The PIC Prototype |
379 |
|
|
done. At the protocol layer we have ServiceUSBInt, StallUSBEP, UnstallUSBEP, SoftDetachUSB, CheckSleep, ConfiguredUSB, and SetConfiguration functions.
The USB without doubt is the most complicated of the interfaces we need to implement. What makes the USB software more complex is the fact that the host implementation differs more than RS-232 or Ethernet. In addition, defining USB descriptors can be a time-consuming and tedious task. While there are no cross-platform tools to aid in this development effort, we can expedite our Windows development through the use of HIDmaker. Since Windows is the most complex of the USB driver models this will reduce the amount of time needed on the most time-consuming platform.
|
|
|
|
Note: |
|
9 |
|
|
During the course of writing this book I made several |
Chapter |
|
|
|
||
|
attempts to reach the people at usb.org and never received |
|
|
|
any reply or answers to my inquiries. My questions were |
|
|
|
related to why the documentation for USB devices wasn’t |
|
|
|
|
||
|
written to be more friendly to engineers who have had |
|
|
|
development experience with RS-232, RS-485, or Ethernet. |
|
|
|
I also questioned the expense for small developers in the |
|
|
|
purchase of a device ID. Neither question was answered |
|
|
|
nor did I receive a response to my emails. Being as usb.org |
|
|
|
is made up of very large companies, it has occurred to me |
|
|
|
that if the USB standard were easier to understand the |
|
|
|
companies who make the standards would not have the |
|
|
|
marketing edge for USB appliances they currently enjoy. |
|
|
