Lesson 19 – I2C & SPI

 


Setting up our environment –

Follow each of these steps to set up the Kittenbot Meowbit for programming with MicroPython.

  1. Connect up the Kittenbot Meowbit to your laptop using a USB cable
  2. Power on the Kittenbot Meowbit by sliding the power button at the top
  3. Click the reset button once so that you get into the bootloader mode
  4. Refer to the following article if you are unclear about what bootloader mode is – Article@OzToyLib
  5. You should now see your Kittenbot Meowbit as an additional drive on your laptop
  6. Let’s install the Python firmware onto our Meowbit.
    1. Drag and drop this file onto the new drive – <Python Bootloader>
    2. Hit reset twice and you should see the Kittenbot Meowbit boot into Python.
    3. You should see the following message on your Meowbit, “Welcome to MicroPython”
  7. You should now see your Kittenbot Meowbit as an additional drive on your laptop
  8. Download and install the Python mu editor from – <Kittenbot software>
  9. Edit the “main.py” file and let’s start writing our code

If you’ve already got the Python firmware i.e. you’ve performed step 1 – 9 earlier, skip step 1 – 7 and hit the reset button twice (with 5 seconds gap) to enter into the Python mode.


Let’s get coding

See code below for I2C.

 

from pyb import I2C

i2c = I2C(1)

Here’s the code for SPI.

 

from pyb import SPI

spi = SPI(1)  #创建一个SPI对象,如果没有没有附加参数,则SP1对象创建但未初始化

SPI.init(SPI.MASTER, baudrate = 600000, polarity = 1, crc = 0x7) #(mode, baudrate=328125, *, prescaler, polarity=1, phase=0, bits=8, firstbit=SPI.MSB, ti=False, crc=None)

data = spi.send_recv(b'1234') #发送4个字节和接收4个字节
buf = bytearray(4)
spi.send_recv(b'1234', buf)   #发送4个字节和接收4个字节存储到buf


Prerequisites –

Here are the hardware prerequisites for this course –

  1. This tutorial makes extensive use of the Kittenbot Meowbit.
  2. If you haven’t purchased a Kittenbot Meowbit yet you might want to head over and pick one up now.
  3. We also recommend picking up a Kittenbot IoBit for working on electronics projects and a Kittenbot Robobit to be able to create a programmable robot.

About the Programming Games with the Kittenbot Meowbit development track –

As part of this development track you will learn to work with the Kittenbot Meowbit board and learn how to create retro style games and even play those games on the Kittenbot Meowbit. You will program the games using Microsoft Makecode Arcade (https://arcade.makecode.com/). Microsoft Makecode Arcade is currently in Beta but has support for a number of different boards including the Kittenbot Meowbit. We will program games for the Kittenbot Meowbit using block based coding platform and also using Javascript.

Here’s the outcomes we will be working towards as part of this development track.

  1. Explore the fundamentals of computational thinking
  2. Explore fundamentals of programming i.e. program structure, decisions, loops, variables, etc.
  3. Learn how to design, create games using Makecode Arcade
  4. Learn how to program retro style games using a block based programming approach
  5. Learn how to program retro style games using Javascript
  6. Learn Learn to work with the Kittenbot Meowbit, program the board with custom games and play them when not connected to the computer/internet

About the Kittenbot Meowbit Gaming & Development board –

The Meowbit is a card-sized graphical retro game computer with allows you coding with Makecode arcade and Python. In other words, it can combines game programming, design while presenting learners the opportunity to learn mainstream programming languages like micropython. The Meowbit is a card-sized graphical programming video game console designed for kids of all ages.

The Kittenbot Meowbit comes with a 1.8′ full-color screen, 6 x programmable buttons, 1 x buzzer, built-in light sensor, temperature sensor, SD card slot (For external storage), multiplayer connector and edge connector. A special feature of the Meowbit is the in-built edge connector which should (**no extensive testing or certification of other 3rd party expansion boards has been conducted) be compatible with most micro:bit expansion boards out there. When combined with the Kittenbot Robot:bit board you have the ability to create a programmable versatile robot in a very short time. The Kittenbot Meowbit can also be used with the Kittenbot IoBit to create different electronics projects.

If you haven’t purchased a Kittenbot Meowbit yet you might want to head over and pick one up now.

Questions