|
|
View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
OpenAI's ChatGPT knows how to write CCS PIC24 programs |
Posted: Fri Dec 09, 2022 2:25 pm |
|
|
For those who have been under a rock the past week, OpenAI's new ChatGPT is pretty amazing. In addition to generating haiku, poems, song lyrics, advertising text and limericks, it can also analyze and write code.
Much to my surprise, it knows the CCS IDE. The following program was generated with about five exchanges.
I asked it to write a program for PIC24 using the CCS IDE, and specified the processor.
I asked it to enable output using the IDE debugger for printfs and such.
I asked it to write a routine to convert dBm to watts. Initially it did not produce what I expected, so I told it to make sure "30 dBm is 1 watt" and it corrected the function.
I asked it to add code to use that function with a specific set of values.
I asked it to write the reverse function. It did, and generated test code for it.
I asked it to make the function names camelCase.
The only things I had to tell it about were "remove BURNOUT" which it put in the #fuses, and "pow() can't be found". It added <math.h> but at the top, which doesn't work. I had to tell it "move math.h below #fuses".
It's great fun. I actually used it to look at a C routine we have for our own dBm to Watt conversions (that make more use of integer math) and it was able to tweak them a bit so instead of generating 112 lines of PIC24 assembly it was about 86. Smaller code, and faster.
Really wild.
Code: |
#include <24FJ64GA002.h>
#device *=16
#use delay(internal=16Mhz)
#fuses NOWDT, NOPROTECT
#include <math.h>
// Subroutine to convert dBm to watts
float convertDBMToWatts(float dbm)
{
// Convert dBm to watts
return pow(10, (dbm - 30) / 10);
}
// Subroutine to convert watts to dBm
float convertWattsToDBM(float watts)
{
// Convert watts to dBm
return 10 * log10(watts) + 30;
}
void main()
{
char *str = "Hello, World!";
// Use the ICD debugger output
#use rs232(ICD)
printf(str);
// Test the convertDBMToWatts() function
float dbm_values[] = {1, 30, 40, 50, 60};
for (int i = 0; i < 5; i++)
{
float dbm = dbm_values[i];
float watts = convertDBMToWatts(dbm);
printf("%f dBm = %f watts\r\n", dbm, watts);
}
// Test the convertWattsToDBM() function
float watt_values[] = {0.001, 0.01, 0.1, 1, 10};
for (int i = 0; i < 5; i++)
{
float watts = watt_values[i];
float dbm = convertWattsToDBM(watts);
printf("%f watts = %f dBm\r\n", watts, dbm);
}
}
|
But mostly I use it for stuff like this:
"Write a limerick about the CCS compiler for PIC24"
here once was a CCS compiler
For PIC24 microcontrollers
It made code so neat
And bug-free, complete
And all without causing any disorder
With its easy-to-use interface
It made programming a breeze
No more coding by hand
With this tool at your command
You'll have your microcontroller doing as you please
P.S. - Its generated BATCH fies, PowerShell scripts, and even old Microsoft BASIC stuff for my ancient Radio Shack computer. I've been having alot of fun with it on my website/blog lately. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Last edited by allenhuffman on Fri Dec 09, 2022 2:48 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Dec 09, 2022 2:43 pm |
|
|
re: even old Microsoft BASIC stuff for my ancient Radio Shack computer. I
gee, can you make a dinosaur feel even OLDER !! thanks.....
I still have 4 working TRS80 MODEL100s as well as 2 TRS80 MODEL IIIs here
I miss the old days of Teletypes and quartz windowed PICs ..
Jay, the dinosaur |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Fri Dec 09, 2022 2:50 pm |
|
|
temtronic wrote: |
gee, can you make a dinosaur feel even OLDER !! thanks.....
I still have 4 working TRS80 MODEL100s as well as 2 TRS80 MODEL IIIs here
|
I understand that. I've been blogging about my TRS-80 Color Computer ;-) The A.I. tries to make modern BASIC but you can keep saying "I don't have LOOP, I don't have WHILE, variables cannot be longer than two characters" it gets the job done. I'm getting it to write an old style text screen game, and I've had it write a simple text adventure in PowerShell. Just amazing.
I continue my experiment and am having it modify the functions to reduce the amount of floating point it uses. Fun stuff! _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Dec 09, 2022 3:48 pm |
|
|
ah, the COCO...
used to do 64KB RAM upgrades,then load a OS ROM 'image' into RAM along with the game, then save everything to cassette. 'Magically' you could then run the game without the 'cart'.
I had access to all the game carts since I worked at 3 Radio Shacks...
somehow I feel even older now... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Dec 10, 2022 5:00 am |
|
|
I must admit that most of the 'AI's floating around, I have found to be
Genuinely Thick, rather than Artificial Intelligence's....
The supposedly 'AI' search engines make you cry for the old versions
that would find what they were asked for.
A little play with this suggests it is one of the better ones, and this is
quite pleasant to see.
A a comment, it looks as if it 'knows' about PCM or PCH rather than
PCD. Hence it adding the #device *=16, which does not apply to PCD.
Trying to work out where it might have got 'BURNOUT' from?. Can't think of
any similar language using this as an instruction. Odd.
It must have pulled the core layout from CCS code on the web, which it
then modified using examples in other code that did the required conversion.
Very impressive really. Would be really nice to actually see what it used
as it's sources.
Worth looking at:
[url]
https://www.theatlantic.com/technology/archive/2022/12/chatgpt-openai-artificial-intelligence-writing-ethics/672386/
[/url] |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Dec 10, 2022 6:17 am |
|
|
Maybe its shooting for BROWNOUT? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Dec 10, 2022 6:36 am |
|
|
sems logcal to me, jkowing how I tpye
sigh...
good thing I know how to cut and paste and saved a LOT of working functions years ago.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Dec 10, 2022 8:38 am |
|
|
If so, it is a very interesting insight into how it works.
Think about it. Brownout would not be in a dictionary, but burnout would
be. So it has subsituted it's nearest dictionary word for one it does not
'understand'. It needs a bit of electronic nomenclature added to it's
dictionary.... |
|
|
diode_blade
Joined: 18 Aug 2014 Posts: 55 Location: Sheffield, South Yorkshire
|
|
Posted: Mon Dec 12, 2022 2:58 am |
|
|
Code: |
#include <18F46K22.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=8000000)
#define PWM_FREQUENCY 500 // PWM frequency in Hz
// Set up PWM1, PWM2, and PWM3 on pins RA0, RA1, and RA2 respectively
#define PWM_1 PIN_A0
#define PWM_2 PIN_A1
#define PWM_3 PIN_A2
// Set up the ADC to read from AN0, AN1, and AN2
#define ADC_1 0
#define ADC_2 1
#define ADC_3 2
void main()
{
setup_adc(ADC_CLOCK_INTERNAL); // Set up the ADC with an internal clock
setup_adc_ports(AN0_TO_AN3); // Set up the ADC to read from AN0 to AN3
// Set up PWM1, PWM2, and PWM3 with a frequency of PWM_FREQUENCY
setup_CCP1(CCP_PWM);
setup_CCP2(CCP_PWM);
setup_CCP3(CCP_PWM);
set_pwm1_duty(0);
set_pwm2_duty(0);
set_pwm3_duty(0);
set_pwm1_period(PWM_FREQUENCY);
set_pwm2_period(PWM_FREQUENCY);
set_pwm3_period(PWM_FREQUENCY);
while(
// AT this point the AI faulted the error message was "error in body stream" and the AI stopped and the code dissapeared, but I kept hitting the copy button to save the code as it wrote it.
|
Just tried it, I asked it to write code for using 3 pwm channels on a 18f46k22 and to use the adc for the duty cycle.
Interesting though. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Dec 12, 2022 6:05 am |
|
|
Yes, it is interesting.
You can to an extent see 'how' it is getting to things. Very like some
of the crude student versions, where they have pulled 'bits' from various
sources, without any hint of understanding how to actually put things
together. So you have it using ADC_CLOCK_INTERNAL, which is wrong,
for a 16Mhz master clock, but is a very common fault. Then no actual
specification of how the clock is going to be generated. It then doesn't
realise that the CCP needs to use specific pins (not a PPS chip). Then
that the ADC selections clash with the PWM pin selections. It is quite
remarkably reminiscent of a very young student trying to program without
reading any notes or reference works, but instead doing it by putting
together pieces from lots of unconnected stuff.
A very good example of how to write something that has no hope at all
of working.... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|