Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

Committer:
el15pjt
Date:
Fri Mar 18 15:04:05 2016 +0000
Revision:
0:0b180348c7e4
Child:
1:5b991c2302e1
Basic;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15pjt 0:0b180348c7e4 1 /*
el15pjt 0:0b180348c7e4 2 Philip Thompsonn
el15pjt 0:0b180348c7e4 3 EL15PJT
el15pjt 0:0b180348c7e4 4 Embedded System Project
el15pjt 0:0b180348c7e4 5 EL2645
el15pjt 0:0b180348c7e4 6
el15pjt 0:0b180348c7e4 7
el15pjt 0:0b180348c7e4 8 Have added the N5110 Libary and set up a a basic disply of the Range reading
el15pjt 0:0b180348c7e4 9 as well as added LED indictors to range
el15pjt 0:0b180348c7e4 10 */
el15pjt 0:0b180348c7e4 11
el15pjt 0:0b180348c7e4 12
el15pjt 0:0b180348c7e4 13 #include "mbed.h"
el15pjt 0:0b180348c7e4 14 #include "SRF02.h"
el15pjt 0:0b180348c7e4 15 #include "N5110.h"
el15pjt 0:0b180348c7e4 16
el15pjt 0:0b180348c7e4 17 BusOut output(PTB2,PTB3,PTB10,PTB11,PTC11,PTC10);
el15pjt 0:0b180348c7e4 18
el15pjt 0:0b180348c7e4 19 // Ranger object
el15pjt 0:0b180348c7e4 20 SRF02 srf02(I2C_SDA,I2C_SCL);
el15pjt 0:0b180348c7e4 21
el15pjt 0:0b180348c7e4 22 // UART connection for PC
el15pjt 0:0b180348c7e4 23 Serial pc(USBTX,USBRX);
el15pjt 0:0b180348c7e4 24
el15pjt 0:0b180348c7e4 25 //N5110 Object VCC, SCE,RST, D/C, MOSI,SCLK, LED
el15pjt 0:0b180348c7e4 26 N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
el15pjt 0:0b180348c7e4 27
el15pjt 0:0b180348c7e4 28 // K64F on-board LEDs
el15pjt 0:0b180348c7e4 29 DigitalOut r_led(LED_RED);
el15pjt 0:0b180348c7e4 30 DigitalOut g_led(LED_GREEN);
el15pjt 0:0b180348c7e4 31 DigitalOut b_led(LED_BLUE);
el15pjt 0:0b180348c7e4 32
el15pjt 0:0b180348c7e4 33 // K64F on-board switches
el15pjt 0:0b180348c7e4 34 InterruptIn sw2(SW2);
el15pjt 0:0b180348c7e4 35 InterruptIn sw3(SW3);
el15pjt 0:0b180348c7e4 36
el15pjt 0:0b180348c7e4 37 int distance;
el15pjt 0:0b180348c7e4 38 int length;
el15pjt 0:0b180348c7e4 39
el15pjt 0:0b180348c7e4 40 // setup serial port
el15pjt 0:0b180348c7e4 41 void init_serial();
el15pjt 0:0b180348c7e4 42 // set up board
el15pjt 0:0b180348c7e4 43 void init_K64F();
el15pjt 0:0b180348c7e4 44
el15pjt 0:0b180348c7e4 45 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el15pjt 0:0b180348c7e4 46
el15pjt 0:0b180348c7e4 47 int main()
el15pjt 0:0b180348c7e4 48 {
el15pjt 0:0b180348c7e4 49 lcd.init();
el15pjt 0:0b180348c7e4 50 init_serial();
el15pjt 0:0b180348c7e4 51 init_K64F();
el15pjt 0:0b180348c7e4 52 while(1) {
el15pjt 0:0b180348c7e4 53
el15pjt 0:0b180348c7e4 54 // read sensor distance in cm and print over serial port
el15pjt 0:0b180348c7e4 55 int distance = srf02.getDistanceCm();
el15pjt 0:0b180348c7e4 56 //serial link reading of range
el15pjt 0:0b180348c7e4 57 pc.printf("Distance = %i Cm\n",distance);
el15pjt 0:0b180348c7e4 58 // short delay before next measurement
el15pjt 0:0b180348c7e4 59 wait(0.25);
el15pjt 0:0b180348c7e4 60
el15pjt 0:0b180348c7e4 61 length = sprintf(buffer,"D = %i Cm",distance);
el15pjt 0:0b180348c7e4 62
el15pjt 0:0b180348c7e4 63 if (length <= 14)
el15pjt 0:0b180348c7e4 64
el15pjt 0:0b180348c7e4 65 lcd.printString(buffer,0,2);
el15pjt 0:0b180348c7e4 66 // need to refresh display after setting pixels
el15pjt 0:0b180348c7e4 67 lcd.refresh();
el15pjt 0:0b180348c7e4 68
el15pjt 0:0b180348c7e4 69 //LED range indicator runn of the bus out
el15pjt 0:0b180348c7e4 70 if (distance >= 150) {
el15pjt 0:0b180348c7e4 71 output = 3;
el15pjt 0:0b180348c7e4 72 }
el15pjt 0:0b180348c7e4 73
el15pjt 0:0b180348c7e4 74 else if (100 <= distance < 150) {
el15pjt 0:0b180348c7e4 75 output = 15;
el15pjt 0:0b180348c7e4 76 }
el15pjt 0:0b180348c7e4 77
el15pjt 0:0b180348c7e4 78 else if (distance <= 100) {
el15pjt 0:0b180348c7e4 79 output = 67;
el15pjt 0:0b180348c7e4 80 }
el15pjt 0:0b180348c7e4 81
el15pjt 0:0b180348c7e4 82 else {
el15pjt 0:0b180348c7e4 83 output = 0
el15pjt 0:0b180348c7e4 84
el15pjt 0:0b180348c7e4 85 }
el15pjt 0:0b180348c7e4 86 }
el15pjt 0:0b180348c7e4 87 }
el15pjt 0:0b180348c7e4 88
el15pjt 0:0b180348c7e4 89 // Used to return a range readig thru a serial connection
el15pjt 0:0b180348c7e4 90 void init_serial()
el15pjt 0:0b180348c7e4 91 {
el15pjt 0:0b180348c7e4 92 // set to highest baud - ensure terminal software matches
el15pjt 0:0b180348c7e4 93 pc.baud(115200);
el15pjt 0:0b180348c7e4 94 }
el15pjt 0:0b180348c7e4 95 //Set up board switches and LEDS
el15pjt 0:0b180348c7e4 96 void init_K64F()
el15pjt 0:0b180348c7e4 97 {
el15pjt 0:0b180348c7e4 98 //on-board LEDs are active-low, so set pin high to turn them off.
el15pjt 0:0b180348c7e4 99 r_led = 1;
el15pjt 0:0b180348c7e4 100 g_led = 1;
el15pjt 0:0b180348c7e4 101 b_led = 1;
el15pjt 0:0b180348c7e4 102
el15pjt 0:0b180348c7e4 103 // since the on-board switches have external pull-ups, we should disable the internal pull-down
el15pjt 0:0b180348c7e4 104 // resistors that are enabled by default using InterruptIn
el15pjt 0:0b180348c7e4 105 sw2.mode(PullNone);
el15pjt 0:0b180348c7e4 106 sw3.mode(PullNone);
el15pjt 0:0b180348c7e4 107 }