Yucheng Yang
/
xcminilab4
1
main.cpp@0:f123c8d6edd2, 2018-12-11 (annotated)
- Committer:
- C41x140
- Date:
- Tue Dec 11 14:11:22 2018 +0000
- Revision:
- 0:f123c8d6edd2
xiangcong vision1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
C41x140 | 0:f123c8d6edd2 | 1 | // This file is used to set the current time to the buffer |
C41x140 | 0:f123c8d6edd2 | 2 | |
C41x140 | 0:f123c8d6edd2 | 3 | #include "mbed.h" |
C41x140 | 0:f123c8d6edd2 | 4 | #include "N5110.h" |
C41x140 | 0:f123c8d6edd2 | 5 | |
C41x140 | 0:f123c8d6edd2 | 6 | BusOut leds(LED1,LED2,LED3,LED4); |
C41x140 | 0:f123c8d6edd2 | 7 | |
C41x140 | 0:f123c8d6edd2 | 8 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
C41x140 | 0:f123c8d6edd2 | 9 | N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // LPC1768 - pwr from GPIO |
C41x140 | 0:f123c8d6edd2 | 10 | //N5110 lcd(p8,p9,p10,p11,p13,p21); // LPC1768 - powered from +3V3 - JP1 in 2/3 position |
C41x140 | 0:f123c8d6edd2 | 11 | //N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3 |
C41x140 | 0:f123c8d6edd2 | 12 | |
C41x140 | 0:f123c8d6edd2 | 13 | //------------------------TMP102 START----------------- |
C41x140 | 0:f123c8d6edd2 | 14 | // addresses for ADD0 connected to GND |
C41x140 | 0:f123c8d6edd2 | 15 | #define TMP102_ADD 0x48 |
C41x140 | 0:f123c8d6edd2 | 16 | #define TMP102_R_ADD 0x91 |
C41x140 | 0:f123c8d6edd2 | 17 | #define TMP102_W_ADD 0x90 |
C41x140 | 0:f123c8d6edd2 | 18 | |
C41x140 | 0:f123c8d6edd2 | 19 | // register addresses |
C41x140 | 0:f123c8d6edd2 | 20 | #define TEMP_REG 0x00 |
C41x140 | 0:f123c8d6edd2 | 21 | #define CONFIG_REG 0x01 |
C41x140 | 0:f123c8d6edd2 | 22 | #define THIGH_REG 0x02 |
C41x140 | 0:f123c8d6edd2 | 23 | #define TLOW_REG 0x03 |
C41x140 | 0:f123c8d6edd2 | 24 | |
C41x140 | 0:f123c8d6edd2 | 25 | |
C41x140 | 0:f123c8d6edd2 | 26 | I2C tmp102(p28,p27); // SDA, SCL |
C41x140 | 0:f123c8d6edd2 | 27 | |
C41x140 | 0:f123c8d6edd2 | 28 | |
C41x140 | 0:f123c8d6edd2 | 29 | // hang in infinite loop flashing error code |
C41x140 | 0:f123c8d6edd2 | 30 | void error(int code) |
C41x140 | 0:f123c8d6edd2 | 31 | { |
C41x140 | 0:f123c8d6edd2 | 32 | while(1) { |
C41x140 | 0:f123c8d6edd2 | 33 | leds = 0; |
C41x140 | 0:f123c8d6edd2 | 34 | wait(0.25); |
C41x140 | 0:f123c8d6edd2 | 35 | leds = code; |
C41x140 | 0:f123c8d6edd2 | 36 | wait(0.25); |
C41x140 | 0:f123c8d6edd2 | 37 | } |
C41x140 | 0:f123c8d6edd2 | 38 | } |
C41x140 | 0:f123c8d6edd2 | 39 | |
C41x140 | 0:f123c8d6edd2 | 40 | void initTMP102() |
C41x140 | 0:f123c8d6edd2 | 41 | { |
C41x140 | 0:f123c8d6edd2 | 42 | tmp102.frequency(400000); // set bus speed to 400 kHz |
C41x140 | 0:f123c8d6edd2 | 43 | int ack; // used to store acknowledgement bit |
C41x140 | 0:f123c8d6edd2 | 44 | char data[2]; // array for data |
C41x140 | 0:f123c8d6edd2 | 45 | char reg = CONFIG_REG; // register address |
C41x140 | 0:f123c8d6edd2 | 46 | //////// Read current status of configuration register /////// |
C41x140 | 0:f123c8d6edd2 | 47 | ack = tmp102.write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address |
C41x140 | 0:f123c8d6edd2 | 48 | if (ack) |
C41x140 | 0:f123c8d6edd2 | 49 | error(1); // if we don't receive acknowledgement, flash error message |
C41x140 | 0:f123c8d6edd2 | 50 | ack = tmp102.read(TMP102_R_ADD,data,2); // read default 2 bytes from configuration register and store in buffer |
C41x140 | 0:f123c8d6edd2 | 51 | if (ack) |
C41x140 | 0:f123c8d6edd2 | 52 | error(2); // if we don't receive acknowledgement, flash error message |
C41x140 | 0:f123c8d6edd2 | 53 | ///////// Configure the register ////////// |
C41x140 | 0:f123c8d6edd2 | 54 | // set conversion rate to 1 Hz |
C41x140 | 0:f123c8d6edd2 | 55 | data[1] |= (1 << 6); // set bit 6 |
C41x140 | 0:f123c8d6edd2 | 56 | data[1] &= ~(1 << 7); // clear bit 7 |
C41x140 | 0:f123c8d6edd2 | 57 | //////// Send the configured register to the slave //////////// |
C41x140 | 0:f123c8d6edd2 | 58 | char tx_data[3] = {reg,data[0],data[1]}; // create 3-byte packet for writing (p12 datasheet) |
C41x140 | 0:f123c8d6edd2 | 59 | ack = tmp102.write(TMP102_W_ADD,tx_data,3); // send the slave write address, config reg address and contents |
C41x140 | 0:f123c8d6edd2 | 60 | if (ack) |
C41x140 | 0:f123c8d6edd2 | 61 | error(3); // if we don't receive acknowledgement, flash error message |
C41x140 | 0:f123c8d6edd2 | 62 | } |
C41x140 | 0:f123c8d6edd2 | 63 | |
C41x140 | 0:f123c8d6edd2 | 64 | float getTemperature() |
C41x140 | 0:f123c8d6edd2 | 65 | { |
C41x140 | 0:f123c8d6edd2 | 66 | int ack; // used to store acknowledgement bit |
C41x140 | 0:f123c8d6edd2 | 67 | char data[2]; // array for data |
C41x140 | 0:f123c8d6edd2 | 68 | char reg = TEMP_REG; // temperature register address |
C41x140 | 0:f123c8d6edd2 | 69 | ack = tmp102.write(TMP102_W_ADD,®,1); // send temperature register address |
C41x140 | 0:f123c8d6edd2 | 70 | if (ack) |
C41x140 | 0:f123c8d6edd2 | 71 | error(5); // if we don't receive acknowledgement, flash error message |
C41x140 | 0:f123c8d6edd2 | 72 | ack = tmp102.read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array |
C41x140 | 0:f123c8d6edd2 | 73 | if (ack) |
C41x140 | 0:f123c8d6edd2 | 74 | error(6); // if we don't receive acknowledgement, flash error message |
C41x140 | 0:f123c8d6edd2 | 75 | int temperature = (data[0] << 4) | (data[1] >> 4); |
C41x140 | 0:f123c8d6edd2 | 76 | return temperature*0.0625; |
C41x140 | 0:f123c8d6edd2 | 77 | } |
C41x140 | 0:f123c8d6edd2 | 78 | |
C41x140 | 0:f123c8d6edd2 | 79 | //--------------------------TMP102 END----------------------- |
C41x140 | 0:f123c8d6edd2 | 80 | |
C41x140 | 0:f123c8d6edd2 | 81 | void setTime(); // function to set the UNIX time |
C41x140 | 0:f123c8d6edd2 | 82 | |
C41x140 | 0:f123c8d6edd2 | 83 | //--------------------------set time----------------------- |
C41x140 | 0:f123c8d6edd2 | 84 | void setTime() { |
C41x140 | 0:f123c8d6edd2 | 85 | int year=2018; |
C41x140 | 0:f123c8d6edd2 | 86 | int mon=12; |
C41x140 | 0:f123c8d6edd2 | 87 | int day=11; |
C41x140 | 0:f123c8d6edd2 | 88 | int hour=14; |
C41x140 | 0:f123c8d6edd2 | 89 | int min=0; |
C41x140 | 0:f123c8d6edd2 | 90 | int sec=0; |
C41x140 | 0:f123c8d6edd2 | 91 | |
C41x140 | 0:f123c8d6edd2 | 92 | struct tm timeinfo; |
C41x140 | 0:f123c8d6edd2 | 93 | timeinfo.tm_year=year-1900; |
C41x140 | 0:f123c8d6edd2 | 94 | timeinfo.tm_mon=mon-1; |
C41x140 | 0:f123c8d6edd2 | 95 | timeinfo.tm_mday=day; |
C41x140 | 0:f123c8d6edd2 | 96 | timeinfo.tm_hour=hour; |
C41x140 | 0:f123c8d6edd2 | 97 | timeinfo.tm_min=min; |
C41x140 | 0:f123c8d6edd2 | 98 | timeinfo.tm_sec=sec; |
C41x140 | 0:f123c8d6edd2 | 99 | |
C41x140 | 0:f123c8d6edd2 | 100 | // update the time |
C41x140 | 0:f123c8d6edd2 | 101 | time_t current=mktime(&timeinfo); |
C41x140 | 0:f123c8d6edd2 | 102 | set_time(current); |
C41x140 | 0:f123c8d6edd2 | 103 | } |
C41x140 | 0:f123c8d6edd2 | 104 | //-------------------------set time end-------------------- |
C41x140 | 0:f123c8d6edd2 | 105 | |
C41x140 | 0:f123c8d6edd2 | 106 | int main() |
C41x140 | 0:f123c8d6edd2 | 107 | { |
C41x140 | 0:f123c8d6edd2 | 108 | |
C41x140 | 0:f123c8d6edd2 | 109 | initTMP102(); // initial TMP102 |
C41x140 | 0:f123c8d6edd2 | 110 | |
C41x140 | 0:f123c8d6edd2 | 111 | // first need to initialise display |
C41x140 | 0:f123c8d6edd2 | 112 | lcd.init(); |
C41x140 | 0:f123c8d6edd2 | 113 | |
C41x140 | 0:f123c8d6edd2 | 114 | // change set contrast in range 0.0 to 1.0 |
C41x140 | 0:f123c8d6edd2 | 115 | // 0.5 appears to be a good starting point |
C41x140 | 0:f123c8d6edd2 | 116 | lcd.setContrast(0.5); |
C41x140 | 0:f123c8d6edd2 | 117 | |
C41x140 | 0:f123c8d6edd2 | 118 | |
C41x140 | 0:f123c8d6edd2 | 119 | char buffert[30]; // buffer used to store time string |
C41x140 | 0:f123c8d6edd2 | 120 | char bufferd[30]; // buffer used to store date string |
C41x140 | 0:f123c8d6edd2 | 121 | |
C41x140 | 0:f123c8d6edd2 | 122 | |
C41x140 | 0:f123c8d6edd2 | 123 | setTime(); // initialise time to 1st January 1970 |
C41x140 | 0:f123c8d6edd2 | 124 | |
C41x140 | 0:f123c8d6edd2 | 125 | |
C41x140 | 0:f123c8d6edd2 | 126 | |
C41x140 | 0:f123c8d6edd2 | 127 | |
C41x140 | 0:f123c8d6edd2 | 128 | while(1) { |
C41x140 | 0:f123c8d6edd2 | 129 | |
C41x140 | 0:f123c8d6edd2 | 130 | // these are default settings so not strictly needed |
C41x140 | 0:f123c8d6edd2 | 131 | lcd.normalMode(); // normal colour mode |
C41x140 | 0:f123c8d6edd2 | 132 | lcd.setBrightness(0.5); // put LED backlight on 50% |
C41x140 | 0:f123c8d6edd2 | 133 | |
C41x140 | 0:f123c8d6edd2 | 134 | lcd.clear(); |
C41x140 | 0:f123c8d6edd2 | 135 | |
C41x140 | 0:f123c8d6edd2 | 136 | time_t seconds = time(NULL); // get current time |
C41x140 | 0:f123c8d6edd2 | 137 | // format time into a string (time and date) |
C41x140 | 0:f123c8d6edd2 | 138 | strftime(buffert, 30 , "%X", localtime(&seconds)); |
C41x140 | 0:f123c8d6edd2 | 139 | strftime(bufferd, 30 , "%F", localtime(&seconds)); |
C41x140 | 0:f123c8d6edd2 | 140 | // print over serial |
C41x140 | 0:f123c8d6edd2 | 141 | |
C41x140 | 0:f123c8d6edd2 | 142 | wait(1.0); // delay for a second |
C41x140 | 0:f123c8d6edd2 | 143 | |
C41x140 | 0:f123c8d6edd2 | 144 | // clear buffer at start of every loop |
C41x140 | 0:f123c8d6edd2 | 145 | // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) |
C41x140 | 0:f123c8d6edd2 | 146 | lcd.printString("Hello, Xiang!",0,0); |
C41x140 | 0:f123c8d6edd2 | 147 | |
C41x140 | 0:f123c8d6edd2 | 148 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
C41x140 | 0:f123c8d6edd2 | 149 | // so can display a string of a maximum 14 characters in length |
C41x140 | 0:f123c8d6edd2 | 150 | // or create formatted strings - ensure they aren't more than 14 characters long |
C41x140 | 0:f123c8d6edd2 | 151 | |
C41x140 | 0:f123c8d6edd2 | 152 | float temperature = getTemperature(); // temperature from TMP102 |
C41x140 | 0:f123c8d6edd2 | 153 | int temp = ceil(temperature); |
C41x140 | 0:f123c8d6edd2 | 154 | //int temp_test = 27; |
C41x140 | 0:f123c8d6edd2 | 155 | |
C41x140 | 0:f123c8d6edd2 | 156 | //int length = sprintf(buffer,"T = %d C",temp_test); // print formatted data to buffer |
C41x140 | 0:f123c8d6edd2 | 157 | int length = sprintf(buffer,"T = %2d C",temp); // print formatted data to buffer |
C41x140 | 0:f123c8d6edd2 | 158 | // it is important the format specifier ensures the length will fit in the buffer |
C41x140 | 0:f123c8d6edd2 | 159 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
C41x140 | 0:f123c8d6edd2 | 160 | lcd.printString(buffer,0,1); // display on screen |
C41x140 | 0:f123c8d6edd2 | 161 | |
C41x140 | 0:f123c8d6edd2 | 162 | lcd.printString(buffert, 0, 2); |
C41x140 | 0:f123c8d6edd2 | 163 | lcd.printString(bufferd, 0, 3); |
C41x140 | 0:f123c8d6edd2 | 164 | |
C41x140 | 0:f123c8d6edd2 | 165 | lcd.refresh(); |
C41x140 | 0:f123c8d6edd2 | 166 | } |
C41x140 | 0:f123c8d6edd2 | 167 | } |