/Designs/Measuring_instruments/AWS01A/SW/PIC16F887/main.c
1,5 → 1,5
/**** Automatic weather station 01A ****/
#define VERSION "0.2"
#define VERSION "0.1"
#define ID "$Id$"
#include "main.h"
#include ".\common\dbloader.h"
28,22 → 28,24
 
unsigned int16 timer0_overflow_count;
unsigned int16 timer1_overflow_count;
unsigned int16 timer0_overflow_count_last;
unsigned int16 timer0_last;
unsigned int16 anemo_count_max;
float anemo_max;
 
int1 barometer_present;
 
float anemo_compute()
{
float anemo;
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(((timer1_overflow_count * 0xFFFF) + get_timer1())/32768.0); // pulses per second calculation
anemo = anemo / 10.5; // frequency divided by anemomether constant.
return anemo;
}
 
#int_TIMER1
void TIMER1_isr(void)
{
// 32.768 kHz crystal, 16bit counter => every 2secs interrupt
unsigned int16 anemo_count;
unsigned int16 timer0 = get_timer0();
anemo_count = (((timer0_overflow_count - timer0_overflow_count_last) << 8) + (timer0 - timer0_last));
timer0_overflow_count_last = timer0_overflow_count;
timer0_last = timer0;
if (anemo_count > anemo_count_max) anemo_count_max=anemo_count;
float anemo;
anemo = anemo_compute();
if (anemo > anemo_max) anemo_max=anemo;
 
timer1_overflow_count++;
}
88,10 → 90,6
void main()
{
unsigned int16 seq=0;
timer0_overflow_count=0;
timer1_overflow_count=0;
timer0_overflow_count_last=0;
timer0_last=0;
 
setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovat
setup_wdt(WDT_2304MS);
106,6 → 104,7
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
output_high(CSN_SPI);
int1 repeat;
float anemo;
 
welcome(); // welcome print and device indentification
126,6 → 125,7
set_timer0(0);
set_timer1(0);
timer0_overflow_count=0;
anemo=0;
repeat=TRUE;
restart_wdt(); //---WDT
171,22 → 171,23
int8 check=0; // Checksum is calculated between '$' and '*'
float SHT_temp1=0,SHT_hum1=0;
float SHT_temp2=0,SHT_hum2=0;
int16 local_temp;
float barometer_temperature;
float barometer_pressure;
float local_temp;
float barometer_temperature, barometer_pressure;
float anemo;
 
 
{ // printf
local_temp = (int16)ds1820_read();
 
local_temp = ds1820_read()+27315;
sht_rd(SHT_temp1,SHT_hum1);
//SHT_temp1 = (SHT_temp1 + 273.15)*100;
SHT_temp1 = (SHT_temp1 + 273.15)*100;
SHT_temp2 = SHT25_get_temp();
SHT_hum2 = SHT25_get_hum();
//SHT_temp2 = (SHT_temp2 + 273.15)*100;
SHT_temp2 = (SHT_temp2 + 273.15)*100;
if (barometer_present == TRUE)
{
barometer_temperature = MPL_get_temperature();
barometer_temperature = (MPL_get_temperature() + 273.15)*100;
barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals
}
else
198,50 → 199,40
delay_us(SEND_DELAY);
putc('$');
delay_us(SEND_DELAY);
sprintf(output,"AWS%s \0",VER);
print_slow(output, &check);
sprintf(output,"%Lu \0", seq);
print_slow(output, &check);
sprintf(output,"%Ld \0", local_temp);
sprintf(output,"%5.0f \0", local_temp );
print_slow(output, &check);
sprintf(output,"%3.1f \0", SHT_temp1);
sprintf(output,"%5.0f \0", SHT_temp1);
print_slow(output, &check);
sprintf(output,"%3.1f \0", SHT_hum1);
print_slow(output, &check);
sprintf(output,"%3.1f \0", SHT_temp2);
sprintf(output,"%5.0f \0", SHT_temp2);
print_slow(output, &check);
sprintf(output,"%3.1f \0", SHT_hum2);
print_slow(output, &check);
sprintf(output,"%3.1f \0", barometer_temperature);
sprintf(output,"%5.0f \0", barometer_temperature);
print_slow(output, &check);
sprintf(output,"%5.1f \0", barometer_pressure);
print_slow(output, &check);
 
// optimization: (timer1_overflow_count << 16)/32768.0 = timer1_overflow_count << 1, so we can use int16 (and not int32)
anemo = ((float)((timer0_overflow_count << 8) + get_timer0()))/((float)(timer1_overflow_count << 1) + (float)(get_timer1())/32768.0); // pulses per second calculation
anemo = anemo / 10.5; // frequency divided by anemomether constant.
//anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(((timer1_overflow_count * 0xFFFF) + get_timer1())/32768.0); // pulses per second calculation
//anemo = anemo / 10.5; // frequency divided by anemomether constant.
sprintf(output,"%3.1f \0", anemo_compute());
 
timer0_overflow_count=0;
timer1_overflow_count=0;
set_timer0(0);
set_timer1(0);
timer0_overflow_count=0;
timer1_overflow_count=0;
timer0_overflow_count_last=0;
timer0_last=0;
sprintf(output,"%3.1f \0", anemo);
print_slow(output, &check);
//sprintf(output,"%3.1f \0", anemo_max);
//print_slow(output, &check);
if (anemo_count_max > 0)
{
// anemo_max comptutation; >>1 is division by two, which comes from the 2secs interval from timer1
anemo = (float)(anemo_count_max >> 1) / 10.5; // frequency divided by anemomether constant.
anemo_count_max = 0;
}
//anemo_max = 0;
 
sprintf(output,"%3.1f \0", anemo);
print_slow(output, &check);
sprintf(output,"*%X\r\n\0", check);
print_slow(output, &check);