Plamen Totev
/
peltier
peltier with 2 fans
peltier/peltier.cpp@4:5213bee8158e, 2014-04-14 (annotated)
- Committer:
- redplam
- Date:
- Mon Apr 14 02:15:32 2014 +0000
- Revision:
- 4:5213bee8158e
- Parent:
- 3:0ef2ced1f02d
myproject;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
redplam | 1:3aec489c7366 | 1 | #include "mbed.h" |
redplam | 1:3aec489c7366 | 2 | |
redplam | 1:3aec489c7366 | 3 | #include "TextLCD.h" |
redplam | 4:5213bee8158e | 4 | //#include "DS18B20.h" |
redplam | 1:3aec489c7366 | 5 | #include "peltier.h" |
redplam | 4:5213bee8158e | 6 | //---------------------------------- |
redplam | 4:5213bee8158e | 7 | #include "DS18B20.h" |
redplam | 4:5213bee8158e | 8 | #include "OneWireDefs.h" |
redplam | 4:5213bee8158e | 9 | |
redplam | 4:5213bee8158e | 10 | #define THERMOMETER DS18B20 |
redplam | 4:5213bee8158e | 11 | |
redplam | 4:5213bee8158e | 12 | float temp; |
redplam | 4:5213bee8158e | 13 | float dsen_temp_in; |
redplam | 4:5213bee8158e | 14 | float dsen_temp_out; |
redplam | 4:5213bee8158e | 15 | // device( mbed pin ) |
redplam | 4:5213bee8158e | 16 | THERMOMETER device(PTE0); |
redplam | 4:5213bee8158e | 17 | |
redplam | 4:5213bee8158e | 18 | |
redplam | 4:5213bee8158e | 19 | //----------------------------------- |
redplam | 4:5213bee8158e | 20 | Serial pc(USBTX, USBRX); // tx, rx |
redplam | 1:3aec489c7366 | 21 | |
redplam | 1:3aec489c7366 | 22 | unsigned int peltier_hot_pulsewidth=1000; |
redplam | 1:3aec489c7366 | 23 | unsigned int peltier_cold_pulsewidth=1000; |
redplam | 1:3aec489c7366 | 24 | unsigned int peltier_period =1000; |
redplam | 1:3aec489c7366 | 25 | unsigned int peltier_pulsewidth = 0; |
redplam | 1:3aec489c7366 | 26 | unsigned int peltier_step = 10; |
redplam | 1:3aec489c7366 | 27 | |
redplam | 1:3aec489c7366 | 28 | bool alarm_hi_temp_in=false; |
redplam | 1:3aec489c7366 | 29 | bool alarm_lo_temp_in=false; |
redplam | 1:3aec489c7366 | 30 | bool alarm_hi_temp_out=false; |
redplam | 1:3aec489c7366 | 31 | bool alarm_lo_temp_out=false; |
redplam | 1:3aec489c7366 | 32 | |
redplam | 1:3aec489c7366 | 33 | TextLCD lcd(PTE29, PTE30, PTC12, PTD0, PTD5, PTA13, TextLCD::LCD20x2); // rs, e, d4-d7 ok |
redplam | 4:5213bee8158e | 34 | //DigitalOut backlight(PTD6); |
redplam | 1:3aec489c7366 | 35 | //unsigned int peltier_hot_pulsewidth=1000; |
redplam | 1:3aec489c7366 | 36 | //unsigned int peltier_cold_pulsewidth=1000; |
redplam | 1:3aec489c7366 | 37 | //unsigned int peltier_period =1000; |
redplam | 1:3aec489c7366 | 38 | //unsigned int peltier_pulsewidth = 0; |
redplam | 1:3aec489c7366 | 39 | //unsigned int peltier_step = 10; |
redplam | 1:3aec489c7366 | 40 | //bool alarm_hi_temp_in=false; |
redplam | 1:3aec489c7366 | 41 | //bool alarm_lo_temp_in=false; |
redplam | 1:3aec489c7366 | 42 | //bool alarm_hi_temp_out=false; |
redplam | 1:3aec489c7366 | 43 | //bool alarm_lo_temp_out=false; |
redplam | 1:3aec489c7366 | 44 | float dsen_temp; |
redplam | 1:3aec489c7366 | 45 | |
redplam | 4:5213bee8158e | 46 | void readTemp(int deviceNum) |
redplam | 4:5213bee8158e | 47 | { |
redplam | 4:5213bee8158e | 48 | temp = device.readTemperature(deviceNum); |
redplam | 4:5213bee8158e | 49 | // pc.printf("Device %d is %f\n\r",deviceNum, temp); |
redplam | 4:5213bee8158e | 50 | wait(0.5); |
redplam | 4:5213bee8158e | 51 | } |
redplam | 4:5213bee8158e | 52 | void tempinit(void) |
redplam | 4:5213bee8158e | 53 | { |
redplam | 4:5213bee8158e | 54 | while (!device.initialize()); // keep calling until it works |
redplam | 4:5213bee8158e | 55 | |
redplam | 4:5213bee8158e | 56 | int deviceCount = device.getDeviceCount(); |
redplam | 4:5213bee8158e | 57 | pc.printf("Found %d sensors\n\r",deviceCount); |
redplam | 4:5213bee8158e | 58 | |
redplam | 4:5213bee8158e | 59 | device.setResolution(twelveBit); |
redplam | 4:5213bee8158e | 60 | } |
redplam | 1:3aec489c7366 | 61 | unsigned int pwm_cold_peltier(int brightness ) |
redplam | 1:3aec489c7366 | 62 | { |
redplam | 1:3aec489c7366 | 63 | if (brightness==1) { |
redplam | 1:3aec489c7366 | 64 | peltier_cold_pulsewidth=peltier_cold_pulsewidth+peltier_step; |
redplam | 1:3aec489c7366 | 65 | peltier_cold.pulsewidth_us(peltier_cold_pulsewidth); |
redplam | 1:3aec489c7366 | 66 | }; |
redplam | 1:3aec489c7366 | 67 | if (brightness==0) { |
redplam | 1:3aec489c7366 | 68 | peltier_cold_pulsewidth=peltier_cold_pulsewidth-peltier_step; |
redplam | 1:3aec489c7366 | 69 | peltier_cold.pulsewidth_us(peltier_cold_pulsewidth); |
redplam | 1:3aec489c7366 | 70 | }; |
redplam | 1:3aec489c7366 | 71 | return peltier_cold_pulsewidth; |
redplam | 1:3aec489c7366 | 72 | } |
redplam | 1:3aec489c7366 | 73 | unsigned int pwm_hot_peltier(int brightness) |
redplam | 1:3aec489c7366 | 74 | { |
redplam | 1:3aec489c7366 | 75 | if (brightness==1) { |
redplam | 1:3aec489c7366 | 76 | peltier_hot_pulsewidth=peltier_hot_pulsewidth+peltier_step; |
redplam | 1:3aec489c7366 | 77 | peltier_hot.pulsewidth_us(peltier_hot_pulsewidth); |
redplam | 1:3aec489c7366 | 78 | } |
redplam | 1:3aec489c7366 | 79 | if (brightness==0) { |
redplam | 1:3aec489c7366 | 80 | peltier_hot_pulsewidth=peltier_hot_pulsewidth-peltier_step; |
redplam | 1:3aec489c7366 | 81 | peltier_hot.pulsewidth_us(peltier_hot_pulsewidth); |
redplam | 1:3aec489c7366 | 82 | } |
redplam | 1:3aec489c7366 | 83 | return peltier_hot_pulsewidth; |
redplam | 1:3aec489c7366 | 84 | } |
redplam | 1:3aec489c7366 | 85 | void peltier(bool fan_in,bool fan_out,bool cold,bool hot,int pwm_procent) |
redplam | 1:3aec489c7366 | 86 | { |
redplam | 4:5213bee8158e | 87 | //tempinit(); |
redplam | 4:5213bee8158e | 88 | //sensor.mode(PullUp); |
redplam | 4:5213bee8158e | 89 | // readTemp(1); |
redplam | 4:5213bee8158e | 90 | // dsen_temp_in=temp; |
redplam | 1:3aec489c7366 | 91 | // ROM_Code_t ROM_Code = ReadROM(); |
redplam | 1:3aec489c7366 | 92 | // lcd.setCursor(TextLCD::CurOff_BlkOn); |
redplam | 1:3aec489c7366 | 93 | // lcd.locate(0,0); |
redplam | 1:3aec489c7366 | 94 | // lcd.printf("Family code: 0x%X\n\r", ROM_Code.BYTES.familyCode); |
redplam | 1:3aec489c7366 | 95 | // wait(5.0); |
redplam | 1:3aec489c7366 | 96 | |
redplam | 1:3aec489c7366 | 97 | en_drv1=0;//disable |
redplam | 1:3aec489c7366 | 98 | en_drv2=0;//disable |
redplam | 1:3aec489c7366 | 99 | fan_in_peltier=0;//disable |
redplam | 1:3aec489c7366 | 100 | fan_out_peltier=0;//disable |
redplam | 1:3aec489c7366 | 101 | |
redplam | 1:3aec489c7366 | 102 | |
redplam | 1:3aec489c7366 | 103 | if(fan_in==true) { |
redplam | 1:3aec489c7366 | 104 | fan_in_peltier=1;//enable |
redplam | 1:3aec489c7366 | 105 | } |
redplam | 1:3aec489c7366 | 106 | if(fan_out==true) { |
redplam | 1:3aec489c7366 | 107 | fan_out_peltier=1;//enable |
redplam | 1:3aec489c7366 | 108 | } |
redplam | 1:3aec489c7366 | 109 | if ((cold==true)||(hot==true)) { |
redplam | 1:3aec489c7366 | 110 | en_drv1=1; |
redplam | 1:3aec489c7366 | 111 | en_drv2=1; |
redplam | 1:3aec489c7366 | 112 | } |
redplam | 1:3aec489c7366 | 113 | |
redplam | 1:3aec489c7366 | 114 | if ((cold==true)&&(hot==false)) { |
redplam | 1:3aec489c7366 | 115 | |
redplam | 1:3aec489c7366 | 116 | peltier_cold.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 117 | peltier_cold_pulsewidth=pwm_procent*10; |
redplam | 1:3aec489c7366 | 118 | peltier_cold.pulsewidth_us(peltier_cold_pulsewidth); |
redplam | 1:3aec489c7366 | 119 | } |
redplam | 1:3aec489c7366 | 120 | if ((hot==true)&&(cold==false)) { |
redplam | 1:3aec489c7366 | 121 | |
redplam | 1:3aec489c7366 | 122 | peltier_hot.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 123 | peltier_hot_pulsewidth=pwm_procent*10; |
redplam | 1:3aec489c7366 | 124 | peltier_hot.pulsewidth_us(peltier_hot_pulsewidth); |
redplam | 1:3aec489c7366 | 125 | } |
redplam | 4:5213bee8158e | 126 | |
redplam | 4:5213bee8158e | 127 | readTemp(1); |
redplam | 4:5213bee8158e | 128 | dsen_temp_in=temp; |
redplam | 4:5213bee8158e | 129 | readTemp(0); |
redplam | 4:5213bee8158e | 130 | dsen_temp_out=temp; |
redplam | 4:5213bee8158e | 131 | pc.printf("inside Temp is %2.1fC, outside temp plate is %2.1fC\n\r", dsen_temp_in,dsen_temp_out); |
redplam | 4:5213bee8158e | 132 | lcd.cls(); |
redplam | 4:5213bee8158e | 133 | lcd.locate(0,1); |
redplam | 4:5213bee8158e | 134 | lcd.printf("Temp is %2.1fC\n\r", dsen_temp_in); |
redplam | 1:3aec489c7366 | 135 | |
redplam | 1:3aec489c7366 | 136 | } |
redplam | 1:3aec489c7366 | 137 | void pel_c(int pwm) |
redplam | 1:3aec489c7366 | 138 | { |
redplam | 1:3aec489c7366 | 139 | peltier_cold.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 140 | peltier_cold_pulsewidth=pwm*10; |
redplam | 1:3aec489c7366 | 141 | peltier_cold.pulsewidth_us(peltier_cold_pulsewidth); |
redplam | 1:3aec489c7366 | 142 | |
redplam | 1:3aec489c7366 | 143 | } |
redplam | 1:3aec489c7366 | 144 | void pel_h(int pwm) |
redplam | 1:3aec489c7366 | 145 | { |
redplam | 1:3aec489c7366 | 146 | peltier_hot.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 147 | peltier_hot_pulsewidth=pwm*10; |
redplam | 1:3aec489c7366 | 148 | peltier_hot.pulsewidth_us(peltier_hot_pulsewidth); |
redplam | 1:3aec489c7366 | 149 | } |
redplam | 4:5213bee8158e | 150 | void peltier_auto(float degree){ |
redplam | 4:5213bee8158e | 151 | //tempinit(); |
redplam | 4:5213bee8158e | 152 | |
redplam | 4:5213bee8158e | 153 | // backlight=1; |
redplam | 4:5213bee8158e | 154 | |
redplam | 1:3aec489c7366 | 155 | en_drv1=1; |
redplam | 1:3aec489c7366 | 156 | en_drv2=1; |
redplam | 1:3aec489c7366 | 157 | fan_in_peltier=1;//enable |
redplam | 1:3aec489c7366 | 158 | fan_out_peltier=1;//enable |
redplam | 1:3aec489c7366 | 159 | while(1) { |
redplam | 1:3aec489c7366 | 160 | |
redplam | 4:5213bee8158e | 161 | readTemp(1); |
redplam | 4:5213bee8158e | 162 | dsen_temp_in=temp; |
redplam | 1:3aec489c7366 | 163 | |
redplam | 4:5213bee8158e | 164 | /* (degree>dsen_temp) { |
redplam | 1:3aec489c7366 | 165 | pel_h(0); |
redplam | 4:5213bee8158e | 166 | pel_c(90); |
redplam | 4:5213bee8158e | 167 | }*/ |
redplam | 4:5213bee8158e | 168 | if (degree<dsen_temp_in) { |
redplam | 1:3aec489c7366 | 169 | pel_c(0); |
redplam | 4:5213bee8158e | 170 | pel_h(90); |
redplam | 1:3aec489c7366 | 171 | |
redplam | 1:3aec489c7366 | 172 | } |
redplam | 4:5213bee8158e | 173 | if (degree==dsen_temp_in) { |
redplam | 1:3aec489c7366 | 174 | pel_c(0); |
redplam | 1:3aec489c7366 | 175 | pel_h(0); |
redplam | 1:3aec489c7366 | 176 | |
redplam | 1:3aec489c7366 | 177 | } |
redplam | 1:3aec489c7366 | 178 | |
redplam | 1:3aec489c7366 | 179 | lcd.cls(); |
redplam | 1:3aec489c7366 | 180 | lcd.locate(0,1); |
redplam | 4:5213bee8158e | 181 | |
redplam | 4:5213bee8158e | 182 | readTemp(1); |
redplam | 4:5213bee8158e | 183 | dsen_temp_in=temp; |
redplam | 4:5213bee8158e | 184 | readTemp(0); |
redplam | 4:5213bee8158e | 185 | dsen_temp_out=temp; |
redplam | 4:5213bee8158e | 186 | pc.printf("Temp is %2.1fC,dsen temp= %2.1f , outchamber plate temp =%2.1fC\n\r", dsen_temp_in,degree,dsen_temp_out); |
redplam | 4:5213bee8158e | 187 | // lcd.printf("Temp is %2.1fC\n\r", dsen_temp); |
redplam | 1:3aec489c7366 | 188 | |
redplam | 1:3aec489c7366 | 189 | |
redplam | 1:3aec489c7366 | 190 | |
redplam | 1:3aec489c7366 | 191 | //peltier_hot.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 192 | //peltier_hot_pulsewidth=1000;//at 50% |
redplam | 1:3aec489c7366 | 193 | //pwm_hot_peltier(1); |
redplam | 1:3aec489c7366 | 194 | // wait(1.0); |
redplam | 1:3aec489c7366 | 195 | |
redplam | 1:3aec489c7366 | 196 | |
redplam | 1:3aec489c7366 | 197 | |
redplam | 1:3aec489c7366 | 198 | /* peltier_cold.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 199 | peltier_cold_pulsewidth=peltier_pulsewidth; |
redplam | 1:3aec489c7366 | 200 | for (int i=1; i<=100; i++) { |
redplam | 1:3aec489c7366 | 201 | pwm_cold_peltier(1); |
redplam | 1:3aec489c7366 | 202 | wait (0.1); |
redplam | 1:3aec489c7366 | 203 | } |
redplam | 1:3aec489c7366 | 204 | for (int i=1; i<=100; i++) { |
redplam | 1:3aec489c7366 | 205 | pwm_cold_peltier(0); |
redplam | 1:3aec489c7366 | 206 | wait (0.1); |
redplam | 1:3aec489c7366 | 207 | } |
redplam | 1:3aec489c7366 | 208 | peltier_cold_pulsewidth=10; // set to 0 |
redplam | 1:3aec489c7366 | 209 | pwm_cold_peltier(0); // set to 0 |
redplam | 1:3aec489c7366 | 210 | |
redplam | 1:3aec489c7366 | 211 | peltier_hot.period_us(peltier_period); |
redplam | 1:3aec489c7366 | 212 | peltier_hot_pulsewidth=peltier_pulsewidth; |
redplam | 1:3aec489c7366 | 213 | for (int i=1; i<=100; i++) { |
redplam | 1:3aec489c7366 | 214 | pwm_hot_peltier(1); |
redplam | 1:3aec489c7366 | 215 | wait (0.1); |
redplam | 1:3aec489c7366 | 216 | } |
redplam | 1:3aec489c7366 | 217 | for (int i=1; i<=100; i++) { |
redplam | 1:3aec489c7366 | 218 | pwm_hot_peltier(0); |
redplam | 1:3aec489c7366 | 219 | wait (0.1); |
redplam | 1:3aec489c7366 | 220 | } |
redplam | 1:3aec489c7366 | 221 | peltier_hot_pulsewidth=10; // set to 0 |
redplam | 1:3aec489c7366 | 222 | pwm_hot_peltier(0); // set to 0 |
redplam | 1:3aec489c7366 | 223 | */ |
redplam | 1:3aec489c7366 | 224 | } |
redplam | 1:3aec489c7366 | 225 | |
redplam | 1:3aec489c7366 | 226 | } |