CatPot 2015-2016
/
Pr034_S11059
S11059-02DT
main.cpp@0:e2155c63b32b, 2015-04-10 (annotated)
- Committer:
- lilac0112_1
- Date:
- Fri Apr 10 14:17:59 2015 +0000
- Revision:
- 0:e2155c63b32b
- Child:
- 1:eabfa27c9374
false
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lilac0112_1 | 0:e2155c63b32b | 1 | #include "mbed.h" |
lilac0112_1 | 0:e2155c63b32b | 2 | |
lilac0112_1 | 0:e2155c63b32b | 3 | DigitalOut Led(LED1); |
lilac0112_1 | 0:e2155c63b32b | 4 | DigitalOut Val(LED2); |
lilac0112_1 | 0:e2155c63b32b | 5 | Serial pc(USBTX,USBRX); |
lilac0112_1 | 0:e2155c63b32b | 6 | I2C Sensor(p9,p10); |
lilac0112_1 | 0:e2155c63b32b | 7 | |
lilac0112_1 | 0:e2155c63b32b | 8 | #define DEVICE_ADDRESS 0x2A |
lilac0112_1 | 0:e2155c63b32b | 9 | #define CONTROL_REG 0x00 |
lilac0112_1 | 0:e2155c63b32b | 10 | #define TIMING_REG_H 0x01 |
lilac0112_1 | 0:e2155c63b32b | 11 | #define TIMING_REG_L 0x02 |
lilac0112_1 | 0:e2155c63b32b | 12 | |
lilac0112_1 | 0:e2155c63b32b | 13 | uint16_t red = 0; |
lilac0112_1 | 0:e2155c63b32b | 14 | uint16_t green = 0; |
lilac0112_1 | 0:e2155c63b32b | 15 | uint16_t blue = 0; |
lilac0112_1 | 0:e2155c63b32b | 16 | uint16_t IR = 0; |
lilac0112_1 | 0:e2155c63b32b | 17 | |
lilac0112_1 | 0:e2155c63b32b | 18 | char address=DEVICE_ADDRESS; |
lilac0112_1 | 0:e2155c63b32b | 19 | char cmd[3]={ |
lilac0112_1 | 0:e2155c63b32b | 20 | CONTROL_REG |
lilac0112_1 | 0:e2155c63b32b | 21 | ,TIMING_REG_H |
lilac0112_1 | 0:e2155c63b32b | 22 | ,TIMING_REG_L |
lilac0112_1 | 0:e2155c63b32b | 23 | }; |
lilac0112_1 | 0:e2155c63b32b | 24 | char ord[2]={}; |
lilac0112_1 | 0:e2155c63b32b | 25 | char data[2]={}; |
lilac0112_1 | 0:e2155c63b32b | 26 | char data_h[1]={}; |
lilac0112_1 | 0:e2155c63b32b | 27 | char data_l[1]={}; |
lilac0112_1 | 0:e2155c63b32b | 28 | |
lilac0112_1 | 0:e2155c63b32b | 29 | void led_blink(){ |
lilac0112_1 | 0:e2155c63b32b | 30 | Led=1; |
lilac0112_1 | 0:e2155c63b32b | 31 | wait_ms(10); |
lilac0112_1 | 0:e2155c63b32b | 32 | Led=0; |
lilac0112_1 | 0:e2155c63b32b | 33 | } |
lilac0112_1 | 0:e2155c63b32b | 34 | |
lilac0112_1 | 0:e2155c63b32b | 35 | void setManualtGain(uint16_t ex_time) |
lilac0112_1 | 0:e2155c63b32b | 36 | { |
lilac0112_1 | 0:e2155c63b32b | 37 | char reg[1]={0}; |
lilac0112_1 | 0:e2155c63b32b | 38 | |
lilac0112_1 | 0:e2155c63b32b | 39 | reg[0] =(ex_time >>= 8); |
lilac0112_1 | 0:e2155c63b32b | 40 | Sensor.write(address, reg, 1); |
lilac0112_1 | 0:e2155c63b32b | 41 | reg[0] |= ex_time; |
lilac0112_1 | 0:e2155c63b32b | 42 | Sensor.write(address, reg, 1); |
lilac0112_1 | 0:e2155c63b32b | 43 | } |
lilac0112_1 | 0:e2155c63b32b | 44 | |
lilac0112_1 | 0:e2155c63b32b | 45 | void getRGB() |
lilac0112_1 | 0:e2155c63b32b | 46 | { |
lilac0112_1 | 0:e2155c63b32b | 47 | |
lilac0112_1 | 0:e2155c63b32b | 48 | /* //manual exposure mode |
lilac0112_1 | 0:e2155c63b32b | 49 | |
lilac0112_1 | 0:e2155c63b32b | 50 | setManualtGain(100); |
lilac0112_1 | 0:e2155c63b32b | 51 | |
lilac0112_1 | 0:e2155c63b32b | 52 | ord[0]=cmd[0]; |
lilac0112_1 | 0:e2155c63b32b | 53 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 54 | |
lilac0112_1 | 0:e2155c63b32b | 55 | ord[0]=0x84;// ADC reset LowGain wakeup |
lilac0112_1 | 0:e2155c63b32b | 56 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 57 | |
lilac0112_1 | 0:e2155c63b32b | 58 | ord[0]=cmd[0]; |
lilac0112_1 | 0:e2155c63b32b | 59 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 60 | |
lilac0112_1 | 0:e2155c63b32b | 61 | ord[0]=0x04;//start |
lilac0112_1 | 0:e2155c63b32b | 62 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 63 | |
lilac0112_1 | 0:e2155c63b32b | 64 | */ |
lilac0112_1 | 0:e2155c63b32b | 65 | |
lilac0112_1 | 0:e2155c63b32b | 66 | //preset gain mode exposure time (each color ch) |
lilac0112_1 | 0:e2155c63b32b | 67 | // 00 = 87.5us |
lilac0112_1 | 0:e2155c63b32b | 68 | // 01= 1.4ms |
lilac0112_1 | 0:e2155c63b32b | 69 | // 10= 22.4ms |
lilac0112_1 | 0:e2155c63b32b | 70 | // 11= 179.2ms |
lilac0112_1 | 0:e2155c63b32b | 71 | |
lilac0112_1 | 0:e2155c63b32b | 72 | |
lilac0112_1 | 0:e2155c63b32b | 73 | Sensor.start(); |
lilac0112_1 | 0:e2155c63b32b | 74 | |
lilac0112_1 | 0:e2155c63b32b | 75 | ord[0]=0x00; |
lilac0112_1 | 0:e2155c63b32b | 76 | ord[1]=0x84; |
lilac0112_1 | 0:e2155c63b32b | 77 | Val = !Sensor.write(address&0xFE, ord, 2, true); |
lilac0112_1 | 0:e2155c63b32b | 78 | ord[0]=0x00; |
lilac0112_1 | 0:e2155c63b32b | 79 | ord[1]=0x04; |
lilac0112_1 | 0:e2155c63b32b | 80 | Val = !Sensor.write(address&0xFE, ord, 2, false); |
lilac0112_1 | 0:e2155c63b32b | 81 | |
lilac0112_1 | 0:e2155c63b32b | 82 | |
lilac0112_1 | 0:e2155c63b32b | 83 | wait_ms(180*4); //total exposure time |
lilac0112_1 | 0:e2155c63b32b | 84 | |
lilac0112_1 | 0:e2155c63b32b | 85 | led_blink(); |
lilac0112_1 | 0:e2155c63b32b | 86 | |
lilac0112_1 | 0:e2155c63b32b | 87 | |
lilac0112_1 | 0:e2155c63b32b | 88 | Sensor.start(); |
lilac0112_1 | 0:e2155c63b32b | 89 | |
lilac0112_1 | 0:e2155c63b32b | 90 | ord[0]=0x03; |
lilac0112_1 | 0:e2155c63b32b | 91 | Val = !Sensor.write(address&0xFE, ord, 1, true); |
lilac0112_1 | 0:e2155c63b32b | 92 | |
lilac0112_1 | 0:e2155c63b32b | 93 | Val = !Sensor.write(address|0x01); |
lilac0112_1 | 0:e2155c63b32b | 94 | |
lilac0112_1 | 0:e2155c63b32b | 95 | //read data |
lilac0112_1 | 0:e2155c63b32b | 96 | |
lilac0112_1 | 0:e2155c63b32b | 97 | data_h[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 98 | data_l[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 99 | |
lilac0112_1 | 0:e2155c63b32b | 100 | red = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); |
lilac0112_1 | 0:e2155c63b32b | 101 | |
lilac0112_1 | 0:e2155c63b32b | 102 | data_h[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 103 | data_l[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 104 | |
lilac0112_1 | 0:e2155c63b32b | 105 | green = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); |
lilac0112_1 | 0:e2155c63b32b | 106 | |
lilac0112_1 | 0:e2155c63b32b | 107 | data_h[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 108 | data_l[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 109 | |
lilac0112_1 | 0:e2155c63b32b | 110 | blue = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); |
lilac0112_1 | 0:e2155c63b32b | 111 | |
lilac0112_1 | 0:e2155c63b32b | 112 | data_h[0] = Sensor.read(false); |
lilac0112_1 | 0:e2155c63b32b | 113 | data_l[0] = Sensor.read(true); |
lilac0112_1 | 0:e2155c63b32b | 114 | |
lilac0112_1 | 0:e2155c63b32b | 115 | IR = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); |
lilac0112_1 | 0:e2155c63b32b | 116 | |
lilac0112_1 | 0:e2155c63b32b | 117 | Sensor.stop(); |
lilac0112_1 | 0:e2155c63b32b | 118 | |
lilac0112_1 | 0:e2155c63b32b | 119 | return; |
lilac0112_1 | 0:e2155c63b32b | 120 | } |
lilac0112_1 | 0:e2155c63b32b | 121 | |
lilac0112_1 | 0:e2155c63b32b | 122 | int main() |
lilac0112_1 | 0:e2155c63b32b | 123 | { |
lilac0112_1 | 0:e2155c63b32b | 124 | Sensor.frequency(10000); |
lilac0112_1 | 0:e2155c63b32b | 125 | Led=0; |
lilac0112_1 | 0:e2155c63b32b | 126 | led_blink(); |
lilac0112_1 | 0:e2155c63b32b | 127 | |
lilac0112_1 | 0:e2155c63b32b | 128 | while(1) |
lilac0112_1 | 0:e2155c63b32b | 129 | { |
lilac0112_1 | 0:e2155c63b32b | 130 | getRGB(); |
lilac0112_1 | 0:e2155c63b32b | 131 | |
lilac0112_1 | 0:e2155c63b32b | 132 | pc.printf("Red = %4d, Green = %4d, Blue = %4d\n" |
lilac0112_1 | 0:e2155c63b32b | 133 | ,red |
lilac0112_1 | 0:e2155c63b32b | 134 | ,green |
lilac0112_1 | 0:e2155c63b32b | 135 | ,blue |
lilac0112_1 | 0:e2155c63b32b | 136 | ); |
lilac0112_1 | 0:e2155c63b32b | 137 | wait_ms(500); |
lilac0112_1 | 0:e2155c63b32b | 138 | } |
lilac0112_1 | 0:e2155c63b32b | 139 | } |