Ryo Yonezawa / Mbed 2 deprecated HalCa-2_Diag

Dependencies:   SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "string"
00003 #include "SDFileSystem.h"
00004  
00005 DigitalOut blue_led(P0_20);
00006 DigitalOut white_led(P0_23);
00007 
00008 SDFileSystem sd(P0_9, P0_8, P0_10, P0_15, "sd"); // mosi, miso, sclk, cs
00009 
00010 InterruptIn in1(P0_19); //for mbed, p8 GPIO
00011 InterruptIn in2(P0_18); //for mbed, p8 GPIO
00012 Timer t1;
00013 Timer t_real;
00014 
00015 I2C i2c(P0_5, P0_4); // sda, scl
00016 
00017 //float t_period = 0;                   // This is the period between interrupts in microseconds
00018 //float t_freq = 0;
00019 //float t_freq_k = 0;
00020 //float moi = 0;
00021 int cnt=0;
00022 float f_cnt=0;
00023 char str1[17];
00024 char str2[17];
00025 
00026 float t = 0;
00027 
00028 //I2C i2c(P0_10, P0_11);
00029 const int AQCM0802_addr = 0x7C;
00030 
00031 // unsigned char mode;
00032 unsigned char contrast = 0; // 0-63
00033 //unsigned char contrastFlag = false;
00034 // int CGcounter;
00035 // int FADEcounter;
00036 
00037 void lcd_cmd(char x) {
00038   char data[2];
00039   data[0] = 0x00; // CO = 0,RS = 0
00040   data[1] = x;
00041   i2c.write(AQCM0802_addr, data, 2);
00042 }
00043  
00044 void lcd_contdata(char x) {
00045   char data[2];
00046   data[0] = 0xC0; //0b11000000 CO = 1, RS = 1
00047   data[1] = x;
00048   i2c.write(AQCM0802_addr, data, 2);
00049 }
00050  
00051 void lcd_lastdata(char x) {
00052   char data[2];
00053   data[0] = 0x40; //0b11000000 CO = 0, RS = 1
00054   data[1] = x;
00055   i2c.write(AQCM0802_addr, data, 2);
00056 }
00057  
00058 void lcd_printStr(const char *s) {
00059   while(*s) {
00060     if(*(s + 1)) {
00061       lcd_contdata(*s);
00062     } else {
00063       lcd_lastdata(*s);
00064     }
00065     s++;
00066   }
00067 }
00068  
00069 void lcd_printHex(unsigned char num) {
00070   lcd_contdata(num);
00071 }
00072  
00073 void lcd_init() {
00074   wait(0.04);
00075   // LCD initialize
00076   lcd_cmd(0x38); // function set
00077   lcd_cmd(0x39); // function set
00078   lcd_cmd(0x04); // EntryModeSet
00079   lcd_cmd(0x14); // interval osc
00080   lcd_cmd(0x70 | (contrast & 0xF)); // contrast Low
00081   lcd_cmd(0x5C | ((contrast >> 4) & 0x3)); // contast High/icon/power
00082   lcd_cmd(0x6C); // follower control
00083   wait(0.2);
00084   lcd_cmd(0x38); // function set
00085   lcd_cmd(0x0C); // Display On
00086   lcd_cmd(0x01); // Clear Display
00087   wait(0.2); // need additional wait to Clear Display
00088 }
00089  
00090 void lcd_setCursor(unsigned char x,unsigned char y) {
00091   lcd_cmd(0x80 | (y * 0x40 + x));
00092 }
00093  
00094 void setContrast(unsigned char c) {
00095   lcd_cmd(0x39);
00096   lcd_cmd(0x70 | (c & 0x0f)); // contrast Low
00097   lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power
00098   lcd_cmd(0x38);
00099 }
00100  
00101 void flip1(){
00102         lcd_setCursor(0, 1);
00103         lcd_printStr("sw1");
00104         white_led=1;
00105         wait(0.5);
00106         lcd_setCursor(0, 1);
00107         lcd_printStr("   ");
00108 }
00109 
00110 void flip2(){
00111         lcd_setCursor(4, 1);
00112         lcd_printStr("sw2");
00113         white_led=0;
00114         wait(0.5);
00115         lcd_setCursor(4, 1);
00116         lcd_printStr("   ");
00117 }
00118 
00119 int main() {
00120     white_led = 1;
00121     
00122     // LCD init
00123     lcd_init();
00124     contrast = 35;
00125     setContrast(contrast);
00126     lcd_printStr("...");
00127     
00128     // Timer start
00129     t1.start();
00130     t_real.start();
00131     
00132     // Interuupt start
00133     in1.mode(PullUp);  // Set the pin to Pull Down mode.
00134     in1.fall(&flip1);    // Set up the interrupt for rising edge
00135     
00136     in2.mode(PullUp);  // Set the pin to Pull Down mode.
00137     in2.fall(&flip2);    // Set up the interrupt for rising edge
00138  
00139     white_led=0;
00140     
00141     // SD init
00142     lcd_setCursor(0, 0);
00143     lcd_printStr("SD:");
00144     FILE *fp = fopen("/sd/TEST.txt", "w");
00145     fprintf(fp, "second, time\r\n");
00146     fclose(fp);
00147     lcd_printStr("OK");
00148 
00149 
00150     while(1) {
00151         blue_led = 1;
00152         wait(1);
00153         blue_led = 0;
00154         wait(1);
00155     }
00156 }