Which stands for ... "Mirror Imaged Bit Angle Modulation"
An include module for PicBasic Pro.
Click image for larger Interactive version.
Click image for larger Interactive version.
This module is a modification of the BAM (Bit Angle Modulation) idea presented by Artistic License.
For more information about BAM, it's blinking problem and how this program came to be, please see this thread ...
http://www.picbasic.co.uk/forum/showthread.php?t=7393
What can it do?
Well ... it can turn almost every pin on your PIC into an LED dimmer. (limitations apply)
First, let's take a look at the basic MIBAM setup,
For an RGB LED (3 outputs) it might look like this ....
;----[ MIBAM Setup ]-------------------------------------------------------- BAM_COUNT CON 3 ; How many BAM Pins are used? INCLUDE "MIBAM.pbp" ; Mirror Image BAM module RED VAR BYTE ; Dutycycle variables GREEN VAR BYTE BLUE VAR BYTE ASM BAM_LIST macro ; Define PIN's to use for BAM BAM_PIN (PORTB,0, RED) ; and the associated Duty variables BAM_PIN (PORTB,1, GREEN) BAM_PIN (PORTB,2, BLUE) endm BAM_INIT BAM_LIST ; Initialize the Pins ENDASM
The Dutycycle range is from 0-255, 0 = OFF, 255 = brightest.
To change the brightness of an LED simply set the Dutycycle variable to the desired level.
You don't need to call any subroutines.
Here's an example of just fading the RGB LED's up and down.
Speed CON 20 ; Smaller=Faster Brightness CON 200 ; Max DutyCycle Main: FOR Red = 0 to Brightness -1 ; Ramp up 1 by 1 PAUSE Speed NEXT RED FOR GREEN = 0 to Brightness -1 PAUSE Speed NEXT GREEN FOR BLUE = 0 to Brightness -1 PAUSE Speed NEXT BLUE FOR Red = Brightness to 1 STEP -1 ; Ramp down 1 by 1 PAUSE Speed NEXT RED FOR GREEN = Brightness to 1 STEP -1 PAUSE Speed NEXT GREEN FOR BLUE = Brightness to 1 STEP -1 PAUSE Speed NEXT BLUE GOTO Main
when using direct FOR loops, you must account for the difference.
The module will work on most 16F's, ALL 18F's, ALL 14-bit enhanced core devices, and some 12F's
If you are using a standard 14-bit PIC. You'll need to add these variables to your program for the Interrupt Context.
For 18F's and Enhanced Cores, you don't need to add these save locations. And they may cause errors if you do.
;____[ For 12F/16F only - Interrupt Context save locations]_________________ wsave var byte $20 SYSTEM ' location for W if in bank0 ;wsave var byte $70 SYSTEM ' Alternate save location for W ' if using $70, comment out wsave1-3 ' --- IF any of these next three lines cause an error ?? ------------------- ' Comment them out to fix the problem ---- ' -- The chip being used determines which variables are needed ------------- wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1 wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2 wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3 '---DO NOT change these----------------------------------------------------- ssave VAR BYTE BANK0 SYSTEM ' location for STATUS register psave VAR BYTE BANK0 SYSTEM ' location for PCLATH register
- In order to use this module, you MUST be using MPASM for the assembler.
PBP's default PM.exe assembler will not work.
- This module takes over the Interrupts on the PIC because the timing of the waveforms must be exact.
With 16F's, You can NOT use any other interrupts, this includes ON INTERRUPT.
With 18F's, The module uses High Priority interrupts, and no other High Priority interrupts can be used.
Low priority Interrupts are available, but you still can NOT use ON INTERRUPT.
- The module uses Timer1, and it cannot be used for any other purposes.
Consequently, the PIC being used must have a Timer1.
- The number of LED's you can use is limited by the OSC frequency.
It's not that the module uses so much processor time that it needs more speed.
The limitation is the LSB (Least Significant Bit) of the DutyCycle, which is so short that it doesn't leave much time to do a whole lot of code.
@ 4Mhz, you can only run 4 LEDs MAX. This can be useful for RGB LED's on small chips.
@ 20Mhz, you can run 20 LEDs since there are more instructions available per period.
@ 48Mhz, you can run 48 LEDs, and anywhere in-between you can have the equivelant number of LEDs to match the OSC frequency.
If you attempt to use too many LEDs for a specific OSC frequency, the program will give a warning to indicate the results will be "Blinky".
- DO NOT overload your PIC.A PIC can only source so much current.
The specific amount is listed in the datasheet for the PIC you are using.
If you are driving a lot of LED's, you may need to buffer them with transistors or various other drive mechanisms.
Option DEFINEs and Constants
- BAM_COUNT CON xx
This constant must match the number of BAM_PINS being used.
If they do not match, an error will be displayed.
- DEFINE BAM_FREQ xxx
By default, the module determines what the maximum refresh rate is for any given setup, and uses that rate. Depending on the conditions, it may be 700hz or more.If desired, you can set the frequency lower with this define.
- DEFINE BAM_INFO 1
When using this define, MIBAM will display some information about the Refresh Rate (Frequency), Minimum Period, and number of pins used.
- ScopeSync VAR PORTx.x
If this alias is used, MIBAM will output a sync signal on the specified pin for use with an oscilloscope.
This gives a nice stable view of the waveform.
The Pin is automatically set to output.
Error/Warning messages
- Error: Symbol not previously defined (wsave)
When using a 12F or 16F, you need to add the wsave block shown above to your program.
This is not necessary for 18F's, so the vars have been omitted from the module.
- ERROR: Variable wsaveX position request beyond RAM_END xx
If the chip you are using doesn't have GP RAM in all 4 banks, then you have to comment out the wsave variables for any of the banks that don't have any.
The ERROR message(s) tell you which ones need to be commented.
If the chip has ACCESS RAM at address $70, then it's best to use that location, instead of wsave1-3.
- Error: BAM_COUNT (x) is less than # of Pins used (x)
The BAM_COUNT constant MUST match the number of BAM_PINs being used.
Increase BAM_COUNT to fix the Error.
- Error: Duplicate label ("BAM_PIN" or redefining symbol that cannot be redefined)
The module requires case sensitivity in MPASM. But sensitivity has been turned off in MCS.
In MicroCode Sudio, View | Compile and Program Options | Compiler Tab ...Check the "Case sensitive" box.
- WARNING: Too many BAM pins for xx Mhz - Results will be BLINKY!
The module can only run 1 LED per Mhz of the CPU's main Oscillator.Reducing the number of LED's or increasing the OSC frequency should eliminate the warning.
- WARNING: BAM frequency (xxHz) is Less than Requested (xxHz)
The module determines what the maximum refresh rate is for any given setup.
If a BAM_FREQ has been defined that is higher than the maximum frequency, the module will warn you of the situation.
This warning is not critical, and may still work in your application.Commenting the DEFINE BAM_FREQ line will squelch the warning.
- MESSAGE: 'BAM_INFO' - MinPeriod= xx inst, Cycle= xxxx inst, Pins= xx, FREQ= xxx Hz
This is an informational message that will be shown if you have set DEFINE BAM_INFO 1 or get a "BAM frequency WARNING".
Attached is the MIBAM.pbp module.
Download and unzip it to your PBP folder. (The one with PBPW.exe or PBPX.exe, usually C:\PBP or c:\PBP3)
Re: New project - MSFS C++ USB interface
So I have a need to override a pushbutton (from ON to OFF) and came up with this concept; a solenoid piggyback on a pushbutton switch with a 3D printed cap held by a strand of wire.
Demon Yesterday, 23:21...