/Designs/Measuring_instruments/AWS01B/SW/PIC16F887/i2c_wind_sensor/main.c
0,0 → 1,157
#define VERSION "0.1"
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"
 
#include "main.h"
#use i2c(SLAVE,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2
 
const int8 buf_len=8;
 
int8 buffer[buf_len]; // I2C buffer
 
int8 address=0;
 
#include "..\common\dbloader.h"
 
unsigned int16 timer0_overflow_count;
unsigned int16 anemo=0;
unsigned int16 rain=0;
 
//we are using the rtctimer.c library, in which a counter is incremented
//every time the timer2 interrupt occurs (timer2 overflow). the time math
//needs to know what rate the timer2 interrupt occurs. this definition
//must match the rate the timer2 is configured for.
#define CLOCKS_PER_SECOND 1000
 
#INT_SSP
void ssp_interupt ()
{
BYTE incoming, state;
 
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
{
address = incoming;
if (incoming == 2)
{
buffer[0]=make8(anemo,0);
buffer[1]=make8(anemo,1);
buffer[2]=make8(rain,0);
buffer[3]=make8(rain,1);
}
}
if(state == 2) //Second received byte is data
buffer[address] = incoming;
}
if(state == 0x80) //Master is requesting data
{
if(address <= buf_len) i2c_write(buffer[address]);
else i2c_write(ID[address - buf_len]);
}
}
 
 
#int_TIMER0 //osetruje preteceni citace od anemometru (RA4)
void TIMER0_isr(void)
{
timer0_overflow_count++;
}
 
#int_TIMER1
void TIMER1_isr(void)
{
// 32.768 kHz krystal pro timer1 oscilátor
anemo = ((timer0_overflow_count * 0xFF) + get_timer0()); // pocet pulzu za 1s
 
timer0_overflow_count=0; //nulovani
set_timer0(0);
set_timer1(0);
output_toggle(PIN_E0);
}
 
#int_TIMER2
void TIMER2_isr(void)
{
 
}
 
#INT_EXT
void EXT_isr() //interrup from rain sensor clip.
{
rain++;
// if (input(PIN_B0)) ext_int_edge( H_TO_L ); osetreni pro pripad, ze by bylo treba cist obe hrany impulzu
// if (!input(PIN_B0)) ext_int_edge( L_TO_H );
}
 
 
void welcome(void) // uvodni zprava
{
printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION);
printf("\r\n %s \r\n",ID);// Welcome message
printf("# ver poradi ");
printf("check\r\n\r\n");
}
 
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
// setup_adc(ADC_CLOCK_DIV_2);
setup_adc(ADC_OFF);
// setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use!
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
// setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
// setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// setup_oscillator(OSC_8MHZ|OSC_INTRC);
 
 
/* Setup timer 2
* On a 4 Mhz clock, this will trigger a timer2 interrupt every 1.0 ms
* For time.h to work properly, Timer2 must overflow every millisecond
* OverflowTime = 4 * (1/OscFrequency) * Prescale * Period * Postscale
* For 4 Mhz: .001 seconds = 4 * (1/4000000 seconds) * 4 * 250 * 1
*/
#if getenv("CLOCK")==4000000)
setup_timer_2(T2_DIV_BY_1,250,4);
#elif getenv("CLOCK")==20000000)
setup_timer_2(T2_DIV_BY_4,250,5);
#else
#error Configure TIMER2 so it interrupts at a rate defined by CLOCKS_PER_SECOND
#endif
 
enable_interrupts(INT_SSP);
enable_interrupts(INT_TIMER2);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
 
 
set_timer0(0);
set_timer1(0);
timer0_overflow_count=0;
 
buffer[2]=0;
buffer[3]=0;
buffer[4]=0;
buffer[5]=0;
 
welcome();
 
while(true)
{
printf("count: %X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
printf("%Lu %Lu \n\r", anemo, rain);
delay_ms(1000);
 
}
}
 
 
 
/Designs/Measuring_instruments/AWS01B/SW/PIC16F887/i2c_wind_sensor/main.h
0,0 → 1,22
#include <16F887.h>
#device adc=8
 
//#FUSES WDT // Watch Dog Timer
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //external crystal oscillator
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V
 
#use delay(clock=20000000)
 
//set RS232
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
/Designs/Measuring_instruments/AWS01B/SW/PIC16F887/i2c_wind_sensor/README.cs.txt
0,0 → 1,8
I2C snimac vycitajici rychlost větru a srážky.
 
 
===== Měření srážek =====
 
V MCU je čítač použitý pro počítání překlopení člunku ve srážkoměru. Je ošetřeno jeho přetečení tak, aby mohl čítat z hlediska srážek až do velkých čísel. Tento čítač se nenuluje, ale nechává se přetéct. K němu paralelně běží uptimo hodiny v MCU. A MCU se chová jako I²C SLAVE a hodnotu čítače tak lze vyčíst přes I2C.
MCU pak ve zbytku výpočetního času kontroluje hodnotu čítače a do dalšího registu zapisuje čas jeho poslední změny. (podobne jako dmesg, ale zaokrouhleno na cele sekundy)
Uptime bude zaznamenavan do registru, ktere je mozne precist pres I2C. Tento registr může být použit jako zdroj korekce v případě komplikací s nastavením aktuálního času.
/Designs/Measuring_instruments/AWS01B/SW/PIC16F887/i2c_wind_sensor/main.pjt
0,0 → 1,40
[PROJECT]
Target=motor.HEX
Development_Mode=
Processor=0x688F
ToolSuite=CCS
 
[Directories]
Include=C:\Program Files\PICC\devices\;C:\Program Files\PICC\Dr
Library=
LinkerScript=
 
[Target Data]
FileList=C:\dokumenty\svn\Kaklik\roboti\Robotour\SW\motor\motor.c
BuildTool=C-COMPILER
OptionString=+FM
AdditionalOptionString=
BuildRequired=1
 
[motor.c]
Type=4
Path=
FileList=
BuildTool=
OptionString=
AdditionalOptionString=
 
[mru-list]
1=motor.c
 
[Windows]
0=0000 motor.c 0 0 796 451 3 0
 
[Opened Files]
1=D:\svn\svnrobozor\roboti\Robotour\SW\motor\motor.c
2=C:\Program Files\PICC\devices\16F88.h
3=
4=
[Units]
Count=1
1=C:\dokumenty\svn\Kaklik\roboti\Robotour\SW\motor\motor.c (main)