//********************************************************************** /*    『アナログデータロガーV4(SDカード対応)』  ■A/D変換の精度   ・10ビット  ■チャネル数   ・4チャネル  ■記録媒体   ・SDカード  ■記録形式   ・CSV形式  ■記録周期   ・約0.1秒 */ //********************************************************************** #define LED PORTB.F7 #define SW_START PORTA.F4 #define SW_STOP PORTA.F5 #define ON 1 #define OFF 0 #define CR 0x0d #define LF 0x0a //********************************************************************** void main() { //変数の定義 static char buf[50]; short cnt, channel, pnt; unsigned ad; long dataCnt; //A/D変換の設定 ADCON1.PCFG3 = 1; ADCON1.PCFG2 = 0; ADCON1.PCFG1 = 1; ADCON1.PCFG0 = 1; //ポートの設定 TRISA = 0b11111111; TRISB = 0b00000000; TRISC = 0b00000000; //LCDの初期化 Lcd_Custom_Config(&PORTB,5,4,3,2,&PORTC,0,1,6); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Cmd(LCD_CLEAR); for (cnt = 1; cnt <= 16; cnt++) { Lcd_Custom_Chr(1, cnt, 0xFF); LED = ON; Delay_ms(50); LED = OFF; Delay_ms(50); } for (cnt = 1; cnt <= 16; cnt++) { Lcd_Custom_Chr(2, cnt, 0xFF); LED = ON; Delay_ms(50); LED = OFF; Delay_ms(50); } Lcd_Custom_Cmd(LCD_CLEAR); // while (1) { //開始スイッチが押されるのをチェックする。 while (SW_START == 1) { Delay_ms(10); } dataCnt = 0; //MMCの初期化 Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH); if (!Mmc_Fat_Init(&PORTC, 2)) { Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH); Mmc_Fat_Assign("log.csv", 0xA0); Mmc_Fat_Rewrite(); Mmc_Fat_Write("$START\r\n", 8); //停止スイッチが押されるまで処理を繰り返す。 while (SW_STOP == 1) { pnt = 0; //4チャンネル分のA/D変換を行い、LCDに表示する。 for (channel = 0; channel < 4; channel++) { ad = Adc_Read(channel); ad = (double)ad * 4.8828125; WordToStr(ad, &buf[pnt]); switch (channel) { case 0: Lcd_Custom_Out(1, 1, "1:"); Lcd_Custom_Out(1, 3, &buf[pnt + 1]); break; case 1: Lcd_Custom_Out(1, 9, "2:"); Lcd_Custom_Out(1, 11, &buf[pnt + 1]); break; case 2: Lcd_Custom_Out(2, 1, "3:"); Lcd_Custom_Out(2, 3, &buf[pnt + 1]); break; case 3: Lcd_Custom_Out(2, 9, "4:"); Lcd_Custom_Out(2, 11, &buf[pnt + 1]); break; } pnt += 6; } buf[5] = ','; buf[11] = ','; buf[17] = ','; buf[23] = CR; buf[24] = LF; //4チャンネル分まとめてMMCに書き込む。 Mmc_Fat_Write(buf, 25); // LED = ON; Delay_ms(100); LED = OFF; // dataCnt++; } Mmc_Fat_Write("$STOP\r\n", 7); //書き込んだ件数をLCDに表示する。 LongToStr(dataCnt, buf); Lcd_Custom_Cmd(LCD_CLEAR); Lcd_Custom_Out(1, 1, buf); // for (cnt = 0; cnt < 10; cnt++) { LED = ON; Delay_ms(50); LED = OFF; Delay_ms(50); } } } }//~! //**********************************************************************