//********************************************************************** /* <歪率計> */ //********************************************************************** unsigned int measurement(unsigned short channel) { unsigned int ad, max, min; unsigned char cnt; // ad = 0; max = 0; min = 1024; for (cnt = 0; cnt < 100; cnt++) { ad = Adc_Read(channel); max = ad > max ? ad : max; min = ad < min ? ad : min; } return (max - min); } //********************************************************************** void Pwm_Change_DutyEx(unsigned int duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.F6 = duty_ratio & 0b00000001; CCP1CON.F7 = (duty_ratio & 0b00000010) >> 1; } //********************************************************************** void main() { unsigned int ad2, ad3; unsigned long ad2l, ad3l, tmp; double ad2d, ad3d; unsigned char buf[20], cnt, len; // OSCCON = 0b01110000; // クロックは8Mhz CMCON = 0b00000111; // コンパレータは使用しない。 ANSEL = 0b00001100; // AD2,3を使用する。 TRISA = 0b00111100; TRISB = 0b00001111; OPTION_REG.F7 = 0; ADCON1.VCFG0 = 0; ADCON1.VCFG1 = 1; // Lcd_Custom_Config(&PORTA, 1, 0, 7, 6, &PORTB, 4, 5, 6); Lcd_Custom_Cmd(LCD_CURSOR_OFF); // Lcd_Custom_Out(1, 1, "DistortionAnalyz"); // Lcd_Custom_Out(2, 1, " JF3SFB 2008.1"); Delay_ms(100); Lcd_Custom_Cmd(LCD_CLEAR); // Pwm_Init(10000); // 10kHz Pwm_Change_DutyEx((PR2 * 4) / 2); Pwm_Start(); // while (1) { if (PORTB.F3 == 0) { // ad2 = measurement(2); WordToStr((ad2 * 25) / 10, buf); Lcd_Custom_Out(1, 1, buf); Lcd_Custom_Out(1, 6, "mV"); // ad3 = measurement(4); WordToStr((ad3 * 25) / 10, buf); Lcd_Custom_Out(1, 9, buf); Lcd_Custom_Out(1, 14, "mV"); // for (cnt = 0; cnt < 16; cnt++) { if (ad3 > cnt) buf[cnt] = 0xFF; else buf[cnt] = ' '; } buf[16] = 0x00; Lcd_Custom_Out(2, 1, buf); continue; } ad2 = 0; for (cnt = 0; cnt < 25; cnt++) { ad2 += measurement(2); } // ad2 = (ad2 / 25) * 2.5; ad2 = ad2 / 10; // ad3 = 0; for (cnt = 0; cnt < 25; cnt++) { ad3 += measurement(4); } // ad3 = (ad3 / 25) * 2.5; ad3 = ad3 / 10; // Lcd_Custom_Cmd(LCD_CLEAR); // WordToStr(ad2, buf); Lcd_Custom_Out(1, 1, buf); Lcd_Custom_Out(1, 6, "mV"); // WordToStr(ad3, buf); Lcd_Custom_Out(1, 9, buf); Lcd_Custom_Out(1, 14, "mV"); // ad2l = ad2; ad3l = ad3; tmp = (ad3l * 10000) / ad2l; WordToStr((unsigned int)tmp, buf); buf[5] = buf[4]; buf[4] = buf[3]; buf[3] = '.'; buf[6] = 0x00; Lcd_Custom_Out(2, 1, buf); Lcd_Custom_Out(2, 7, "%"); } } //**********************************************************************