User Tools

Site Tools


digispark:tricks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
digispark:tricks [2013/05/19 11:40]
RC Navy [How to increase Hardware PWM Frequency?]
digispark:tricks [2013/10/16 01:40] (current)
digistump [Get rid of 5 second startup delay]
Line 3: Line 3:
 ====== Detect if system clock was calibrated ====== ====== Detect if system clock was calibrated ======
  
-When the digispark boots up, it uses the USB signal to calibrate ​it'​s ​clock to 16.5mhz. When powered by an external power supply this doesn'​t happen and the clock speed is closer to 16.0mhz, with roughly 10% accuracy. If you want to detect if the clock was calibrated before starting your program you can use these two functions:+When the digispark boots up, it uses the USB signal to calibrate ​its clock to 16.5mhz. When powered by an external power supply this doesn'​t happen and the clock speed is closer to 16.0mhz, with roughly 10% accuracy. If you want to detect if the clock was calibrated before starting your program you can use these two functions:
  
 <code c> <code c>
Line 32: Line 32:
 ====== Reboot in to the bootloader from your program ====== ====== Reboot in to the bootloader from your program ======
  
-In some situations you might find it difficult to unplug and reconnect the digispark in order to replace ​it'​s ​program. You can use the function below to reboot in to the bootloader without needing to disconnect the digispark physically.+In some situations you might find it difficult to unplug and reconnect the digispark in order to replace ​its program. You can use the function below to reboot in to the bootloader without needing to disconnect the digispark physically.
  
 <code c> <code c>
Line 48: Line 48:
 ====== Get rid of 5 second startup delay ====== ====== Get rid of 5 second startup delay ======
  
-When the digispark is powered up, it waits 5 seconds to give your computer a chance to upload a different program. For some projects this delay is less than ideal. For these situations we have another version of the bootloader which only accepts uploads if you connect D5 to ground with a wire or a button before plugging the digispark in. You can add an '​update'​ button to your project for times you want to change ​it'​s ​software, and use the button for other stuff in your app, or use the reboot to bootloader code above to make it reboot and accept a program without having to unplug and replug the device.+When the digispark is powered up, it waits 5 seconds to give your computer a chance to upload a different program. For some projects this delay is less than ideal. For these situations we have another version of the bootloader which only accepts uploads if you connect D5 to ground with a wire or a button before plugging the digispark in. You can add an '​update'​ button to your project for times you want to change ​its software, and use the button for other stuff in your app, or use the reboot to bootloader code above to make it reboot and accept a program without having to unplug and replug the device.
  
 Instructions are on the forum for [[http://​digistump.com/​board/​index.php/​topic,​320.msg1711.html#​msg1711|Mac]] and [[http://​digistump.com/​board/​index.php/​topic,​320.msg1778.html#​msg1778|Windows]]. Linux users should follow the Mac instructions pretty much. Note that this is an unsupported modification and does currently void your digispark'​s warranty. So far nobody seems to have had any trouble with it though. Instructions are on the forum for [[http://​digistump.com/​board/​index.php/​topic,​320.msg1711.html#​msg1711|Mac]] and [[http://​digistump.com/​board/​index.php/​topic,​320.msg1778.html#​msg1778|Windows]]. Linux users should follow the Mac instructions pretty much. Note that this is an unsupported modification and does currently void your digispark'​s warranty. So far nobody seems to have had any trouble with it though.
  
 +The newest version of the jumper firmware can be found here: https://​github.com/​Bluebie/​micronucleus-t85/​tree/​master/​upgrade/​releases
  
 ====== Debug with blinkenlights ====== ====== Debug with blinkenlights ======
Line 100: Line 101:
 You can use an I/O pin as a ground/+5V if you have some left and the current that flow in it is low (a few milliamps if you want the pin to stay around 0V/+5V. If you don't care about the exact voltage you can go up to 40mA per pin  according to the datasheet but it's good to keep a safety margin). You can use an I/O pin as a ground/+5V if you have some left and the current that flow in it is low (a few milliamps if you want the pin to stay around 0V/+5V. If you don't care about the exact voltage you can go up to 40mA per pin  according to the datasheet but it's good to keep a safety margin).
  
-Configure the pin as an output and set it'​s ​value to either LOW or HIGH (for ground or +5V).+Configure the pin as an output and set its value to either LOW or HIGH (for ground or +5V).
  
 For example to connect a button between pin 0(set as input) and pin 2(acts as ground): For example to connect a button between pin 0(set as input) and pin 2(acts as ground):
Line 176: Line 177:
 ===== Introduction ===== ===== Introduction =====
 The Digispark -based on the ATtiny85 microcontroller- has 4 built-in hardware PWM. The Digispark -based on the ATtiny85 microcontroller- has 4 built-in hardware PWM.
-Currently, only 3 of them are usable ​within ​the arduino environment:​+Currently, only 3 of them are usable ​in the arduino environment:​
   * Pin0   * Pin0
   * Pin1   * Pin1
Line 189: Line 190:
    
 ===== Hardware PWM ===== ===== Hardware PWM =====
 +To use hardware PWM in your sketch, use the //​[[http://​digistump.com/​wiki/​digispark/​tutorials/​basics#​analog_writeaka_pwm|analogWrite()]]//​ function.\\ ​
 +
 In the default implementation of PWM in arduino IDE, hardware PWM frequency is quite low:​\\ ​ In the default implementation of PWM in arduino IDE, hardware PWM frequency is quite low:​\\ ​
 ^        Digispark @ 16.5MHz ​       ^^ ^        Digispark @ 16.5MHz ​       ^^
-^ Digispark Pin ^ PWM Frequency (Hz) ^ + Digispark Pin   PWM Frequency (Hz)  
-| Pin0          | 504                +      ​Pin0              ​504          
-| Pin1          | 504                +      ​Pin1              ​504          
-| Pin4          | 1007               ​|+      ​Pin4              ​1007         ​|
 By setting //​FAVOR_PHASE_CORRECT_PWM//​ to 0 in //​arduino-1.0x/​hardware/​digispark/​cores/​tiny/​core_build_options.h//​ file, it's possible to double the frequency on Pin1:​\\ ​ By setting //​FAVOR_PHASE_CORRECT_PWM//​ to 0 in //​arduino-1.0x/​hardware/​digispark/​cores/​tiny/​core_build_options.h//​ file, it's possible to double the frequency on Pin1:​\\ ​
 ^  Digispark @ 16.5MHz ​             ^^ ^  Digispark @ 16.5MHz ​             ^^
 ^  + FAVOR_PHASE_CORRECT_PWM set to 0  ^^ ^  + FAVOR_PHASE_CORRECT_PWM set to 0  ^^
-^ Digispark Pin ^ PWM Frequency (Hz) ^ + Digispark Pin   PWM Frequency (Hz)  
-| Pin0          | 504                +      ​Pin0              ​504          
-| Pin1          | 1007               ​+      ​Pin1             1007          
-| Pin4          | 1007               ​|+      ​Pin4             1007          |
 But for some applications,​ this may be not sufficient. For example, if the Digispark is used as **E**lectronic **S**peed **C**ontroller for brushed motors, using hardware PWM which such a low frequency is not perfect: the **ESC** will be noisy (audible) since PWM frequency is within the audio range.\\ ​ But for some applications,​ this may be not sufficient. For example, if the Digispark is used as **E**lectronic **S**peed **C**ontroller for brushed motors, using hardware PWM which such a low frequency is not perfect: the **ESC** will be noisy (audible) since PWM frequency is within the audio range.\\ ​
 Another application where "​high"​ frequency is required: **D**igital **A**nalog **C**onverter. Using a PWM pin followed with a simple RC low pass filter, it's very easy to build a **DAC**. Using a high PWM frequency will increase the response time and will reduce the ripple.\\ ​ Another application where "​high"​ frequency is required: **D**igital **A**nalog **C**onverter. Using a PWM pin followed with a simple RC low pass filter, it's very easy to build a **DAC**. Using a high PWM frequency will increase the response time and will reduce the ripple.\\ ​
Line 216: Line 219:
 ^  + FAVOR_PHASE_CORRECT_PWM set to 0  ^^ ^  + FAVOR_PHASE_CORRECT_PWM set to 0  ^^
 ^  + %%MS_TIMER_TICK_EVERY_X_CYCLES%% set to 1  ^^ ^  + %%MS_TIMER_TICK_EVERY_X_CYCLES%% set to 1  ^^
-^ Digispark Pin ^ PWM Frequency (Hz) ^ + Digispark Pin   PWM Frequency (Hz)  
-| Pin0          | 32227              +      ​Pin0             32227         ​
-| Pin1          | 64453              +      ​Pin1             64453         ​
-| Pin4          | 64453              |+      ​Pin4             64453         ​|
  
 __Note__: Frequencies for other //​%%MS_TIMER_TICK_EVERY_X_CYCLES%%//​ values are easy to compute: __Note__: Frequencies for other //​%%MS_TIMER_TICK_EVERY_X_CYCLES%%//​ values are easy to compute:
Line 226: Line 229:
 ===== Software PWM ===== ===== Software PWM =====
 New Digispark IDE release (may 2013) introduces a new library: <​TinySoftPwm>,​ a tiny Software PWM library.\\ ​ New Digispark IDE release (may 2013) introduces a new library: <​TinySoftPwm>,​ a tiny Software PWM library.\\ ​
 +
 +To use software PWM in your sketch, use the //​TinySoftPwm_analogWrite()//​ function (just add the //​TinySoftPwm_//​ prefix to the common analogWrite() function).\\ ​
 +
 ==== <​TinySoftPwm>​ library features ==== ==== <​TinySoftPwm>​ library features ====
-  * Easy to use, only 3 functions: TinySoftPwm_begin(),​ TinySoftPwm_analogWrite() ​and TinySoftPwm_process()+  * Easy to use, only 3 functions:\\  
 +    * TinySoftPwm_begin(), ​which shall be called in the setup() function 
 +    * TinySoftPwm_analogWrite() 
 +    * TinySoftPwm_process(), which shall be called periodically in the main loop or in timer interruption
   * Can configure all the 6 pins of the Digispark for software PWM   * Can configure all the 6 pins of the Digispark for software PWM
   * Compact: each additional declared PWM pin requires around 60 bytes   * Compact: each additional declared PWM pin requires around 60 bytes
Line 237: Line 246:
 The PWM Manager of the <​TinySoftPwm>​ library can be called from the ISR of the version of the <​VirtualWire>​ delivered with the new Digispark IDE release (may 2013): nice to control remotely a RGB strip led through cheap RF links! The PWM Manager of the <​TinySoftPwm>​ library can be called from the ISR of the version of the <​VirtualWire>​ delivered with the new Digispark IDE release (may 2013): nice to control remotely a RGB strip led through cheap RF links!
  
-===== Let's mix! =====+===== Let's mix Hardware and Software PWM! =====
 <​TinySoftPwm>​ library can cohabit with Hardware PWM and with DigiUSB: see demo sketch below.\\ ​ <​TinySoftPwm>​ library can cohabit with Hardware PWM and with DigiUSB: see demo sketch below.\\ ​
 <code c> <code c>
digispark/tricks.1368988813.txt.gz · Last modified: 2013/05/19 11:40 by RC Navy