//********************************************************************** /*   『ワットメータ』 */ //********************************************************************** unsigned int measurement() { unsigned int ad, max, min, cnt; // ad = 0; max = 0; min = 1024; for (cnt = 0; cnt < 1000; cnt++) { ad = Adc_Read(1); max = ad > max ? ad : max; min = ad < min ? ad : min; } return (max - min); } //********************************************************************** void main() { static unsigned char buf[8], cnt, tmp1, tmp2; static unsigned int ad, wattOffset; // OSCCON = 0b01110000; // クロックは8Mhz CMCON = 0b00000111; // コンパレータは使用しない。 // A/D変換を使用する。 ANSEL = 0b00000010; // ポートを初期化する。 TRISA = 0b00111010; 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, "WattMeter2 V1.0"); Delay_ms(500); Lcd_Custom_Cmd(LCD_CLEAR); // 保存されたデータを読み込む tmp1 = Eeprom_Read(0); Delay_ms(20); tmp2 = Eeprom_Read(1); Delay_ms(20); wattOffset = (tmp2 << 8) | tmp1; wattOffset = (wattOffset > 10000) ? 10 : wattOffset; // while (1) { ad = 0; for (cnt = 0; cnt < 10; cnt++) { ad += measurement(); } ad = ad * 5; // WordToStr(ad, buf); buf[6] = 0x00; buf[5] = buf[4]; buf[4] = '.'; Lcd_Custom_Out(2, 1, buf); Lcd_Custom_Out(2, 7, "mV"); // WordToStr((ad * 10) / wattOffset, buf); buf[6] = 0x00; buf[5] = buf[4]; buf[4] = '.'; Lcd_Custom_Out(1, 1, buf); Lcd_Custom_Out(1, 7, "W"); // WordToStr(wattOffset, buf); buf[6] = 0x00; buf[5] = buf[4]; buf[4] = '.'; Lcd_Custom_Out(2, 9, buf); Lcd_Custom_Out(2, 15, "mV"); // if (PORTA.F5 == 0) { wattOffset = ad / 54; // ★校正用です。 // データを保存する。 Eeprom_Write(0, wattOffset & 0xFF); Delay_ms(20); Eeprom_Write(1, (wattOffset >> 8) & 0xFF); Delay_ms(20); } } } //**********************************************************************