HalCa-2 diag test program
Dependencies: SDFileSystem mbed
main.cpp@0:86eefbc31367, 2015-08-05 (annotated)
- Committer:
- yone2
- Date:
- Wed Aug 05 08:38:31 2015 +0000
- Revision:
- 0:86eefbc31367
Diag program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yone2 | 0:86eefbc31367 | 1 | #include "mbed.h" |
yone2 | 0:86eefbc31367 | 2 | #include "string" |
yone2 | 0:86eefbc31367 | 3 | #include "SDFileSystem.h" |
yone2 | 0:86eefbc31367 | 4 | |
yone2 | 0:86eefbc31367 | 5 | DigitalOut blue_led(P0_20); |
yone2 | 0:86eefbc31367 | 6 | DigitalOut white_led(P0_23); |
yone2 | 0:86eefbc31367 | 7 | |
yone2 | 0:86eefbc31367 | 8 | SDFileSystem sd(P0_9, P0_8, P0_10, P0_15, "sd"); // mosi, miso, sclk, cs |
yone2 | 0:86eefbc31367 | 9 | |
yone2 | 0:86eefbc31367 | 10 | InterruptIn in1(P0_19); //for mbed, p8 GPIO |
yone2 | 0:86eefbc31367 | 11 | InterruptIn in2(P0_18); //for mbed, p8 GPIO |
yone2 | 0:86eefbc31367 | 12 | Timer t1; |
yone2 | 0:86eefbc31367 | 13 | Timer t_real; |
yone2 | 0:86eefbc31367 | 14 | |
yone2 | 0:86eefbc31367 | 15 | I2C i2c(P0_5, P0_4); // sda, scl |
yone2 | 0:86eefbc31367 | 16 | |
yone2 | 0:86eefbc31367 | 17 | //float t_period = 0; // This is the period between interrupts in microseconds |
yone2 | 0:86eefbc31367 | 18 | //float t_freq = 0; |
yone2 | 0:86eefbc31367 | 19 | //float t_freq_k = 0; |
yone2 | 0:86eefbc31367 | 20 | //float moi = 0; |
yone2 | 0:86eefbc31367 | 21 | int cnt=0; |
yone2 | 0:86eefbc31367 | 22 | float f_cnt=0; |
yone2 | 0:86eefbc31367 | 23 | char str1[17]; |
yone2 | 0:86eefbc31367 | 24 | char str2[17]; |
yone2 | 0:86eefbc31367 | 25 | |
yone2 | 0:86eefbc31367 | 26 | float t = 0; |
yone2 | 0:86eefbc31367 | 27 | |
yone2 | 0:86eefbc31367 | 28 | //I2C i2c(P0_10, P0_11); |
yone2 | 0:86eefbc31367 | 29 | const int AQCM0802_addr = 0x7C; |
yone2 | 0:86eefbc31367 | 30 | |
yone2 | 0:86eefbc31367 | 31 | // unsigned char mode; |
yone2 | 0:86eefbc31367 | 32 | unsigned char contrast = 0; // 0-63 |
yone2 | 0:86eefbc31367 | 33 | //unsigned char contrastFlag = false; |
yone2 | 0:86eefbc31367 | 34 | // int CGcounter; |
yone2 | 0:86eefbc31367 | 35 | // int FADEcounter; |
yone2 | 0:86eefbc31367 | 36 | |
yone2 | 0:86eefbc31367 | 37 | void lcd_cmd(char x) { |
yone2 | 0:86eefbc31367 | 38 | char data[2]; |
yone2 | 0:86eefbc31367 | 39 | data[0] = 0x00; // CO = 0,RS = 0 |
yone2 | 0:86eefbc31367 | 40 | data[1] = x; |
yone2 | 0:86eefbc31367 | 41 | i2c.write(AQCM0802_addr, data, 2); |
yone2 | 0:86eefbc31367 | 42 | } |
yone2 | 0:86eefbc31367 | 43 | |
yone2 | 0:86eefbc31367 | 44 | void lcd_contdata(char x) { |
yone2 | 0:86eefbc31367 | 45 | char data[2]; |
yone2 | 0:86eefbc31367 | 46 | data[0] = 0xC0; //0b11000000 CO = 1, RS = 1 |
yone2 | 0:86eefbc31367 | 47 | data[1] = x; |
yone2 | 0:86eefbc31367 | 48 | i2c.write(AQCM0802_addr, data, 2); |
yone2 | 0:86eefbc31367 | 49 | } |
yone2 | 0:86eefbc31367 | 50 | |
yone2 | 0:86eefbc31367 | 51 | void lcd_lastdata(char x) { |
yone2 | 0:86eefbc31367 | 52 | char data[2]; |
yone2 | 0:86eefbc31367 | 53 | data[0] = 0x40; //0b11000000 CO = 0, RS = 1 |
yone2 | 0:86eefbc31367 | 54 | data[1] = x; |
yone2 | 0:86eefbc31367 | 55 | i2c.write(AQCM0802_addr, data, 2); |
yone2 | 0:86eefbc31367 | 56 | } |
yone2 | 0:86eefbc31367 | 57 | |
yone2 | 0:86eefbc31367 | 58 | void lcd_printStr(const char *s) { |
yone2 | 0:86eefbc31367 | 59 | while(*s) { |
yone2 | 0:86eefbc31367 | 60 | if(*(s + 1)) { |
yone2 | 0:86eefbc31367 | 61 | lcd_contdata(*s); |
yone2 | 0:86eefbc31367 | 62 | } else { |
yone2 | 0:86eefbc31367 | 63 | lcd_lastdata(*s); |
yone2 | 0:86eefbc31367 | 64 | } |
yone2 | 0:86eefbc31367 | 65 | s++; |
yone2 | 0:86eefbc31367 | 66 | } |
yone2 | 0:86eefbc31367 | 67 | } |
yone2 | 0:86eefbc31367 | 68 | |
yone2 | 0:86eefbc31367 | 69 | void lcd_printHex(unsigned char num) { |
yone2 | 0:86eefbc31367 | 70 | lcd_contdata(num); |
yone2 | 0:86eefbc31367 | 71 | } |
yone2 | 0:86eefbc31367 | 72 | |
yone2 | 0:86eefbc31367 | 73 | void lcd_init() { |
yone2 | 0:86eefbc31367 | 74 | wait(0.04); |
yone2 | 0:86eefbc31367 | 75 | // LCD initialize |
yone2 | 0:86eefbc31367 | 76 | lcd_cmd(0x38); // function set |
yone2 | 0:86eefbc31367 | 77 | lcd_cmd(0x39); // function set |
yone2 | 0:86eefbc31367 | 78 | lcd_cmd(0x04); // EntryModeSet |
yone2 | 0:86eefbc31367 | 79 | lcd_cmd(0x14); // interval osc |
yone2 | 0:86eefbc31367 | 80 | lcd_cmd(0x70 | (contrast & 0xF)); // contrast Low |
yone2 | 0:86eefbc31367 | 81 | lcd_cmd(0x5C | ((contrast >> 4) & 0x3)); // contast High/icon/power |
yone2 | 0:86eefbc31367 | 82 | lcd_cmd(0x6C); // follower control |
yone2 | 0:86eefbc31367 | 83 | wait(0.2); |
yone2 | 0:86eefbc31367 | 84 | lcd_cmd(0x38); // function set |
yone2 | 0:86eefbc31367 | 85 | lcd_cmd(0x0C); // Display On |
yone2 | 0:86eefbc31367 | 86 | lcd_cmd(0x01); // Clear Display |
yone2 | 0:86eefbc31367 | 87 | wait(0.2); // need additional wait to Clear Display |
yone2 | 0:86eefbc31367 | 88 | } |
yone2 | 0:86eefbc31367 | 89 | |
yone2 | 0:86eefbc31367 | 90 | void lcd_setCursor(unsigned char x,unsigned char y) { |
yone2 | 0:86eefbc31367 | 91 | lcd_cmd(0x80 | (y * 0x40 + x)); |
yone2 | 0:86eefbc31367 | 92 | } |
yone2 | 0:86eefbc31367 | 93 | |
yone2 | 0:86eefbc31367 | 94 | void setContrast(unsigned char c) { |
yone2 | 0:86eefbc31367 | 95 | lcd_cmd(0x39); |
yone2 | 0:86eefbc31367 | 96 | lcd_cmd(0x70 | (c & 0x0f)); // contrast Low |
yone2 | 0:86eefbc31367 | 97 | lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power |
yone2 | 0:86eefbc31367 | 98 | lcd_cmd(0x38); |
yone2 | 0:86eefbc31367 | 99 | } |
yone2 | 0:86eefbc31367 | 100 | |
yone2 | 0:86eefbc31367 | 101 | void flip1(){ |
yone2 | 0:86eefbc31367 | 102 | lcd_setCursor(0, 1); |
yone2 | 0:86eefbc31367 | 103 | lcd_printStr("sw1"); |
yone2 | 0:86eefbc31367 | 104 | white_led=1; |
yone2 | 0:86eefbc31367 | 105 | wait(0.5); |
yone2 | 0:86eefbc31367 | 106 | lcd_setCursor(0, 1); |
yone2 | 0:86eefbc31367 | 107 | lcd_printStr(" "); |
yone2 | 0:86eefbc31367 | 108 | } |
yone2 | 0:86eefbc31367 | 109 | |
yone2 | 0:86eefbc31367 | 110 | void flip2(){ |
yone2 | 0:86eefbc31367 | 111 | lcd_setCursor(4, 1); |
yone2 | 0:86eefbc31367 | 112 | lcd_printStr("sw2"); |
yone2 | 0:86eefbc31367 | 113 | white_led=0; |
yone2 | 0:86eefbc31367 | 114 | wait(0.5); |
yone2 | 0:86eefbc31367 | 115 | lcd_setCursor(4, 1); |
yone2 | 0:86eefbc31367 | 116 | lcd_printStr(" "); |
yone2 | 0:86eefbc31367 | 117 | } |
yone2 | 0:86eefbc31367 | 118 | |
yone2 | 0:86eefbc31367 | 119 | int main() { |
yone2 | 0:86eefbc31367 | 120 | white_led = 1; |
yone2 | 0:86eefbc31367 | 121 | |
yone2 | 0:86eefbc31367 | 122 | // LCD init |
yone2 | 0:86eefbc31367 | 123 | lcd_init(); |
yone2 | 0:86eefbc31367 | 124 | contrast = 35; |
yone2 | 0:86eefbc31367 | 125 | setContrast(contrast); |
yone2 | 0:86eefbc31367 | 126 | lcd_printStr("..."); |
yone2 | 0:86eefbc31367 | 127 | |
yone2 | 0:86eefbc31367 | 128 | // Timer start |
yone2 | 0:86eefbc31367 | 129 | t1.start(); |
yone2 | 0:86eefbc31367 | 130 | t_real.start(); |
yone2 | 0:86eefbc31367 | 131 | |
yone2 | 0:86eefbc31367 | 132 | // Interuupt start |
yone2 | 0:86eefbc31367 | 133 | in1.mode(PullUp); // Set the pin to Pull Down mode. |
yone2 | 0:86eefbc31367 | 134 | in1.fall(&flip1); // Set up the interrupt for rising edge |
yone2 | 0:86eefbc31367 | 135 | |
yone2 | 0:86eefbc31367 | 136 | in2.mode(PullUp); // Set the pin to Pull Down mode. |
yone2 | 0:86eefbc31367 | 137 | in2.fall(&flip2); // Set up the interrupt for rising edge |
yone2 | 0:86eefbc31367 | 138 | |
yone2 | 0:86eefbc31367 | 139 | white_led=0; |
yone2 | 0:86eefbc31367 | 140 | |
yone2 | 0:86eefbc31367 | 141 | // SD init |
yone2 | 0:86eefbc31367 | 142 | lcd_setCursor(0, 0); |
yone2 | 0:86eefbc31367 | 143 | lcd_printStr("SD:"); |
yone2 | 0:86eefbc31367 | 144 | FILE *fp = fopen("/sd/TEST.txt", "w"); |
yone2 | 0:86eefbc31367 | 145 | fprintf(fp, "second, time\r\n"); |
yone2 | 0:86eefbc31367 | 146 | fclose(fp); |
yone2 | 0:86eefbc31367 | 147 | lcd_printStr("OK"); |
yone2 | 0:86eefbc31367 | 148 | |
yone2 | 0:86eefbc31367 | 149 | |
yone2 | 0:86eefbc31367 | 150 | while(1) { |
yone2 | 0:86eefbc31367 | 151 | blue_led = 1; |
yone2 | 0:86eefbc31367 | 152 | wait(1); |
yone2 | 0:86eefbc31367 | 153 | blue_led = 0; |
yone2 | 0:86eefbc31367 | 154 | wait(1); |
yone2 | 0:86eefbc31367 | 155 | } |
yone2 | 0:86eefbc31367 | 156 | } |