1 |
#define VERSION "0.1" |
1 |
#define VERSION "0.1" |
2 |
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" |
2 |
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" |
3 |
|
3 |
|
4 |
#include "main.h" |
4 |
#include "main.h" |
5 |
#use i2c(SLAVE,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2 |
5 |
#use i2c(SLAVE,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2 |
6 |
|
6 |
|
7 |
#include <time.h> //standard C time library |
7 |
//#include <time.h> //standard C time library |
8 |
#include <rtctimer.c> //library for time.h that uses timer2 as time base |
8 |
//#include <rtctimer.c> //library for time.h that uses timer2 as time base |
9 |
|
9 |
|
10 |
#include <stdlib.h> |
10 |
//#include <stdlib.h> |
11 |
#include <input.c> //needed for the rs232 input routines |
11 |
//#include <input.c> //needed for the rs232 input routines |
12 |
|
12 |
|
13 |
|
13 |
|
14 |
int16 count=0xA5A5; |
14 |
int16 count=0xA5A5; |
15 |
const int8 buf_len=8; |
15 |
const int8 buf_len=8; |
16 |
|
16 |
|
17 |
int8 buffer[buf_len]; // I2C buffer |
17 |
int8 buffer[buf_len]; // I2C buffer |
18 |
|
18 |
|
19 |
int8 address=0; |
19 |
int8 address=0; |
20 |
|
20 |
|
21 |
#include "..\common\dbloader.h" |
21 |
#include "..\common\dbloader.h" |
22 |
|
22 |
|
23 |
unsigned int16 timer0_overflow_count; |
23 |
unsigned int16 timer0_overflow_count; |
24 |
float anemo=0; |
24 |
float anemo=0; |
25 |
unsigned int8 rain; |
25 |
unsigned int8 rain; |
26 |
|
26 |
|
27 |
//we are using the rtctimer.c library, in which a counter is incremented |
27 |
//we are using the rtctimer.c library, in which a counter is incremented |
28 |
//every time the timer2 interrupt occurs (timer2 overflow). the time math |
28 |
//every time the timer2 interrupt occurs (timer2 overflow). the time math |
29 |
//needs to know what rate the timer2 interrupt occurs. this definition |
29 |
//needs to know what rate the timer2 interrupt occurs. this definition |
30 |
//must match the rate the timer2 is configured for. |
30 |
//must match the rate the timer2 is configured for. |
31 |
#define CLOCKS_PER_SECOND 1000 |
31 |
#define CLOCKS_PER_SECOND 1000 |
32 |
|
32 |
|
33 |
#INT_SSP |
33 |
#INT_SSP |
34 |
void ssp_interupt () |
34 |
void ssp_interupt () |
35 |
{ |
35 |
{ |
36 |
BYTE incoming, state; |
36 |
BYTE incoming, state; |
37 |
|
37 |
|
38 |
state = i2c_isr_state(); |
38 |
state = i2c_isr_state(); |
39 |
if(state < 0x80) //Master is sending data |
39 |
if(state < 0x80) //Master is sending data |
40 |
{ |
40 |
{ |
41 |
incoming = i2c_read(); |
41 |
incoming = i2c_read(); |
42 |
if(state == 1) //First received byte is address |
42 |
if(state == 1) //First received byte is address |
43 |
{ |
43 |
{ |
44 |
address = incoming; |
44 |
address = incoming; |
45 |
if (incoming == 2) |
45 |
if (incoming == 2) |
46 |
{ |
46 |
{ |
47 |
buffer[0]=make8(count,0); |
47 |
buffer[0]=make8(count,0); |
48 |
buffer[1]=make8(count,1); |
48 |
buffer[1]=make8(count,1); |
49 |
} |
49 |
} |
50 |
} |
50 |
} |
51 |
if(state == 2) //Second received byte is data |
51 |
if(state == 2) //Second received byte is data |
52 |
buffer[address] = incoming; |
52 |
buffer[address] = incoming; |
53 |
} |
53 |
} |
54 |
if(state == 0x80) //Master is requesting data |
54 |
if(state == 0x80) //Master is requesting data |
55 |
{ |
55 |
{ |
56 |
if(address <= buf_len) i2c_write(buffer[address]); |
56 |
if(address <= buf_len) i2c_write(buffer[address]); |
57 |
else i2c_write(ID[address - buf_len]); |
57 |
else i2c_write(ID[address - buf_len]); |
58 |
} |
58 |
} |
59 |
} |
59 |
} |
60 |
|
60 |
|
61 |
#int_TIMER1 |
61 |
#int_TIMER1 |
62 |
void TIMER1_isr(void) |
62 |
void TIMER1_isr(void) |
63 |
{ |
63 |
{ |
64 |
// 32.768 kHz krystal pro timer1 oscilátor |
64 |
// 32.768 kHz krystal pro timer1 oscilátor |
65 |
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(0xFFFF/32768.0); // pocet pulzu za 1s |
65 |
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(0xFFFF/32768.0); // pocet pulzu za 1s |
66 |
|
66 |
|
67 |
timer0_overflow_count=0; //nulovani |
67 |
timer0_overflow_count=0; //nulovani |
68 |
set_timer0(0); |
68 |
set_timer0(0); |
69 |
set_timer1(0); |
69 |
set_timer1(0); |
70 |
output_toggle(PIN_E0); |
70 |
output_toggle(PIN_E0); |
71 |
} |
71 |
} |
72 |
|
72 |
|
73 |
#int_TIMER0 //pro preteceni \u010díta\u010de p\u016flz\u016f od anemometru (RA4) |
73 |
#int_TIMER0 //osetruje preteceni citace od anemometru (RA4) |
74 |
void TIMER0_isr(void) |
74 |
void TIMER0_isr(void) |
75 |
{ |
75 |
{ |
76 |
timer0_overflow_count++; |
76 |
timer0_overflow_count++; |
77 |
} |
77 |
} |
78 |
|
78 |
|
79 |
#INT_EXT |
79 |
#INT_EXT |
80 |
void EXT_isr() //interrup from rain sensor clip. |
80 |
void EXT_isr() //interrup from rain sensor clip. |
81 |
{ |
81 |
{ |
82 |
rain++; |
82 |
rain++; |
83 |
// if (input(PIN_B0)) ext_int_edge( H_TO_L ); osetreni pro pripad, ze by bylo treba cist obe hrany impulzu |
83 |
// if (input(PIN_B0)) ext_int_edge( H_TO_L ); osetreni pro pripad, ze by bylo treba cist obe hrany impulzu |
84 |
// if (!input(PIN_B0)) ext_int_edge( L_TO_H ); |
84 |
// if (!input(PIN_B0)) ext_int_edge( L_TO_H ); |
85 |
} |
85 |
} |
86 |
|
86 |
|
87 |
|
87 |
|
88 |
void welcome(void) // uvodni zprava |
88 |
void welcome(void) // uvodni zprava |
89 |
{ |
89 |
{ |
90 |
printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION); |
90 |
printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION); |
91 |
printf("\r\n %s \r\n",ID);// Welcome message |
91 |
printf("\r\n %s \r\n",ID);// Welcome message |
92 |
printf("# ver poradi "); |
92 |
printf("# ver poradi "); |
93 |
printf("check\r\n\r\n"); |
93 |
printf("check\r\n\r\n"); |
94 |
} |
94 |
} |
95 |
|
95 |
|
96 |
void InitTime(void) |
96 |
/*void InitTime(void) |
97 |
{ |
97 |
{ |
98 |
struct_tm t; |
98 |
struct_tm t; |
99 |
|
99 |
|
100 |
//tm_year is years since 1900. |
100 |
//tm_year is years since 1900. |
101 |
printf("\r\nYear (0-99): "); |
101 |
printf("\r\nYear (0-99): "); |
102 |
t.tm_year = (int16)get_int() + (int16)100; //add 100 to put is into 2000 |
102 |
t.tm_year = (int16)get_int() + (int16)100; //add 100 to put is into 2000 |
103 |
|
103 |
|
104 |
printf("\r\nMonth (1-12): "); |
104 |
printf("\r\nMonth (1-12): "); |
105 |
t.tm_mon = get_int() - 1; |
105 |
t.tm_mon = get_int() - 1; |
106 |
|
106 |
|
107 |
printf("\r\nDay (1-31): "); |
107 |
printf("\r\nDay (1-31): "); |
108 |
t.tm_mday = get_int() - 1; |
108 |
t.tm_mday = get_int() - 1; |
109 |
|
109 |
|
110 |
printf("\r\nHour (0-23): "); |
110 |
printf("\r\nHour (0-23): "); |
111 |
t.tm_hour = get_int(); |
111 |
t.tm_hour = get_int(); |
112 |
|
112 |
|
113 |
printf("\r\nMinute (0-59): "); |
113 |
printf("\r\nMinute (0-59): "); |
114 |
t.tm_min = get_int(); |
114 |
t.tm_min = get_int(); |
115 |
|
115 |
|
116 |
SetTime(&t); |
116 |
SetTime(&t); |
117 |
|
117 |
|
118 |
printf("\r\n\n"); |
118 |
printf("\r\n\n"); |
119 |
} |
119 |
} |
120 |
|
120 |
*/ |
121 |
|
121 |
|
122 |
void main() |
122 |
void main() |
123 |
{ |
123 |
{ |
124 |
|
124 |
|
125 |
char tString[32]; |
125 |
// char tString[32]; |
126 |
unsigned int32 t; |
126 |
// unsigned int32 t; |
127 |
time_t tTime = 0; |
127 |
// time_t tTime = 0; |
128 |
|
128 |
|
129 |
|
129 |
|
130 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
130 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
131 |
// setup_adc(ADC_CLOCK_DIV_2); |
131 |
// setup_adc(ADC_CLOCK_DIV_2); |
132 |
setup_adc(ADC_OFF); |
132 |
setup_adc(ADC_OFF); |
133 |
// setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use! |
133 |
// setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use! |
134 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
134 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
135 |
// setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS); |
135 |
// setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS); |
136 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
136 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
137 |
// setup_timer_2(T2_DISABLED,0,1); |
137 |
// setup_timer_2(T2_DISABLED,0,1); |
138 |
setup_comparator(NC_NC_NC_NC); |
138 |
setup_comparator(NC_NC_NC_NC); |
139 |
setup_vref(FALSE); |
139 |
setup_vref(FALSE); |
140 |
// setup_oscillator(OSC_8MHZ|OSC_INTRC); |
140 |
// setup_oscillator(OSC_8MHZ|OSC_INTRC); |
141 |
|
141 |
|
142 |
|
142 |
|
143 |
InitTime(); |
143 |
// InitTime(); |
144 |
|
144 |
|
145 |
/* Setup timer 2 |
145 |
/* Setup timer 2 |
146 |
* On a 4 Mhz clock, this will trigger a timer2 interrupt every 1.0 ms |
146 |
* On a 4 Mhz clock, this will trigger a timer2 interrupt every 1.0 ms |
147 |
* For time.h to work properly, Timer2 must overflow every millisecond |
147 |
* For time.h to work properly, Timer2 must overflow every millisecond |
148 |
* OverflowTime = 4 * (1/OscFrequency) * Prescale * Period * Postscale |
148 |
* OverflowTime = 4 * (1/OscFrequency) * Prescale * Period * Postscale |
149 |
* For 4 Mhz: .001 seconds = 4 * (1/4000000 seconds) * 4 * 250 * 1 |
149 |
* For 4 Mhz: .001 seconds = 4 * (1/4000000 seconds) * 4 * 250 * 1 |
150 |
*/ |
150 |
*/ |
151 |
#if getenv("CLOCK")==4000000) |
151 |
/* #if getenv("CLOCK")==4000000) |
152 |
setup_timer_2(T2_DIV_BY_1,250,4); |
152 |
setup_timer_2(T2_DIV_BY_1,250,4); |
153 |
#elif getenv("CLOCK")==20000000) |
153 |
#elif getenv("CLOCK")==20000000) |
154 |
setup_timer_2(T2_DIV_BY_4,250,5); |
154 |
setup_timer_2(T2_DIV_BY_4,250,5); |
155 |
#else |
155 |
#else |
156 |
#error Configure TIMER2 so it interrupts at a rate defined by CLOCKS_PER_SECOND |
156 |
#error Configure TIMER2 so it interrupts at a rate defined by CLOCKS_PER_SECOND |
157 |
#endif |
157 |
#endif |
158 |
|
158 |
|
159 |
/* Enable the timer 2 interrupt, or it will not fire */ |
159 |
/* Enable the timer 2 interrupt, or it will not fire */ |
160 |
enable_interrupts(INT_TIMER2); |
160 |
// enable_interrupts(INT_TIMER2); |
161 |
/* Enable interrupts globally too, otherwise no interrupt will fire */ |
161 |
/* Enable interrupts globally too, otherwise no interrupt will fire */ |
162 |
|
162 |
|
163 |
|
163 |
|
164 |
|
164 |
|
165 |
enable_interrupts(INT_SSP); |
165 |
enable_interrupts(INT_SSP); |
166 |
// enable_interrupts(INT_TIMER2); |
166 |
// enable_interrupts(INT_TIMER2); |
167 |
enable_interrupts(INT_TIMER1); |
167 |
enable_interrupts(INT_TIMER1); |
168 |
enable_interrupts(INT_TIMER0); |
168 |
enable_interrupts(INT_TIMER0); |
169 |
enable_interrupts(INT_EXT); |
169 |
enable_interrupts(INT_EXT); |
170 |
enable_interrupts(GLOBAL); |
170 |
enable_interrupts(GLOBAL); |
171 |
|
171 |
|
172 |
|
172 |
|
173 |
set_timer0(0); |
173 |
set_timer0(0); |
174 |
set_timer1(0); |
174 |
set_timer1(0); |
175 |
timer0_overflow_count=0; |
175 |
timer0_overflow_count=0; |
176 |
rain=0; |
176 |
rain=0; |
177 |
|
177 |
|
178 |
buffer[2]=0; |
178 |
buffer[2]=0; |
179 |
buffer[3]=0; |
179 |
buffer[3]=0; |
180 |
buffer[4]=0; |
180 |
buffer[4]=0; |
181 |
buffer[5]=0; |
181 |
buffer[5]=0; |
182 |
|
182 |
|
183 |
|
183 |
|
184 |
welcome(); |
184 |
welcome(); |
185 |
|
185 |
|
186 |
set_timer1(0); |
186 |
set_timer1(0); |
187 |
|
187 |
|
188 |
while(true) |
188 |
while(true) |
189 |
{ |
- |
|
190 |
|
189 |
{ |
191 |
set_timer1(0); |
- |
|
192 |
delay_ms(999); |
- |
|
193 |
delay_us(966); |
- |
|
194 |
// count=get_timer1(); |
- |
|
195 |
// Get the time |
- |
|
196 |
tTime = time(NULL); |
- |
|
197 |
// Get the string representation of the time */ |
- |
|
198 |
|
- |
|
199 |
ctime(&tTime, tString); |
- |
|
200 |
|
- |
|
201 |
|
- |
|
202 |
/* Print the time to RS-232 */ |
- |
|
203 |
printf("Time: %s\n\r", tString); |
- |
|
204 |
|
- |
|
205 |
printf("count: %Lu %X %X %X %X\r\n",count, buffer[0],buffer[1],buffer[2],buffer[3]); |
190 |
printf("count: %X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); |
206 |
printf("%6.1f %u \n\r", anemo, rain); |
191 |
printf("%6.1f %u \n\r", anemo, rain); |
207 |
|
192 |
|
208 |
delay_ms(1000); |
193 |
delay_ms(1000); |
209 |
|
194 |
|
210 |
} |
195 |
} |
211 |
} |
196 |
} |
212 |
|
197 |
|
213 |
|
198 |
|
214 |
|
199 |
|