Code:
'* Notes : derived from Darrel Taylor & Mister E's work
'* : 18F1320a @ 8 mHz, PWM Freq = 95 Hz
clear
DEFINE OSC 8
OSCCON = %01110001 ' INTOSC primary, 8MHz
INCLUDE "AllDigital.pbp"
include "EE_Vars.PBP" ' manipulation of EEPROM
BAM_COUNT CON 8 ; BAM Pins are used?
INCLUDE "MIBAM.pbp" ;
;----[ MIBAM Setup ]-------------
BAM_DUTY VAR BYTE[BAM_COUNT]
LED1 VAR BAM_DUTY[0] ; array for easy access
LED2 VAR BAM_DUTY[1] ; with FOR loops etc.
LED3 VAR BAM_DUTY[2]
LED4 VAR BAM_DUTY[3]
LED5 VAR BAM_DUTY[4]
LED6 VAR BAM_DUTY[5]
LED7 VAR BAM_DUTY[6]
LED8 VAR BAM_DUTY[7]
ASM
BAM_LIST macro ; Define PIN's for BAM
BAM_PIN (PORTB,5, LED1) ; and Duty variables
BAM_PIN (PORTB,0, LED2)
BAM_PIN (PORTA,3, LED3)
BAM_PIN (PORTA,2, LED4)
BAM_PIN (PORTA,1, LED5)
BAM_PIN (PORTA,0, LED6)
BAM_PIN (PORTA,7, LED7)
BAM_PIN (PORTB,2, LED8)
endm
BAM_INIT BAM_LIST ; Init Pins
ENDASM
DEFINE HSER_RCSTA 90h ' En serial port & cont rx
DEFINE HSER_TXSTA 24h ' En transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 160 ' 4800 Baud @ 8MHz, -0.08%
SPBRGH = 1
BAUDCTL.3 = 1 '
address var byte
command var byte
charcnt var byte '
Cnt var byte ' counter for ramping the display brighter
Counter var byte
CyLoop var word ' MAY be able to combine this later
Dly var byte ' used on delay loop sub
holdoff var word
i var byte
IDloop var byte ' loop cntr in ID Loop
Idx VAR BYTE
loop var byte
LoopCount VAR WORD: LoopCount = 0 ' from Random Sub
ModeCnt var byte: @ EE_var _ModeCnt, BYTE, 8 ' count flashes of 1&8 LEDs when Switch
NextLED VAR BYTE: NextLED = 1
PowerOn VAR WORD: @ EE_var _PowerOn, WORD, 0 '
RandVar var word: RandVar = 12345
RandVar8 var byte '
SerialString var byte[40] '
Speed VAR BYTE: @ EE_var _Speed, BYTE, 50 ' delay length /control speed
state var byte ' condition bits
state2 var byte ' condition bits
time var WORD ' delay loop sub
temp var byte
value var byte
Sync VAR state.0 ' Sync byte rcvd
ForMe VAR state.1 ' Packet is for this device
CmdRcvd VAR state.2 ' flag for Command rcvd
CntState var state2.0 ' flag for counting pulse
ERROR VAR state.3 ' Sync rcvd out of order
header var state2.2
tlate VAR state.4 ' command rcvd before last one done
ValRcvd var state.5 ' flag: Value data rcvd
Success var state.6
idle var state.7 ' flag: idle polarity
TraceDIR VAR state2.1 ' cyclon
CREN var RCSTA.4 ' RX enable bit
OERR var RCSTA.1 ' Overrun error
RCIF VAR PIR1.5 ' Receive int flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit int flag (1=empty, 0=full
Brightness CON 100 ; Tracers DutyCycle
ID CON 123 ' ID. unique to each, 255=all
SyncByte CON 85 ' "U" for SYNC. constant on all
TracerSpeed CON 15 ; Smaller = Faster L-R
DrainSpeed CON 30 ; Smaller = Shorter Trail
'*************************
Main:
' once comm is figured out, Command will be used for selecting ModeCnt or speed
' and value will be the rate that it flashes the LEDs
'ManMod:
' branch ModeCnt,[fadem,smoothm,rampm,twinklem,cyclonm,dimm,RanMainm,IDm,autom]'
'.....
'autom:
gosub FadeUp
gosub SmoothUp ' as soon as I enable more than one-
' gosub Fillup ' comm starts to get erratic
' gosub StrobeUp
' gosub cyclon
' gosub drvdim
' gosub RanSub
' gosub shoid
goto Main:
'**********Sub for polled comm *********
RX:
IF OERR then
hserout ["OERR", 13,10]
CREN=0
CREN=1
endif
getcomm:
i=RCREG ' take it
if header then
if i ="#" then Discard ' is it the end? "#" = yes
Serialstring[Counter]=i ' Store into the array
Counter=Counter+1
endif
if RCIF then getcomm 'try again of still data
if i ="!" then header=1 ' header character? "!" = yes
Discard:
while RCIF
temp=RCREG
wend
if i="#" then ' End of String character
Success=1 ' Yeah i got a valid string
header=0
endif
if Success then
CREN=0 ' Disable Receiver
address = Serialstring[0] 'do deciphering here
command = Serialstring[1]
value = Serialstring[2]
hserout ["Str rx : ",str Serialstring\Counter," Ch : ", dec Counter,13,10]
CREN=1 ' Enable receiver
Counter = 0
Success = 0
endif
return
'***** Timing Sub: *****
DlyTime3: ' longest delay loop
for time = 0 to speed
next time
DlyTime2: ' mid delay loop
for time = 0 to speed
PAUSE 2
next time
DlyTime1: ' smallest delay loop
for Dly = speed to 0 step -1
if RCIF then ' check here for a comm flag r
gosub RX ' and get the character
goto short ' reduce delay, avoid blinking
endif
PAUSEUS 180 ' times need to be refined
Short:
'pauseus 5 ' times need to be refined
next Dly
return
'**********************
' Subs for patterns
'
'***** Fade Sub: MODE 0 ******
FadeUp:
hserout ["fadeUp",13,10]
for cnt = 0 to Brightness ' fade up
gosub DlyTime1
LED1 = Cnt: LED2 = Cnt: LED3 = Cnt: LED4 = Cnt
LED5 = Cnt: LED6 = Cnt: LED7 = Cnt: LED8 = Cnt '
next Cnt '
hserout ["fadeDown",13,10]
FadeDown:
for Cnt = Brightness to 0 step -1 ' fade down
gosub DlyTime1
LED1 = Cnt: LED2 = Cnt: LED3 = Cnt: LED4 = Cnt
LED5 = Cnt: LED6 = Cnt: LED7 = Cnt: LED8 = Cnt '
next Cnt '
return 'stay in fade loop
'**** Smooth Sub: MODE 1 ******
SmoothUp:
hserout ["SmoothUp",13,10]
for loop = 0 to 7
BAM_DUTY(loop) = Brightness ' smooth up
gosub DlyTime2
BAM_DUTY(loop) = 0
next loop
SmoothDown:
hserout ["SmoothDown",13,10]
for loop = 6 to 0 step -1
BAM_DUTY(loop) = Brightness ' smooth up
gosub DlyTime2
BAM_DUTY(loop) = 0
next loop
RETURN
'***** Ramp Sub: MODE 2 ******
' .......
'
end
Thanks
Bookmarks