/Designs/Measuring_instruments/RMDS02C/SW/PIC16F887/main.c
0,0 → 1,171
// Atomic counter with I2C and RS232 output
 
// Usage conditions:
// 1. The first I2C or RS232 readout can be performed minimally 20 s after power up.
// 2. The I2C internal address 0 has to be read first.
// 3. An I2C readout can be performed at 15-th, 35-th and 55-th second of UTC.
//
// Counter gives 32 bit value:
// I2C register address 0 = LSB
// I2C register address 3 = MSB
 
#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)
 
#include <string.h>
 
#define SEL0 PIN_E0 // external counter division ratio
#define SEL1 PIN_E1 // external counter division ratio
#define MR PIN_E2 // external counter master reset
#define CLKI PIN_C0 // internal counter input
 
unsigned int32 count; // count per second
 
#define BUF_LEN 4
int8 buffer[BUF_LEN]; // I2C buffer
int8 address=0;
 
unsigned int16 of=0; // count of overflow
 
// 1x 100 us per 10 s UTC synchronised
const char cmd[40]={0xB5, 0x62, 0x06, 0x31, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x80, 0x96, 0x98, 0x00, 0xE0, 0xC8, 0x10, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0xC6, 0x51};
#INT_SSP
void ssp_interupt ()
{
int8 incoming, state;
 
state = i2c_isr_state();
 
if(state < 0x80) //Master is sending data
{
incoming = i2c_read(); // Read byte
 
if(state == 1) //Second received byte is address of register
{
address = incoming;
}
}
if(state == 0x80) //Master is requesting data
{
//i2c_read(); // Dummy read of I2C device address
if(address == 0) // Change buffer atomically at reading of the first byte
{
buffer[0]=make8(count,0);
buffer[1]=make8(count,1);
buffer[2]=make8(count,2);
buffer[3]=make8(count,3);
}
if(address <= BUF_LEN) i2c_write(buffer[address]); // Prepare one byte to SSP buffer
else
{
i2c_write(0x00); // There is nothing to prepare, so zero
}
}
 
if(state == 0x81) //Master is requesting data
{
i2c_write(buffer[1]); // Prepare next byte to SSP buffer
}
if(state == 0x82) //Master is requesting data
{
i2c_write(buffer[2]); // Prepare next byte to SSP buffer
}
if(state == 0x83) //Master is requesting data
{
i2c_write(buffer[3]); // Prepare next byte to SSP buffer
}
 
if(state > 0x83) //Master is requesting data
{
i2c_write(0x00); // There is nothing to prepare, so zero
}
}
 
 
 
#int_EXT // Interrupt from 1PPS (RB0)
void EXT_isr(void)
{
unsigned int16 countH;
unsigned int8 countL;
int16 of2;
of2=of; // read overflow counter
countH=get_timer1(); // read internal counter
countL=0;
output_low(SEL0);
output_low(SEL1);
countL=input(CLKI); // read bit 0 of external counter
output_high(SEL0);
// output_low(SEL1);
countL|=input(CLKI)<<1; // read bit 1 of external counter
output_low(SEL0);
output_high(SEL1);
countL|=input(CLKI)<<2; // read bit 2 of external counter
output_high(SEL0);
// output_high(SEL1);
countL|=input(CLKI)<<3; // read bit 3 of external counter
 
output_low(MR); // External counter Master Reset
output_high(MR);
set_timer1(0); // Internal counter reset
of=0; // Overflow counter reset
count=((unsigned int32)of2<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate
 
// printf("%010Lu\r\n", count);
}
 
#int_TIMER1 // Interrupf from overflow
void TIMER1_isr(void)
{
of++;
}
 
 
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
// setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use!
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_wdt(WDT_2304MS);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
 
restart_wdt();
delay_ms(1000);
restart_wdt();
// setup GPS
{
int n;
for (n=0;n<40;n++) putc(cmd[n]);
}
 
ext_int_edge( L_TO_H ); // set 1PPS active edge
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
buffer[0]=0x0; // Clear I2C output buffer
buffer[1]=0x0;
buffer[2]=0x0;
buffer[3]=0x0;
 
printf("\r\ncvak...\r\n");
 
while(true)
{
restart_wdt();
delay_ms(1500);
// printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
printf("%010Lu\r\n", count);
}
}
/Designs/Measuring_instruments/RMDS02C/SW/PIC16F887/main.hex
0,0 → 1,112
:1000000002308A00A12A0000FF00030E8301A10034
:100010000A08A0008A010408A2007708A300780853
:10002000A4007908A5007A08A600831383120B1E8A
:100030001B288B1838288C308400001C21280C18B1
:100040003B288C308400801D27288C193E282208EC
:1000500084002308F7002408F8002508F900260882
:10006000FA0020088A00210E8300FF0E7F0E09008F
:100070008A110A12D4288A110A1295298A110A12A1
:100080007A280A108A100A118207B53462340634BD
:10009000313420340034003400340034003432343D
:1000A0000034003400348034963498340034E03422
:1000B000C834103400346434003400340034003464
:1000C000003400340034003400340034003473341D
:1000D000003400340034C6345134130854089300FB
:1000E00014168C11831614187328F8018312141A2D
:1000F000F80A08008316941A85288312B60183161D
:10010000141D85288312B617831683123608B60A83
:10011000D20052087F3C031C972814138C1D8E2894
:1001200013081416D100520B97285108B300520837
:10013000803C031DB528B308031DA6282B08AF007B
:100140002C08B0002D08B1002E08B2003308043C82
:10015000031CB3282F303307840083130008D30017
:10016000D4006D20B528D4016D205208813C031DB8
:10017000BC283008D4006D205208823C031DC328DF
:100180003108D4006D205208833C031DCA28320870
:10019000D4006D205208833C0318D028D4016D2070
:1001A0008C118A110A1227283508D5003408D4008A
:1001B0000F08FA000E08F7000F087A02031DD8286E
:1001C0007708D1007A08D200D30183160910831270
:1001D00009108316891083128910281428088316A1
:1001E00087008312D3010718D30A831609108312DC
:1001F0000914281428088316870000308312071872
:100200000130F7000310F70D7708D30483160910A7
:100210008312091083168910831289142814280860
:10022000831687000030831207180130F700F70D9E
:10023000F70DFC30F7057708D304831609108312F5
:100240000914281428088316870000308312071821
:100250000130F700F70DF70DF70DF830F7057708C7
:10026000D30483160911831209118316091183120D
:1002700009158F018E01B501B401D901D8015508C6
:10028000D7005408D6005508D9005408D800D60124
:10029000D701D80DD90DD80DD90DD80DD90DD80D40
:1002A000D90DF030D805DE01DD01510DF700520DFA
:1002B000F8005D0DF9005E0DFA00F70DF80DF90D6F
:1002C000FA0DF70DF80DF90DFA0DF70DF80DF90D02
:1002D000FA0DF030F7057708D60778080318780F7D
:1002E000D70779080318790FD8077A0803187A0F07
:1002F000D907F801F901FA0153085607AB0057086E
:10030000AC0078080318780FAC075808AD007908DE
:100310000318790FAD075908AE007A0803187A0F51
:10032000AE078B108A110A122728B40A0319B50ADE
:100330000C108A110A1227280D05633BE1352E1790
:10034000AE060A0039308400831300080319B5296A
:100350000630F800F701F70BAB29F80BAA297B3020
:10036000F700F70BB129800BA829080083160317A3
:100370008C170C140000000083120C087F3903193D
:10038000FC290313B80003170D080313B900031762
:100390000F080313BA0038080C1ECC29990039083D
:1003A00003178D0003133A0803178F0083168C1769
:1003B0000C140000000083120C0D0E0D7F39031980
:1003C000FC290313B80003170D080313B900031722
:1003D0000F080313BA0038080C1EEC2999003908DD
:1003E00003178D0003133A0803178F008D0A0319B2
:1003F0008F0A0313B629031703138A110A123E2B1F
:10040000F701F801F901FA01CC01CD01CE01CF01CC
:100410004B084A044904480403193E2A2030D000FE
:100420000310C40DC50DC60DC70DCC0DCD0DCE0DE1
:10043000CF0D4B084F02031D272A4A084E02031D09
:10044000272A49084D02031D272A48084C02031C8D
:10045000382A4808CC024908031C490FCD024A0833
:10046000031C4A0FCE024B08031C4B0FCF02031490
:10047000F70DF80DF90DFA0DD00B102A4C30840051
:10048000831308000408BC003B30C3009A30C2004C
:10049000CA30C100C0010A30BE003B08C7003A089C
:1004A000C6003908C5003808C4004308CB0042081C
:1004B000CA004108C9004008C80000220008B8006E
:1004C000840A0008B900840A0008BA00840A0008F7
:1004D000BB00F708031D7F2A3E08013A03197F2A53
:1004E0003C080319802A0F393E020319792A0318A0
:1004F000862ABC1B862A3C1B802A2030812ABC010C
:100500003030F70777080C1E832A99004308C7008C
:100510004208C6004108C5004008C400CB01CA011A
:10052000C9010A30C80000227A08C3007908C20055
:100530007808C1007708C000BE0B4D2A8A110A1244
:10054000512B840183131F308305831603178715EE
:1005500008300313990002309A00A63098009030BA
:1005600083129800A815280883168700831228167E
:10057000280883168700A230930036308312940037
:10058000B301B501B401831603170908C039890006
:1005900003131F129F120030031788008312870174
:1005A000880189010313A701FF30A800A901AA014E
:1005B0000330B600831603170908C03989000313F6
:1005C0001F129F12003003178800831203131F109D
:1005D00083160108C73908388100093083120317D0
:1005E00085000F30F70003138101813084008313ED
:1005F0000008F0390738800064000008F739F7195F
:10060000F03977048000073090000030F800920045
:100610000030831692008312031787018801890135
:10062000831603139701640004308312B800FA3074
:10063000B900A221B80B172B6400B7013708273C7B
:10064000031C292B370841200C1E242B9900B70AC4
:100650001E2B831601170C1483120B1683168C1590
:10066000C03083128B04AF01B001B101B2019C30E4
:1006700003178D0001308F000313B6296400063084
:10068000B800FA30B900A221B80B412B4A308400DF
:100690002E08BB002D08BA002C08B9002B08B800A2
:1006A000422A0D300C1E522B99000A300C1E562B7C
:0606B00099003E2B6300DF
:04400E00EA2CFF3F5A
:00000001FF
;PIC16F887
;CRC=F2B1 CREATED="21-II-14 12:29"
/Designs/Measuring_instruments/RMDS02C/SW/PIC16F887/main.h
0,0 → 1,21
#include <16F887.h>
#device adc=8
 
#FUSES WDT //Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT //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)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)