PWMOut

Syntax:

    PWMOut channel, duty cycle, cycles

Command Availability:

Available on all microcontrollers.

Explanation :

This command uses a software PWM routine included in Great Cow BASIC to produce a PWM signal on the selected port of the chip. This routine does NOT require a PWM module on the chip.

channel sets the channel that the PWM is to be generated on. This must have been defined previously by setting the constants PWM_Out1, PWM_Out2, PWM_Out3 or PWM_Out4. The maximum number of channels available is 4.
duty cycle specifies the PWM duty cycle, and ranges from 0 to 255. 255 corresponds to 100%, 127 to 50%, 63 to 25%, and so on.
cycles is used to set the amount of PWM pulses to supply. This is useful for situations in which a pulse of a specific length is required. The formula for calculating the time taken for one cycle is:

    TCYCLE = (28 + 10C)TOSC+ 255PWM_Delay,

where C is the number of channels used and TOSC is the length of time taken to execute 1 instruction on the chip (0.2 us on a 20 MHz chip, 1 us on a 4 Mhz chip). PWM_Delay is a length of time specified using the PWM_Delay constant.

Example 1 :

    'This program controls the brightness of an LED on PORTB.0
    'using the software PWM routine and a potentiometer.
    #chip 16f877a, 4

    ; ----- Constants
      'PWM constant. This is a required constant.
      #define PWM_Out1 portb.0

    ; ----- Define Hardware settings
      'PWM port out.  This is not required but good practice.
      dir PWM_Out1 out

      'A potentiometer is attached to ANO

    ; ----- Variables
      ' No Variables specified in this example.

    ; ----- Main body of program commences here.
        do
            '100 cycles is a purely arbitrary value
            PWMOut 1, ReadAD(AN0), 100
        loop

    end

Example 2 :

    'This program controls the brightness of an LED on gpio.1
    'using the software PWM routine and a potentiometer.
    #chip 12f675, 4

    ; ----- Constants
      'PWM constant. This is a required constant.
      #define PWM_Out1 gpio.1

    ; ----- Define Hardware settings
      'PWM port out.  This is not required but good practice.
      dir PWM_Out1 out

      'A potentiometer is attached to ANO

    ; ----- Variables
      ' No Variables specified in this example.

    ; ----- Main body of program commences here.
        do
          '100 cycles is a purely arbitrary value
            PWMOut 1, ReadAD(AN0), 100
        loop
    end