| 960 | jacho | 1 |  | 
      
        |  |  | 2 | //Meteorologicka cast | 
      
        |  |  | 3 | #define VERSION "0.1" | 
      
        |  |  | 4 | #define AUTOR "Jan Chroust" | 
      
        |  |  | 5 | #define DATE "15.4.2013" | 
      
        |  |  | 6 |  | 
      
        |  |  | 7 | #include <main.h> | 
      
        |  |  | 8 | #include <math.h> | 
      
        |  |  | 9 | #include <string.h> | 
      
        |  |  | 10 |  | 
      
        |  |  | 11 | #include "SHT25.h" | 
      
        |  |  | 12 | #include "MPL3115.h" | 
      
        |  |  | 13 | #include "HMC5883L.h" | 
      
        |  |  | 14 |  | 
      
        |  |  | 15 | #define  SEND_DELAY     50       // Cas mezi dvema znaky na RS232 | 
      
        |  |  | 16 |  | 
      
        |  |  | 17 | char  VER[4]=VERSION; | 
      
        |  |  | 18 |  | 
      
        |  |  | 19 | unsigned int16 timer0_overflow_count; | 
      
        |  |  | 20 | float anemo=0; | 
      
        |  |  | 21 |  | 
      
        |  |  | 22 |  | 
      
        |  |  | 23 |  | 
      
        |  |  | 24 | #int_TIMER1 | 
      
        |  |  | 25 | void  TIMER1_isr(void)  | 
      
        |  |  | 26 | { | 
      
        |  |  | 27 | // 32.768 kHz krystal pro timer1 oscilátor | 
      
        |  |  | 28 |   anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); // pocet pulzu za 1s | 
      
        |  |  | 29 |   anemo = anemo / 2;  //pocet otacek za sekundu | 
      
        |  |  | 30 |  | 
      
        |  |  | 31 |   timer0_overflow_count=0; //nulovani | 
      
        |  |  | 32 |   set_timer0(0); | 
      
        |  |  | 33 |   set_timer1(0); | 
      
        |  |  | 34 |  | 
      
        |  |  | 35 | } | 
      
        |  |  | 36 |  | 
      
        |  |  | 37 | #int_TIMER0 //pro preteceni èítaèe pùlzù od anemometru (RA4) | 
      
        |  |  | 38 | void  TIMER0_isr(void)  | 
      
        |  |  | 39 | { | 
      
        |  |  | 40 |  timer0_overflow_count++; | 
      
        |  |  | 41 | } | 
      
        |  |  | 42 |  | 
      
        |  |  | 43 |  | 
      
        |  |  | 44 | float azimut (void) //vypocet azimutu smeru vetru | 
      
        |  |  | 45 | { | 
      
        |  |  | 46 | signed int16 X,Y,Z; | 
      
        |  |  | 47 | X= mag_readX();   | 
      
        |  |  | 48 | Y= mag_readY(); | 
      
        |  |  | 49 | Z= mag_readZ(); | 
      
        |  |  | 50 |  | 
      
        |  |  | 51 | float a, b; | 
      
        |  |  | 52 | a=(float)Y/X; | 
      
        |  |  | 53 | b=atan(a); | 
      
        |  |  | 54 | b = (b/3.14)*180; | 
      
        |  |  | 55 | b=abs(b); | 
      
        |  |  | 56 |  | 
      
        |  |  | 57 | if(X==0) //osetreni proti deleni 0 | 
      
        |  |  | 58 |    {  | 
      
        |  |  | 59 |        if(Y>0) | 
      
        |  |  | 60 |          {  | 
      
        |  |  | 61 |          b=90; | 
      
        |  |  | 62 |          } | 
      
        |  |  | 63 |       else | 
      
        |  |  | 64 |          { | 
      
        |  |  | 65 |          b=270; | 
      
        |  |  | 66 |          } | 
      
        |  |  | 67 |    }   | 
      
        |  |  | 68 |    else | 
      
        |  |  | 69 |       { | 
      
        |  |  | 70 |       if(X>0) | 
      
        |  |  | 71 |        { | 
      
        |  |  | 72 |        if(Y>=0) | 
      
        |  |  | 73 |        { | 
      
        |  |  | 74 |          b=180+b; | 
      
        |  |  | 75 |  | 
      
        |  |  | 76 |        } | 
      
        |  |  | 77 |        else | 
      
        |  |  | 78 |        { | 
      
        |  |  | 79 |          b=180-b; | 
      
        |  |  | 80 |  | 
      
        |  |  | 81 |        } | 
      
        |  |  | 82 |        } | 
      
        |  |  | 83 |       else | 
      
        |  |  | 84 |        { | 
      
        |  |  | 85 |        if(Y>=0) | 
      
        |  |  | 86 |        { | 
      
        |  |  | 87 |          b=360-b; | 
      
        |  |  | 88 |  | 
      
        |  |  | 89 |        } | 
      
        |  |  | 90 |        else | 
      
        |  |  | 91 |        { | 
      
        |  |  | 92 |          b=b; | 
      
        |  |  | 93 |  | 
      
        |  |  | 94 |        } | 
      
        |  |  | 95 |        } | 
      
        |  |  | 96 |  | 
      
        |  |  | 97 |       }    | 
      
        |  |  | 98 |  | 
      
        |  |  | 99 | return b; | 
      
        |  |  | 100 |  | 
      
        |  |  | 101 | } | 
      
        |  |  | 102 |  | 
      
        |  |  | 103 |  | 
      
        |  |  | 104 |  | 
      
        |  |  | 105 |  | 
      
        |  |  | 106 |  | 
      
        |  |  | 107 |  | 
      
        |  |  | 108 | void uvitani(void)               // uvodni zprava | 
      
        |  |  | 109 | { | 
      
        |  |  | 110 |    printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION);  | 
      
        |  |  | 111 |    printf("\r\n# Autor: %s Posledni uprava: %s \r\n",AUTOR, DATE);// Welcome message | 
      
        |  |  | 112 |    printf("# ver poradi "); | 
      
        |  |  | 113 |    printf("altimet_t[°C] altimet_a[m] altimet_p[Pa] "); | 
      
        |  |  | 114 |    printf("sht_t[°C] sht_h[%%]  Anemo[m/s]check\r\n\r\n"); | 
      
        |  |  | 115 | } | 
      
        |  |  | 116 |  | 
      
        |  |  | 117 | void main() | 
      
        |  |  | 118 | { | 
      
        |  |  | 119 |    setup_wdt(WDT_2304MS); //nastavení resetu pokud nedojde v cas k jeho vynulovani | 
      
        |  |  | 120 |    restart_wdt();  //---WDT | 
      
        |  |  | 121 |    setup_adc_ports(NO_ANALOGS|VSS_VDD); | 
      
        |  |  | 122 |    setup_adc(ADC_CLOCK_DIV_2); | 
      
        |  |  | 123 |    setup_spi(SPI_SS_DISABLED); | 
      
        |  |  | 124 |    setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); | 
      
        |  |  | 125 |    setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); | 
      
        |  |  | 126 |    setup_timer_2(T2_DISABLED,0,1); | 
      
        |  |  | 127 |    setup_ccp1(CCP_OFF); | 
      
        |  |  | 128 |    setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard | 
      
        |  |  | 129 |  | 
      
        |  |  | 130 |  | 
      
        |  |  | 131 | float altimet_t;  //teplota z MPL3115 | 
      
        |  |  | 132 | float altimet_p;  //tlak z MPL3115 | 
      
        |  |  | 133 | float altimet_a;  //vyska z MPL3115 | 
      
        |  |  | 134 | float sht25_t;    //teplota z SHT25 | 
      
        |  |  | 135 | float sht25_h;    //relativni vlhkost z SHT25 | 
      
        |  |  | 136 | float smer_v;     //smer vetru | 
      
        |  |  | 137 |  | 
      
        |  |  | 138 | unsigned int16 poradi=0; //cislo vzorku | 
      
        |  |  | 139 | unsigned int8 sht_config; | 
      
        |  |  | 140 |  | 
      
        |  |  | 141 | //nastaveni SHT25 | 
      
        |  |  | 142 | SHT25_soft_reset(); | 
      
        |  |  | 143 | sht_config = SHT25_RH12_T14 | SHT25_HEATER_OFF; //vypnuti topeni v SHT25 | 
      
        |  |  | 144 | SHT25_setup(sht_config); | 
      
        |  |  | 145 |  | 
      
        |  |  | 146 | //nastavení pøeruení pro anemometr | 
      
        |  |  | 147 | enable_interrupts(INT_TIMER1);    | 
      
        |  |  | 148 | enable_interrupts(INT_TIMER0); | 
      
        |  |  | 149 | enable_interrupts(GLOBAL); | 
      
        |  |  | 150 | restart_wdt();  //---WDT | 
      
        |  |  | 151 |  | 
      
        |  |  | 152 | uvitani(); | 
      
        |  |  | 153 | set_mag(); //nastaveni magnetometru pro smer vetru | 
      
        |  |  | 154 |  | 
      
        |  |  | 155 | // vynulovani promenych pro anemometr | 
      
        |  |  | 156 |    set_timer0(0); | 
      
        |  |  | 157 |    set_timer1(0); | 
      
        |  |  | 158 |    timer0_overflow_count=0; | 
      
        |  |  | 159 |    anemo=0; | 
      
        |  |  | 160 |  | 
      
        |  |  | 161 |    restart_wdt();  //---WDT | 
      
        |  |  | 162 |  | 
      
        |  |  | 163 | while(TRUE) | 
      
        |  |  | 164 |    { | 
      
        |  |  | 165 |    char output[12];   // vystupni zasobnik | 
      
        |  |  | 166 |    int8 j;           // ukazatel na retezec | 
      
        |  |  | 167 |    int8 check=0;     // Checksum is calculated between '$' and '*' | 
      
        |  |  | 168 |  | 
      
        |  |  | 169 |  | 
      
        |  |  | 170 |    mpl3115_setP(); //nastaveni pro tlak a teplotu | 
      
        |  |  | 171 |    delay_ms (500); | 
      
        |  |  | 172 |    altimet_t=mpl3115_T(); | 
      
        |  |  | 173 |    altimet_p=mpl3115_P(); | 
      
        |  |  | 174 |  | 
      
        |  |  | 175 |    mpl3115_setA(); //nastaveni pro vysku a teplotu | 
      
        |  |  | 176 |    delay_ms (500); | 
      
        |  |  | 177 |    altimet_a=mpl3115_A(); | 
      
        |  |  | 178 |  | 
      
        |  |  | 179 |    sht25_t=SHT25_get_temp(); //mereni hodnot z SHT25 | 
      
        |  |  | 180 |    sht25_h=SHT25_get_hum(); | 
      
        |  |  | 181 |  | 
      
        |  |  | 182 |    smer_v=azimut(); //vrati azimut aktualniho smeru vetru | 
      
        |  |  | 183 |  | 
      
        |  |  | 184 |  | 
      
        |  |  | 185 |          delay_us(SEND_DELAY); | 
      
        |  |  | 186 |          putc('$'); | 
      
        |  |  | 187 |          delay_us(SEND_DELAY); | 
      
        |  |  | 188 |          sprintf(output,"MST%s \0",VER); | 
      
        |  |  | 189 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 190 |          sprintf(output,"%4.0Lu \0", poradi); | 
      
        |  |  | 191 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 192 |          sprintf(output,"%6.2f \0", altimet_t ); | 
      
        |  |  | 193 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 194 |          sprintf(output,"%7.2f \0", altimet_a); | 
      
        |  |  | 195 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 196 |          sprintf(output,"%9.2f \0", altimet_p); | 
      
        |  |  | 197 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 198 |          sprintf(output,"%6.2f \0", sht25_t); | 
      
        |  |  | 199 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 200 |          sprintf(output,"%6.2f \0", sht25_h); | 
      
        |  |  | 201 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 202 |          sprintf(output,"%6.2f \0", smer_v); | 
      
        |  |  | 203 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 204 |          sprintf(output,"%5.2f \0", anemo); | 
      
        |  |  | 205 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } | 
      
        |  |  | 206 |          sprintf(output,"*%X\r\n\0", check); | 
      
        |  |  | 207 |          j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j++]); } | 
      
        |  |  | 208 |          delay_us(SEND_DELAY); | 
      
        |  |  | 209 |  | 
      
        |  |  | 210 |   poradi++; | 
      
        |  |  | 211 |    restart_wdt(); | 
      
        |  |  | 212 |   } | 
      
        |  |  | 213 |  | 
      
        |  |  | 214 | } | 
      
        |  |  | 215 |  | 
      
        |  |  | 216 |  | 
      
        |  |  | 217 |  |