Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* Libraries */ 00002 #include "mbed.h" 00003 #include "TextLCD.h" 00004 00005 /* */ 00006 #define DELAY_M 50 //Delay before Measuring Voltage at ADC-Pin (TempNow) 00007 #define TEMP_BASEGAIN 0.137931 //Temp which should equal 1mV - changed by TEMP_FACTOR 00008 #define TEMP_OFFSET 0 //Offset of Temperature 00009 #define CTRL_GAIN 10 //TempDifference to PWM-Value Gain 00010 #define TEMP_FACTOR 1.5 //TEMP_BASEGAIN * TEMP_FACTOR = TEMP_GAIN 00011 00012 /* */ 00013 Ticker dispRefresh; 00014 TextLCD lcd (p18, p16, p11, p10, p9, p8); // rs, e, d4-d7 00015 DigitalOut db5(p10); //Data Pin 5 on LCD, used for Initialization 00016 DigitalOut rw(p17); //Read-Write Pin on LCD: LOW-->WRITE 00017 PwmOut pHeat(p26); //Heater-Pin 00018 AnalogIn adc(p20); //ADC-Pin 10Bit 00019 PwmOut pSpeaker(p25); //Piezo-Speaker Pin 00020 InterruptIn rotSw1(p35); //Pin 1 of Rotary Encoder 00021 DigitalIn rotSw2(p33); //Pin 2 of Rotary Encoder 00022 InterruptIn rotSwB(p30); //Button of Rotary Encoder 00023 DigitalOut tempCtrlLED(LED1); 00024 00025 /* */ 00026 float TempNow = 0; //Current Temperature Reading 00027 float TempSet = 200; //Set Temperature 00028 float pwmVal = 0; //PWM-Value for Heater-Pin 00029 float adcVal = 0; //Value of ADC-Reading 00030 00031 /* */ 00032 void writeLCD(); 00033 void init4BitLCD(); 00034 void readTemp(); 00035 void calcPWM(); 00036 void RotEnc(); 00037 void RotBut(); 00038 00039 int ticker = 500; 00040 Ticker t; 00041 void tick_1(){ 00042 if(ticker > 0) ticker--; 00043 } 00044 00045 int main() { 00046 //Initialize 00047 init4BitLCD(); //Set Display to 4 Bit-Mode 00048 rw = 0; // Set Read/Write Pin to Write 00049 rotSw1.rise(&RotEnc); 00050 rotSwB.rise(&RotBut); 00051 pSpeaker.period_ms(2); //Set Speaker Frequency to 500Hz 00052 //pHeat = 0.1; 00053 00054 t.attach(&tick_1, 0.001); 00055 while(1) { 00056 readTemp(); //Read Temperature - Done 00057 calcPWM(); //Calculate PWM-Value - Done 00058 pHeat = pwmVal; //Write PWM-Value to Heater - Done 00059 if(ticker == 0){ 00060 writeLCD(); //Write To LCD - Done 00061 ticker = 500; 00062 } 00063 //Repeat 00064 } 00065 } 00066 00067 void RotBut() 00068 {tempCtrlLED = !tempCtrlLED; 00069 if (pSpeaker == 0){pSpeaker = 0.5;} 00070 else {pSpeaker = 0;}} 00071 00072 void RotEnc() 00073 { 00074 if (rotSw2 == 0){TempSet++;} 00075 else if (rotSw2 == 1){TempSet--;} 00076 wait_ms(1); 00077 } 00078 00079 void calcPWM() 00080 { 00081 pwmVal = ((TempSet - TempNow) * CTRL_GAIN) / 255; //Calculate PWM-Val. 00082 if (pwmVal > 1) pwmVal = 1; 00083 else if (pwmVal < 0) pwmVal = 0; 00084 } 00085 00086 void readTemp() 00087 { pHeat = 0; //Switching Off Heater || Thermoelement --> No VoltageSupply 00088 wait_ms(DELAY_M); //Waiting for a bit|| Low-Pass Filter --> Ready State 00089 adcVal = adc.read(); //Read ADC 00090 TempNow = adcVal*3300*(TEMP_BASEGAIN * TEMP_FACTOR) + TEMP_OFFSET; //Calculate current Temperature 00091 pHeat = pwmVal; //Re-Activate Heater Pin 00092 } 00093 00094 void writeLCD() 00095 { char cHeat; 00096 if(pwmVal > 0.5){cHeat = 'H';} 00097 else{cHeat = ' ';} 00098 lcd.cls(); 00099 lcd.printf("Set: %.0f C %c\nNow: %.0f C", TempSet, cHeat, TempNow);} 00100 00101 void init4BitLCD() 00102 { db5 = 1; wait_ms(1); lcd.cls();}
Generated on Wed Jul 20 2022 03:45:17 by
1.7.2