Rev Author Line No. Line
3458 kaklik 1 #define VERSION "0.1"
2 #define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"
3  
3379 kaklik 4 #include "main.h"
3458 kaklik 5 #use i2c(SLAVE,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2
3379 kaklik 6  
3458 kaklik 7 const int8 buf_len=8;
8  
9 int8 buffer[buf_len]; // I2C buffer
10  
11 int8 address=0;
12  
13 #include "..\common\dbloader.h"
14  
15 unsigned int16 timer0_overflow_count;
3463 kaklik 16 unsigned int16 anemo=0;
17 unsigned int16 rain=0;
3458 kaklik 18  
19 //we are using the rtctimer.c library, in which a counter is incremented
20 //every time the timer2 interrupt occurs (timer2 overflow). the time math
21 //needs to know what rate the timer2 interrupt occurs. this definition
22 //must match the rate the timer2 is configured for.
23 #define CLOCKS_PER_SECOND 1000
24  
3379 kaklik 25 #INT_SSP
26 void ssp_interupt ()
27 {
3458 kaklik 28 BYTE incoming, state;
3379 kaklik 29  
3380 kaklik 30 state = i2c_isr_state();
31 if(state < 0x80) //Master is sending data
32 {
3458 kaklik 33 incoming = i2c_read();
34 if(state == 1) //First received byte is address
35 {
36 address = incoming;
37 if (incoming == 2)
38 {
3463 kaklik 39 buffer[0]=make8(anemo,0);
40 buffer[1]=make8(anemo,1);
41 buffer[2]=make8(rain,0);
42 buffer[3]=make8(rain,1);
3458 kaklik 43 }
44 }
45 if(state == 2) //Second received byte is data
46 buffer[address] = incoming;
3380 kaklik 47 }
48 if(state == 0x80) //Master is requesting data
49 {
3458 kaklik 50 if(address <= buf_len) i2c_write(buffer[address]);
51 else i2c_write(ID[address - buf_len]);
3380 kaklik 52 }
3379 kaklik 53 }
54  
3464 kaklik 55  
56 #int_TIMER0 //osetruje preteceni citace od anemometru (RA4)
57 void TIMER0_isr(void)
58 {
59 timer0_overflow_count++;
60 }
61  
3458 kaklik 62 #int_TIMER1
63 void TIMER1_isr(void)
64 {
65 // 32.768 kHz krystal pro timer1 oscilátor
3463 kaklik 66 anemo = ((timer0_overflow_count * 0xFF) + get_timer0()); // pocet pulzu za 1s
3379 kaklik 67  
3458 kaklik 68 timer0_overflow_count=0; //nulovani
69 set_timer0(0);
70 set_timer1(0);
71 output_toggle(PIN_E0);
72 }
73  
3464 kaklik 74 #int_TIMER2
75 void TIMER2_isr(void)
3458 kaklik 76 {
3464 kaklik 77  
3458 kaklik 78 }
79  
80 #INT_EXT
81 void EXT_isr() //interrup from rain sensor clip.
82 {
83 rain++;
84 // if (input(PIN_B0)) ext_int_edge( H_TO_L ); osetreni pro pripad, ze by bylo treba cist obe hrany impulzu
85 // if (!input(PIN_B0)) ext_int_edge( L_TO_H );
86 }
87  
88  
89 void welcome(void) // uvodni zprava
90 {
91 printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION);
92 printf("\r\n %s \r\n",ID);// Welcome message
93 printf("# ver poradi ");
94 printf("check\r\n\r\n");
95 }
96  
3379 kaklik 97 void main()
98 {
99 setup_adc_ports(NO_ANALOGS|VSS_VDD);
3458 kaklik 100 // setup_adc(ADC_CLOCK_DIV_2);
3379 kaklik 101 setup_adc(ADC_OFF);
3458 kaklik 102 // setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use!
103 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
104 // setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS);
105 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
106 // setup_timer_2(T2_DISABLED,0,1);
3379 kaklik 107 setup_comparator(NC_NC_NC_NC);
108 setup_vref(FALSE);
3458 kaklik 109 // setup_oscillator(OSC_8MHZ|OSC_INTRC);
3379 kaklik 110  
3458 kaklik 111  
112 /* Setup timer 2
113 * On a 4 Mhz clock, this will trigger a timer2 interrupt every 1.0 ms
114 * For time.h to work properly, Timer2 must overflow every millisecond
115 * OverflowTime = 4 * (1/OscFrequency) * Prescale * Period * Postscale
116 * For 4 Mhz: .001 seconds = 4 * (1/4000000 seconds) * 4 * 250 * 1
117 */
3464 kaklik 118 #if getenv("CLOCK")==4000000)
3458 kaklik 119 setup_timer_2(T2_DIV_BY_1,250,4);
120 #elif getenv("CLOCK")==20000000)
121 setup_timer_2(T2_DIV_BY_4,250,5);
122 #else
123 #error Configure TIMER2 so it interrupts at a rate defined by CLOCKS_PER_SECOND
124 #endif
125  
126  
127 enable_interrupts(INT_SSP);
3464 kaklik 128 enable_interrupts(INT_TIMER2);
3458 kaklik 129 enable_interrupts(INT_TIMER1);
130 enable_interrupts(INT_TIMER0);
131 enable_interrupts(INT_EXT);
3379 kaklik 132 enable_interrupts(GLOBAL);
133  
134  
3458 kaklik 135 set_timer0(0);
136 set_timer1(0);
137 timer0_overflow_count=0;
138  
139 buffer[2]=0;
140 buffer[3]=0;
141 buffer[4]=0;
142 buffer[5]=0;
143  
144 welcome();
145  
3379 kaklik 146 while(true)
3462 kaklik 147 {
148 printf("count: %X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
3463 kaklik 149 printf("%Lu %Lu \n\r", anemo, rain);
3458 kaklik 150  
151 delay_ms(1000);
152  
3379 kaklik 153 }
154 }
155  
156  
157