//********************************************************************** /*   『電圧&電流計』 */ //********************************************************************** unsigned int measurement(unsigned short channel) { unsigned int ad, cnt; // ad = 0; for (cnt = 0; cnt < 50; cnt++) { ad += Adc_Read(channel); } return (ad); } //********************************************************************** void main() { static unsigned char buf[8], cnt; static unsigned int v1, v2, i1; static double ad, offset; // OSCCON = 0b01110000; // クロックは8Mhz CMCON = 0b00000111; // コンパレータは使用しない。 // A/D変換を使用する。 ANSEL = 0b00000110; ADCON1.VCFG1 = 1; ADCON1.VCFG0 = 0; // ポートを初期化する。 TRISA = 0b00111110; TRISB = 0b00001111; // LCDを初期化する。 Lcd_Custom_Config(&PORTB,4,5,6,7,&PORTA,0,7,6); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Cmd(LCD_CLEAR); // Lcd_Custom_Out(1, 1, "V&I Meter V1.0"); // Delay_ms(500); // Lcd_Custom_Cmd(LCD_CLEAR); // offset = 0.0; // while (1) { // 電圧の測定 ad = 0.0; for (cnt = 0; cnt < 10; cnt++) { ad += measurement(1); } ad = (ad / 500.0) * 2.44140625 * 11.0; ad = ad - offset; v1 = (unsigned int)(ad); v2 = (unsigned int)(ad / 100.0); if ((v1 - (v2 * 100)) >= 50) v2++; // 電圧の表示 WordToStr(v2, buf); buf[6] = 0x00; buf[5] = buf[4]; buf[4] = '.'; Lcd_Custom_Out(1, 1, buf); Lcd_Custom_Out(1, 7, "V"); // 電流の測定 ad = 0.0; for (cnt = 0; cnt < 10; cnt++) { ad += measurement(2); } ad = (ad / 500.0) * 2.44140625 / 11.0; offset = ad; i1 = (unsigned int)(ad * 10.0); // 電流の表示 WordToStr(i1, buf); Lcd_Custom_Out(1, 9, buf); Lcd_Custom_Out(1, 14, "mA"); // buf[0] = 0xFF; buf[1] = 0x00; for (cnt = 1; cnt <= 16; cnt++) { if ((i1 / (62 * cnt)) == 0) break; Lcd_Custom_Out(2, cnt, buf); } for (; cnt <= 16; cnt++) { Lcd_Custom_Out(2, cnt, " "); } } } //**********************************************************************