//********************************************************************** #define RELAY PORTB.F4 #define SW1 PORTA.F5 #define SW2 PORTB.F0 #define SW3 PORTB.F1 #define SW4 PORTB.F2 //********************************************************************** unsigned int nowData, maxData, minData, aveData, thresholdData; unsigned char buf[10]; //********************************************************************** void interrupt(){ if (PIR1.TMR1IF == 1) { PIR1.TMR1IF = 0; PORTA.F4 = ~PORTA.F4; } } //********************************************************************** /* void Pwm_Change_DutyEx(unsigned int duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.F6 = duty_ratio & 0b00000001; CCP1CON.F7 = (duty_ratio & 0b00000010) >> 1; } */ //********************************************************************** void IntToStrEx(int number, char *buf) { IntToStr(number, buf); buf[7] = 0x00; buf[6] = buf[5]; buf[5] = '.'; } //********************************************************************** void buzzer() { Pwm_Start(); Delay_ms(100); Pwm_Stop(); } //********************************************************************** void ThermoCntl() { // 温度制御をするかしないかを判断する。 if (SW2 == 1) { Lcd_Custom_Out(1, 9, "<----->"); // 温度制御しない。 RELAY = 0; T1CON.T1CKPS1 = 1; } else { Lcd_Custom_Out(1, 9, ""); // 温度制御する。 // 制御出力をONにするかを判断する。 if ((nowData > thresholdData) && (RELAY == 0)) { buzzer(); RELAY = 1; T1CON.T1CKPS1 = 0; } // 制御出力をOFFにするかを判断する。ヒステリシス制御! if ((nowData < (thresholdData - 10)) && (RELAY == 1)) { buzzer(); RELAY = 0; T1CON.T1CKPS1 = 1; } } // if (SW3 == 0) { // 閾値を+1する。 if (thresholdData < 990) thresholdData = ((thresholdData / 10) + 1) * 10; Eeprom_Write(0, thresholdData & 0xFF); Eeprom_Write(1, ((thresholdData >> 8) & 0xFF)); } // if (SW4 == 0) { // 閾値をー1する。 if (thresholdData > 0) thresholdData = ((thresholdData / 10) - 1) * 10; Eeprom_Write(0, thresholdData & 0xFF); Eeprom_Write(1, ((thresholdData >> 8) & 0xFF)); } // 閾値温度を表示する。 IntToStrEx(thresholdData, buf); Lcd_Custom_Out(2, 1, "threshold->"); Lcd_Custom_Out(2, 12, &buf[3]); } //********************************************************************** void main() { unsigned int ad2, tmp; unsigned char cnt; // OSCCON = 0b01110000; // 内臓クロックを8Mhzに設定する。 CMCON = 0b00000111; // コンパレータは使用しない。 ANSEL = 0b00000100; // A/D変換はAN2を使用する。 TRISA = 0b00101100; TRISB = 0b00000111; // タイマー1を設定する。 PIE1.TMR1IE = 1; PIR1.TMR1IF = 0; T1CON.T1CKPS0 = 1; T1CON.T1CKPS1 = 1; T1CON.TMR1ON = 1; // ADCON1.VCFG1 = 1; ADCON1.VCFG0 = 0; // Lcd_Custom_Config(&PORTA, 6, 7, 0, 1, &PORTB, 5, 6, 7); Lcd_Custom_Cmd(LCD_CURSOR_OFF); // Lcd_Custom_Cmd(Lcd_Clear); // Pwm_Init(5000); // 5Khz tmp = (PR2 << 2) / 2; CCPR1L = tmp >> 2; CCP1CON.F6 = tmp & 0b00000001; CCP1CON.F7 = (tmp & 0b00000010) >> 1; buzzer(); // RELAY = 0; nowData = 0; maxData = 0; minData = 1000; thresholdData = Eeprom_Read(1); thresholdData = thresholdData << 8; thresholdData = thresholdData | Eeprom_Read(0); if ((thresholdData < 0) || (thresholdData > 990)) thresholdData = 0; cnt = 0; // INTCON.PEIE = 1; // これ以降の処理で割り込みを許可する。 INTCON.GIE = 1; // これ以降の処理で割り込みを許可する。 // while (1) { aveData = 0; for (cnt = 0; cnt < 60; cnt++) { Lcd_Custom_Cmd(Lcd_Clear); ad2 = Adc_Read(2); nowData = (int)((double)ad2 * 2.44); maxData = maxData < nowData ? nowData : maxData; minData = minData > nowData ? nowData : minData; aveData += nowData; // 現在温度を表示する。 IntToStrEx(nowData, buf); Lcd_Custom_Out(1, 1, "now"); Lcd_Custom_Out(1, 4, &buf[3]); // 温度制御をするかをチェックする。 if (SW1 == 0) { ThermoCntl(); // Delay_ms(500); continue; } // 最大温度を表示する。 IntToStrEx(maxData, buf); Lcd_Custom_Out(2, 9, "max"); Lcd_Custom_Out(2, 12, &buf[3]); // 最小温度を表示する。 IntToStrEx(minData, buf); Lcd_Custom_Out(2, 1, "min"); Lcd_Custom_Out(2, 4, &buf[3]); // 平均温度を表示する。 IntToStrEx(aveData / (cnt + 1), buf); Lcd_Custom_Out(1, 9, "ave"); Lcd_Custom_Out(1, 12, &buf[3]); // Delay_ms(500); } } } //**********************************************************************