Problem with comparison.
/Designs/Measuring_instruments/RMDS02C/SW/PIC16F887/main.c |
---|
0,0 → 1,175 |
// 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 |
// configure GPS |
void setup_GPS() |
{ |
// 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 n; |
for (n=0;n<40;n++) putc(cmd[n]); |
} |
#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 |
setup_GPS(); |
} |
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 |
setup_GPS(); |
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,113 |
:1000000002308A00AF2A0000FF00030E8301A10026 |
:100010000A08A0008A010408A2007708A300780853 |
:10002000A4007908A5007A08A600831383120B1E8A |
:100030001B288B1838288C308400001C21280C18B1 |
:100040003B288C308400801D27288C193E282208EC |
:1000500084002308F7002408F8002508F900260882 |
:10006000FA0020088A00210E8300FF0E7F0E09008F |
:100070008A110A12E2288A110A12A3298A110A1285 |
:1000800087280A108A100A118207B53462340634B0 |
:10009000313420340034003400340034003432343D |
:1000A0000034003400348034963498340034E03422 |
:1000B000C834103400346434003400340034003464 |
:1000C000003400340034003400340034003473341D |
:1000D000003400340034C6345134130854089300FB |
:1000E00014168C11831614187328F8018312141A2D |
:1000F000F80A0800D3015308273C031C862853083C |
:1001000041200C1E81289900D30A7B280800831601 |
:10011000941A92288312B7018316141D9228831211 |
:10012000B717831683123708B70AD20052087F3CEC |
:10013000031CA42814138C1D9B2813081416D1002B |
:10014000520BA4285108B4005208803C031DC22859 |
:10015000B408031DB3282C08B0002D08B1002E08E8 |
:10016000B2002F08B3003408043C031CC028303010 |
:100170003407840083130008D300D4006D20C22804 |
:10018000D4016D205208813C031DC9283108D400D8 |
:100190006D205208823C031DD0283208D4006D2007 |
:1001A0005208833C031DD8283308D4006D207A20E0 |
:1001B0005208833C0318DE28D4016D208C118A116B |
:1001C0000A1227283608D5003508D4000F08FA008F |
:1001D0000E08F7000F087A02031DE6287708D10001 |
:1001E0007A08D200D30183160910831209108316EE |
:1001F0008910831289102914290883168700831215 |
:10020000D3010718D30A831609108312091429147D |
:100210002908831687000030831207180130F70081 |
:100220000310F70D7708D304831609108312091001 |
:1002300083168910831289142914290883168700CC |
:100240000030831207180130F700F70DF70DFC306E |
:10025000F7057708D30483160910831209142914AB |
:100260002908831687000030831207180130F70031 |
:10027000F70DF70DF70DF830F7057708D30483165F |
:1002800009118312091183160911831209158F01AF |
:100290008E01B601B501D901D8015508D70054081F |
:1002A000D6005508D9005408D800D601D701D80D7A |
:1002B000D90DD80DD90DD80DD90DD80DD90DF030D7 |
:1002C000D805DE01DD01510DF700520DF8005D0D7E |
:1002D000F9005E0DFA00F70DF80DF90DFA0DF70DA6 |
:1002E000F80DF90DFA0DF70DF80DF90DFA0DF030C6 |
:1002F000F7057708D60778080318780FD707790825 |
:100300000318790FD8077A0803187A0FD907F8016C |
:10031000F901FA0153085607AC005708AD007808F8 |
:100320000318780FAD075808AE0079080318790F45 |
:10033000AE075908AF007A0803187A0FAF078B1081 |
:100340008A110A122728B50A0319B60A0C108A1155 |
:100350000A1227280D05633BE1352E17AE060A0069 |
:1003600039308400831300080319C3290630F800CC |
:10037000F701F70BB929F80BB8297B30F700F70B19 |
:10038000BF29800BB6290800831603178C170C149D |
:100390000000000083120C087F3903190A2A031396 |
:1003A000B80003170D080313B90003170F08031350 |
:1003B000BA0038080C1EDA299900390803178D0095 |
:1003C00003133A0803178F0083168C170C140000D0 |
:1003D000000083120C0D0E0D7F3903190A2A031336 |
:1003E000B80003170D080313B90003170F08031310 |
:1003F000BA0038080C1EFA299900390803178D0035 |
:1004000003133A0803178F008D0A03198F0A031389 |
:10041000C429031703138A110A12472BF701F801A5 |
:10042000F901FA01CC01CD01CE01CF014B084A04FC |
:100430004904480403194C2A2030D0000310C40D8D |
:10044000C50DC60DC70DCC0DCD0DCE0DCF0D4B0876 |
:100450004F02031D352A4A084E02031D352A49085A |
:100460004D02031D352A48084C02031C462A480841 |
:10047000CC024908031C490FCD024A08031C4A0F4D |
:10048000CE024B08031C4B0FCF020314F70DF80DDF |
:10049000F90DFA0DD00B1E2A4C308400831308008E |
:1004A0000408BC003B30C3009A30C200CA30C1000F |
:1004B000C0010A30BE003B08C7003A08C600390830 |
:1004C000C5003808C4004308CB004208CA004108F0 |
:1004D000C9004008C8000E220008B800840A0008BD |
:1004E000B900840A0008BA00840A0008BB00F708B3 |
:1004F000031D8D2A3E08013A03198D2A3C08031971 |
:100500008E2A0F393E020319872A0318942ABC1B2E |
:10051000942A3C1B8E2A20308F2ABC013030F707EA |
:1005200077080C1E912A99004308C7004208C600AC |
:100530004108C5004008C400CB01CA01C9010A3006 |
:10054000C8000E227A08C3007908C2007808C100EA |
:100550007708C000BE0B5B2A8A110A125A2B84014D |
:1005600083131F3083058316031787150830031381 |
:10057000990002309A00A6309800903083129800BB |
:10058000A9152908831687008312291629088316BE |
:100590008700A2309300363083129400B401B60174 |
:1005A000B501831603170908C039890003131F1208 |
:1005B0009F12003003178800831287018801890188 |
:1005C0000313A801FF30A900AA01AB010330B70053 |
:1005D000831603170908C039890003131F129F12DD |
:1005E000003003178800831203131F1083160108BD |
:1005F000C7390838810009308312031785000F308E |
:10060000F700031381018130840083130008F0395F |
:100610000738800064000008F739F719F0397704CB |
:100620008000073090000030F80092000030831600 |
:10063000920083120317870188018901831603132F |
:100640009701640004308312B800FA30B900B02179 |
:10065000B80B252B6400A7018B1BA7178B137A20DF |
:10066000A71B8B17831601170C1483120B16831606 |
:100670008C15C03083128B04B001B101B201B301FB |
:10068000AA3003178D0001308F000313C4296400C2 |
:100690000630B800FA30B900B021B80B4A2B4A3006 |
:1006A00084002F08BB002E08BA002D08B9002C08C2 |
:1006B000B800502A0D300C1E5B2B99000A300C1E1E |
:0806C0005F2B9900472B63003A |
:04400E00EA2CFF3F5A |
:00000001FF |
;PIC16F887 |
;CRC=E464 CREATED="16-Oct-14 22:09" |
/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) |