udamonic.com
  • Start
    • What is a Scamp?
    • Getting Started
    • Getting Started (Windows)
  • Learn
    • What is Forth?
    • Arithmetic
    • The Stack
    • Creating Words
    • Compilation Tricks
    • Decompilation
    • Comments
    • Characters and Strings
    • Accessing Memory
    • Constants, Variables and Values
    • Flow Control
    • Loops
    • Data Structures
    • Doubles, Triples and Quads
    • FlashForth Dictionary Reference
    • Scamp Dictionary Reference
  • Extras
    • Interrupts
    • Timers
    • Multitasking
    • Delays and Tick Count
    • Processor Words
    • Starting up with turnkey
    • Changing the Prompt
    • Formatting Text on Screen
  • Interfacing
    • Using the LED Array
    • Temperature Sensor
    • Input/Output
    • Analog Input
    • Serial >
      • Serial Communication
      • Scamp2 UART Pins
    • I2C >
      • I2C Overview
      • I2C Commands
    • SPI
    • PWM
    • Peripheral Pin Select
    • Input Capture
    • Digital Signal Modulator
  • Create
    • Creating PCBs >
      • Designing PCBs
      • Fabricating PCBs
      • Soldering
    • Sensing >
      • Measuring Temperature
    • LEDs >
      • Adding LEDs
      • LED arc-welder effect
    • Displays >
      • Adding a 7 Segment Display
      • Adding a Touch LCD
      • Touch LCD GUI
      • Game of Pong
    • Adding Extra GPIO
    • Adding a Low Side Switch
    • FizzBuzz
    • Adding MRAM
    • Model Train Control
    • Adding a Real Time Clock
    • Scamp Projects on Youtube
  • Resources
  • Store
  • About
  • Contact
Your Udamonic computer has 6 PWM channels (channels 1 through 6), each capable of generating waveforms with up to 16 bits of resolution. Channels can be mapped to most pins, giving you flexibility in your application. PWM can be used for motor control, generating audio tones, or producing an analog output.
On a Scamp1/Scamp2, pins 0 ... 3, 5, 6, 9...12 can be assigned to PWM channels. Essentially, the same pins that support analog input (white background) also support PWM, with the addition of pin 9. Pins 4, 7 and 8 do not support PWM on a Scamp1/Scamp2.
Picture
Scamp1 - pins with white background (plus pin 9) support PWM


​On a Scamp3, all 12 pins (no limitations) can be assigned to PWM channels. 
Picture
Scamp3 - pins 0..11 support PWM

About Pulse Width Modulation
​

Consider these square waves: 
Picture
​In the first square wave, the width of the high is equal to the width of the low, so this wave is said to have a 50% duty cycle. In other words, it is high for exactly half the cycle. Now, if the amplitude of this square wave is 3.3 V, the average voltage over the cycle is 1.65 V. It is as though we had a constant voltage of 1.65 V.  The second wave has a 10% duty cycle, which means that the average voltage over the cycle is 0.33 V
A low-pass (averaging) filter on the PWM output will convert the pulses to an analog voltage, proportional to the duty cycle of the PWM signal. By varying the duty cycle, we can vary the analog voltage. 

PWM can be used (with appropriate interface hardware) to drive motors, generate audio tones, fade LEDs, or any other application that requires an output more sophisticated than simple on/off.

​Selecting an Output Pin
​

Your Udamonic computer has 6 PWM channels. Any pin can be used for PWM on a Scamp3. On a Scamp1/Scamp2 all pins with the exception of pins 4, 7 and 8​, can be allocated to any PWM channel. Supported channels are channels 1 through 6. 

The word pwm will assign a PWM channel to a pin. The format is pin channel pwm. For example, to assign pin 2 to PWM channel 6:
2 6 pwm
It is possible (and allowed) to have the same channel assigned to multiple pins if your application requires it. For example, to assign channel 5 to both pins 2 and 3:
2 5 pwm
​3 5 pwm
The signal generated by PWM channel 5 will be output on both pins. This can useful if you want to drive an external circuit with a pin, and monitor the PWM values using an oscilloscope connected to second pin. 
​

Setting the Period
​

The word period takes two parameters from the stack, a period value and a channel number, and sets the period of that PWM channel. For example, to set the period of channel 2 to be $400:
$400 2 period
A period of $ffff gives a base frequency of 245 Hz. A period of 2 gives a base frequency of 5.3 MHz, but with only a 2 bit resolution. 

Setting the Duty Cycle
​

The duty cycle for a given PWM channel is set by the word duty. duty takes a value and a channel from the stack. For example, to set the duty cycle for channel 2 to be $300​:
$300 2 duty
For motor control, the duty cycle corresponds to the speed of the motor.

Note that the duty cycle value should always be less than period value for a waveform to be generated. If the duty cycle is equal to or greater than the period, the output will stay high.

Ken Merk has a great video on Scamp PWM.

Learn : Interfacing : PWM
Site powered by Weebly. Managed by Hostwinds
  • Start
    • What is a Scamp?
    • Getting Started
    • Getting Started (Windows)
  • Learn
    • What is Forth?
    • Arithmetic
    • The Stack
    • Creating Words
    • Compilation Tricks
    • Decompilation
    • Comments
    • Characters and Strings
    • Accessing Memory
    • Constants, Variables and Values
    • Flow Control
    • Loops
    • Data Structures
    • Doubles, Triples and Quads
    • FlashForth Dictionary Reference
    • Scamp Dictionary Reference
  • Extras
    • Interrupts
    • Timers
    • Multitasking
    • Delays and Tick Count
    • Processor Words
    • Starting up with turnkey
    • Changing the Prompt
    • Formatting Text on Screen
  • Interfacing
    • Using the LED Array
    • Temperature Sensor
    • Input/Output
    • Analog Input
    • Serial >
      • Serial Communication
      • Scamp2 UART Pins
    • I2C >
      • I2C Overview
      • I2C Commands
    • SPI
    • PWM
    • Peripheral Pin Select
    • Input Capture
    • Digital Signal Modulator
  • Create
    • Creating PCBs >
      • Designing PCBs
      • Fabricating PCBs
      • Soldering
    • Sensing >
      • Measuring Temperature
    • LEDs >
      • Adding LEDs
      • LED arc-welder effect
    • Displays >
      • Adding a 7 Segment Display
      • Adding a Touch LCD
      • Touch LCD GUI
      • Game of Pong
    • Adding Extra GPIO
    • Adding a Low Side Switch
    • FizzBuzz
    • Adding MRAM
    • Model Train Control
    • Adding a Real Time Clock
    • Scamp Projects on Youtube
  • Resources
  • Store
  • About
  • Contact