EE lab track, module 12

Serial buses: UART, I2C and SPI

Graded serial bus labs that run your sketch: set a UART baud rate by its bit time, scan an I2C bus, read a DS3231 register, latch a 74HC595 over SPI.

On the syllabus

  • UART framing: start bit, eight data bits, stop bit, and the baud rate
  • I2C addresses, ACK and scanning a bus
  • Reading a device register over I2C
  • SPI transfers and the latch of a 74HC595

You will be able to

  • Relate a baud rate to the bit time on the wire.
  • Find the devices on an I2C bus.
  • Read a register from an I2C device by its datasheet.
  • Send a byte over SPI and latch it to a shift register’s outputs.

The first challenge is free. The rest, the viva and the certificate are part of Pro, and everything is here to read.

Graded challenges

Values like 4.7k, 100n or 2.2u are fine. Readings are the number only, in the unit shown. Every check measures the simulation, not a bench: ideal parts within their models, no noise.

1. Set the baud rate by the character rate

A UART sends each byte as a frame: a start bit, eight data bits and a stop bit (8N1). The baud rate is bits per second, so the character rate is the baud rate over ten.

Formulacharacters a second = baud / 10, bit time = 1 / baud

Worked exampleAt 19 200 baud a bit lasts 52 µs, a frame 0.52 ms, so at most 1920 characters a second.

On the scope or meterThe simulator counts characters as they leave. It does not draw the bits on the TX pin, so on a real bench you would see this on a scope as bit widths.

Your task

The sketch sends the letter U as fast as the UART will take it. Each character is a frame of ten bits: a start bit, eight data bits and a stop bit (8N1).

A receiver expects 9600 baud. Fix the sketch so it sends 960 characters a second, within 2%.

The simulator’s UART delivers each byte one frame time after it starts, as the chip does, but it does not draw the bits on the TX pin, so the check counts characters rather than timing bits on a scope.

    Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

    It is checked for

    • It sends 960 characters in a second

    2. Scan the I2C bus

    Every I2C device has a 7-bit address. The master sends an address; a device that has it pulls SDA low for the ACK bit. A scanner tries each address and lists the ones that answer.

    Formulavalid addresses: 1 to 126; endTransmission() == 0 means ACK

    Worked exampleA 24C32 EEPROM sits at 0x50, which is 80 in decimal. A scan from 1 to 126 prints addr=80.

    On the scope or meterThe serial monitor lists each address that answers, then done. Nothing between the starter’s range and the clock answers.

    Your task

    A DS3231 real-time clock sits on the I2C bus (SDA on A4, SCL on A5). The scanner tries each address and prints addr= and the number of each one that answers.

    It only tries addresses 1 to 7, so it finds nothing. Fix it so it scans every valid 7-bit address and prints the clock’s.

      Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

      Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

      It is checked for

      • It finds the clock at address 104 (0x68)

      3. Read a register from the DS3231

      An I2C device is a set of numbered registers. To read one, write its number to set the device’s pointer, then request the bytes. The datasheet’s register map says what lives where.

      FormulabeginTransmission(addr); write(reg); endTransmission(); requestFrom(addr, n)

      Worked exampleThe DS3231 keeps minutes in register 0x01. Write 0x01, request one byte, and you get the minutes in BCD.

      On the scope or meterThe serial monitor prints t= every 200 ms. The starter prints the seconds, which count up.

      Your task

      The DS3231 keeps the temperature of its own crystal in register 0x11 (whole degrees, signed) and 0x12 (quarter degrees). Here it reads 25 °C.

      The starter reads register 0x00, the seconds. Fix it to print the temperature in whole degrees as t=.

        Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

        Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

        It is checked for

        • It prints 25 degrees

        4. Latch a byte into a 74HC595 over SPI

        SPI clocks bits out on MOSI, one per SCK edge. A 74HC595 shifts them into an internal register, and only a rising edge on its latch, STCP, copies that register to the outputs, so they all change at once.

        Formulalatch low, transfer the byte, latch high

        Worked exampleTo show 0x0F: latch low, SPI.transfer(0x0F), latch high. Q0 to Q3 go high together on the rising edge.

        On the scope or meterQ0 to Q7 stay as they were while bits shift in, then change all at once when the latch rises.

        Your task

        SPI clocks 0xA5 into a 74HC595: MOSI (pin 11) to DS, SCK (pin 13) to SHCP, and pin 10 to the latch, STCP.

        The starter shifts the byte in but the outputs never change. Fix it so Q0 to Q7 show 0xA5 (165). Note that SPI.begin() drives pin 10, the SS pin, high.

          Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

          Write it here, or paste it from the editor. It is built in your browser with the editor’s compiler, then run in the lab’s own circuit.

          It is checked for

          • Q0 to Q7 show 0xA5 (165)

          Viva questions

          The questions an examiner asks at the bench. 75% passes; every answer comes with why.

          Part of Pro. Read it all here. The trial has no card, and module 1 and the first challenge of every module stay free.

          1. How long does one bit last at 115 200 baud?

          2. What does a UART frame of 8N1 contain?

          3. Why do I2C lines need pull-up resistors?

          4. How many wires does I2C need, besides ground, and how many does SPI need for one device?

          5. What tells an I2C master that a device is at an address?

          6. Which is usually fastest?

          7. What are SPI modes 0 to 3?

          8. Why is a UART called asynchronous?