Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:18 17 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PIO control - I'm confused.

Author Message
George H
Newbie

Joined: 03/07/2020
Location: United States
Posts: 33
Posted: 02:04am 09 Aug 2025
Copy link to clipboard 
Print this post

I have searched the forum, and I have tried about every combination I can think of for the PIO() command.

I'm trying to use the PIO system to set up a 4 mhz clock signal amd a timer.  Currently using GP2/GP3 for the output.  I've got an LED on each.  I know I won't see 4 MHz but it should illuminate about half bright - right?  I get nothing. I can turn them on and off with DOUT though.

The current manual still shows using a variable for the pin, I'm using a literal "GP2" in the command.  Anyway, here's some code:

' Simple Dual Clock Generator
' Outputs 4MHz on GPIO2 (pin 4) and 100kHz on GPIO3 (pin 5)
' Uses PIO state machines for precise timing

Print "Simple Dual Clock Generator"
Print "=========================="
Print "4MHz clock on GPIO2 (physical pin 4)"
Print "100kHz clock on GPIO3 (physical pin 5)"
Print

' Set CPU frequency (420MHz for this Pico)
Dim cpu_freq = 420000000
Print "CPU Frequency: "; cpu_freq; " Hz"

' Reserve pins for PIO use
SetPin 4, PIO1
SetPin 5, PIO1
Print "Pins reserved for PIO"

' Program PIO state machine 0 for 4MHz clock
Print "Programming 4MHz clock generator..."
PIO ASSEMBLE 1, ".program clock_4mhz"
PIO ASSEMBLE 1, ".line 0"
PIO ASSEMBLE 1, "SET PINDIRS, 1"
PIO ASSEMBLE 1, "loop1:"
PIO ASSEMBLE 1, "SET PINS, 1"
PIO ASSEMBLE 1, "SET PINS, 0"
PIO ASSEMBLE 1, "JMP loop1"
PIO ASSEMBLE 1, ".end program"

' Program PIO state machine 1 for 100kHz clock
Print "Programming 100kHz clock generator..."
PIO ASSEMBLE 1, ".program clock_100khz"
PIO ASSEMBLE 1, ".line 8"
PIO ASSEMBLE 1, "SET PINDIRS, 1"
PIO ASSEMBLE 1, "loop2:"
PIO ASSEMBLE 1, "SET PINS, 1"
PIO ASSEMBLE 1, "SET PINS, 0"
PIO ASSEMBLE 1, "JMP loop2"
PIO ASSEMBLE 1, ".end program"

' Calculate PIO clock frequencies (2x output frequency for high+low cycle)
Dim pio_freq_4mhz = 4000000 * 2
Dim pio_freq_100khz = 100000 * 2

Print "4MHz PIO frequency: "; pio_freq_4mhz
Print "100kHz PIO frequency: "; pio_freq_100khz

' Configure and start 4MHz clock on GPIO2
Dim config_4mhz = Pio(pinctrl 0,1,,,,GP2,)
PIO INIT MACHINE 1, 0, pio_freq_4mhz, config_4mhz, 0, 0
PIO START 1, 0

' Configure and start 100kHz clock on GPIO3
Dim config_100khz = Pio(pinctrl 0,1,,,,GP3,)
PIO INIT MACHINE 1, 1, pio_freq_100khz, config_100khz, 0, 8
PIO START 1, 1

' Report final frequencies
Print
Print "Clock outputs started:"
Print "GPIO2: 4MHz clock ("; pio_freq_4mhz/2; " Hz output)"
Print "GPIO3: 100kHz clock ("; pio_freq_100khz/2; " Hz output)"
Print
Print "Clocks are running. Press CTRL+C to stop."

' Keep program running
Do
   Pause 1000
   Loop



And:

> option list
PicoMite MMBasic RP2040 Edition V6.00.03
OPTION COLOURCODE ON
OPTION CPUSPEED (KHz) 420000
>

Any insight is appreciated!!

-George
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5138
Posted: 09:33am 09 Aug 2025
Copy link to clipboard 
Print this post

I haven't tested the code yet.
But I have 3 things I suggest to look at.

1/ I have never tested the PIO at a CPUSPEED of 420MHz. The ARM may be capable of doing it, but maybe the PIO can't.

2/ The loop is 3 instructions (the JMP also consumes 1 cycle)

3/ The INIT MACHINE is incorrect.
PIO INIT MACHINE 1, 1, pio_freq_100khz, config_100khz, 0, 8

should be
PIO INIT MACHINE 1, 1, pio_freq_100khz, config_100khz,,, 8


both shiftcontrol and executecontrol registers are left at default. You are writing 0 in execute control, and 8 in shiftcontrol, and have no start address (=0).


Volhout

P.S. since the 2 programs are identical, and the difference is in the PIO clock, and in the GPIO pin, you can run both clocks from the same program. Use start address 0 for both clock generators.

P.P.S. if you need 50% duty cycles, you can either
a/ introduce 1 wait state in the "SET PINS, 1" as "SET PINS, 1[1]". The loop is 4 clocks.
b/ use the WRAP function (look at example program 3 in the user manual appendix F). The loop is 2 clocks.
Edited 2025-08-09 19:37 by Volhout
PicomiteVGA PETSCII ROBOTS
 
George H
Newbie

Joined: 03/07/2020
Location: United States
Posts: 33
Posted: 01:47pm 09 Aug 2025
Copy link to clipboard 
Print this post

@ Volhout  Thanks!  It was the init machine statement. Once I corrected that it came to life.  I'll look at WRAP and try to wrap ... my head around it.  Pio is fun, and it's amazing to me to have a BASIC to run it from.  

-G
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1437
Posted: 08:35am 12 Aug 2025
Copy link to clipboard 
Print this post

  George H said  @ Volhout  Thanks!  It was the init machine statement. Once I corrected that it came to life.  I'll look at WRAP and try to wrap ... my head around it.  Pio is fun, and it's amazing to me to have a BASIC to run it from.  

-G



Yes we have a high-level interpreted language but with realtime deterministic capabilities.....Incredible but try to get others to realize the potential here is like banging your head against the proverbial brick wall.
I hang-out at PLCs.net where there are so many problems that would be a trivial issue for the PicoMite, I find myself almost screaming at my monitor  

"Nah, we'd prefer to spend $3K and struggle to make it work"

We need a biggie product to get some recognition.

There used to be a "MicroMite Companion" where the MX170 was coupled to a Propeller-1.

I have spent a few hours coupling the PicoMite to the Propeller-2...What a power-house of a combination  

We have the MMBasic Interpreter + PIO and as a slave, eight 32bit parallel processors and 64 "smartpins". These smartpins are like having another 64 processors that perform all kinds of functions with zero impact on the CPUs. I have just been experimenting with the 16bit (real) DACs and they seem to be pretty darned good.

Imagine; 60+ 16bit DACs (or ADCs) on a $20 chip  
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025