![](/media/cache/profiles/82b0639a82d0cc70b8f5830fd2b06868.50x50_q85.jpg)
Program to read out a Hamamatsu S9226 linear photodiode array. Program averages each pixel's value over a number of scans to reduce noise
main.cpp@0:0a05dbed4fed, 2010-11-26 (annotated)
- Committer:
- marcbax
- Date:
- Fri Nov 26 20:35:49 2010 +0000
- Revision:
- 0:0a05dbed4fed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcbax | 0:0a05dbed4fed | 1 | #include "mbed.h" |
marcbax | 0:0a05dbed4fed | 2 | #include "TextLCD.h" |
marcbax | 0:0a05dbed4fed | 3 | |
marcbax | 0:0a05dbed4fed | 4 | DigitalOut led1(LED1); //Set up all 4 LEDs for debugging purposes |
marcbax | 0:0a05dbed4fed | 5 | DigitalOut led2(LED2); |
marcbax | 0:0a05dbed4fed | 6 | DigitalOut led3(LED3); |
marcbax | 0:0a05dbed4fed | 7 | DigitalOut led4(LED4); |
marcbax | 0:0a05dbed4fed | 8 | PwmOut clk(p22); //CLK signal to pin 2 of S9226 |
marcbax | 0:0a05dbed4fed | 9 | DigitalOut start(p23); //ST signal to pin 4 of S9226 |
marcbax | 0:0a05dbed4fed | 10 | DigitalOut vgain(p24); //Video gain - 0=high, 1=low |
marcbax | 0:0a05dbed4fed | 11 | DigitalOut itrcheck(p5); //itrcheck is "1" during A-to-D conversion - debugging purpose |
marcbax | 0:0a05dbed4fed | 12 | DigitalIn EOS(p17); //EOS signal from pin 7 of S9226 |
marcbax | 0:0a05dbed4fed | 13 | DigitalIn clkin(p21); //loopback CLK input to count integration time |
marcbax | 0:0a05dbed4fed | 14 | DigitalIn button1(p14); //Define start/stop button |
marcbax | 0:0a05dbed4fed | 15 | AnalogIn video(p20); //analog input signal - output from pin 6 on S9226 |
marcbax | 0:0a05dbed4fed | 16 | InterruptIn trigger(p18); //Trig signal from pin 3 of S9226 |
marcbax | 0:0a05dbed4fed | 17 | TextLCD lcd(p8, p9, p10, p11, p12, p13); //Define 16 x 2 character LCD |
marcbax | 0:0a05dbed4fed | 18 | LocalFileSystem local("local"); //Define file system on mbed thumbdrive |
marcbax | 0:0a05dbed4fed | 19 | int i,j,k, pixvalue, avnumber, avarray, avdiv, clkperiod; |
marcbax | 0:0a05dbed4fed | 20 | int pixels[1024], avpixels[1024]; |
marcbax | 0:0a05dbed4fed | 21 | |
marcbax | 0:0a05dbed4fed | 22 | void readpixel() { //ISR that takes one pixel reading, called on falling flank of "trigger" |
marcbax | 0:0a05dbed4fed | 23 | itrcheck=1; |
marcbax | 0:0a05dbed4fed | 24 | pixels[i] = video.read_u16()/avnumber; |
marcbax | 0:0a05dbed4fed | 25 | i++; |
marcbax | 0:0a05dbed4fed | 26 | itrcheck=0; |
marcbax | 0:0a05dbed4fed | 27 | } |
marcbax | 0:0a05dbed4fed | 28 | |
marcbax | 0:0a05dbed4fed | 29 | //void counttrig() { |
marcbax | 0:0a05dbed4fed | 30 | // i++; |
marcbax | 0:0a05dbed4fed | 31 | //} |
marcbax | 0:0a05dbed4fed | 32 | |
marcbax | 0:0a05dbed4fed | 33 | int main() { |
marcbax | 0:0a05dbed4fed | 34 | clkperiod = 40; //clock period in microseconds - must be a multiple of 10 |
marcbax | 0:0a05dbed4fed | 35 | clk.period_us(clkperiod); //set clock frequency |
marcbax | 0:0a05dbed4fed | 36 | clk.write(0.5); //set duty cycle to 50% |
marcbax | 0:0a05dbed4fed | 37 | start = 1; //set start line "high" |
marcbax | 0:0a05dbed4fed | 38 | vgain = 1; //set video gain to "low" |
marcbax | 0:0a05dbed4fed | 39 | |
marcbax | 0:0a05dbed4fed | 40 | //USER SETTINGS |
marcbax | 0:0a05dbed4fed | 41 | avnumber = 32; //THIS IS WHERE YOU SET THE NUMBER OF AVERAGES |
marcbax | 0:0a05dbed4fed | 42 | avdiv = 1024/avnumber; //AVNUMBER MUST BE A POWER OF 2: 2,4,8,16,32,64,128 |
marcbax | 0:0a05dbed4fed | 43 | //END USER SETTINGS |
marcbax | 0:0a05dbed4fed | 44 | |
marcbax | 0:0a05dbed4fed | 45 | itrcheck=0; |
marcbax | 0:0a05dbed4fed | 46 | lcd.cls(); |
marcbax | 0:0a05dbed4fed | 47 | while (1) { |
marcbax | 0:0a05dbed4fed | 48 | lcd.locate(0,0); |
marcbax | 0:0a05dbed4fed | 49 | lcd.printf("Scan __ of%3i ", avnumber); //Show avnumber on display |
marcbax | 0:0a05dbed4fed | 50 | while (!button1) //waiting till start button is pressed |
marcbax | 0:0a05dbed4fed | 51 | lcd.locate(0,1); |
marcbax | 0:0a05dbed4fed | 52 | lcd.printf("Preparing scans "); |
marcbax | 0:0a05dbed4fed | 53 | for (i=0; i<1025; i++) { //clear array of average pixel values |
marcbax | 0:0a05dbed4fed | 54 | avpixels [i] = 0; |
marcbax | 0:0a05dbed4fed | 55 | } |
marcbax | 0:0a05dbed4fed | 56 | while (!clkin) //wait for clkin to go "high" - start condition |
marcbax | 0:0a05dbed4fed | 57 | wait_us(clkperiod/10); //give start pulse of half a clock period, while start is low |
marcbax | 0:0a05dbed4fed | 58 | start = 0; |
marcbax | 0:0a05dbed4fed | 59 | wait_us(clkperiod/2); |
marcbax | 0:0a05dbed4fed | 60 | start = 1; |
marcbax | 0:0a05dbed4fed | 61 | led1=1; |
marcbax | 0:0a05dbed4fed | 62 | avarray=0; |
marcbax | 0:0a05dbed4fed | 63 | i=0; |
marcbax | 0:0a05dbed4fed | 64 | while (EOS) { //read pixels for dummy scan until EOS goes "low" |
marcbax | 0:0a05dbed4fed | 65 | trigger.fall(&readpixel); |
marcbax | 0:0a05dbed4fed | 66 | } |
marcbax | 0:0a05dbed4fed | 67 | wait(0.1); //0.1s delay to make sure all interrupts are handled |
marcbax | 0:0a05dbed4fed | 68 | lcd.cls(); //Clear display |
marcbax | 0:0a05dbed4fed | 69 | for (int j=0; j<avnumber; j++) { //make "avnumber" scans and put average results in the avpixel array |
marcbax | 0:0a05dbed4fed | 70 | wait(0.1); |
marcbax | 0:0a05dbed4fed | 71 | lcd.locate(0,0); |
marcbax | 0:0a05dbed4fed | 72 | lcd.printf("Scan%3i of%3i", j+1, avnumber); |
marcbax | 0:0a05dbed4fed | 73 | while (!clkin) |
marcbax | 0:0a05dbed4fed | 74 | wait_us(clkperiod/10); |
marcbax | 0:0a05dbed4fed | 75 | start = 0; |
marcbax | 0:0a05dbed4fed | 76 | wait_us(clkperiod/2); |
marcbax | 0:0a05dbed4fed | 77 | start = 1; |
marcbax | 0:0a05dbed4fed | 78 | led1 = !led1; //Blink LED1 while scanning |
marcbax | 0:0a05dbed4fed | 79 | i=0; |
marcbax | 0:0a05dbed4fed | 80 | while (EOS) { |
marcbax | 0:0a05dbed4fed | 81 | trigger.fall(&readpixel); |
marcbax | 0:0a05dbed4fed | 82 | } |
marcbax | 0:0a05dbed4fed | 83 | wait(0.1); |
marcbax | 0:0a05dbed4fed | 84 | avarray = 0; |
marcbax | 0:0a05dbed4fed | 85 | for (int i=0; i<1024; i++) { //divide the result by avnumber then add to sum |
marcbax | 0:0a05dbed4fed | 86 | avpixels [i] = avpixels [i] + pixels [i]; |
marcbax | 0:0a05dbed4fed | 87 | avarray = avarray + (pixels[i]/avdiv); //Calculate average value over entire array over this scan |
marcbax | 0:0a05dbed4fed | 88 | } |
marcbax | 0:0a05dbed4fed | 89 | lcd.locate(0,1); |
marcbax | 0:0a05dbed4fed | 90 | lcd.printf("ArrayAv: %6i", avarray); |
marcbax | 0:0a05dbed4fed | 91 | } |
marcbax | 0:0a05dbed4fed | 92 | wait(0.1); |
marcbax | 0:0a05dbed4fed | 93 | led1 = 0; |
marcbax | 0:0a05dbed4fed | 94 | lcd.locate(0,1); |
marcbax | 0:0a05dbed4fed | 95 | FILE *fp = fopen("/local/data.csv", "w"); //create or overwrite file "data.csv" on the mbed thumbdrive |
marcbax | 0:0a05dbed4fed | 96 | lcd.printf("File opened "); |
marcbax | 0:0a05dbed4fed | 97 | for (int i=10; i<1014; i++) { //write pixel numbers and average values, skipping first and last 10 pixels |
marcbax | 0:0a05dbed4fed | 98 | fprintf(fp, "%6i,%8i\n", i, avpixels[i]); |
marcbax | 0:0a05dbed4fed | 99 | } |
marcbax | 0:0a05dbed4fed | 100 | fclose(fp); //close file "data.csv" so it can be read by the PC |
marcbax | 0:0a05dbed4fed | 101 | lcd.locate(0,0); |
marcbax | 0:0a05dbed4fed | 102 | lcd.printf("ArrayAv: %6i", avarray); |
marcbax | 0:0a05dbed4fed | 103 | lcd.locate(0,1); |
marcbax | 0:0a05dbed4fed | 104 | lcd.printf("DATA.CSV ready"); |
marcbax | 0:0a05dbed4fed | 105 | wait(5); |
marcbax | 0:0a05dbed4fed | 106 | } |
marcbax | 0:0a05dbed4fed | 107 | } |