pic.programmer
Advanced Member level 3
Why is this Frequency Counter not working ?
I am using dsPIC30F4013. Crystal used is 8 MHz. XT with 8x PLL is used. So, system clock is 64 MHz. I am using mikroC PRO dsPIC Compiler.
Timer2 is used to generate exact 1 sec delay. Timer1 is used as a counter and input is from T1CK pin. I am trying the design a frequency counter which can measure upto 50 MHz.
1:8 prescalar is used with Timer1 counter. So, the final result is
The display code is correct because it works with PIC18F46K22. I have configured ADPCFG so that all pins are digital IO pins.
What am I missing ?
- - - Updated - - -
I have tested this in hardware and it doesn't display anything (not even 0) on the MAX7219 based 7 segment display. I can tell that the MAX7219 related code is fine because I have used the same code in one of my PIC18F26K22 based frequency counter and it displays data fine. So, the problem is either related to Timer1 and/or Timer2 related codes.
Have I configured the Timer1 properly. I am sure Timer2 configuration to generate 1 sec delay is correct because I have used mikroE Timer Calculator Tool.
For Timer1, PR1 is loaded with 0xFFFF. So, when Timer1 counts 0xFFFF using external clock pulses then it has to overflow and generate the Timer1 interrupt.
Edit:
I had forgotten to call these two functions after port initialization.
Now the display displays 0. So, display is fine. I tested the Timer2 related code in a different project and it blinks an LED at 1 Hz. So, I can tell that Timer2 related code is also correct.
All I can say is interrupts are not occuring. I think the problem is with Timer1 which is not counting the external pulses.
This is the new code. Forum is not allowing me to attach files and hence I am posting the code.
Have I missed configuring any registers ?
I am using dsPIC30F4013. Crystal used is 8 MHz. XT with 8x PLL is used. So, system clock is 64 MHz. I am using mikroC PRO dsPIC Compiler.
Timer2 is used to generate exact 1 sec delay. Timer1 is used as a counter and input is from T1CK pin. I am trying the design a frequency counter which can measure upto 50 MHz.
1:8 prescalar is used with Timer1 counter. So, the final result is
Code:
freq = ((overflow << 16) + TMR1) * 8;
The display code is correct because it works with PIC18F46K22. I have configured ADPCFG so that all pins are digital IO pins.
What am I missing ?
- - - Updated - - -
I have tested this in hardware and it doesn't display anything (not even 0) on the MAX7219 based 7 segment display. I can tell that the MAX7219 related code is fine because I have used the same code in one of my PIC18F26K22 based frequency counter and it displays data fine. So, the problem is either related to Timer1 and/or Timer2 related codes.
Have I configured the Timer1 properly. I am sure Timer2 configuration to generate 1 sec delay is correct because I have used mikroE Timer Calculator Tool.
For Timer1, PR1 is loaded with 0xFFFF. So, when Timer1 counts 0xFFFF using external clock pulses then it has to overflow and generate the Timer1 interrupt.
Edit:
I had forgotten to call these two functions after port initialization.
Code:
MAX7219init();
Display(0);
Now the display displays 0. So, display is fine. I tested the Timer2 related code in a different project and it blinks an LED at 1 Hz. So, I can tell that Timer2 related code is also correct.
All I can say is interrupts are not occuring. I think the problem is with Timer1 which is not counting the external pulses.
This is the new code. Forum is not allowing me to attach files and hence I am posting the code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 sbit Data7219 at LATB0_bit; sbit Load7219 at LATB1_bit ; sbit Clk7219 at LATB2_bit; sbit Data7219_Direction at TRISB0_bit; sbit Load7219_Direction at TRISB1_bit; sbit Clk7219_Direction at TRISB2_bit; const unsigned char Font_B[16] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70, 0x7f,0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47 }; void Send7219 (char,char); void Send7219byte(char); void MAX7219init(); void Display(unsigned long); unsigned long overflow = 0; // Count overflow from interrupt. short send_to_UART = 0; char str[17]; unsigned long freq = 0; //Timer2 //Prescaler 1:256; PR2 Preload = 62500; Actual Interrupt Time = 1 //Place/Copy this part in declaration section void InitTimer2(){ T2CON = 0x8030; T2IE_bit = 1; T2IF_bit = 0; IPC0 = IPC0 | 0x1000; PR2 = 62500; } void Timer2Interrupt() iv IVT_ADDR_T2INTERRUPT { T2IF_bit = 0; //Enter your code here T2CON.TON = 0; T1CON.TON = 0; //LATD0_bit = ~LATD0_bit; send_to_UART = 1; } void Timer1Interrupt() iv IVT_ADDR_T1INTERRUPT ics ICS_AUTO { T1IF_bit = 0; overflow++; } void start_timer_count() { send_to_UART = 0; overflow = 0; NSTDIS_bit = 1; // Disable All interrupts. T1CON = 0x8016; T1IE_bit = 0; TMR1 = 0; overflow = 0; // Clear overflow counter. T1IF_bit = 0; PR1 = 0xFFFF; T1IE_bit = 1; InitTimer2(); NSTDIS_bit = 0; // Enaable All interrupts. } void MAX7219init() { Data7219_Direction = 0; Load7219_Direction = 0; Clk7219_Direction = 0; Data7219 = 0; Load7219 = 0; Clk7219 = 0; Send7219(0x09, 0x00); //Decode Mode Send7219(0x0A, 0x05); //Brightness Send7219(0x0B, 0x07); //Scan limit Send7219(0x0C, 0x01); Send7219(0x0F, 0x00); } void Send7219(char Digit, char Data_) { Send7219byte(Digit); Send7219byte(Data_); Data7219 = 0; Load7219 = 1; Delay_us(20); Load7219 = 0; } void Send7219byte(char byte) { unsigned char i; for(i = 0; i < 8; i++) { if(byte & 0x80) Data7219 = 1; else Data7219 = 0; Clk7219 = 1; Delay_us(40); Clk7219 = 0; byte <<= 1; } } void Display(unsigned long val) { unsigned char dispskip; // add 1 if "." found within string unsigned char i = 0; LongToStr(val, str); //Convert float value to string Ltrim(str); //remove spaces padded to left of string Rtrim(str); dispskip = strlen(str) - 1; if(dispskip >= 8)dispskip = 7; while(i <= dispskip) { if((str[dispskip - i] >= 0x30) && (str[dispskip - i] <= 0x39)) { Send7219(i+1, (Font_B[str[dispskip - i] - 0x30]));//0-9 } i++; } i++; while(i <= 8) { Send7219(i, 0); i++; } } void main() { ADPCFG = 0xFFFF; TRISA = 0x0000; TRISB = 0x0000; TRISC = 0xFFFF; TRISD = 0x0000; TRISF = 0x0000; PORTA = 0x0000; PORTB = 0x0000; PORTC = 0x0000; PORTD = 0x0000; PORTF = 0x0000; LATA = 0x0000; LATB = 0x0000; LATC = 0x0000; LATD = 0x0000; LATF = 0x0000; MAX7219init(); Display(0); start_timer_count(); while(1) { if(send_to_UART) { freq = ((overflow << 16) + TMR1) * 8; Display(freq); start_timer_count(); } } }
Have I missed configuring any registers ?
Attachments
Last edited: