APP 1 S5.

Dependencies:   mbed

Committer:
vinbel93
Date:
Tue Jan 12 15:24:02 2016 +0000
Revision:
14:d48d869ed365
Parent:
13:08ef55cd14c6
UART with mbed.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vinbel93 11:eafa5c07f060 1 // APP.cpp
vinbel93 11:eafa5c07f060 2 // Vincent Belanger - belv1802
vinbel93 11:eafa5c07f060 3 // Jeremy Pare - parj2713
vinbel93 11:eafa5c07f060 4
vinbel93 7:b00843da0530 5 #include <math.h>
vinbel93 7:b00843da0530 6 #include <stdint.h>
vinbel93 7:b00843da0530 7
vinbel93 7:b00843da0530 8 #include "mbed.h"
vinbel93 7:b00843da0530 9 #include "APP.h"
vinbel93 7:b00843da0530 10
vinbel93 7:b00843da0530 11
vinbel93 14:d48d869ed365 12 static I2C i2c(p28, p27);
vinbel93 14:d48d869ed365 13 static Serial uart(p9, p10);
vinbel93 7:b00843da0530 14
vinbel93 7:b00843da0530 15 void setup()
vinbel93 7:b00843da0530 16 {
vinbel93 7:b00843da0530 17 // Setup UART and I2C.
vinbel93 14:d48d869ed365 18 uart.format(8, Serial::None, 1);
vinbel93 14:d48d869ed365 19 uart.baud(9600);
vinbel93 7:b00843da0530 20 i2c.frequency(100000);
vinbel93 14:d48d869ed365 21
vinbel93 7:b00843da0530 22 // Clear display
vinbel93 14:d48d869ed365 23 uart.putc(0x76);
vinbel93 14:d48d869ed365 24
vinbel93 7:b00843da0530 25 // Decimal point for display
vinbel93 14:d48d869ed365 26 uart.putc(0x77);
vinbel93 14:d48d869ed365 27 uart.putc(0x02);
vinbel93 7:b00843da0530 28
vinbel93 7:b00843da0530 29 // Maximum brightness
vinbel93 14:d48d869ed365 30 uart.putc(0x7A);
vinbel93 14:d48d869ed365 31 uart.putc(0xFF);
vinbel93 7:b00843da0530 32 }
vinbel93 14:d48d869ed365 33
vinbel93 7:b00843da0530 34 void activateAccelerometer()
vinbel93 7:b00843da0530 35 {
vinbel93 7:b00843da0530 36 uint8_t data[2] = {CTRL_REG, 0x01};
vinbel93 7:b00843da0530 37 i2c.write(ACC_ADDRESS, (char*) data, 2);
vinbel93 7:b00843da0530 38 }
vinbel93 14:d48d869ed365 39
vinbel93 7:b00843da0530 40 uint8_t readStatus()
vinbel93 7:b00843da0530 41 {
vinbel93 7:b00843da0530 42 uint8_t result[1];
vinbel93 7:b00843da0530 43 uint8_t reg[1] = {STATUS_REG};
vinbel93 7:b00843da0530 44 i2c.write(ACC_ADDRESS, (char*) reg, 1, true);
vinbel93 7:b00843da0530 45 i2c.read(ACC_ADDRESS, (char*) result, 1);
vinbel93 7:b00843da0530 46
vinbel93 7:b00843da0530 47 return result[0];
vinbel93 7:b00843da0530 48 }
vinbel93 14:d48d869ed365 49
vinbel93 7:b00843da0530 50 int16_t convertToAcceleration(uint8_t hi, uint8_t lo)
vinbel93 7:b00843da0530 51 {
vinbel93 7:b00843da0530 52 int16_t value = (lo >> 2) | (hi << 6);
vinbel93 7:b00843da0530 53 if (value > UINT14_MAX/2)
vinbel93 7:b00843da0530 54 {
vinbel93 7:b00843da0530 55 value -= UINT14_MAX;
vinbel93 7:b00843da0530 56 }
vinbel93 7:b00843da0530 57
vinbel93 7:b00843da0530 58 return value;
vinbel93 7:b00843da0530 59 }
vinbel93 14:d48d869ed365 60
vinbel93 7:b00843da0530 61 int16_t readAndComputeAngle()
vinbel93 7:b00843da0530 62 {
vinbel93 7:b00843da0530 63 uint8_t result[6];
vinbel93 7:b00843da0530 64 uint8_t reg[1] = {ACC_X_REG};
vinbel93 7:b00843da0530 65 i2c.write(ACC_ADDRESS, (char*) reg, 1, true);
vinbel93 7:b00843da0530 66 i2c.read(ACC_ADDRESS, (char*) result, 6);
vinbel93 7:b00843da0530 67 int16_t accX = convertToAcceleration(result[0], result[1]);
vinbel93 7:b00843da0530 68 int16_t accY = convertToAcceleration(result[2], result[3]);
vinbel93 7:b00843da0530 69 int16_t accZ = convertToAcceleration(result[4], result[5]);
vinbel93 12:4961ec71bc8f 70
vinbel93 12:4961ec71bc8f 71 float angle = acos((float) accZ / sqrt((float) accX*accX + accY*accY + accZ*accZ)) * 180.0 / PI;
vinbel93 7:b00843da0530 72
vinbel93 7:b00843da0530 73 // To use integer operations instead of floating-point arithmetic, the angle is multiplied by 100.
vinbel93 7:b00843da0530 74 // This allows for two decimals of precision on the display.
vinbel93 7:b00843da0530 75 int16_t angleHundreds = angle * 100;
vinbel93 7:b00843da0530 76 if (angleHundreds > 9000)
vinbel93 7:b00843da0530 77 {
vinbel93 7:b00843da0530 78 angleHundreds = 18000 - angleHundreds;
vinbel93 7:b00843da0530 79 }
vinbel93 7:b00843da0530 80
vinbel93 7:b00843da0530 81 return angleHundreds;
vinbel93 7:b00843da0530 82 }
vinbel93 7:b00843da0530 83
vinbel93 7:b00843da0530 84 void updateDisplay(int16_t angle)
vinbel93 7:b00843da0530 85 {
vinbel93 7:b00843da0530 86 uint8_t digits[4];
vinbel93 7:b00843da0530 87 sprintf((char*) digits, "%d", angle);
vinbel93 14:d48d869ed365 88
vinbel93 12:4961ec71bc8f 89 if (angle < 100)
vinbel93 12:4961ec71bc8f 90 {
vinbel93 14:d48d869ed365 91 uart.putc('0');
vinbel93 14:d48d869ed365 92 uart.putc('0');
vinbel93 14:d48d869ed365 93 uart.putc(digits[0]);
vinbel93 14:d48d869ed365 94 uart.putc(digits[1]);
vinbel93 12:4961ec71bc8f 95 }
vinbel93 12:4961ec71bc8f 96 else if (angle < 1000)
vinbel93 7:b00843da0530 97 {
vinbel93 14:d48d869ed365 98 uart.putc('0');
vinbel93 14:d48d869ed365 99 uart.putc(digits[0]);
vinbel93 14:d48d869ed365 100 uart.putc(digits[1]);
vinbel93 14:d48d869ed365 101 uart.putc(digits[2]);
vinbel93 7:b00843da0530 102 }
vinbel93 7:b00843da0530 103 else
vinbel93 7:b00843da0530 104 {
vinbel93 14:d48d869ed365 105 uart.putc(digits[0]);
vinbel93 14:d48d869ed365 106 uart.putc(digits[1]);
vinbel93 14:d48d869ed365 107 uart.putc(digits[2]);
vinbel93 14:d48d869ed365 108 uart.putc(digits[3]);
vinbel93 7:b00843da0530 109 }
vinbel93 7:b00843da0530 110 }
vinbel93 7:b00843da0530 111
vinbel93 7:b00843da0530 112 int32_t main()
vinbel93 7:b00843da0530 113 {
vinbel93 7:b00843da0530 114 setup();
vinbel93 7:b00843da0530 115 activateAccelerometer();
vinbel93 7:b00843da0530 116
vinbel93 7:b00843da0530 117 while (true)
vinbel93 7:b00843da0530 118 {
vinbel93 7:b00843da0530 119 // Read bit 3 from the status register
vinbel93 7:b00843da0530 120 int8_t status = (readStatus() >> 3) & 0x01;
vinbel93 7:b00843da0530 121
vinbel93 7:b00843da0530 122 if (status == 1)
vinbel93 7:b00843da0530 123 {
vinbel93 7:b00843da0530 124 // Get accelerations, compute angle
vinbel93 7:b00843da0530 125 uint16_t angle = readAndComputeAngle();
vinbel93 14:d48d869ed365 126
vinbel93 7:b00843da0530 127 // Update 7 segment display
vinbel93 7:b00843da0530 128 updateDisplay(angle);
vinbel93 7:b00843da0530 129 }
vinbel93 14:d48d869ed365 130
vinbel93 7:b00843da0530 131 wait(0.4);
vinbel93 7:b00843da0530 132 }
vinbel93 7:b00843da0530 133 }