CatPot 2015-2016
/
Pr034_S11059
S11059-02DT
main.cpp@2:059b9459d344, 2015-04-14 (annotated)
- Committer:
- lilac0112_1
- Date:
- Tue Apr 14 09:10:39 2015 +0000
- Revision:
- 2:059b9459d344
- Parent:
- 1:eabfa27c9374
- Child:
- 3:7fd72fb29c34
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 | 1:eabfa27c9374 | 6 | I2C Sensor(p28,p27); |
lilac0112_1 | 0:e2155c63b32b | 7 | |
lilac0112_1 | 2:059b9459d344 | 8 | #define DEVICE_ADDRESS 0x2A << 1 |
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 | 2:059b9459d344 | 25 | char data[8]={}; |
lilac0112_1 | 0:e2155c63b32b | 26 | |
lilac0112_1 | 0:e2155c63b32b | 27 | void led_blink(){ |
lilac0112_1 | 0:e2155c63b32b | 28 | Led=1; |
lilac0112_1 | 0:e2155c63b32b | 29 | wait_ms(10); |
lilac0112_1 | 0:e2155c63b32b | 30 | Led=0; |
lilac0112_1 | 0:e2155c63b32b | 31 | } |
lilac0112_1 | 0:e2155c63b32b | 32 | |
lilac0112_1 | 0:e2155c63b32b | 33 | void setManualtGain(uint16_t ex_time) |
lilac0112_1 | 0:e2155c63b32b | 34 | { |
lilac0112_1 | 0:e2155c63b32b | 35 | char reg[1]={0}; |
lilac0112_1 | 0:e2155c63b32b | 36 | |
lilac0112_1 | 0:e2155c63b32b | 37 | reg[0] =(ex_time >>= 8); |
lilac0112_1 | 0:e2155c63b32b | 38 | Sensor.write(address, reg, 1); |
lilac0112_1 | 0:e2155c63b32b | 39 | reg[0] |= ex_time; |
lilac0112_1 | 0:e2155c63b32b | 40 | Sensor.write(address, reg, 1); |
lilac0112_1 | 0:e2155c63b32b | 41 | } |
lilac0112_1 | 0:e2155c63b32b | 42 | |
lilac0112_1 | 0:e2155c63b32b | 43 | void getRGB() |
lilac0112_1 | 0:e2155c63b32b | 44 | { |
lilac0112_1 | 0:e2155c63b32b | 45 | |
lilac0112_1 | 0:e2155c63b32b | 46 | /* //manual exposure mode |
lilac0112_1 | 0:e2155c63b32b | 47 | |
lilac0112_1 | 0:e2155c63b32b | 48 | setManualtGain(100); |
lilac0112_1 | 0:e2155c63b32b | 49 | |
lilac0112_1 | 0:e2155c63b32b | 50 | ord[0]=cmd[0]; |
lilac0112_1 | 0:e2155c63b32b | 51 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 52 | |
lilac0112_1 | 0:e2155c63b32b | 53 | ord[0]=0x84;// ADC reset LowGain wakeup |
lilac0112_1 | 0:e2155c63b32b | 54 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 55 | |
lilac0112_1 | 0:e2155c63b32b | 56 | ord[0]=cmd[0]; |
lilac0112_1 | 0:e2155c63b32b | 57 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 58 | |
lilac0112_1 | 0:e2155c63b32b | 59 | ord[0]=0x04;//start |
lilac0112_1 | 0:e2155c63b32b | 60 | Sensor.write(address, ord, 1); |
lilac0112_1 | 0:e2155c63b32b | 61 | |
lilac0112_1 | 0:e2155c63b32b | 62 | */ |
lilac0112_1 | 0:e2155c63b32b | 63 | |
lilac0112_1 | 0:e2155c63b32b | 64 | //preset gain mode exposure time (each color ch) |
lilac0112_1 | 0:e2155c63b32b | 65 | // 00 = 87.5us |
lilac0112_1 | 0:e2155c63b32b | 66 | // 01= 1.4ms |
lilac0112_1 | 0:e2155c63b32b | 67 | // 10= 22.4ms |
lilac0112_1 | 0:e2155c63b32b | 68 | // 11= 179.2ms |
lilac0112_1 | 0:e2155c63b32b | 69 | |
lilac0112_1 | 1:eabfa27c9374 | 70 | |
lilac0112_1 | 2:059b9459d344 | 71 | ord[0]=0x00; |
lilac0112_1 | 2:059b9459d344 | 72 | ord[1]=0x89; |
lilac0112_1 | 2:059b9459d344 | 73 | Val = !Sensor.write(address&0xFE, ord, 2, true); |
lilac0112_1 | 1:eabfa27c9374 | 74 | |
lilac0112_1 | 2:059b9459d344 | 75 | ord[0]=0x00; |
lilac0112_1 | 2:059b9459d344 | 76 | ord[1]=0x09; |
lilac0112_1 | 2:059b9459d344 | 77 | Val = !Sensor.write(address&0xFE, ord, 2); |
lilac0112_1 | 1:eabfa27c9374 | 78 | |
lilac0112_1 | 1:eabfa27c9374 | 79 | |
lilac0112_1 | 2:059b9459d344 | 80 | wait_ms(5.6*4); //total exposure time |
lilac0112_1 | 2:059b9459d344 | 81 | //wait_ms(180*4); //total exposure time |
lilac0112_1 | 0:e2155c63b32b | 82 | |
lilac0112_1 | 0:e2155c63b32b | 83 | led_blink(); |
lilac0112_1 | 0:e2155c63b32b | 84 | |
lilac0112_1 | 0:e2155c63b32b | 85 | //read data |
lilac0112_1 | 0:e2155c63b32b | 86 | |
lilac0112_1 | 2:059b9459d344 | 87 | ord[0]=0x03; |
lilac0112_1 | 2:059b9459d344 | 88 | Val = !Sensor.write(address&0xFE, ord, 1, true); |
lilac0112_1 | 0:e2155c63b32b | 89 | |
lilac0112_1 | 2:059b9459d344 | 90 | Val = !Sensor.read(address|0x01, data, 8, true); |
lilac0112_1 | 0:e2155c63b32b | 91 | |
lilac0112_1 | 2:059b9459d344 | 92 | red = (uint8_t)(data[0])<<8 | (uint8_t)(data[1]); |
lilac0112_1 | 2:059b9459d344 | 93 | green = (uint8_t)(data[2])<<8 | (uint8_t)(data[3]); |
lilac0112_1 | 2:059b9459d344 | 94 | blue = (uint8_t)(data[4])<<8 | (uint8_t)(data[5]); |
lilac0112_1 | 2:059b9459d344 | 95 | IR = (uint8_t)(data[6])<<8 | (uint8_t)(data[7]); |
lilac0112_1 | 0:e2155c63b32b | 96 | |
lilac0112_1 | 0:e2155c63b32b | 97 | return; |
lilac0112_1 | 0:e2155c63b32b | 98 | } |
lilac0112_1 | 0:e2155c63b32b | 99 | |
lilac0112_1 | 0:e2155c63b32b | 100 | int main() |
lilac0112_1 | 0:e2155c63b32b | 101 | { |
lilac0112_1 | 2:059b9459d344 | 102 | Sensor.frequency(100000); |
lilac0112_1 | 0:e2155c63b32b | 103 | Led=0; |
lilac0112_1 | 0:e2155c63b32b | 104 | led_blink(); |
lilac0112_1 | 0:e2155c63b32b | 105 | |
lilac0112_1 | 0:e2155c63b32b | 106 | while(1) |
lilac0112_1 | 0:e2155c63b32b | 107 | { |
lilac0112_1 | 0:e2155c63b32b | 108 | getRGB(); |
lilac0112_1 | 0:e2155c63b32b | 109 | |
lilac0112_1 | 0:e2155c63b32b | 110 | pc.printf("Red = %4d, Green = %4d, Blue = %4d\n" |
lilac0112_1 | 0:e2155c63b32b | 111 | ,red |
lilac0112_1 | 0:e2155c63b32b | 112 | ,green |
lilac0112_1 | 0:e2155c63b32b | 113 | ,blue |
lilac0112_1 | 0:e2155c63b32b | 114 | ); |
lilac0112_1 | 0:e2155c63b32b | 115 | wait_ms(500); |
lilac0112_1 | 0:e2155c63b32b | 116 | } |
lilac0112_1 | 0:e2155c63b32b | 117 | } |