/Designs/Measuring_instruments/RMDS01C/SW/PIC16F887/main.c |
---|
1,100 → 1,122 |
// 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> |
#include <string.h> |
#define LED PIN_E1 |
#define CE PIN_E2 |
#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 |
#define BEEP PIN_C3 // buzzer |
unsigned int32 count; |
unsigned int32 count; // count per second |
const int8 buf_len=8; |
int8 buffer[buf_len]; // I2C buffer |
#define BUF_LEN 4 |
int8 buffer[BUF_LEN]; // I2C buffer |
int8 address=0; |
unsigned int16 of=0; // count of overflow |
const char cmd[40]={0xB5, 0x62, 0x06, 0x31, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x80, 0x84, 0x1E, 0x00, 0xE0, 0xC8, 0x10, 0x00, 0x40, 0x42, 0x0F, 0x00, 0xA0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7, 0x00, 0x00, 0x00, 0x12, 0x03}; |
// 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 () |
{ |
BYTE incoming, state; |
int8 incoming, state; |
state = i2c_isr_state(); |
if(state < 0x80) //Master is sending data |
{ |
incoming = i2c_read(); |
if(state == 1) //First received byte is address |
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 (incoming == 2) |
{ |
/* buffer[0]=make8(anemo,0); |
buffer[1]=make8(anemo,1); |
buffer[2]=make8(rain,0); |
buffer[3]=make8(rain,1); |
*/ } |
address = incoming; |
} |
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]); |
//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 |
#int_EXT // Interrupt from 1PPS (RB0) |
void EXT_isr(void) |
{ |
unsigned int16 countH; |
unsigned int8 countL; |
char countS[10], a[4], b[4], c[4]; // strings for printing results |
int16 of2; |
of2=of; // read overflow counter |
countH=get_timer1(); // read internal counter |
countL=0; |
countH=get_timer1(); // read internal counter |
output_low(SEL0); |
output_low(SEL1); |
countL=input(CLKI); // read bit 0 of external counter |
output_high(SEL0); |
output_low(SEL1); |
// 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); |
// 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)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate |
count=((unsigned int32)of2<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate |
sprintf(countS,"%09Lu", count); // engeneering values conversion |
strncpy(a, countS, 3); a[3]='\0'; |
strncpy(b, &countS[3], 3); b[3]='\0'; |
strncpy(c, &countS[6], 3); c[3]='\0'; |
printf("%s\r\n", countS); // output to RS232 |
output_toggle(BEEP); // cvak... |
of=0; // reset overflow counter |
printf("%010Lu\r\n", count); |
} |
#int_TIMER1 // Interrupf from overflow |
104,72 → 126,45 |
} |
/*#int_TIMER2 // every 10 ms |
void TIMER2_isr(void) |
{ |
output_low(CE); |
count=get_timer1(); |
set_timer1(0); |
output_high(CE); |
}*/ |
void main() |
{ |
/* setup_adc_ports(NO_ANALOGS|VSS_VDD); |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
setup_adc(ADC_OFF); |
// setup_spi(SPI_SS_DISABLED); |
// 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_144MS); |
setup_wdt(WDT_2304MS); |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); |
setup_timer_2(T2_DIV_BY_16,196,16); |
setup_ccp1(CCP_OFF); |
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]); |
} |
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); |
// ext_int_edge( L_TO_H ); // set 1PPS active edge |
// enable_interrupts(INT_TIMER1); |
// enable_interrupts(INT_EXT); |
ext_int_edge( L_TO_H ); // set 1PPS active edge |
enable_interrupts(INT_TIMER1); |
enable_interrupts(INT_EXT); |
enable_interrupts(INT_SSP); |
// enable_interrupts(INT_TIMER2); |
enable_interrupts(GLOBAL); |
/* delay_ms(1000); |
int n; |
for (n=0;n<40;n++) putc(cmd[n]); // setup GPS |
*/ |
printf("cvak...\r\n"); |
buffer[0]=0x0; // Clear I2C output buffer |
buffer[1]=0x0; |
buffer[2]=0x0; |
buffer[3]=0x0; |
buffer[2]=0; |
buffer[3]=0; |
buffer[4]=0; |
buffer[5]=0; |
printf("\r\ncvak...\r\n"); |
while(true) |
{ |
// output_high(LED); |
delay_ms(1000); |
// output_low(LED); |
// delay_ms(999); |
printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); |
restart_wdt(); |
delay_ms(1000); |
// printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); |
} |
} |
/Designs/Measuring_instruments/RMDS01C/SW/PIC16F887/main.h |
---|
1,9 → 1,9 |
#include <16F887.h> |
#device adc=8 |
#FUSES NOWDT //No Watch Dog Timer |
#FUSES WDT //Watch Dog Timer |
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) |
#FUSES NOPUT //No Power Up Timer |
#FUSES PUT //Power Up Timer |
#FUSES MCLR //Master Clear pin enabled |
#FUSES NOPROTECT //Code not protected from reading |
#FUSES NOCPD //No EE protection |
/Designs/Measuring_instruments/RMDS01C/SW/PIC16F887/main.hex |
---|
1,57 → 1,111 |
:1000000001308A0011290000FF00030E8301A100C6 |
:1000000002308A00B02A0000FF00030E8301A10025 |
:100010000A08A0008A010408A2007708A300780853 |
:10002000A4007908A5007A08A600831383128C30F7 |
:100030008400801D1D288C192E2822088400230886 |
:10004000F7002408F8002508F9002608FA0020081F |
:100050008A00210E8300FF0E7F0E09008A110A120A |
:100060003E2813083F08930014168C1183161418A9 |
:100070003728F8018312141AF80A08008316941A14 |
:1000800049288312B8018316141D49288312B81712 |
:10009000831683123808B80ABD003D087F3C031C54 |
:1000A000692814138C1D522813081416BC003D0B2C |
:1000B0005F283C08B5003C08023C031D5F283D0852 |
:1000C000023C031D69282D303507840083133C084A |
:1000D00080003D08803C031D85283508083C031C32 |
:1000E0007A282D303507840083130008BE00BF0036 |
:1000F0003120852808303502000784008313561804 |
:1001000083170008BE00BF0031208C118A110A122B |
:100110001D28633BE1352E17AE060A008316031730 |
:100120008C170C140000000083120C087F3903198F |
:10013000D4280313B90003170D080313BA000317DB |
:100140000F080313BB0039080C1EA42899003A08B5 |
:1001500003178D0003133B0803178F0083168C17BA |
:100160000C140000000083120C0D0E0D7F390319D2 |
:10017000D4280313B90003170D080313BA0003179B |
:100180000F080313BB0039080C1EC42899003A0855 |
:1001900003178D0003133B0803178F008D0A031903 |
:1001A0008F0A03138E28031703138A110A1270296A |
:1001B0003A308400831300080319EB280630F80056 |
:1001C000F701F70BE128F80BE0287B30F700F70B7D |
:1001D000E728800BDE288A110A127929BA1B012927 |
:1001E0000F30F700390EF7050A3077020318FB28A5 |
:1001F0003030F707FD283A08F70777080C1EFE286D |
:1002000099000F30B9050A30390203180929303036 |
:100210000B29BA133A08B90739080C1E0D299900A1 |
:100220000800840183131F30830583160317871585 |
:1002300008300313990002309A00A63098009030DD |
:1002400083129800A81528088316870083122816A1 |
:10025000280883168700A23093003630831294005A |
:10026000B501B701B601831603170908C039890023 |
:1002700003131F129F120030031788008312870197 |
:10028000880189010313A701FF30A8000330B800DB |
:10029000831603170908C039890003131F129F1220 |
:1002A000003003178800831203131F108316010800 |
:1002B000C739283881000F30831290000317870157 |
:1002C000880189018316031397018C15C0308312AE |
:1002D0008B04893003178D0000308F0003138E28A4 |
:1002E000AF01B001B101B2010430B900FA30BA0077 |
:1002F000D828B90B76292D08B9003730BA00EE207E |
:1003000020300C1E812999002E08B9003730BA0020 |
:10031000EE2020300C1E8A2999002F08B9003730B2 |
:10032000BA00EE2020300C1E932999003008B90045 |
:100330003730BA00EE200D300C1E9C2999000A308F |
:0A0340000C1EA02999007429630027 |
:04400E00F22CFF3F52 |
:10002000A4007908A5007A08A600831383120B1E8A |
:100030001B288B1838288C308400001C21280C18B1 |
:100040003B288C308400801D27288C193E282208EC |
:1000500084002308F7002408F8002508F900260882 |
:10006000FA0020088A00210E8300FF0E7F0E09008F |
:100070008A110A1216298A110A12452A8A110A12AD |
:100080007A280A108A100A118207B53462340634BD |
:10009000313420340034003400340034003432343D |
:1000A0000034003400348034963498340034E03422 |
:1000B000C834103400346434003400340034003464 |
:1000C000003400340034003400340034003473341D |
:1000D000003400340034C634513413083E08930011 |
:1000E00014168C11831614187328F8018312141A2D |
:1000F000F80A08008316941A85288312B60183161D |
:10010000141D85288312B617831683123608B60A83 |
:10011000BC003C087F3C031C972814138C1D8E28C0 |
:1001200013081416BB003C0B97283B08B3003C088F |
:10013000803C031DB528B308031DA6282B08AF007B |
:100140002C08B0002D08B1002E08B2003308043C82 |
:10015000031CB3282F303307840083130008BD002D |
:10016000BE006D20B528BE016D203C08813C031DFA |
:10017000BC283008BE006D203C08823C031DC3280B |
:100180003108BE006D203C08833C031DCA2832089C |
:10019000BE006D203C08833C0318D028BE016D20B2 |
:1001A0008C118A110A122728F701F801F901FA01C6 |
:1001B000D401D501D601D70153085204510450048B |
:1001C000031912292030D8000310CC0DCD0DCE0D0F |
:1001D000CF0DD40DD50DD60DD70D53085702031DE5 |
:1001E000FB2852085602031DFB2851085502031D27 |
:1001F000FB2850085402031C0C295008D402510853 |
:10020000031C510FD5025208031C520FD60253088B |
:10021000031C530FD7020314F70DF80DF90DFA0D57 |
:10022000D80BE42854308400831308003508BF003D |
:100230003408BE000F08FA000E08F7000F087A0213 |
:10024000031D1A297708BB007A08BC00BD0183167C |
:10025000091083120910831689108312891028143B |
:100260002808831687008312BD010718BD0A83166C |
:1002700009108312091428142808831687000030F7 |
:10028000831207180130F7000310F70D7708BD043B |
:10029000831609108312091083168910831289149A |
:1002A00028142808831687000030831207180130AD |
:1002B000F700F70DF70DFC30F7057708BD0483163E |
:1002C00009108312091428142808831687000030A7 |
:1002D000831207180130F700F70DF70DF70DF8300E |
:1002E000F7057708BD0483160911831209118316D7 |
:1002F0000911831209158F018E01B501B401C301E3 |
:10030000C2013F08C1003E08C0003F08C3003E08CC |
:10031000C200C001C101C20DC30DC20DC30DC20D8B |
:10032000C30DC20DC30DF030C205C801C7013B0D9E |
:10033000F7003C0DF800470DF900480DFA00F70DE5 |
:10034000F80DF90DFA0DF70DF80DF90DFA0DF70D81 |
:10035000F80DF90DFA0DF030F7057708C0077808A9 |
:100360000318780FC10779080318790FC2077A08B4 |
:1003700003187A0FC307F801F901FA013D08400795 |
:10038000AB004108AC0078080318780FAC074208AE |
:10039000AD0079080318790FAD074308AE007A085D |
:1003A00003187A0FAE074A3084002E08C3002D08C8 |
:1003B000C2002C08C1002B08C0000408C4003B3058 |
:1003C000CB009A30CA00CA30C900C8010A30C60042 |
:1003D0004308CF004208CE004108CD004008CC00C1 |
:1003E0004B08D3004A08D2004908D1004808D00081 |
:1003F000D4200008C000840A0008C100840A000854 |
:10040000C200840A0008C300F708031D1A2A460820 |
:10041000013A03191A2A440803191B2A0F39460204 |
:100420000319142A0318212AC41B212A441B1B2A3E |
:1004300020301C2AC4013030F70777080C1E1E2A12 |
:1004400099004B08CF004A08CE004908CD00480863 |
:10045000CC00D301D201D1010A30D000D4207A08D7 |
:10046000CB007908CA007808C9007708C800C60B15 |
:10047000E8290D300C1E3A2A99000A300C1E3E2A3B |
:1004800099008B108A110A122728B40A0319B50A99 |
:100490000C108A110A1227280D05633BE1352E172F |
:1004A000AE060A0039308400831300080319652A58 |
:1004B0000630F800F701F70B5B2AF80B5A2A7B305D |
:1004C000F700F70B612A800B582A080083160317E0 |
:1004D0008C170C140000000083120C087F390319DC |
:1004E000AC2A0313B80003170D080313B900031750 |
:1004F0000F080313BA0038080C1E7C2A990039082B |
:1005000003178D0003133A0803178F0083168C1707 |
:100510000C140000000083120C0D0E0D7F3903191E |
:10052000AC2A0313B80003170D080313B90003170F |
:100530000F080313BA0038080C1E9C2A99003908CA |
:1005400003178D0003133A0803178F008D0A031950 |
:100550008F0A0313662A031703138A110A124D2BFD |
:10056000840183131F308305831603178715083012 |
:100570000313990002309A00A6309800903083123D |
:100580009800A815280883168700831228162808C3 |
:1005900083168700A2309300363083129400B30193 |
:1005A000B501B401831603170908C0398900031384 |
:1005B0001F129F12003003178800831287018801E1 |
:1005C00089010313A701FF30A800A901AA01033084 |
:1005D000B600831603170908C039890003131F12D8 |
:1005E0009F12003003178800831203131F10831615 |
:1005F0000108C739083881000930831203178500C4 |
:100600000F30F70003138101813084008313000849 |
:10061000F0390738800064000008F739F719F0391D |
:1006200077048000073090000030F800920000301E |
:1006300083169200831203178701880189018316AC |
:1006400003139701640004308312B800FA30B90034 |
:100650005222B80B262B6400B7013708273C031C35 |
:10066000382B370841200C1E332B9900B70A2D2B4D |
:10067000831601170C1483120B1683168C15C030C9 |
:1006800083128B04AF01B001B101B2014C300317EA |
:100690008D0002308F000313662A64000430B80016 |
:0E06A000FA30B9005222B80B502B4D2B6300DC |
:04400E00EA2CFF3F5A |
:00000001FF |
;PIC16F887 |
;CRC=6DDF CREATED="04-II-14 15:49" |
;CRC=904D CREATED="09-II-14 11:12" |