Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microcontroller basics Embedded systems for mortals.

Similar presentations


Presentation on theme: "Microcontroller basics Embedded systems for mortals."— Presentation transcript:

1 Microcontroller basics Embedded systems for mortals

2 Lesson 3 Serial communication (Devices) I2C communication SPI communication Using a heatsensor

3 Serial communication Point-to-point communication between two devices Requires two wires minimum (RX and TX) Also recommended to connect the ground wires of the devices together TTL (transistor-transistor-logic) and RS232 voltage levels are different!

4 Serial communication

5 Adjustable parameters: Baud rate - Communication speed Data bits - Number of actual data bits in one byte Stop bit - Length of the stop after each sent byte Parity - Used for error checking

6 Serial communication with Teensy Provides two hardware serial communication methods Serial communication over USB Serial over RX and TX pins (2 & 3) – Works using Serial1 -class in Arduino IDE

7 Example 8 - Schematic Using hardware serial with Teensy

8 Example 8 – Breadboard setup Using hardware serial with Teensy

9 Example 8 - Code byte rxPin = 2; void setup() { pinMode(rxPin, INPUT_PULLUP); Serial.begin(9600); Serial1.begin(9600); } void loop() { if(Serial.available()) //If you recieve data over the USB serial { Serial1.write(Serial.read()); //Send it to hardware serial } if(Serial1.available()) //If you recieve data over hardware serial { Serial.write(Serial1.read()); //Send it to USB serial } Using hardware serial with Teensy

10 Arduino C – More Serial functions Serial.available() Serial.write() Returns the number of bytes available in the serial input buffer. Write a single byte into the serial port MORE INFO: http://arduino.cc/en/reference/serial Serial.read() Read a single byte from the input buffer

11 I2C communication Bus type communication (one transmits, multiple receives) All the devices in the bus have a specific 7-bit address Requires 2 wires (SDA & SCL) SDA = Serial Data Line SCL = Serial Clock Line

12 I2C wiring

13 I2C communication with Teensy Works using the Wire -library in Arduino IDE Not easily understandable library

14 Example 9 - Schematic Using I2C with Teensy

15 Example 9 – Breadboard setup Using I2C with Teensy The ADXL345 in the picture is not the one you have! Check the wiring, you have to do your own.

16 Example 9 - Code #define ADXL345_I2CADD 0x53 #include void setup() { Wire.begin(); //Start up the Wire librarys I2C Serial.begin(9600); } void loop() { //Wire writing Wire.beginTransmission(ADXL345_I2CADD); // starts communication to address (0x53) Wire.write(0x00); // sets register pointer to the command register (0x00) Wire.endTransmission(); //Wire reading Wire.beginTransmission(ADXL345_I2CADD); // starts communication to address (0x53) Wire.requestFrom(ADXL345_I2CADD, 1); // requestFrom(address, quantity) while(Wire.available()) { Serial.println(Wire.receive(), BIN); //show the } Wire.endTransmission(); delay(200); } Using I2C with Teensy

17 Example 10 1/2 #define ADXL345_I2CADD 0x53 #include byte accelData[2]; //make a vector with a lenght of 2 void setup() { Wire.begin(); Serial.begin(9600); Wire.beginTransmission(ADXL345_I2CADD); //start transmission to device Wire.write(0x2D); // 0x2D is the Power Control register of the ADXL345 Wire.write(0x08); // send value to write (0x08 = 8), so it goes to Measure mode (datasheet) Wire.endTransmission(); //end transmission } Read acceleration value of X-axis with I2C

18 Example 10 2/2 void loop() { Wire.beginTransmission(ADXL345_I2CADD); Wire.write(0x32); // Go to place in memory where the data acceleration values start, first // two are x-axis. (It needs two bytes for reading 13 bit resolution data) Wire.endTransmission(); Wire.beginTransmission(ADXL345_I2CADD); //begin transmission to 0x53 address Wire.requestFrom(ADXL345_I2CADD, 2); //get two bytes from the adress 0x53 int i = 0; while(Wire.available()) { accelData[i] = Wire.receive(); //write the x-axis acceleration value to the vector i++; } Wire.endTransmission(); i = (int)accelData[1]<<8 | accelData[0]; //left-Bit shifting and bitwise OR two add these //bytes together Serial.println(i); //print the value delay(100); } Read acceleration value of X-axis with I2C

19 SPI communication Bus type communication The desired recipient is select individually with dedicated wire Requires 2 or 3 wires for the communication +1 wire for each device in the bus Slaves send data to master at the same time when master is sending data to them Our example is the 3 wire spi (MISO, MOSI, SCLK pins) Works using the SPI -library in Arduino IDE

20 SPI wiring

21 Example 11: DYI TMP36 Tmp36 is a heatsensor It gives you an analog input signal How can you connect it and read values for it?

22 Example 11: Wiring

23 Example 11: Code int sensorPin = 38; //the analog pin the TMP36's Vout (sense) pin is connected to //the resolution is 10 mV / degree centigrade with a //500 mV offset to allow for negative temperatures void setup(){ Serial.begin(9600); //Start the serial connection } void loop() { int reading = analogRead(sensorPin); //getting the voltage reading from the temperature sensor float voltage = reading * 5.0; // converting that reading to voltage voltage /= 1024.0; Serial.print(voltage); Serial.println(" volts"); // print out the voltage float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset //to degrees ((voltage - 500mV) times 100) Serial.print(temperatureC); Serial.println(" degrees C"); // now print out the temperature (Celsius) delay(1000); //waiting a second }

24 Feedback Answer to the short survey about the µC exercises Bonus reading: http://codeyoung.blogspot.fi/2009/11/adxl345- accelerometer-breakout-board.html ADXL345 accelerometer project for Arduino

25 In next lesson Advanced components in embedded systems PWM and servo motors Stepper motor


Download ppt "Microcontroller basics Embedded systems for mortals."

Similar presentations


Ads by Google