3437 |
kakl |
1 |
//*** Atomic counter up to 800 MHz *** |
|
|
2 |
#include "acounter.h" |
|
|
3 |
#include <string.h> |
|
|
4 |
|
|
|
5 |
#define LED PIN_C1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5 |
|
|
6 |
#define SEL0 PIN_E0 // external counter division ratio |
|
|
7 |
#define SEL1 PIN_E1 // external counter division ratio |
|
|
8 |
#define MR PIN_E2 // external counter master reset |
|
|
9 |
#define CLKI PIN_C0 // internal counter input |
|
|
10 |
#define BEEP PIN_C3 // buzzer |
|
|
11 |
|
|
|
12 |
// LCD definitions |
|
|
13 |
#define LCD_ENABLE_PIN PIN_D4 //// |
|
|
14 |
#define LCD_RS_PIN PIN_D6 //// |
|
|
15 |
#define LCD_RW_PIN PIN_D5 //// |
|
|
16 |
#define LCD_DATA4 PIN_D0 //// |
|
|
17 |
#define LCD_DATA5 PIN_D1 //// |
|
|
18 |
#define LCD_DATA6 PIN_D2 //// |
|
|
19 |
#define LCD_DATA7 PIN_D3 |
|
|
20 |
#include <lcd.c> |
|
|
21 |
|
|
|
22 |
int16 of=0; // count of overflow |
|
|
23 |
int1 flag; // flag for a blinking dot |
|
|
24 |
|
3457 |
kaklik |
25 |
// GPS setup for frequency measurement |
|
|
26 |
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}; |
|
|
27 |
|
|
|
28 |
|
3437 |
kakl |
29 |
#int_EXT // Interrupt from 1PPS |
|
|
30 |
void EXT_isr(void) |
|
|
31 |
{ |
|
|
32 |
unsigned int16 countH; |
|
|
33 |
unsigned int8 countL; |
|
|
34 |
unsigned int32 count; |
|
|
35 |
char countS[10], a[4], b[4], c[4]; // strings for printing results |
|
|
36 |
|
|
|
37 |
countL=0; |
|
|
38 |
countH=get_timer1(); // read internal counter |
|
|
39 |
output_low(SEL0); |
|
|
40 |
output_low(SEL1); |
|
|
41 |
countL=input(CLKI); // read bit 0 of external counter |
|
|
42 |
output_high(SEL0); |
|
|
43 |
output_low(SEL1); |
|
|
44 |
countL|=input(CLKI)<<1; // read bit 1 of external counter |
|
|
45 |
output_low(SEL0); |
|
|
46 |
output_high(SEL1); |
|
|
47 |
countL|=input(CLKI)<<2; // read bit 2 of external counter |
|
|
48 |
output_high(SEL0); |
|
|
49 |
output_high(SEL1); |
|
|
50 |
countL|=input(CLKI)<<3; // read bit 3 of external counter |
|
|
51 |
|
|
|
52 |
output_low(MR); // External counter Master Reset |
|
|
53 |
output_high(MR); |
|
|
54 |
|
|
|
55 |
set_timer1(0); // Internal counter reset |
|
|
56 |
|
|
|
57 |
count=((unsigned int32)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate |
|
|
58 |
|
|
|
59 |
sprintf(countS,"%09Lu", count); // engeneering values conversion |
|
|
60 |
strncpy(a, countS, 3); a[3]='\0'; |
|
|
61 |
strncpy(b, &countS[3], 3); b[3]='\0'; |
|
|
62 |
strncpy(c, &countS[6], 3); c[3]='\0'; |
|
|
63 |
|
|
|
64 |
printf("%s\r\n", countS); // output to RS232 |
|
|
65 |
if(flag==0){lcd_putc("\fCvak... \n"); flag=1;} else {lcd_putc("\fCvak....\n"); flag=0;}; |
|
|
66 |
printf(lcd_putc, "%s %s %s Hz\n", a, b, c); // output to LCD |
|
|
67 |
|
|
|
68 |
output_toggle(BEEP); // cvak... |
|
|
69 |
|
|
|
70 |
of=0; // reset overflow counter |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
#int_TIMER1 // Interrupf from overflow |
|
|
74 |
void TIMER1_isr(void) |
|
|
75 |
{ |
|
|
76 |
of++; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
void main() |
|
|
80 |
{ |
|
|
81 |
|
|
|
82 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
83 |
setup_adc(ADC_CLOCK_DIV_2); |
|
|
84 |
setup_spi(SPI_SS_DISABLED); |
|
|
85 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
86 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); |
|
|
87 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
88 |
setup_ccp1(CCP_OFF); |
|
|
89 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
90 |
|
3457 |
kaklik |
91 |
output_toggle(BEEP); // cvak... |
3437 |
kakl |
92 |
|
|
|
93 |
ext_int_edge( L_TO_H ); // set 1PPS active edge |
|
|
94 |
enable_interrupts(INT_TIMER1); |
|
|
95 |
enable_interrupts(INT_EXT); |
|
|
96 |
enable_interrupts(GLOBAL); |
|
|
97 |
|
|
|
98 |
lcd_init(); |
3457 |
kaklik |
99 |
delay_ms(100); |
|
|
100 |
lcd_putc("\f ACOUNTER02A\n (c)mlab.cz 2014\n"); |
3437 |
kakl |
101 |
|
3457 |
kaklik |
102 |
|
|
|
103 |
delay_ms(1000); //wait for GPS init. |
|
|
104 |
int n; |
|
|
105 |
for (n=0;n<40;n++) putc(cmd[n]); // send setup to GPS |
|
|
106 |
|
3437 |
kakl |
107 |
lcd_putc("\fCvak...\nHmmm...\n"); |
|
|
108 |
|
|
|
109 |
while(true) |
|
|
110 |
{ |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
} |