This is the device firmware for the imagingBoard on the DIY 3D Printable Raspberry Pi Raman Spectrometer. For more details please visit: http://hackaday.io/project/1279
main.cpp@6:fe473ca8b625, 2014-08-19 (annotated)
- Committer:
- flatcat
- Date:
- Tue Aug 19 06:48:45 2014 +0000
- Revision:
- 6:fe473ca8b625
- Parent:
- 5:cbf08f942178
- Child:
- 7:0f8b3da4fb24
Fixed timing..; Fixed Analog Sample Buffering..;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flatcat | 0:984447b91a04 | 1 | #include "mbed.h" |
flatcat | 0:984447b91a04 | 2 | |
flatcat | 0:984447b91a04 | 3 | // *** BE SURE TO TEST BEFORE AND AFTER FOR 'IDLING'... CLEAR THE CCD EVERY OTHER FRAME TO TRY AND ELIMINATE NOISE BUILDUP |
flatcat | 0:984447b91a04 | 4 | //From http://www.ing.iac.es/~docs/ins/das/ins-das-29/integration.html |
flatcat | 0:984447b91a04 | 5 | //"Idling" vs. integrating |
flatcat | 0:984447b91a04 | 6 | //Charge from light leaks and thermal noise builds up on the detector between observations. If this charge is integrated by the detector, it may not be completely |
flatcat | 0:984447b91a04 | 7 | //cleared away by the clear cycle of the next observation. In that case, the observation will be contaminated by extra counts. (Often, this appears as a ramp in the |
flatcat | 0:984447b91a04 | 8 | //background leading up to a saturated region in the low-numbered rows.) |
flatcat | 0:984447b91a04 | 9 | //To avoid this problem, the detector is made to clear itself continuously between observations. This is called "idling", and is reported as such on the mimic. |
flatcat | 0:984447b91a04 | 10 | |
flatcat | 1:a220fd937508 | 11 | PwmOut masterClock(PB_4); |
flatcat | 0:984447b91a04 | 12 | PwmOut shiftGate(PB_8); |
flatcat | 1:a220fd937508 | 13 | InterruptIn shiftGate_int(PC_6); |
flatcat | 1:a220fd937508 | 14 | DigitalOut ICG(PB_3); |
flatcat | 0:984447b91a04 | 15 | AnalogIn imageIn(A0); |
flatcat | 1:a220fd937508 | 16 | DigitalOut LED(LED1); |
flatcat | 0:984447b91a04 | 17 | Serial raspi(USBTX, USBRX); |
flatcat | 0:984447b91a04 | 18 | |
flatcat | 6:fe473ca8b625 | 19 | int masterFreq_period = 20; //microseconds |
flatcat | 6:fe473ca8b625 | 20 | int masterFreq_width = 10; //microseconds |
flatcat | 6:fe473ca8b625 | 21 | int shiftGate_period = 60; //microseconds |
flatcat | 6:fe473ca8b625 | 22 | int shiftGate_width = 30; //microseconds |
flatcat | 1:a220fd937508 | 23 | |
flatcat | 3:83cb6eb61adf | 24 | int none = 0; |
flatcat | 1:a220fd937508 | 25 | int veryLow = 1; |
flatcat | 1:a220fd937508 | 26 | int low = 100; |
flatcat | 1:a220fd937508 | 27 | int medium = 100000; |
flatcat | 1:a220fd937508 | 28 | int high = 1000000; |
flatcat | 1:a220fd937508 | 29 | int veryHigh = 10000000; |
flatcat | 1:a220fd937508 | 30 | |
flatcat | 2:7751080fb267 | 31 | double imageData; |
flatcat | 3:83cb6eb61adf | 32 | int sensitivity = none; |
flatcat | 1:a220fd937508 | 33 | int pixelTotal = 3694; |
flatcat | 1:a220fd937508 | 34 | int leadingDummyElements = 16; |
flatcat | 1:a220fd937508 | 35 | int leadShieldedElements = 13; |
flatcat | 1:a220fd937508 | 36 | int headerElements = 3; |
flatcat | 5:cbf08f942178 | 37 | const int signalElements = 3648; |
flatcat | 1:a220fd937508 | 38 | int trailingDummyElements = 14; |
flatcat | 1:a220fd937508 | 39 | int pixelCount; |
flatcat | 1:a220fd937508 | 40 | int readOutTrigger; |
flatcat | 1:a220fd937508 | 41 | int state; |
flatcat | 1:a220fd937508 | 42 | |
flatcat | 1:a220fd937508 | 43 | #define readOut_Begin 1 |
flatcat | 1:a220fd937508 | 44 | #define readOut_ACTIVE 2 |
flatcat | 1:a220fd937508 | 45 | #define readOut_LeadingDummy 3 |
flatcat | 1:a220fd937508 | 46 | #define readOut_LeadingShielded 4 |
flatcat | 1:a220fd937508 | 47 | #define readOut_headerElements 5 |
flatcat | 1:a220fd937508 | 48 | #define readOut_signalElements 6 |
flatcat | 1:a220fd937508 | 49 | #define readOut_trailingDummy 7 |
flatcat | 1:a220fd937508 | 50 | #define readOut_integrationTime 8 |
flatcat | 1:a220fd937508 | 51 | #define readOut_IDLE 9 |
flatcat | 1:a220fd937508 | 52 | #define readOut_Finish 0 |
flatcat | 0:984447b91a04 | 53 | |
flatcat | 0:984447b91a04 | 54 | #define MV(x) ((0xFFF*x)/3300) |
flatcat | 0:984447b91a04 | 55 | |
flatcat | 6:fe473ca8b625 | 56 | double pixelValue[signalElements] = { 0 }; |
flatcat | 1:a220fd937508 | 57 | |
flatcat | 1:a220fd937508 | 58 | void error() |
flatcat | 1:a220fd937508 | 59 | { |
flatcat | 1:a220fd937508 | 60 | while(1) { |
flatcat | 1:a220fd937508 | 61 | LED = !LED; |
flatcat | 1:a220fd937508 | 62 | wait(0.5); |
flatcat | 1:a220fd937508 | 63 | } |
flatcat | 1:a220fd937508 | 64 | } |
flatcat | 1:a220fd937508 | 65 | |
flatcat | 1:a220fd937508 | 66 | void checkState() |
flatcat | 1:a220fd937508 | 67 | { |
flatcat | 1:a220fd937508 | 68 | if (readOutTrigger == 1) { |
flatcat | 1:a220fd937508 | 69 | // state = readOut_LeadingDummy; |
flatcat | 1:a220fd937508 | 70 | } |
flatcat | 1:a220fd937508 | 71 | switch (state) { |
flatcat | 1:a220fd937508 | 72 | case readOut_Begin: |
flatcat | 1:a220fd937508 | 73 | readOutTrigger = 1; |
flatcat | 1:a220fd937508 | 74 | state = readOut_ACTIVE; |
flatcat | 6:fe473ca8b625 | 75 | // raspi.printf("+++\r\n"); |
flatcat | 1:a220fd937508 | 76 | LED = 1; |
flatcat | 1:a220fd937508 | 77 | break; |
flatcat | 1:a220fd937508 | 78 | case readOut_ACTIVE: |
flatcat | 1:a220fd937508 | 79 | ICG = 1; |
flatcat | 1:a220fd937508 | 80 | state = readOut_LeadingDummy; |
flatcat | 1:a220fd937508 | 81 | break; |
flatcat | 1:a220fd937508 | 82 | case readOut_LeadingDummy: |
flatcat | 1:a220fd937508 | 83 | pixelCount++; |
flatcat | 1:a220fd937508 | 84 | if (pixelCount == leadingDummyElements) { |
flatcat | 1:a220fd937508 | 85 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 86 | state = readOut_LeadingShielded; |
flatcat | 1:a220fd937508 | 87 | } |
flatcat | 1:a220fd937508 | 88 | break; |
flatcat | 1:a220fd937508 | 89 | case readOut_LeadingShielded: |
flatcat | 1:a220fd937508 | 90 | pixelCount++; |
flatcat | 1:a220fd937508 | 91 | if (pixelCount == leadShieldedElements) { |
flatcat | 1:a220fd937508 | 92 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 93 | state = readOut_headerElements; |
flatcat | 1:a220fd937508 | 94 | } |
flatcat | 1:a220fd937508 | 95 | break; |
flatcat | 1:a220fd937508 | 96 | case readOut_headerElements: |
flatcat | 1:a220fd937508 | 97 | pixelCount++; |
flatcat | 1:a220fd937508 | 98 | if (pixelCount == headerElements) { |
flatcat | 1:a220fd937508 | 99 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 100 | state = readOut_signalElements; |
flatcat | 1:a220fd937508 | 101 | } |
flatcat | 1:a220fd937508 | 102 | break; |
flatcat | 1:a220fd937508 | 103 | case readOut_signalElements: |
flatcat | 1:a220fd937508 | 104 | pixelCount++; |
flatcat | 6:fe473ca8b625 | 105 | pixelValue[pixelCount] = imageIn.read_u16(); |
flatcat | 6:fe473ca8b625 | 106 | // raspi.printf("%i\t%4.12f\r\n", pixelCount,(imageIn.read_u16() * 5.0) / 4096.0); |
flatcat | 1:a220fd937508 | 107 | if (pixelCount == signalElements) { |
flatcat | 1:a220fd937508 | 108 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 109 | state = readOut_trailingDummy; |
flatcat | 1:a220fd937508 | 110 | } |
flatcat | 1:a220fd937508 | 111 | break; |
flatcat | 1:a220fd937508 | 112 | case readOut_trailingDummy: |
flatcat | 1:a220fd937508 | 113 | pixelCount++; |
flatcat | 1:a220fd937508 | 114 | if (pixelCount == trailingDummyElements) { |
flatcat | 1:a220fd937508 | 115 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 116 | state = readOut_integrationTime; |
flatcat | 2:7751080fb267 | 117 | ICG = 0; |
flatcat | 1:a220fd937508 | 118 | } |
flatcat | 1:a220fd937508 | 119 | break; |
flatcat | 1:a220fd937508 | 120 | case readOut_integrationTime: |
flatcat | 1:a220fd937508 | 121 | wait_us(sensitivity); |
flatcat | 2:7751080fb267 | 122 | state = readOut_Finish; |
flatcat | 6:fe473ca8b625 | 123 | for (int pixelNumber=0; pixelNumber<signalElements; pixelNumber++) { |
flatcat | 6:fe473ca8b625 | 124 | raspi.printf("%i\t%4.12f\r\n", pixelNumber, (pixelValue[pixelNumber] * 5) / 4096.0); |
flatcat | 6:fe473ca8b625 | 125 | } |
flatcat | 6:fe473ca8b625 | 126 | // raspi.printf("---\r\n"); |
flatcat | 1:a220fd937508 | 127 | break; |
flatcat | 1:a220fd937508 | 128 | case readOut_Finish: |
flatcat | 1:a220fd937508 | 129 | state = readOut_IDLE; |
flatcat | 2:7751080fb267 | 130 | wait_us(sensitivity); |
flatcat | 1:a220fd937508 | 131 | LED = 0; |
flatcat | 1:a220fd937508 | 132 | ICG = 1; |
flatcat | 1:a220fd937508 | 133 | break; |
flatcat | 1:a220fd937508 | 134 | case readOut_IDLE: |
flatcat | 1:a220fd937508 | 135 | if (ICG == 1) { |
flatcat | 4:4eedf8292719 | 136 | // ICG = 0; |
flatcat | 4:4eedf8292719 | 137 | // state = readOut_Begin; |
flatcat | 1:a220fd937508 | 138 | } |
flatcat | 1:a220fd937508 | 139 | break; |
flatcat | 1:a220fd937508 | 140 | default: |
flatcat | 1:a220fd937508 | 141 | break; |
flatcat | 1:a220fd937508 | 142 | } |
flatcat | 1:a220fd937508 | 143 | } |
flatcat | 1:a220fd937508 | 144 | |
flatcat | 0:984447b91a04 | 145 | int main() |
flatcat | 0:984447b91a04 | 146 | { |
flatcat | 1:a220fd937508 | 147 | ICG = 1; |
flatcat | 1:a220fd937508 | 148 | LED = 0; |
flatcat | 1:a220fd937508 | 149 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 150 | readOutTrigger = 0; |
flatcat | 1:a220fd937508 | 151 | state = readOut_IDLE; |
flatcat | 1:a220fd937508 | 152 | |
flatcat | 0:984447b91a04 | 153 | masterClock.period_us(masterFreq_period); |
flatcat | 0:984447b91a04 | 154 | masterClock.pulsewidth_us(masterFreq_width); |
flatcat | 0:984447b91a04 | 155 | |
flatcat | 0:984447b91a04 | 156 | shiftGate.period_us(shiftGate_period); |
flatcat | 0:984447b91a04 | 157 | shiftGate.pulsewidth_us(shiftGate_width); |
flatcat | 0:984447b91a04 | 158 | |
flatcat | 2:7751080fb267 | 159 | raspi.baud(921600); |
flatcat | 1:a220fd937508 | 160 | wait(0.5); |
flatcat | 1:a220fd937508 | 161 | |
flatcat | 1:a220fd937508 | 162 | shiftGate_int.rise(checkState); |
flatcat | 1:a220fd937508 | 163 | |
flatcat | 0:984447b91a04 | 164 | raspi.baud(921600); |
flatcat | 0:984447b91a04 | 165 | while(1) { |
flatcat | 4:4eedf8292719 | 166 | char c = raspi.getc(); |
flatcat | 6:fe473ca8b625 | 167 | switch (c) { |
flatcat | 4:4eedf8292719 | 168 | case 'r': |
flatcat | 4:4eedf8292719 | 169 | ICG = 0; |
flatcat | 4:4eedf8292719 | 170 | state = readOut_Begin; |
flatcat | 4:4eedf8292719 | 171 | break; |
flatcat | 4:4eedf8292719 | 172 | default: |
flatcat | 4:4eedf8292719 | 173 | break; |
flatcat | 4:4eedf8292719 | 174 | } |
flatcat | 0:984447b91a04 | 175 | } |
flatcat | 0:984447b91a04 | 176 | } |