|
Forum Index : Microcontroller and PC projects : MX170 running mmbasic 5.05.05, what is the maximum count frequency?
| Author | Message | ||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2572 |
this is perhaps one for Geoff... the micromite Mk2 (MX170) manual has two comments: (page 39) and: (page 51) it seems to me that if simply counting incoming pulses (ie, the pin is configured with SETPIN pin, CIN) then the maximum count rate should be determined entirely by the hardware - not dependent on what mmbasic is doing, or indeed upon mmbasic even being present. what i want to do: take the output of a 10MHz OCXO (Oven-Controlled Crystal Oscillator) and gate it using the output of a flip-flop that is fed from the 1pps pin of a GPS module. this allows me to measure the counts that occur over a precise 1 second interval, with the gating entirely controlled outside of the MX170. accumulating measurements over an extended period, mmbasic can then generate an adjustment voltage that is fed back to the OCXO to discipline it to the GPS 1pps. but this would require the MX170 to be able to count pulses 50nS wide that occur every 100nS, ie at 10MHz. i am hoping someone has tried something similar and can confirm if it works (or not). cheers, rob :-) Edited 2026-11-27 12:28 by robert.rozee |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3460 |
Did a crude test with a Pico producing a 10MHz 50% PWM for 1S connected to a MM2 and got this:- > RUN 494804 pulses counted > Pico code:- SetPin gp4, pwm : PWM 2,10000000,50 : Pause 999.9 : PWM 2,10000000,0 MM2 code:- CPU 48 : Pause 100 SetPin 15,CIN :Pause 9999 :Print Pin(15); " pulses counted" End the MM2 is started first. Edit. The code above has been adjusted for CPU 48MHz. Edited 2026-11-27 13:29 by phil99 |
||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2572 |
hmmm, that looks pretty dismal! if it represents a maximum frequency of 500kHz then that is in the ballpark of what the mmbasic manual says. Phil: many thanks for running the test i wonder if i could manually/directly configure the hardware counter? in response to the query 'pic32mx170 maximum pin counting rate', Gemini replies: "The maximum hardware pin counting rate on the PIC32MX170 (such as the PIC32MX170F256B) is up to 20 MHz in synchronous counter mode (when using an input prescaler) and 14.29 MHz with a 1:1 prescaler at the maximum 50 MHz peripheral clock (PBCLK). In asynchronous mode (Timer1 only), the hardware bypasses the peripheral clock synchronizer and can count significantly faster—typically ~20–25 MHz depending on operating voltage" the alternative is an external counter chip, preferably something that can be read over I2C. cheers, rob :-) addendum: Gemini, with further prompting, goes on to recommend: "Alternative: Use Timer2/3 in Synchronous 32-bit Mode. At 10 MHz (100 ns period), you are well within the 14.29 MHz (70 ns) limit for synchronous 1:1 counting at a 50 MHz PBCLK. Pairing Timer2 and Timer3 (T32 = 1) gives you a true 32-bit hardware counter (4,294,967,296 max count) with zero interrupt overhead and no asynchronous read/write synchronization " this looks interesting - if it can be manually set up without breaking mmbasic! Edited 2026-11-27 13:56 by robert.rozee |
||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2572 |
as i need to make use of a PWM output as well, the Timer2/3 option suggested by Gemini is a no-go. however, she came up with the below alternative that uses timers 3 and 5 - one direct, the other with a 256:1 prescaler: ' Generated by GEMINI (AI) and 100% untested! ' ' ============================================================================ ' PIC32MX170 (28-Pin) GPSDO 10 MHz Frequency Counter (Console-Safe!) ' - Pin 11 & Pin 12 remain untouched for MMBasic Console TX/RX ' - PWM Channel 1 (Timer 2) on Pin 4 drives OCXO EFC ' - Timer 5 (1:256 Sync, Pin 14) + Timer 3 (1:1 Sync, Pin 15) count 10 MHz ' ============================================================================ ' --- PIC32MX170 Peripheral Register Addresses (Virtual KSEG1) --- CONST T3CON = &HBF800A00 CONST TMR3 = &HBF800A10 CONST PR3 = &HBF800A20 CONST T5CON = &HBF800E00 CONST TMR5 = &HBF800E10 CONST PR5 = &HBF800E20 CONST T3CKR = &HBF80FA30 ' PPS input select for Timer3 Clock (T3CK) CONST T5CKR = &HBF80FA38 ' PPS input select for Timer5 Clock (T5CK) ' --- Configure Pins --- SETPIN 14, DIN ' Pin 14 (RPB5) = T5CK (Coarse 1:256 counter) SETPIN 15, DIN ' Pin 15 (RPB6) = T3CK (Fine 1:1 counter) SETPIN 16, DIN ' Pin 16 (RB7) = Gate State Input (1 = Open, 0 = Closed) ' Map PPS inputs (On MX170: T5CKR=1 -> RPB5 [Pin 14]; T3CKR=1 -> RPB6 [Pin 15]) POKE WORD T5CKR, 1 POKE WORD T3CKR, 1 ' Start PWM Channel 1 on Pin 4 (uses Timer 2, leaving Timer 3 & Timer 5 free) PWM 1, 20000, 50 ' --- Initialize Timers (Stopped, Synchronous External Clock) --- ' T5CON = &H0072: ON=0, TCKPS=111 (1:256), TCS=1 (External T5CK) ' T3CON = &H0002: ON=0, TCKPS=000 (1:1), TCS=1 (External T3CK) POKE WORD T5CON, &H0072 POKE WORD T3CON, &H0002 POKE WORD PR5, &HFFFF POKE WORD PR3, &HFFFF DO ' 1. Wait until gate is CLOSED (Pin 16 = 0) DO WHILE PIN(16) = 1 : LOOP ' 2. Stop both counters, reset counts & prescalers to 0, then arm them POKE WORD T5CON, &H0072 ' Stop Timer 5 POKE WORD T3CON, &H0002 ' Stop Timer 3 POKE WORD TMR5, 0 ' Reset Timer 5 count & internal 1:256 prescaler POKE WORD TMR3, 0 ' Reset Timer 3 count POKE WORD T5CON, &H8072 ' Arm Timer 5 (ON=1, 1:256 prescaler, T5CK) POKE WORD T3CON, &H8002 ' Arm Timer 3 (ON=1, 1:1 prescaler, T3CK) ' 3. Wait for the 1-second gate to OPEN (Pin 16 goes HIGH) DO WHILE PIN(16) = 0 : LOOP ' 4. Wait for the 1-second gate to CLOSE (Pin 16 goes LOW) DO WHILE PIN(16) = 1 : LOOP ' 5. Gate is closed; freeze both timers and calculate exact count POKE WORD T5CON, &H0072 POKE WORD T3CON, &H0002 Coarse% = PEEK(WORD TMR5) Fine% = PEEK(WORD TMR3) AND 255 Freq% = (Coarse% * 256) + Fine% PRINT "1s Count:"; Freq%; " Hz (Error:"; Freq% - 10000000; " Hz)" LOOP one reservation i do have is that she suggests the gate (1s open, 1s closed ad infinitum) should be synchronized to the 10MHz clock that is being measured. i can see why this is a good idea, but am wary that this may in itself introduce an offset in the final goal - of locking the 10MHz OCXO to the 1pps GPS pulses. note that there WILL be some stuttering in the 1pps signal, as well as slow drift over a 24 hour period. comments? cheers, rob :-) |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3460 |
I don't know how fast Pico PIO can go but it can go quite fast. Perhaps that can do the job. |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9402 |
A Pico PIO can run at 1 state machine instruction per CPU clock cycle at top speed. Obviously you need several instructions to implement a counter. :) A RP2040-Zero is smaller, cheaper, has more GPIO and is more versatile than a MX170+regulator+microbridge too. :) If you have to use the 1s GPS pulses as your gate then, unless you can synchronize your waveform to be measured in some way, you are guaranteed to be inaccurate by +/- 1 count, I think. At least it's consistent. :) Mick Zilog Inside! nascom.info for Nascom & Gemini |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 2175 |
|
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9402 |
... and there's a RP2350-Zero available in an identical format. :) Mick Zilog Inside! nascom.info for Nascom & Gemini |
||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2572 |
the pico (RP2040 or RP2350) is unsuitable in this particular application. assuming Gemini's code is moderately sane, i may need to plug together some actual hardware and do testing cheers, rob :-) |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 12301 |
It is definitely possible to connect one of the MM2 pins to an internal timer and count at up to the cpuspeed/2. I remember doing it way back when and there may even be a csub but I can't find anything. The datasheet should give you the relevant pins and then a poke or two should enable it. |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3374 |
Addressing the original question: A pulse on a counting edge causes a hardware interrupt on the MX170 which pushes the return address onto the stack, branches to the interrupt code where there is a little logic and then the counter is incremented. This is quite a few instructions and with a 48MHz CPU clock that will take about 2.5µs, so you cannot count at 10MHz. I remember testing this with a function generator and deciding that 300KHz was the highest reliable spec. You might be able to get a little higher in practice, but nowhere near 10MHz. Geoff Geoff Graham - http://geoffg.net |
||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2572 |
Geoff: many thanks for explaining this, i now understand the reasons for the limitations. Gemini was able to furnish me a list of the hardware counters on the MX170 and what each handles within the mmbasic firmware, and i can now see why the counting pins were served by an ISR in the firmware - it kept the timers free for PWM, SERVO, IR, PAUSE, TIMER, SETTICK etc uses. Peter: i do remember something about you using timers for frequency measurement (?) a number of years back. although it is not impossible that that may of been on the pico. as you say, and if Gemini is to be believed, i should be able to accomplish what i want with just a few judicious pokes. btw, i did also look at using an external counter chip, and may indeed end up using a low-end PIC as a 'helper' to achieve this - an 8-pin PIC (or AVR) would be simpler than the external gate logic i would need anyway! cheers, rob :-) |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |