Hi, I was playing around with GCB again to see if I could do a project for PWMout with variable frequency and variable duty.
I encountered 3 problems using (yes I know, I just love it) GCBASIC IDE GCB@SYN version.
Didn't know if it should be seperate topics.
1) Is there a GCB command like PICAXE for PWMout(Frequency, Duty)
I found this demo but thought it could be usefull to have a PWM.h file for it if not available.
2) If I HexFlash a chip then the G+Stool(FrankSteinberg) disappears quick before I could read any errors.
The window has it's own "Close" button and could remember using it in the past.
How can I eliminate the automatic close option?
3) I used a degree sign for Celsius on a SSD1306 128x32 display before but it is not showing on a 128x64 display.
Any ideas
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) Depends what chip you are using. Some chips have proper PWM controls. So, it depends on chip selected.
2) Have a look athe setting for Synwrite. One of the GCB options does not close the window. Add the parameter to the option you want to keep open
3) Check the ASM. Is the ASM send the correct char value to the GLCD routine?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Anobium,
1) I use a ArduinoNano now for testing, but if I would swap later to another type of chip I will have to check this in the datasheet if pwm is mentioned on a specific pin?
2) Where to find this setting for Synwrite with the GCB options does not close the window?
3) According to the ASCII table we made is it chr(248), unless I made a mistake somewhere.
But I found that the location on the display was overwritten by the temperature value. Now the degree sign is visible.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I found these PWM demo's for the mega328p:
interrupt_driving_led_via_pwm_mega328p.gcb
led_interrupt_driven_via_pwm_mega328p.gcb
I couldn't understand the PWM demo's enough to implement it in my EDM (Electrical Discharge Machine) test setup.
Circuit is just a PWM, with adjustable frequency and adjustable duty, to a mosfet that will spark for tiny metal removing.
I want to make something like this as a procedure: (H)PWM pin, freq, duty
-Freq and Duty will be set by a POTmeter
-The period (length of 1 on/off cycle) is here not relevant because the PWMpin should pulse ongoing until the pin is set Off.
Is this possible in GCB?
Last edit: Haroen 2024-04-07
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I can do the research, hence I ask the question. If the Nano is a 328p then the PWM commands work as shown in the Help. If based on a 168 then this should work also.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I use a 328p Nano.
This code is what I have so far but have not checked functionality yet.
Please correct my code for proper working.
I will then check the code for correct adjustable frequency and duty on my oscilloscope.
What range of frequency will I be able to set?
;-----Configuration#chip mega328p, 16'#option explicit;-----DefineHardwaresettingsdirPortB.5out' Define constants#define POT_FREQ_PIN A0 ' Analog pin connected to the frequency potentiometer#define POT_DUTY_PIN A1 ' Analog pin connected to the duty_cycle potentiometer#define PWM_PIN PortB.5 ' PWM output pin' Define variablesDimfreqAsWordDimdutyAsByteMAIN:'Read frequency and duty cycle from potentiometersfreq=ReadFreqPot()duty=ReadDutyPot()StartPwm(PWM_PIN,freq,duty)' Start PWM outputWait100ms' Delay to prevent rapid reading of potentiometersGotoMAIN' Loop back to MAINFunctionReadFreqPot()AsWord' Procedure to read frequency potentiometer and map value to frequency rangeDimrawValueAsWordrawValue=READAD10(POT_FREQ_PIN,TRUE)' Read analog input and scale to 10-bit value (0-1023)ReturnMap(rawValue,0,1023,100,5000)' Map raw value to desired frequency range (100-5000 Hz)EndFunctionFunctionReadDutyPot()AsByte' Procedure to read duty cycle potentiometer and map value to duty cycle rangeDimrawValueAsWordrawValue=READAD10(POT_DUTY_PIN,TRUE)' Read analog input and scale to 10-bit value (0-1023)ReturnMap(rawValue,0,1023,0,255)' Map raw value to duty cycle range (0-255)EndFunctionSubStartPwm(pinAsByte,freqAsWord,dutyAsByte)' Procedure to start PWM output on specified pin with given frequency and duty cycleDimperiodAsWordDimpulseWidthAsWord'Calculate period and pulse width based on frequency and duty cycleperiod=1000000/freq' Convert frequency to period in microsecondspulseWidth=(duty*period)/255' Calculate pulse width based on duty cycle'Generate PWM-like output on the specified pinDopin=High' Set pin highWaitpulseWidthms' Wait for pulse width durationpin=Low' Set pin lowWait(period-pulseWidth)ms' Wait for remaining period durationLoopEndSubSubStopPwm(pinAsByte)' Procedure to stop PWM output on specified pinpin=Low' Set pin lowEndSub
Last edit: Haroen 2024-04-08
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The AVR HPWM command is fully described in Help. The example for the Mega328p should be just the ticket for the NANO? The code is set to adjust the duty cycle over its full range at a fixed frequency. One could use nested loops to demonstrate every permutation of freq and duty. Writing a 0 to the duty cycle shuts the PWM enable bit off.
Please test the example for expected behavior, then progress from there.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I downloaded and installed the new version of GCstudio and browsed through the help commands and couldn't find any PWM command.
I found 4 demo's with 1 "Mega328pb_fixedmode_pwm.gcb" that has Freq, Duty settings:
#chip MEGA328PB, 20
#option Explicit
#Define PWMSIGNAL portD.5
'Set direction of PWMSIGNAL
Dir PWMSIGNAL out
'define PWM_Freq in kHz and define PWM_Duty in %
#define PWM_Freq 38
#define PWM_Duty 50
do
PWMOn
wait 5 s
PWMOff
wait 1 s
loop
Will this mega328pb code work for a mega328p chip?
And can the frequency be set in decimals?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To my knowledge their is not a software PWM command for the AVR?. There is a hardware PWM command (i.e. HPWM). The HPWM command is whole number based over the 1-255khz range. If more resolution, accuracy, or sub 1khz values required then manually setting the registers would be in order.
From Help:
'Using HPWM command to alternate ramping leds with the UNO board
#chip mega328,16
'************pwm************************
'Must define AVRTCx, AVRCHANx, and set OCnX pin dir to out
#define AVRTC0 'Timer0
#define AVRCHAN2
dir PortD.5 Out 'OC0B, UNO pin 5
#define AVRTC1 'Timer1
#define AVRCHAN3
#define AVRCHAN4
dir PortB.1 out 'OC1A, UNO pin 9
dir PortB.2 Out 'OC1B, UNO pin 10
#define AVRTC2 'Timer2
#define AVRCHAN7
dir PortD.3 Out 'OC2B, UNO pin 3
do
'63khz works good with 16MHz
'32khz with 8MHz intosc
'4KHz with 8MHz intosc and ckDiv8 fuse
freq = 63
For PWMled1 = 0 to 255
HPWM 2,freq,PWMled1
PWMled2 = NOT PWMled1
HPWM 3,freq,PWMled2
HPWM 4,freq,PWMled2
HPWM 7,freq,PWMled1
wait 5 ms
Next
For PWMled1 = 255 to 0
HPWM 2,freq,PWMled1
PWMled2 = NOT PWMled1
HPWM 3,freq,PWMled2
HPWM 4,freq,PWMled2
HPWM 7,freq,PWMled1
wait 5 ms
Next
loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the replies,
I have read the help.
A working example on youtube was in the range 4,865KHz and 5KHz where tuning the freq and duty is needed for different materials to EDM.
So more resolution is needed to tweek the freq, but no sub 1khz values is required.
How to manually set the registers?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Agree with Anobium, try "Just" the HPWM command first. Divide and conquer as they say. Nothing more frustrating then trying to run down multiple bugs, all at the same time, impossible to troubleshoot.
If you are going to do servos, then there is your servo code. Consider the Mega2560 board, lots of resources there, with the loads of timers/PWM and pins; could possibly run the whole show.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I can't find if this assumption of mine is true:
PortD.5 Out 'OC0B, UNO pin 5 => HPWM 2
PortB.1 Out 'OC1A, UNO pin 9 => HPWM 3
PortB.2 Out 'OC1B, UNO pin 10 => HPWM 4
PortD.3 Out 'OC2B, UNO pin 3 => HPWM 7
Then I've purged the code for just 1 PWMpin to...
#defineAVRTC0'Timer0#define AVRCHAN2dirPortD.5Out'OC0B, UNO pin 5do'63khz works good with 16MHz'32khz with 8MHz intosc'4KHz with 8MHz intosc and ckDiv8 fuse freq = 4 duty = 50 HPWM 2,freq,Dutyloop
I've set the correct COMport, but get these errors when Flashing the ArduinoNano328p: In SynWrite
A warning has been generated:
Warning: The chip may not have programmed properly. Programmer returned
status (1)
The message has been logged to the file Errors.txt.
Aborting due to runtime error 2 (file not found) at line 17933 of D:\code\GCBASIC\trunk\gcbasic.bas()
In GCstudio
Downloading program ...Arduino UNO @115200
Calling : C:\Program Files\GCstudio\gcbasic..\avrdude\avrdude.exe
Parameters : -c Arduino -b 115200 -P COM6 -p ATMEGA328 -U flash:w:"D:_HR_GCB\HPWM from help.hex":i
avrdude.exe: stk500_recv(): programmer is not responding
avrdude.exe: stk500_getsync() attempt 1 of 10: not in sync: resp=0xd4
avrdude.exe: stk500_recv(): programmer is not responding
avrdude.exe: stk500_getsync() attempt 2 of 10: not in sync: resp=0xd4
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, I was playing around with GCB again to see if I could do a project for PWMout with variable frequency and variable duty.
I encountered 3 problems using (yes I know, I just love it) GCBASIC IDE GCB@SYN version.
Didn't know if it should be seperate topics.
1) Is there a GCB command like PICAXE for PWMout(Frequency, Duty)
I found this demo but thought it could be usefull to have a PWM.h file for it if not available.
2) If I HexFlash a chip then the G+Stool(FrankSteinberg) disappears quick before I could read any errors.
The window has it's own "Close" button and could remember using it in the past.
How can I eliminate the automatic close option?
3) I used a degree sign for Celsius on a SSD1306 128x32 display before but it is not showing on a 128x64 display.
Any ideas
:-)
1) Depends what chip you are using. Some chips have proper PWM controls. So, it depends on chip selected.
2) Have a look athe setting for Synwrite. One of the GCB options does not close the window. Add the parameter to the option you want to keep open
3) Check the ASM. Is the ASM send the correct char value to the GLCD routine?
Hi Anobium,
1) I use a ArduinoNano now for testing, but if I would swap later to another type of chip I will have to check this in the datasheet if pwm is mentioned on a specific pin?
2) Where to find this setting for Synwrite with the GCB options does not close the window?
3) According to the ASCII table we made is it chr(248), unless I made a mistake somewhere.
But I found that the location on the display was overwritten by the temperature value. Now the degree sign is visible.
1) Lots of demos for AVR PWM. Should be the same controls for the NANO
2) Now you testing me. See https://sourceforge.net/p/gcbasic/discussion/579126/thread/450a96ed54/?limit=250#eed4
3) Good. All sorted. :-)
1) I will try some more demo's to figure it out.
2) Found it and works wel, thanks.
I found these PWM demo's for the mega328p:
interrupt_driving_led_via_pwm_mega328p.gcb
led_interrupt_driven_via_pwm_mega328p.gcb
I couldn't understand the PWM demo's enough to implement it in my EDM (Electrical Discharge Machine) test setup.
Circuit is just a PWM, with adjustable frequency and adjustable duty, to a mosfet that will spark for tiny metal removing.
I want to make something like this as a procedure: (H)PWM pin, freq, duty
-Freq and Duty will be set by a POTmeter
-The period (length of 1 on/off cycle) is here not relevant because the PWMpin should pulse ongoing until the pin is set Off.
Is this possible in GCB?
Last edit: Haroen 2024-04-07
I can do the research, hence I ask the question. If the Nano is a 328p then the PWM commands work as shown in the Help. If based on a 168 then this should work also.
I use a 328p Nano.
This code is what I have so far but have not checked functionality yet.
Please correct my code for proper working.
I will then check the code for correct adjustable frequency and duty on my oscilloscope.
What range of frequency will I be able to set?
Last edit: Haroen 2024-04-08
Once the StartPWM sub enters the loop, it will never leave it again.
@kent_twt4 Can you help ? The code above need a thorough review. Thanks.
If my code is way off then please just disgard it.
It was just an indication of what I thought.
The AVR HPWM command is fully described in Help. The example for the Mega328p should be just the ticket for the NANO? The code is set to adjust the duty cycle over its full range at a fixed frequency. One could use nested loops to demonstrate every permutation of freq and duty. Writing a 0 to the duty cycle shuts the PWM enable bit off.
Please test the example for expected behavior, then progress from there.
I downloaded and installed the new version of GCstudio and browsed through the help commands and couldn't find any PWM command.
I found 4 demo's with 1 "Mega328pb_fixedmode_pwm.gcb" that has Freq, Duty settings:
Will this mega328pb code work for a mega328p chip?
And can the frequency be set in decimals?
Kent is the expert. But, look up
HPWM AVR OCRnx
in the Help andC:\GCstudio\gcbasic\demos\PWM_Solutions\AVR_Command_Usage_Examples
in the Demos.To my knowledge their is not a software PWM command for the AVR?. There is a hardware PWM command (i.e. HPWM). The HPWM command is whole number based over the 1-255khz range. If more resolution, accuracy, or sub 1khz values required then manually setting the registers would be in order.
From Help:
Thanks for the replies,
I have read the help.
A working example on youtube was in the range 4,865KHz and 5KHz where tuning the freq and duty is needed for different materials to EDM.
So more resolution is needed to tweek the freq, but no sub 1khz values is required.
How to manually set the registers?
To answer we would need to know what method you are using.
Share the method you are using.
I want to try the method of edm based on this video
Frequency indication is mentioned at 3.00 minutes.
I meant the GCBASIC mehod :-) . But, you should use the AVR shown in the Help with the HPWM() command.
Agree with Anobium, try "Just" the HPWM command first. Divide and conquer as they say. Nothing more frustrating then trying to run down multiple bugs, all at the same time, impossible to troubleshoot.
Or, just dive right in. Then, take a look at https://sourceforge.net/p/gcbasic/discussion/projects&guides/thread/66153766/#ae47 to set up the registers manually, and which registers to manipulate for the period and duty cycle.
If you are going to do servos, then there is your servo code. Consider the Mega2560 board, lots of resources there, with the loads of timers/PWM and pins; could possibly run the whole show.
I can't find if this assumption of mine is true:
PortD.5 Out 'OC0B, UNO pin 5 => HPWM 2
PortB.1 Out 'OC1A, UNO pin 9 => HPWM 3
PortB.2 Out 'OC1B, UNO pin 10 => HPWM 4
PortD.3 Out 'OC2B, UNO pin 3 => HPWM 7
Then I've purged the code for just 1 PWMpin to...
I've set the correct COMport, but get these errors when Flashing the ArduinoNano328p:
In SynWrite
In GCstudio
Please post your source. I can look at why the compiler is aborting. I need your GCB and ASM file.
In SynWrite
GCBforum02 purged.gcb
GCBforum02 purged.asm
In GCstudio
HPWM from help.gcb
HPWM from help.asm
Last edit: Haroen 2024-04-12
As I thought. Please update to the latest build, your build is (0.99.01 2022-01-27)... over two years old. Much has changed since then.
Then, retry.
So the PWM code will only work in the newest GCstudio?
I will test the code in the new version...