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

Dependencies:   mbed

Committer:
flatcat
Date:
Mon Aug 18 04:26:00 2014 +0000
Revision:
3:83cb6eb61adf
Parent:
2:7751080fb267
Child:
4:4eedf8292719
from TEST_02;

Who changed what in which revision?

UserRevisionLine numberNew 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 //PwmOut icg(PB_3);
flatcat 1:a220fd937508 15 DigitalOut ICG(PB_3);
flatcat 1:a220fd937508 16 //interruptIn icg_int(PB_3);
flatcat 0:984447b91a04 17 AnalogIn imageIn(A0);
flatcat 1:a220fd937508 18 //AnalogOut imageOut(A5);
flatcat 1:a220fd937508 19 DigitalOut LED(LED1);
flatcat 0:984447b91a04 20 Serial raspi(USBTX, USBRX);
flatcat 0:984447b91a04 21
flatcat 1:a220fd937508 22 int masterFreq_period = 2; //microseconds
flatcat 1:a220fd937508 23 int masterFreq_width = 1; //microseconds
flatcat 1:a220fd937508 24 int shiftGate_period = 200; //microseconds
flatcat 1:a220fd937508 25 int shiftGate_width = 100; //microseconds
flatcat 1:a220fd937508 26
flatcat 3:83cb6eb61adf 27 int none = 0;
flatcat 1:a220fd937508 28 int veryLow = 1;
flatcat 1:a220fd937508 29 int low = 100;
flatcat 1:a220fd937508 30 int medium = 100000;
flatcat 1:a220fd937508 31 int high = 1000000;
flatcat 1:a220fd937508 32 int veryHigh = 10000000;
flatcat 1:a220fd937508 33
flatcat 2:7751080fb267 34 double imageData;
flatcat 3:83cb6eb61adf 35 int sensitivity = none;
flatcat 1:a220fd937508 36 int pixelTotal = 3694;
flatcat 1:a220fd937508 37 int leadingDummyElements = 16;
flatcat 1:a220fd937508 38 int leadShieldedElements = 13;
flatcat 1:a220fd937508 39 int headerElements = 3;
flatcat 1:a220fd937508 40 const int signalElements = 3648;
flatcat 1:a220fd937508 41 int trailingDummyElements = 14;
flatcat 1:a220fd937508 42 int pixelCount;
flatcat 1:a220fd937508 43 int readOutTrigger;
flatcat 1:a220fd937508 44 int state;
flatcat 1:a220fd937508 45
flatcat 1:a220fd937508 46 #define readOut_Begin 1
flatcat 1:a220fd937508 47 #define readOut_ACTIVE 2
flatcat 1:a220fd937508 48 #define readOut_LeadingDummy 3
flatcat 1:a220fd937508 49 #define readOut_LeadingShielded 4
flatcat 1:a220fd937508 50 #define readOut_headerElements 5
flatcat 1:a220fd937508 51 #define readOut_signalElements 6
flatcat 1:a220fd937508 52 #define readOut_trailingDummy 7
flatcat 1:a220fd937508 53 #define readOut_integrationTime 8
flatcat 1:a220fd937508 54 #define readOut_IDLE 9
flatcat 1:a220fd937508 55 #define readOut_Finish 0
flatcat 0:984447b91a04 56
flatcat 0:984447b91a04 57 #define MV(x) ((0xFFF*x)/3300)
flatcat 0:984447b91a04 58
flatcat 1:a220fd937508 59 float pixelValue[signalElements];
flatcat 1:a220fd937508 60
flatcat 1:a220fd937508 61 void error()
flatcat 1:a220fd937508 62 {
flatcat 1:a220fd937508 63 while(1) {
flatcat 1:a220fd937508 64 LED = !LED;
flatcat 1:a220fd937508 65 wait(0.5);
flatcat 1:a220fd937508 66 }
flatcat 1:a220fd937508 67 }
flatcat 1:a220fd937508 68
flatcat 1:a220fd937508 69 void checkState()
flatcat 1:a220fd937508 70 {
flatcat 1:a220fd937508 71 if (readOutTrigger == 1) {
flatcat 1:a220fd937508 72 // state = readOut_LeadingDummy;
flatcat 1:a220fd937508 73 }
flatcat 1:a220fd937508 74 switch (state) {
flatcat 1:a220fd937508 75 case readOut_Begin:
flatcat 1:a220fd937508 76 readOutTrigger = 1;
flatcat 1:a220fd937508 77 state = readOut_ACTIVE;
flatcat 1:a220fd937508 78 // ICG = 1;
flatcat 1:a220fd937508 79 LED = 1;
flatcat 1:a220fd937508 80 break;
flatcat 1:a220fd937508 81 case readOut_ACTIVE:
flatcat 1:a220fd937508 82 ICG = 1;
flatcat 1:a220fd937508 83 state = readOut_LeadingDummy;
flatcat 1:a220fd937508 84 break;
flatcat 1:a220fd937508 85 case readOut_LeadingDummy:
flatcat 1:a220fd937508 86 pixelCount++;
flatcat 1:a220fd937508 87 if (pixelCount == leadingDummyElements) {
flatcat 1:a220fd937508 88 pixelCount = 0;
flatcat 1:a220fd937508 89 state = readOut_LeadingShielded;
flatcat 1:a220fd937508 90 }
flatcat 1:a220fd937508 91 break;
flatcat 1:a220fd937508 92 case readOut_LeadingShielded:
flatcat 1:a220fd937508 93 pixelCount++;
flatcat 1:a220fd937508 94 if (pixelCount == leadShieldedElements) {
flatcat 1:a220fd937508 95 pixelCount = 0;
flatcat 1:a220fd937508 96 state = readOut_headerElements;
flatcat 1:a220fd937508 97 }
flatcat 1:a220fd937508 98 break;
flatcat 1:a220fd937508 99 case readOut_headerElements:
flatcat 1:a220fd937508 100 pixelCount++;
flatcat 1:a220fd937508 101 if (pixelCount == headerElements) {
flatcat 1:a220fd937508 102 pixelCount = 0;
flatcat 1:a220fd937508 103 state = readOut_signalElements;
flatcat 1:a220fd937508 104 }
flatcat 1:a220fd937508 105 break;
flatcat 1:a220fd937508 106 case readOut_signalElements:
flatcat 1:a220fd937508 107 pixelCount++;
flatcat 1:a220fd937508 108 // pixelValue[pixelCount] = imageIn.read();
flatcat 2:7751080fb267 109 // imageData = ((imageIn.read_u16() * 5.0) / 4096.0);
flatcat 3:83cb6eb61adf 110 // raspi.printf("%4.12f \r\n", (imageIn.read_u16() * 5.0) / 4096.0);
flatcat 1:a220fd937508 111 LED = !LED;
flatcat 1:a220fd937508 112 if (pixelCount == signalElements) {
flatcat 1:a220fd937508 113 pixelCount = 0;
flatcat 1:a220fd937508 114 state = readOut_trailingDummy;
flatcat 1:a220fd937508 115 }
flatcat 1:a220fd937508 116 break;
flatcat 1:a220fd937508 117 case readOut_trailingDummy:
flatcat 1:a220fd937508 118 pixelCount++;
flatcat 1:a220fd937508 119 if (pixelCount == trailingDummyElements) {
flatcat 1:a220fd937508 120 pixelCount = 0;
flatcat 1:a220fd937508 121 state = readOut_integrationTime;
flatcat 2:7751080fb267 122 ICG = 0;
flatcat 1:a220fd937508 123 }
flatcat 1:a220fd937508 124 break;
flatcat 1:a220fd937508 125 case readOut_integrationTime:
flatcat 1:a220fd937508 126 wait_us(sensitivity);
flatcat 2:7751080fb267 127 state = readOut_Finish;
flatcat 2:7751080fb267 128 raspi.printf("---\r\n");
flatcat 1:a220fd937508 129 break;
flatcat 1:a220fd937508 130 case readOut_Finish:
flatcat 1:a220fd937508 131 state = readOut_IDLE;
flatcat 2:7751080fb267 132 wait_us(sensitivity);
flatcat 1:a220fd937508 133 LED = 0;
flatcat 1:a220fd937508 134 ICG = 1;
flatcat 1:a220fd937508 135 break;
flatcat 1:a220fd937508 136 case readOut_IDLE:
flatcat 1:a220fd937508 137 if (ICG == 1) {
flatcat 1:a220fd937508 138 ICG = 0;
flatcat 1:a220fd937508 139 state = readOut_Begin;
flatcat 1:a220fd937508 140 }
flatcat 1:a220fd937508 141 break;
flatcat 1:a220fd937508 142 default:
flatcat 1:a220fd937508 143 break;
flatcat 1:a220fd937508 144 }
flatcat 1:a220fd937508 145 }
flatcat 1:a220fd937508 146
flatcat 0:984447b91a04 147 int main()
flatcat 0:984447b91a04 148 {
flatcat 1:a220fd937508 149 ICG = 1;
flatcat 1:a220fd937508 150 LED = 0;
flatcat 1:a220fd937508 151 pixelCount = 0;
flatcat 1:a220fd937508 152 readOutTrigger = 0;
flatcat 1:a220fd937508 153 state = readOut_IDLE;
flatcat 1:a220fd937508 154
flatcat 0:984447b91a04 155 masterClock.period_us(masterFreq_period);
flatcat 0:984447b91a04 156 masterClock.pulsewidth_us(masterFreq_width);
flatcat 0:984447b91a04 157
flatcat 0:984447b91a04 158 shiftGate.period_us(shiftGate_period);
flatcat 0:984447b91a04 159 shiftGate.pulsewidth_us(shiftGate_width);
flatcat 0:984447b91a04 160
flatcat 2:7751080fb267 161 raspi.baud(921600);
flatcat 1:a220fd937508 162 wait(0.5);
flatcat 1:a220fd937508 163
flatcat 1:a220fd937508 164 shiftGate_int.rise(checkState);
flatcat 1:a220fd937508 165
flatcat 0:984447b91a04 166 raspi.baud(921600);
flatcat 0:984447b91a04 167 while(1) {
flatcat 1:a220fd937508 168 wait(1);
flatcat 0:984447b91a04 169 }
flatcat 0:984447b91a04 170 }