Rev0

Dependencies:   PID QEI-Intruptinmode-set SB1602E mbed

Fork of PreHeater-Rev2 by Kazu Zamasu

プリヒータ基板のファームウェアです。 Rev2基板用ですので、温度センサーなどが変更になっています。

This is Pre Heater device firmware. This firmware is Rev2 PCB design.

Revision:
2:6315824f8a29
Parent:
1:1440321bc326
Child:
3:e4253d33ff0c
--- a/main.cpp	Tue Jul 28 18:59:40 2015 +0000
+++ b/main.cpp	Wed Jul 29 04:40:07 2015 +0000
@@ -28,21 +28,21 @@
 #include "math.h"
 #include "SB1602E.h"
 #include "QEI.h"
-#include "rtos.h"
+
 //GPIO initilaize
-AnalogIn THAI(dp4);
-PwmOut   out(dp18);
-DigitalOut ledR(dp17),ledG(dp25),ledB(dp26);
-InterruptIn RunPB(dp10);
-Serial pc(dp16, dp15); // tx, rx
-
+AnalogIn    THAI(dp4);
+PwmOut      out(dp18);
+DigitalOut  ledR(dp17),ledG(dp25),ledB(dp26);
+DigitalIn   RunPB(dp10);
+Serial      pc(dp16, dp15); // tx, rx
+Ticker      Timer_ticker;
 
 /* PID constant initialize Kc, Ti, Td, interval */
-#define P 1.5 //propotional band
-#define I 50.0 //Integral
-#define D 0.5 //Devide
+#define P 5.0 //propotional band
+#define I 2.0 //Integral
+#define D 0.0 //Devide
 #define RATE 0.1 //update time sec
-//#define InitialSP 45.0 // Boot Setpoint initial temeprature
+#define InitialSP 45.0 // Boot Setpoint initial temeprature
 PID TIC(P, I, D, RATE);
 
 /*LCD I2C pin initialize */
@@ -56,8 +56,11 @@
 float temp_sv_input, waittime,OV_LL,ticout,qei,Bias;
 bool Run(false);
 double temp_pv,temp_cal;
-int rtostime;
-int encPulseCnt = 0;
+
+#define EncoderDiv 20.0   //Celcius coefficient encoder 25Pulse to 1℃
+#define RangeSPL    30.0    //Celcius low side temperature 
+#define RangeSPH    100.0   //Celcius high side temperature
+int encPulseCnt = ( (InitialSP - RangeSPL) * EncoderDiv );
 int encPulses = 0;
 int end_getPulses_old = 0;
 
@@ -67,65 +70,77 @@
     Run = !Run;
 }
 
-void cal_temp(void const *argument)
+void TimerCall_10ms()
+{
+    static bool old_RunPB=0;
+    bool tmp_RunPB=0;
+    tmp_RunPB = RunPB;
+    if( (old_RunPB != tmp_RunPB) && (tmp_RunPB == 1) )
+    {
+        runmode();
+    }
+    old_RunPB = tmp_RunPB;
+}
+
+void cal_temp()
 {
-    while(1) {
-        /*input for change to 0 to 100% range by 30C to 120C */
-        /*Temperature setpoint low high range */
-#define RangeSPL    45.0 //Celcius low side temperature 
-#define EncoderDiv  25.0
-#define RangeSPH    100.0 //Celcius high side temperature
+    /*input for change to 0 to 100% range by 30C to 100C */
+    /*Temperature setpoint low high range */
+
+    encPulseCnt = encPulseCnt + (encPulses - end_getPulses_old);
+    if( encPulseCnt < 0 )                                   //Lower Limit
+    {
+        encPulseCnt = 0;
+    }
+    if( encPulseCnt > ( (RangeSPH-RangeSPL) * EncoderDiv) ) //Upper Limit
+    {
+        encPulseCnt = (RangeSPH-RangeSPL) * EncoderDiv;
+    }
+
+    temp_sv_input = encPulseCnt / EncoderDiv + RangeSPL;    //calc encoder Pulse to SetPoint
 
+    /*six order polynomial calculation value
+    Thermister pull up resiter 560R
+    Thermister B value 3380K
+    Thermister Resistance 10K ohm at 25C
+    This NTC is NCP18XH103F03RB muRata
+    */
+    //temp_cal = THAI.read() * 3.3;
+    //temp_pv =-0.7964*pow(temp_cal,6.0) - 2.5431*pow(temp_cal,5.0) +63.605*pow(temp_cal,4.0) - 274.1*pow(temp_cal,3.0) + 522.57*pow(temp_cal,2.0) - 539.26*temp_cal + 405.76;
+
+    /*LCD Display section */
+    lcd.printf(0,0, "SP %.1f", temp_sv_input);
+
+    //lcd.printf(1, "OUT %.2f", out.read() * 100);
+    //lcd.printf(1, "PB %s\n", Run);
+
+    /*Tenperature indicater */
+    /* 1.5C high temperature */
+}
+
+
+int main()
+{
+    Timer_ticker.attach_us(&TimerCall_10ms, 10000);
+    RunPB.mode(PullDown);
+#define LCDCont 0x32 //LCD contrast set from 00 to 3f 64resolution defult set is 32step
+    lcd.contrast(LCDCont);
+    out.period(0.02);
+    cal_temp();
+
+    while (1) {
         encPulses = wheel.getPulses();
-        if(end_getPulses_old != encPulses)
+        if(end_getPulses_old != encPulses)  
         {
-            encPulseCnt = encPulseCnt + (encPulses - end_getPulses_old);
-
-            if( encPulseCnt < 0 )
-            {
-                encPulseCnt = 0;
-            }
-            if( encPulseCnt > ( (RangeSPH-RangeSPL) * EncoderDiv) )
-            {
-                encPulseCnt = (RangeSPH-RangeSPL) * EncoderDiv;
-            }
+            cal_temp();
         }
         end_getPulses_old = encPulses;
 
-        
-        temp_sv_input = encPulseCnt / EncoderDiv + RangeSPL;
-
-        /*six order polynomial calculation value
-        Thermister pull up resiter 560R
-        Thermister B value 3380K
-        Thermister Resistance 10K ohm at 25C
-        This NTC is NCP18XH103F03RB muRata
-        */
-        //temp_cal = THAI.read() * 3.3;
-        //temp_pv =-0.7964*pow(temp_cal,6.0) - 2.5431*pow(temp_cal,5.0) +63.605*pow(temp_cal,4.0) - 274.1*pow(temp_cal,3.0) + 522.57*pow(temp_cal,2.0) - 539.26*temp_cal + 405.76;
-        
         /*LM26LVCISDX-115  Factory Preset Temperature Switch and Temperature Sensor Gain3 115C trip sensor calculation value.*/
         temp_cal = THAI.read() * 3300;
         temp_pv = -0.00000000007*pow(temp_cal,3.0)-0.000002*pow(temp_cal,2.0)-0.091*temp_cal+201.5;
+        lcd.printf(0,1, "PV %.1f\n", temp_pv);
 
-        /*LCD Display section */
-        /*LCD Display section */
-        lcd.printf(0,0, "SP %.1f", temp_sv_input);
-        lcd.printf(0,1, "PV %.1f\n", temp_pv);
-        //lcd.printf(1, "OUT %.2f", out.read() * 100);
-        //lcd.printf(1, "PB %s\n", Run);
-        printf("\033[1;1H");
-        printf("OUT %.6f", out.read() * 100);
-        printf("\033[1;20H");
-        printf("PB %s\n", Run);
-        printf("\033[2;1H");
-        printf("PV %.6f\n", temp_pv);
-        printf("\033[2;20H");
-        printf("SP %.6f", temp_sv_input);
-        printf("\033[3;1H");
-        printf("PV %.6f\n", THAI.read());
-        /*Tenperature indicater */
-        /* 1.5C high temperature */
         if (Run == true) {
             if ((temp_pv - temp_sv_input) >= 1.5) {
                 ledR = 0;
@@ -147,21 +162,16 @@
             ledG = 1;
             ledB = 1;
         }
-        Thread::wait(rtostime * 1);
-    }
-}
-
-
-int main()
-{
-    RunPB.mode(PullDown);
-#define LCDCont 0x32 //LCD contrast set from 00 to 3f 64resolution defult set is 32step
-    lcd.contrast(LCDCont);
-    out.period(0.02);
-    RunPB.rise(&runmode);
-    Thread thread(cal_temp);
-    rtostime = RATE * 1000;
-    while (1) {
+    printf("\033[1;1H");
+    printf("OUT %.6f", out.read() * 100);
+    printf("\033[1;20H");
+    printf("PB %d\n", Run);
+    printf("\033[2;1H");
+    printf("PV %.6f\n", temp_pv);
+    printf("\033[2;20H");
+    printf("SP %.6f", temp_sv_input);
+    printf("\033[3;1H");
+    printf("PV %.6f\n", THAI.read());
         /*PID control*/
 #define SV_LL 0.0 //PID setpoint % value for lo limit
 #define SV_HL 100.0 //PID setpoint % value for high limit
@@ -181,7 +191,5 @@
             TIC.reset();
             out = 0.0;
         }
-        Thread::wait(rtostime);
-
     }
 }
\ No newline at end of file