|
Forum Index : Microcontroller and PC projects : MX170 running mmbasic 5.05.05, what is the maximum count frequency?
| Page 1 of 2 |
|||||
| Author | Message | ||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2575 |
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: 3475 |
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: 2575 |
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: 2575 |
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: 3475 |
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: 9420 |
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: 2178 |
|
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9420 |
... 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: 2575 |
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: 12306 |
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: 2575 |
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 :-) |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4408 |
Rob, Might be fastcount John |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 12306 |
Well found . I tried and failed |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3475 |
Also found it in the v5.05.05 download package. Micromite_Firmware_50505\Embedded C Modules\Fast Counter Cfunction (10MHz+) MM2, MM+.txt So I guess Rob already has a copy. Edited 2026-11-28 20:10 by phil99 |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3475 |
Same Pico 10MHz source as previously. MM2 code:- CPU 48 Pause 100 dim a% a%=FASTCOUNT(1, 10000) pause 10005 a%=FASTCOUNT(0) a%=FASTCOUNT(2) ? a%; " pulses counted" ' Fast counter CFunction for MM2/MM+ ' Pins are 25 on 28pin, 14 on 44-pin, 13 on 64-pin, and 22 on 100pin ' a%=FASTCOUNT(1, n) ' sets up the counter with window of n milliseconds ' a%=FASTCOUNT(0) 'turns off the counter ' a%= FASTCOUNT(2) ' returns the count for the last period ' CFunction FASTCOUNT(integer, integer) INTEGER . . . Output:- > RUN 9988673 pulses counted > The discrepancy could the time I allowed to stop the Pico PWM was too long (Pause 999.9 instead of 1000 mS). However it should do what Rob wants. Edit. New code for the Pico should be more accurate. SetPin gp4, pwm : PWM 2,10000000,50 'GP4 connected to MM2 pin 25 via 1k0 to form AND gate with GP3 SetPin gp3, dout : Pin(gp3)=0 'GP3 connected to 1N914 diode cathode, anode connected to MM2 pin 25 Pause 1000 Pulse gp3,1000 'this could be from a GPS source Print "1000mS of 10MHz delivered" End Output:- RUN Not sure why there is such a large undercount this time. Perhaps PWM divider ratio is uneven. Should not be an issue with an external 10MHz and 1S pulse.9993525 pulses counted > RUN 9992191 pulses counted > Edit 2. Changing the Pico CPU speed to 320MHz increased the count a little but still undercounting. > RUN 9997661 pulses counted > Edit 3. I think the problem is with the Pico PWM. Looping the PWM back to a count pin on the Pico gives a similarly low reading that varies between runs. Using BITSTREAM to send pulses to the MM2 stays within +/- 1 count of the number sent. Edited 2026-11-29 17:39 by phil99 |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3475 |
Calibrating a 10MHz crystal may be tricky but how about a 518 trillion cycles per second optical frequency reference, with 18 digit resolution. Accurately measuring time "The best cesium clocks might drift by a second over about 300 million years; the most accurate optical lattice clocks would take around 30 billion years." |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9420 |
Use a 10MHz OCXO for the calibration standard. You may need deepish pockets though. :) It's also possible to build your own crystal ovens very cheaply. There's not much in them, they are just bulkier. Standard crystal, BJT or moset as the heater, NTC thermistor, expanded polystyrene insulation, a couple of resistors and a preset pot to trim the frequency. But what calibrates the calibration standard? ;) (We used to use the BBC Light Program radio broadcast on 200kHz (before it moved to 198kHz) as a cheap frequency standard at one time.) Mick Zilog Inside! nascom.info for Nascom & Gemini |
||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2575 |
i have a couple of (cheap/recycled) OSC5A2B02 oven-controlled crystal oscillators (OCXO), and my original plan was to be able to use the 1pps output of a cheap VK16E SIRF III GPS module (of which i have several) to discipline the OCXO, with it all coordinated by an MX170 running mmbasic. criteria of the design was to be extremely 'quiet', highly deterministic, and ultra-reliable (ie, no crashes when a transmitter is keyed nearby!) now i'm leaning more towards manually adjusting the OCXO (it has a 0-4v input pin for fine adjustments) which is extremely simple, and then have an external bit of equipment that can be hooked up to indicate drift of the 10MHz output. code (and binaries) are available here: https://github.com/aewallin/PICDIV for a variety of simple 10,000,000:1 dividers based on a PIC12F675 or similar. using one of these i can get a 1Hz (50/50 duty cycle) output from my 10MHz OCXO. i can then: 1. using the rising edge of the 1pps from the GPS module to set a flip-flop, 2. use the falling edge of the 1Hz from the PIC12F675 to reset the flip-flop. the above gives me a pulse of (nominal) 500ms length that will lengthen (or shorten) by approximately 17ms over 24 hours for every 0.2ppm of drift in the OCXO. measuring this with mmbasic seems easily doable. note: the PIC12F675 code also provides a 'sync' input that can be used to bring the pulse/drift output back to a nominal 500ms. does the above approach make sense? cheers, rob :-) |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3475 |
Your original idea may still be worth a try, Feeding the 10MHz and 1Hz streams to an And gate then the MM2 pin 25, and the 1Hz to another DIN pin. The program could be:- After a falling edge of the 1Hz start CFunction FASTCOUNT After the next falling edge of the 1Hz stop FASTCOUNT Read FASTCOUNT & save or display value, should be 5,000,000. A quick breadboard test would show if this idea is any good. Edited 2026-12-01 22:13 by phil99 |
||||
| Page 1 of 2 |
|||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |