Menu

PWMout with variable frequency and duty.

Haroen
2024-04-06
2024-04-21
1 2 3 > >> (Page 1 of 3)
  • Haroen

    Haroen - 2024-04-06

    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

     
  • Anobium

    Anobium - 2024-04-06

    :-)

    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?

     
  • Haroen

    Haroen - 2024-04-06

    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.

     
  • Haroen

    Haroen - 2024-04-06

    1) I will try some more demo's to figure it out.
    2) Found it and works wel, thanks.

     
  • Haroen

    Haroen - 2024-04-07

    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
  • Anobium

    Anobium - 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.

     
  • Haroen

    Haroen - 2024-04-07

    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
    
    ; ----- Define Hardware settings
    dir PortB.5 out
    
    ' 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 variables
    Dim freq As Word
    Dim duty As Byte
    
    MAIN:
        'Read frequency and duty cycle from potentiometers
        freq = ReadFreqPot()
        duty = ReadDutyPot()
        StartPwm(PWM_PIN, freq, duty)                     ' Start PWM output
        Wait 100 ms                                       ' Delay to prevent rapid reading of potentiometers
    Goto MAIN                                             ' Loop back to MAIN
    
    Function ReadFreqPot() As Word                        ' Procedure to read frequency potentiometer and map value to frequency range
        Dim rawValue As Word
        rawValue = READAD10(POT_FREQ_PIN, TRUE)           ' Read analog input and scale to 10-bit value (0-1023)
        Return Map(rawValue, 0, 1023, 100, 5000)          ' Map raw value to desired frequency range (100-5000 Hz)
    End Function
    
    Function ReadDutyPot() As Byte                        ' Procedure to read duty cycle potentiometer and map value to duty cycle range
        Dim rawValue As Word
        rawValue = READAD10(POT_DUTY_PIN, TRUE)           ' Read analog input and scale to 10-bit value (0-1023)
        Return Map(rawValue, 0, 1023, 0, 255)             ' Map raw value to duty cycle range (0-255)
    End Function
    
    Sub StartPwm(pin As Byte, freq As Word, duty As Byte) ' Procedure to start PWM output on specified pin with given frequency and duty cycle
        Dim period As Word
        Dim pulseWidth As Word
        'Calculate period and pulse width based on frequency and duty cycle
        period = 1000000 / freq                           ' Convert frequency to period in microseconds
        pulseWidth = (duty * period) / 255                ' Calculate pulse width based on duty cycle
        'Generate PWM-like output on the specified pin
        Do
            pin=High                                      ' Set pin high
            Wait pulseWidth ms                            ' Wait for pulse width duration
            pin=Low                                       ' Set pin low
            Wait (period - pulseWidth) ms                 ' Wait for remaining period duration
        Loop
    End Sub
    
    Sub StopPwm(pin As Byte)                              ' Procedure to stop PWM output on specified pin
        pin=Low                                           ' Set pin low
    End Sub
    
     

    Last edit: Haroen 2024-04-08
    • jackjames

      jackjames - 2024-04-08

      Once the StartPWM sub enters the loop, it will never leave it again.

       
  • Anobium

    Anobium - 2024-04-08

    @kent_twt4 Can you help ? The code above need a thorough review. Thanks.

     
  • Haroen

    Haroen - 2024-04-08

    If my code is way off then please just disgard it.
    It was just an indication of what I thought.

     
  • kent_twt4

    kent_twt4 - 2024-04-08

    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.

     
  • Haroen

    Haroen - 2024-04-10

    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?

     
  • Anobium

    Anobium - 2024-04-10

    Kent is the expert. But, look up HPWM AVR OCRnx in the Help and C:\GCstudio\gcbasic\demos\PWM_Solutions\AVR_Command_Usage_Examples in the Demos.

     
  • kent_twt4

    kent_twt4 - 2024-04-10

    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
    
     
  • Haroen

    Haroen - 2024-04-11

    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?

     
    • Anobium

      Anobium - 2024-04-11

      To answer we would need to know what method you are using.

      Share the method you are using.

       
  • Haroen

    Haroen - 2024-04-11

    I want to try the method of edm based on this video
    Frequency indication is mentioned at 3.00 minutes.

     
  • Anobium

    Anobium - 2024-04-11

    I meant the GCBASIC mehod :-) . But, you should use the AVR shown in the Help with the HPWM() command.

     
  • kent_twt4

    kent_twt4 - 2024-04-11

    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.

     
  • Haroen

    Haroen - 2024-04-12

    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...

    #define AVRTC0    'Timer0
    #define AVRCHAN2
    dir PortD.5 Out   'OC0B, UNO pin 5
    
    do
        '63khz works good with 16MHz
        '32khz with 8MHz intosc
        '4KHz with 8MHz intosc and ckDiv8 fuse
        freq = 4
        duty = 50
        HPWM 2,freq,Duty
    loop
    

    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

     
  • Anobium

    Anobium - 2024-04-12

    Please post your source. I can look at why the compiler is aborting. I need your GCB and ASM file.

     
  • Haroen

    Haroen - 2024-04-12

    In SynWrite
    GCBforum02 purged.gcb
    GCBforum02 purged.asm

    In GCstudio
    HPWM from help.gcb
    HPWM from help.asm

     

    Last edit: Haroen 2024-04-12
  • Anobium

    Anobium - 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.

     
  • Haroen

    Haroen - 2024-04-12

    So the PWM code will only work in the newest GCstudio?
    I will test the code in the new version...

     
1 2 3 > >> (Page 1 of 3)

Log in to post a comment.