Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:47 24 Nov 2024 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 : ChatGPT when you need code explaining

Author Message
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 857
Posted: 09:12am 20 Aug 2024
Copy link to clipboard 
Print this post

Found some µpython code that wasn't commented and so I handed the line over to ChatGPT:



  Quote  
Components:
pwm:

This is the variable that will hold the PWM object created by the PIOPWM class.
PIOPWM(0, 25, max_count=(1 << 16) - 1, count_freq=10_000_000):

This is the instantiation of the PIOPWM class. It creates a new PWM object with the specified parameters.
0 (First Argument):

This is likely the index or ID of the PIO (Programmable Input/Output) state machine being used. PIO is a feature in some microcontrollers like the RP2040 that allows for highly flexible control over GPIO pins.
25 (Second Argument):

This is the GPIO pin number to which the PWM signal will be output.
max_count=(1 << 16) - 1:

This sets the maximum count value for the PWM. The expression (1 << 16) - 1 is a bitwise operation that shifts 1 left by 16 bits, resulting in 65536, and then subtracts 1, resulting in 65535. This means the PWM counter will count from 0 to 65535 (a 16-bit counter).
count_freq=10_000_000:

This sets the frequency of the counter to 10 MHz (10 million counts per second). The frequency at which the PWM signal is generated is determined by this count frequency in combination with the max_count value.
Summary:
This code creates a PWM signal on GPIO pin 25 using the PIO (Programmable Input/Output) feature. The signal is generated by a 16-bit counter that counts up to 65535, and the counting is done at a frequency of 10 MHz. The PIOPWM class likely abstracts the low-level details of generating PWM signals using the PIO state machines available in some microcontrollers like the RP2040 (found in the Raspberry Pi Pico).


How cool is that  
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 857
Posted: 09:33am 20 Aug 2024
Copy link to clipboard 
Print this post

I asked: "So this could give me a DAC with 16bit resolution?"

  Quote  
Yes, this setup could indeed be used to create a Digital-to-Analog Converter (DAC) with 16-bit resolution using PWM, although it's technically an approximation rather than a true DAC.

Explanation:
16-bit Resolution:
The max_count of 65535 gives you 16-bit resolution. This means the PWM duty cycle can be set to any value between 0 and 65535, allowing you to finely control the output voltage.

PWM as a DAC:
By filtering the PWM output (usually with a low-pass filter, like an RC circuit), you can smooth out the pulses and produce an analog voltage that corresponds to the average value of the PWM signal. The duty cycle of the PWM signal determines the output voltage:

A duty cycle of 0 corresponds to 0V.
A duty cycle of 65535 corresponds to the maximum voltage (close to the supply voltage).
Intermediate duty cycles correspond to intermediate voltages.
Count Frequency:
The count_freq=10_000_000 means that the PWM signal is operating at a high frequency (10 MHz divided by 65536 gives about 152.6 Hz for the PWM base frequency). This high frequency is beneficial because it makes it easier to filter out the high-frequency components, leaving a stable DC voltage.

Considerations:
Filtering: To achieve a smooth analog output, you'll need an appropriate low-pass filter. The filter's cutoff frequency should be much lower than the PWM frequency to remove the high-frequency components effectively.

Accuracy and Stability: The accuracy of the DAC approximation depends on the stability of the PWM frequency and the quality of the filtering. Any fluctuations in the PWM signal or the supply voltage could affect the analog output.

Summary:
By using PWM with a 16-bit resolution and a suitable low-pass filter, you can create a DAC-like output. This won't be as precise as a dedicated DAC chip, but it can still be very effective for many applications, especially when high precision isn't critical.


Pretty darned impressed.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6780
Posted: 10:28am 20 Aug 2024
Copy link to clipboard 
Print this post

Always bear in mind though that AI will tend to give you the reply that it thinks you want or that it considers would be the most suitable. Neither of these cases means that it has to be accurate, that's a lucky side effect if it happens to be so. :)  It actually has no concept of accuracy - or of anything else, in fact.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1241
Posted: 10:32am 20 Aug 2024
Copy link to clipboard 
Print this post

  Mixtel90 said  Always bear in mind though that AI will tend to give you the reply that it thinks you want or that it considers would be the most suitable. Neither of these cases means that it has to be accurate, that's a lucky side effect if it happens to be so. :)  It actually has no concept of accuracy - or of anything else, in fact.

¡Así es exactamente!  
Regards
Michael
causality ≠ correlation ≠ coincidence
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4223
Posted: 01:15pm 20 Aug 2024
Copy link to clipboard 
Print this post

Phenix,

ChatGTP may explain you a 16 bit ADC pn a pico. I am willing to bet that there is no-one here on this forum that can design the hardware to give you a real 16 bit resolution and accurate DAC on a pico. Offsets that can be 25uV max over temperature. Linearity 0.0016%.

External reference voltage, pwm filter, buffers in and out.

Much easier to buy a 16bit DAC from TI or AnalogDevices, and write tje SPI cpde.

Volhout
Edited 2024-08-21 00:05 by Volhout
PicomiteVGA PETSCII ROBOTS
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 02:04pm 20 Aug 2024
Copy link to clipboard 
Print this post

do remember, also, that it makes stuff up (It gave me a mnemonic of CP B,76 in some Z80 code once   )

and check out this straight contradiction trying to back up a miscalculation



have to say I don't use it much because unless you are already frequented well with a subject, you can't trust it to give good info.

I use it to generate the text of ADO user stories etc. where it just has to spool out tons of guff.

h
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 857
Posted: 02:56pm 20 Aug 2024
Copy link to clipboard 
Print this post

@Volhout

Being a perfect DAC is not the issue. When running a servo-loop @1KHz, the DAC output is never steady. However, I have a range of 20V (+-10v) and with our existing PWM, we have a range of 0 to 100% with a resolution of 0.1% = 1000.

So I have a resolution of 20V / 1000 = 20mV.

I only mentioned 16-bit because the example was 65535 but if I could get 12-bits, my resolution would be 20V / 4096 ~= 5mV.

Right now, I have actual 12-bit DACs and intend to stick with them (PWM updates cost me an entire millisecond) but I stumbled across that code and wanted to be sure I understood it.  
 
DaveJacko
Regular Member

Joined: 25/07/2019
Location: United Kingdom
Posts: 76
Posted: 10:06pm 20 Aug 2024
Copy link to clipboard 
Print this post

Phenix mate,
interesting post, btw.
would it be a good idea to have a general conversation about AI on this forum?
as this is, surely, where the really smart, trusted guys hang out...
I don't think AI could generate MMBASIC just yet.
that's my Turing Test !
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5078
Posted: 11:25pm 20 Aug 2024
Copy link to clipboard 
Print this post

  DaveJacko said  
I don't think AI could generate MMBASIC just yet.


Yeah tried, it was pretty useless. The LLM probably hasn't been introduced to much MMBASIC code, but as it gets updated and trained, this may improve.

Glenn
The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2120
Posted: 11:53pm 20 Aug 2024
Copy link to clipboard 
Print this post

it's new to me and interesting
 
Rickard5

Guru

Joined: 31/03/2022
Location: United States
Posts: 463
Posted: 02:27am 21 Aug 2024
Copy link to clipboard 
Print this post

@ PhenixRising
A Friend of mine's sone is one of those kids that always has the hottest Gaming PC. so last Christmas I took his old Ryxen 7 gaming box when he got his new machine and Built me a OLLAMA Web UI Box so I could Private full featured Un censored Chat GPT anywhere on my Network and the best Part Sir Mark Rowley, Commissioner of the Metropolitan Police Service won't come and get me :) :) No seriously there are TONS of Great Models out there for free Right no I'm using a Python that I can give 2 or 3 sample code snippets and tell it to make one program and done
I may be Vulgar, but , while I'm poor, I'm Industrious, Honest,  and trustworthy! I Know my Place
 
Print this page


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

© JAQ Software 2024