Rev 3439 Rev 3457
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 // 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  
25 #int_EXT // Interrupt from 1PPS 29 #int_EXT // Interrupt from 1PPS
26 void EXT_isr(void) 30 void EXT_isr(void)
27 { 31 {
28 unsigned int16 countH; 32 unsigned int16 countH;
29 unsigned int8 countL; 33 unsigned int8 countL;
30 unsigned int32 count; 34 unsigned int32 count;
31 char countS[10], a[4], b[4], c[4]; // strings for printing results 35 char countS[10], a[4], b[4], c[4]; // strings for printing results
32 36
33 countL=0; 37 countL=0;
34 countH=get_timer1(); // read internal counter 38 countH=get_timer1(); // read internal counter
35 output_low(SEL0); 39 output_low(SEL0);
36 output_low(SEL1); 40 output_low(SEL1);
37 countL=input(CLKI); // read bit 0 of external counter 41 countL=input(CLKI); // read bit 0 of external counter
38 output_high(SEL0); 42 output_high(SEL0);
39 output_low(SEL1); 43 output_low(SEL1);
40 countL|=input(CLKI)<<1; // read bit 1 of external counter 44 countL|=input(CLKI)<<1; // read bit 1 of external counter
41 output_low(SEL0); 45 output_low(SEL0);
42 output_high(SEL1); 46 output_high(SEL1);
43 countL|=input(CLKI)<<2; // read bit 2 of external counter 47 countL|=input(CLKI)<<2; // read bit 2 of external counter
44 output_high(SEL0); 48 output_high(SEL0);
45 output_high(SEL1); 49 output_high(SEL1);
46 countL|=input(CLKI)<<3; // read bit 3 of external counter 50 countL|=input(CLKI)<<3; // read bit 3 of external counter
47   51  
48 output_low(MR); // External counter Master Reset 52 output_low(MR); // External counter Master Reset
49 output_high(MR); 53 output_high(MR);
50 54
51 set_timer1(0); // Internal counter reset 55 set_timer1(0); // Internal counter reset
52 56
53 count=((unsigned int32)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate 57 count=((unsigned int32)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate
54   58  
55 sprintf(countS,"%09Lu", count); // engeneering values conversion 59 sprintf(countS,"%09Lu", count); // engeneering values conversion
56 strncpy(a, countS, 3); a[3]='\0'; 60 strncpy(a, countS, 3); a[3]='\0';
57 strncpy(b, &countS[3], 3); b[3]='\0'; 61 strncpy(b, &countS[3], 3); b[3]='\0';
58 strncpy(c, &countS[6], 3); c[3]='\0'; 62 strncpy(c, &countS[6], 3); c[3]='\0';
59 63
60 printf("%s\r\n", countS); // output to RS232 64 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;}; 65 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 66 printf(lcd_putc, "%s %s %s Hz\n", a, b, c); // output to LCD
63   67  
64 output_toggle(BEEP); // cvak... 68 output_toggle(BEEP); // cvak...
65   69  
66 of=0; // reset overflow counter 70 of=0; // reset overflow counter
67 } 71 }
68   72  
69 #int_TIMER1 // Interrupf from overflow 73 #int_TIMER1 // Interrupf from overflow
70 void TIMER1_isr(void) 74 void TIMER1_isr(void)
71 { 75 {
72 of++; 76 of++;
73 } 77 }
74   78  
75 void main() 79 void main()
76 { 80 {
77   81  
78 setup_adc_ports(NO_ANALOGS|VSS_VDD); 82 setup_adc_ports(NO_ANALOGS|VSS_VDD);
79 setup_adc(ADC_CLOCK_DIV_2); 83 setup_adc(ADC_CLOCK_DIV_2);
80 setup_spi(SPI_SS_DISABLED); 84 setup_spi(SPI_SS_DISABLED);
81 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 85 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
82 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); 86 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
83 setup_timer_2(T2_DISABLED,0,1); 87 setup_timer_2(T2_DISABLED,0,1);
84 setup_ccp1(CCP_OFF); 88 setup_ccp1(CCP_OFF);
85 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 89 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
86   90  
87 output_toggle(BEEP); // cvak... 91 output_toggle(BEEP); // cvak...
88 printf("Cvak...."); -  
89 92
90 ext_int_edge( L_TO_H ); // set 1PPS active edge 93 ext_int_edge( L_TO_H ); // set 1PPS active edge
91 enable_interrupts(INT_TIMER1); 94 enable_interrupts(INT_TIMER1);
92 enable_interrupts(INT_EXT); 95 enable_interrupts(INT_EXT);
93 enable_interrupts(GLOBAL); 96 enable_interrupts(GLOBAL);
94   97  
95 lcd_init(); 98 lcd_init();
-   99 delay_ms(100);
-   100 lcd_putc("\f ACOUNTER02A\n (c)mlab.cz 2014\n");
-   101  
-   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
96   106  
97 lcd_putc("\fCvak...\nHmmm...\n"); 107 lcd_putc("\fCvak...\nHmmm...\n");
98   108  
99 while(true) 109 while(true)
100 { 110 {
101 } 111 }
102   112  
103 } 113 }