Rev Author Line No. Line
1861 kaklik 1 #include "reflow.h"
2 #include <math.h>
3  
4 // nastaveni teplot a casu
5 #define TEPLOTA_PREDEHREVU 120
6 #define DOBA_PREDEHREVU 60
7  
8 #define TEPLOTA_VRCHOLU 210
9 #define DOBA_VRCHOLU 5
10  
11 // CPU IO rozhrani
12 #define LCD_RS PIN_C1 // rizeni registru LCD displeje
13 #define LCD_E PIN_C2 // enable LCD displeje
14 #define LCD_DATA_LSB PIN_D0 // data LCD
15 #include "lcd.c"
16  
17 #define TL1 PIN_B3 // tlacitko S1
18 #define TL2 PIN_B2 // tlacitko S2
19 #define TL3 PIN_B1 // tlacitko S3
20 #define TL4 PIN_B0 // tlacitko S4
21  
22 #define POWER_T4 PIN_A5 // ovladani optotriaku T4
23 #define POWER_T5 PIN_A4 // ovladani optotriaku T5
24  
25 #define ADC_PIN PIN_A0 //info, nelze menit - pin pouzit jako input analog
26 #define ADC_PIN_NC PIN_A1 //info, nelze menit - pin pouzit jako input analog
27 #define REF_PIN PIN_A3 //info, nelze menit - pin pouzit jako input reference 2.5V
28  
29 // interni
30 #define PowerOn() output_low(POWER_T4);output_low(POWER_T5)
31 #define PowerOff() output_high(POWER_T4);output_high(POWER_T5)
32  
33 // globalni promenne
34 struct time
35 {
36 volatile unsigned int8 hod;
37 volatile unsigned int8 min;
38 volatile unsigned int8 sec;
39 }cas;
40  
41 // funkce
42 void GeneralCpuInit()
43 {
44 output_high(POWER_T4);
45 output_high(POWER_T5);
46 port_b_pullups(true);
47  
48 setup_psp(PSP_DISABLED);
49 setup_spi(SPI_SS_DISABLED);
50  
51 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); //nepouzit
52 setup_timer_1(T1_DISABLED); //nepouzit
53 setup_timer_2(T2_DIV_BY_16,249,10); //rtc 40ms
54  
55 setup_comparator(NC_NC_NC_NC);
56 setup_vref(FALSE);
57  
58 enable_interrupts(GLOBAL);
59 enable_interrupts(INT_TIMER2);
60  
61 setup_adc_ports(AN0_AN1_VSS_VREF); //A0 vstup cidla, A1 nepozit, A3 - ref. 2.5V
62 setup_adc(ADC_CLOCK_DIV_8);
63 SET_ADC_CHANNEL(0); //AN0, PIN_A0
64 }
65  
66 unsigned int16 adc(void)
67 {
68 unsigned int16 analog;
69 unsigned int8 a;
70  
71 analog = 0;
72 for (a=0;a<32;a++)
73 {
74 analog += read_adc();
75 delay_us(50);
76 }
77 return (analog >> 5 ); // prumer = analog/32
78 }
79  
80 int16 teplota(void)
81 {
82 float32 Rpt;
83 float32 Tpt;
84  
85 Rpt = 1.0*adc()*2350.0/1024.0; //zapojeni OZ -> R=U/Iconst
86  
87 #define A 3.90802e-3
88 #define B -5.802e-7
89  
90 Tpt = (-A + sqrt(A*A - 4*B*(1.0 - Rpt/1000.0)))/2.0/B;
91  
92 return (unsigned int16)Tpt;
93 }
94  
95 void baterie(void)
96 {
97 static int8 stav=0;
98 switch (stav)
99 {
100 case 0: stav++;
101 lcd_define_char(0,LCD_CHAR_BAT0);
102 break;
103 case 1: stav++;
104 lcd_define_char(0,LCD_CHAR_BAT20);
105 break;
106 case 2: stav++;
107 lcd_define_char(0,LCD_CHAR_BAT40);
108 PowerOff();
109 break;
110 case 3: stav++;
111 lcd_define_char(0,LCD_CHAR_BAT60);
112 break;
113 case 4: stav++;
114 lcd_define_char(0,LCD_CHAR_BAT80);
115 break;
116 case 5: stav=0;
117 lcd_define_char(0,LCD_CHAR_BAT100);
118 PowerOn();
119 break;
120 default: stav=0;
121 }
122 }
123  
124 #int_TIMER2
125 void Rtc(void) //40ms
126 {
127 static unsigned int8 ms40=0;
128 struct time* time;
129  
130 time=&cas;
131 if ( ++ms40 < 25) return;
132  
133 ms40=0;
134 if (++(time->sec) >= 60)
135 {
136 time->sec=0; //1min
137 if (++(time->min) >= 60)
138 {
139 time->min = 0; //1hod
140 (time->hod)++;
141 }
142 }
143 }
144  
145 void nullcas(struct time* time)
146 {
147 disable_interrupts(INT_TIMER2);
148  
149 time->sec=0;
150 time->hod=0;
151 time->min=0;
152  
153 enable_interrupts(INT_TIMER2);
154 }
155  
156 // start
157 void main()
158 {
159 int8 tmp;
160  
161 GeneralCpuInit();
162  
163 PowerOff();
164  
165 lcd_init();
166 lcd_define_char(1,LCD_CHAR_STUPEN);
167  
168 printf(lcd_putc,"\fbatt: \20");
169  
170 nullcas(&cas);
171  
172 for (;;){
173 delay_ms(300);
174  
175 if (cas.sec != tmp){
176 tmp=cas.sec;
177 lcd_gotoxy(9,1);
178 printf(lcd_putc,"%2u:%02u:%02u",cas.hod,cas.min,cas.sec);
179 }
180  
181 baterie();
182 lcd_gotoxy(1,2);
183 printf(lcd_putc,"teplota: %ld\21C ",teplota());
184 }
185 }