lecture13 - Lecture #13 - Blimp September 22, 2010 Outline...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture #13 - Blimp September 22, 2010 Outline for today: SMBus a.k.a. I2C bus bus Compass and Ultrasonic modules Worksheet 8, I2C Smart Car/ Blimp Representation C code Steering System Heading Sensor I2C Propulsion System I2C Altitude Sensor Steering Actuator Output Port I2C Output Port Drive Actuator LCD Display, Key Pad – Lab 4 9/22/2010 Lecture #13 - Blimp 2 9/22/2010 Lecture #13 - Blimp 3 Serial Communication There are many reasons that one needs to send data to or receive data from other devices data to or receive data from other devices. SMBus and I2C bus The I2C bus, Inter IC bus, was developed by Philips in the ’80s to allow for Phili th ’80 communication between devices in a TV SMBus, System Management Bus, is an Intel standard developed in 1995 to avoid patent issues. It includes the I2C patent issues. It includes the I2C standards. standards. We can use either name interchangeable. 4 9/22/2010 Lecture #13 - Blimp 5 The printf and scanf command use an RS-232 serial RSport to communicate between the microcontroller and a terminal program on your laptop. Ethernet and USB ports on your laptop Sensors for our project – compass and ranger Require fewer wires than parallel Easily allow variable data set lengths Lecture #13 - Blimp Serial connections send data one bit at a time 9/22/2010 1 Lecture #13 - Blimp September 22, 2010 How might you set up serial communication? A sender pulses high and low signals on a wire. How does the receiver know when one bit ends and the next starts? Take two bit streams: starts? Take two bit streams: 1-0-1-0-1-0 and 1-1-1-0-0-0 and The first seems easy to identify, the second could look like only two bits sent slowly or six bits sent fast. The problem is solved if there is a clock that knows the bit rate Synchronous communication sends a clock signal with the data I2C is one such system, as is Firewire and USB. 0.1 Asynchronous communication uses separate clocks, one on each end. each end. The serial port on computers and FAX machines are examples Asynchronous communication requires both ends to agree on the clock rate, the baud rate. Even then the clocks won’t exactly match so additional information is sent to match up the data. 1.0 There are versions of the I2C bus that work up to 3.4MHz, we will work at 100kHz which is the original specification http://www.semiconductors.philips.com/acrobat_download/literature/9397/75013239.pdf 9/22/2010 Lecture #13 - Blimp 6 9/22/2010 Lecture #13 - Blimp 7 How does the I2C work? Two wires carry signals, so it is called a 2 wire system How does the I2C work? (Some details are specific to our application.) (Some Still need power and ground, so four wires will be connected SCL SCL – serial clock on one wire Master device sends the clock signal, all slaves use that clock Every device is Synchronized to Master The C8051 will always be the Master for our application Synchronous communication with only one master at a time C8051 is the Master in our application, it always controls the clock line, SCL. Data is sent to the slave devices (write) or data is received from the slave devices (read) on the SDA line. SDA – serial data on another wire Data line is high to signify that a bit is a 1 or true, low for a 0 or false. Because data has to flow in either direction, SDA line can’t be push pull. Both SDA and SCL are configured as open drain (automatically.) PullPull-up resistors are used to pull both the SDA and SCL lines high Any device can pull the lines low, so any device can send data Unit sending data must set the data line high or low while clock is low. When clock goes high data is read by the other device(s). 8 bits sent serially one after another After one byte is sent, receiver must acknowledge data received before next byte can be sent. Data is sent in bytes 1.8k 1.8k 9/22/2010 Lecture #13 - Blimp 8 9/22/2010 Lecture #13 - Blimp 9 2 Lecture #13 - Blimp September 22, 2010 How does the I2C work? Consider two modes Master Transmitter Mode is used to talk to a slave device Master Receiver Mode is used to listen to a slave device Data is sent in bytes 8 bits sent serially, one after another ACK - After one byte is sent, receiver must acknowledge data received before next byte can be sent. Slaves have addresses all devices are on same bus so each Slaves have addresses – all devices are on same bus, so each needs needs an unique identifier. R/W, Start, Stop - Slaves and Master must keep track of who is talking so there is a Read/Write bit, a Start signal and a Stop signal. Lecture #13 - Blimp 10 Master Transmitter Sequence Send a start signal Send a byte with a 7 bit slave address with the R/W bit low. R/W bit low indicates that the Master will write to the slave Slave should acknowledge - ACK The Master will then send one byte of data, check for ACK, repeat till done. Note: Master must know how many bytes to write. The Master sends a stop signal 9/22/2010 9/22/2010 Lecture #13 - Blimp 11 Consider two modes Master Transmitter Mode is used to talk to a slave device Master Receiver Mode is used to listen to a slave device Implementing the I2C Interrupts – Using Interrupts is the most eloquent way to implement the I2C bus but we know that we will have exactly three slaves so we will directly communicate with the the bus. C8051 is always the master Don’t need to worry about conflicts on the bus and limits cases that must be considered Master Receive Sequence, (R/W is high) Send a start signal Send a byte with a 7 bit slave address and a 1 in the R/W bit Slave should acknowledge - ACK Slave will send one byte of data, Master should acknowledge Slave send and Master receive repeats until the Master sends a stop. Note: Master must know how many bytes to read. 9/22/2010 Lecture #13 - Blimp 12 9/22/2010 Lecture #13 - Blimp 13 3 Lecture #13 - Blimp September 22, 2010 Run at about 100kHz, done once SMB0CR=0x93; Set the crossbar, done once XBR0= 0x**; /* use setting from lab part setting from lab 3, part 1 */ */ Enable the SMBus, (I2C), done once ENSMB=1; /* bit 6 of SMB0CN */ Implementing the I2C Need a series of simple functions void start (void) Sends a start signal, must be followed by a write to be useful void write (unsigned char output_data) write (unsigned char output Sends a byte of data or a slave address with the R/W bit Must be preceded by a start or another write void write_and_stop (unsigned char output_data) Used to send last byte of a write to slave, followed by a stop signal unsigned char read (void) Read a byte of data from a slave The first read must be preceded by a start and a write with the slave address in the write. The The slave sends data from a register, subsequent reads will cause the slave to send data for successive registers. For everything else, we need to read and write to the sfr’s. unsigned char read_and_stop (void) Does last read in a sequence and sends a stop signal. 9/22/2010 Lecture #13 - Blimp 14 14 9/22/2010 Lecture #13 - Blimp 15 15 Implementing the I2C Need two higher level functions – one to read data and one to write data: Implementing the I2C, second higher level function void read_data (unsigned char addr, unsigned char slave_reg, addr, slave_reg, unsigned char *buffer, unsigned char num_bytes) num_bytes) Start the I2C Write to the slave address addr with R/W bit low Write to the slave address, addr, with R/W bit low Write and stop - Writes a byte of data which is the register inside the slave to be read, slave_reg, and sends stop slave_reg, Start the I2C Writes to the slave address, addr, again but with R/W bit high addr, Read data, repeats read to get all the data, num_bytes void write_data (unsigned char addr, unsigned char slave_reg, addr, slave_reg, unsigned unsigned char *buffer, unsigned char num_bytes) num_bytes) Start the I2c Write to the slave address, addr, with R/W bit low addr, Sends a byte of data which is the register inside the slave to be written, slave_reg. slave_reg. Sends the data that is to be written in the slave_reg, buffer. slave_reg, Repeat if more than one byte is to be written During each read, set ACK, except on last read ACK is low End with a stop signal on last write Stop the I2C This function is in new .h file, I2C.h Important – “unsigned char *buffer” is a pointer Pointers are used to send arrays to functions. This is different than how we have sent variables in other functions. 9/22/2010 Lecture #13 - Blimp 16 9/22/2010 Lecture #13 - Blimp 17 4 Lecture #13 - Blimp September 22, 2010 Coding Example void i2c_write(unsigned char output_data) { SMB0DAT = output_data; while(!SI); SI = 0; } Coding Example Repeat data byte for num_bytes-1 times Followed by last write with a stop bit. A BASIC FUNCTION //Data to be written put into register //Wait until send is complete //Clear SI A HIGHER ORDER FUNCTION void i2c_write_data(unsigned char dev_addr, unsigned char start_reg, unsigned char *buffer, unsigned char num_bytes) { unsigned char j; i2c_start(); //start i2c i2c_write(dev_addr&~0x01); //identify target and send 0 in R/W bit i2c_write(start_reg); //Specify device register to be written for(j=0; j<num_bytes-1; j++) //write all but last data byte i2c_write(buffer[j]); i2c_write_and_stop(buffer[num_bytes-1]); //write last data byte and stop } 9/22/2010 Lecture #13 - Blimp 18 void i2c_write_data(unsigned char dev_addr, unsigned char start_reg, unsigned char *buffer, unsigned char num_bytes) { unsigned char j; i2c_start(); //start i2c i2c_write(dev_addr&~0x01); //identify target and send 0 in R/W bit i2c_write(start_reg); //Specify device register to be written for(j=0; j<num_bytes-1; j++) //write all but last data byte i2c_write(buffer[j]); i2c_write_and_stop(buffer[num_bytes-1]); //write last data byte and stop } 9/22/2010 Lecture #13 - Blimp 19 Coding Example – also given to you in I2C.h Usage Example Data[0]=0x51; i2c_write_data(0xE0, 0, Data, 1); Size of data in bytes of data in bytes Device Address (ranger in this case) First register that we are writing to. (register 0 or “control register” of ranger. Data that we are writing. In this case 0x51 specifies range to be returned in centimeters. Important – Data is an array. Pointers are used to send arrays to functions. This is different that how we have sent variables in other functions. void i2c_read_data(unsigned char addr, unsigned char start_reg,unsigned char *buffer, unsigned char num_bytes) { Start I2C transfer Write the address of the device to read from Stop and write the first register to be read Start I2C transfer Write address again, this time indicating a read operation For (0 to num_bytes – 2) Set acknowledge bit Read data, save it in the buffer Clear acknowledge bit Read the last byte and stop, save it in the buffer } 9/22/2010 Lecture #13 - Blimp 20 9/22/2010 Lecture #13 - Blimp 21 5 Lecture #13 - Blimp September 22, 2010 i2c.h and “Worksheet #8” You must download the header file: i2c.h This file has functions to write data and read data. Also has functions of use for the LCD display – Lab 4 +5V + 10F IMPORTANT: in your C program, the include for i2c.h must be the last one (at least after stdio.h) stdio.h) <include <include <include <include stdio.h> stdio.h> stdlib.h> stdlib.h> c8051_SDCC.h> i2c.h> 1.8k 1.8k Power SDA SCL GND P ?.? P ?.? C8051 There is a “Worksheet #8” which is not optional. This worksheet is useful when trying to understand the I2C bus and communication sequences. 9/22/2010 Lecture #13 - Blimp 22 9/22/2010 Wiring for Ultrasonic Ranger Page 79 of the manual Lecture #13 - Blimp 23 Wiring for Compass Additional Details - Compass Compass +5V 1.8k Address is 0xC0 Angle can be read as 0 – 255 for a full circle – (don’t do this.) Angle can be read as 0 – 3599 for full circle – (do this.) Requires 2 bytes Read two registers, regs 2 and 3 C8051 1.8k SDA SCL P?.? 2 is high byte 3 is low byte P?.? Compass calculates the heading every 33.3mS We will want to calculate the turning rate (for blimp) Page 78 of the manual Angular velocity = (present heading – previous heading)/(delta t) This only works if we only read new data, so set (delta t)>33.3mS Read the compass every second PCA timer interrupt 9/22/2010 Lecture #13 - Blimp 24 9/22/2010 Lecture #13 - Blimp 25 6 Lecture #13 - Blimp September 22, 2010 Additional Details - Ranger Ranger Additional Details - Ranger Warning – Danger In In the LITEC room, the ranger will not “see” more than 255cm, so the high byte of the range will always be zero. This isn’t the case in the Armory. You must correctly calculate the range using both registers or else – If the blimp goes higher than 255cm in the Armory, it will think it is at the floor and gun the thrust fans for the ceiling. This has happened. Make sure that you compute the range using both bytes, Reg 2 and 3. 9/22/2010 Lecture #13 - Blimp 27 27 Address is 0xE0 Ranger only sends and ultra sonic ping when instructed to do so. Start a ping by writing to reg 0 ping by writing to reg Writing a 0x50 to reg 0 starts a ping, and stores the range in inches, writing a 0x52 starts a ping and stores the result as the echo time, (Don’t do either of these.) Writing a 0x51 to reg 0 starts a ping and stores the range in cm. This This is the one we want. Sound is slow, about 1 foot per millisecond You must wait for the echo – The data sheet says 65mS Wait for 4 PCA timer overflow interrupts – 80mS For reasons we don’t understand – sometimes it is better to wait even longer. You’ll know if you receive some range results that claim the blimp is 65535 cm high. Suggestion – after a read, start a ping, then wait 80mS till next read. First read may be false, but who cares? Lecture #13 - Blimp 26 9/22/2010 What should you know about I2C? Slide 1 Synchronous, two wire serial communication. At any one time, only one Master with one or more Slaves What should you know about I2C? Slide 2 SMBus and I2C are the same for our application All devices on the bus must be configured as open drain All devices on the bus must be configured as open drain, so the SDA and SCL lines need pull up resistors. We will run our bus at 100kHZ There are five sfr, special function registers associated with the I2C bus. While the C8051 can be a slave, we won’t use it that way C8051 is the Master and the sensors are slaves Sends the clock Starts the communication by sending A Start signal, slave address, and a read/write bit Master If it is a write, this is followed by one or more data bytes If it is a read, then the Master looks for data from the slave Slave Looks for its address and R/W bit Either accepts data or sends data as appropriate Any device receiving data must send an ACK, acknowledged signal. SMB0CN – SMBus 0 Control – Used to control the bus SMB0STA SMB SMB0STA – SMBus 0 Status – Read to know present status St SMB0CR – SMBus 0 Clock Register – Set for 100kHz clock SMB0ADR – SMBus 0 Address – We don’t use, slave address SMB0DAT – SMBus 0 data – data for reads and writes Lecture #13 - Blimp 29 9/22/2010 Lecture #13 - Blimp 28 9/22/2010 7 Lecture #13 - Blimp September 22, 2010 What should you know about I2C? Slide 3 Initialize void SMB0_Init() { SMB0CR = 0x93; // set SCL to 100KHz ENSMB = TRUE; // enable SMBUS0 } Functions will read and write to bits of SMB0CN There are five sfr, special function registers associated with the I2C bus. Crossbar Quiz 4 PCA and crossbar I2C Given XBR0=0Xnn; answer which pin, if any, while have TX0 or SDA or CEX2 Determine the correct value for XBR0 if you need to use UART0 and SPI0 but not SMB0 or any PCA pins. Variations of this line How to: to: Enable the PCA Enable the desired CCMs (Capture Compare Modules) Pick input signal Enable PCA overflow interrupts Clear the overflow flag in the interrupt service routine PCA Set the pulse width Determine the pulse width value in counts for a desired pulse width in msec Determine and write the correct value into the correct CCM (Capture Compare Module.) Do the above with correctly using SYSCLK or SYSCLK/4 or SYSCLK/12 SMB0CN – SMBus 0 Control – Used to control the bus (functions use sbits) SMB0CN SMBus Control to control the bus (functions use sbits) SMB0STA – SMBus 0 Status – Read to know present status (not used) SMB0CR – SMBus 0 Clock Register – Set for 100kHz clock (set once) SMB0ADR – SMBus 0 Address – slave address (not used) SMB0DAT – SMBus 0 data – data for reads and writes (used by functions) Set the period the period Use the preset method to shorten the period Determine the correct preset value for a desired frequency (F) or desired period (T) F=1/T Given a preset value, determine the output frequency or period Given the above, what is the duty cycle of the output duty duty cycle = (on time / period)*100% PCA_plotter isn’t allowed during the quiz 9/22/2010 Lecture #13 - Blimp 30 9/22/2010 Lecture #13 - Blimp 31 Today Use functions for I2C from i2c.h Wire sensor to 8051. Recommend that you first just write a main function that reads register 0 of sensor. This will return the version number of the software on the sensor. th th This is just a test of the wiring and code. Do Lab 3, Part 2 – reading your sensor. 9/22/2010 Lecture #13 - Blimp 32 Next Class Work on Lab 3, Part 2 – reading your sensor sensor Wire sensor Read and print sensor data 9/22/2010 Lecture #13 - Blimp 33 8 ...
View Full Document

This note was uploaded on 04/08/2011 for the course ENGR 2350 taught by Professor Fukanari during the Spring '08 term at Rensselaer Polytechnic Institute.

Ask a homework question - tutors are online