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