I knew this was going to be a busy week for me, so I decided to re-implement a simple serial network using boards I fabricated in previous weeks.
I spent at least an hour debugging a sneaky bug introduced by an implicit cast. The bug isn't very interesting, but while I was trouble shooting, I learned about sign extension
. Sometimes you hear "Only use Bit Shift operators on unsigned values". The behavior of the right shift operator is different for signed and unsigned values.
// pseudo code
0b10000010 >> 3; // evaluates to 0b11110001 for signed values
The way that C stores negative numbers varies depending on your compiler and architecture, so bit shifting signed values is never portable.
I also learned the behavior of bit shifting values larger than a byte.
The master is connected to the FTDI serial cable. Every 6 seconds, it sets the clock to low, and sends 16 bits of data on MOSI (each binary 1 triggers a fast led flash on all nodes). The first bit is an address. The second bit is an integer instructing the addressed node to blink n times.
Example of serial communication on the avr attiny from Charles Holbrow on Vimeo.
Before I could get working this week, I had to review some of the content that Neil discussed in class.
I²C is a Communications protocol commonly used for chip to chip communication.
Two wires plus ground - clock wire, data wire both held 'high' with pull-ups.
A master and multiple slaves hanging off the wire. The master sends a clock. While the clock line is high, the master brings the data line low (I'm starting a message)...
SPI is another Communication Protocol, very similar to i2c - the master sends a clock.
Don't confuse ISP with the SPI communication protocol.
ISP is a means of Programing a chip while the chip is in the board. ISP describes the 2x3 header pin configuration and the Communications Protocol.
Confusingly, ISP uses the SPI communication Protocol.
Each pin on the attiny is a tri-state pin (Wikipedia). It can be in one of three configurations:
When a pin is configured as an input it is not connected to the hi or the low. In floating mode a pull-up resistor can be enabled or disabled.
[Check Accuracy]: When the pull-up resistor is enabled. The pin will always read "high" when the pin is not being driven by other electronics.
The master transmit is connected to the receive of ALL the slaves. The slave's transmit pins are ALL connected to the master receive pin.
By default all the transmit pins are floating. [CA: pull-up resistor should be on]
The master sends a message to the telling which chip may speak. That chip toggles it's transmit pin to output mode, and sends info back to the master. Then after the slave has finished answering the master, the slave sets it's transmit pin to floating mode again.