PIC16F84電子遊戯(18)
最終更新日:2001年4月2日
ミラクルライト(1ページ)へ (2ページ)へ (3ページ)へ (4ページ)

ミラクルライトの全ソースリスト公開
ミラクルライトの全ソースリストです。
皆さんも各自のオリジナルになるよう改造してみて下さい。
;**********************************************************************
;    Filename:      miracle.asm                                       *
;    Date:          2001/3/26                                        *
;    File Version:                                                    *
;**********************************************************************

        list      p=16F84             ; list directive to define processor
        #include <p16F84.inc>         ; processor specific variable definitions

        __CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

;***** VARIABLE DEFINITIONS
w_temp        EQU     0x0C        ; variable used for context saving 
status_temp   EQU     0x0D        ; variable used for context saving

led_data                EQU     0x11            ;LED set data
                                        ;led_data,0  RED LED
                                        ;led_data,1  GREEN LED
                                        ;led_data,2  BLUE LED
led_mode                EQU     0x12            ;LED mode ram
                                        ;led_mode,0  manual/auto select
acount1         EQU     0x13            ;LED auto count ram1
acount2         EQU     0x14            ;LED auto count ram2 

wait_data               EQU     0x15            ;wait data ram

key_count               EQU     0x16            ;key count ram
key_sub         EQU     0x17            ;key count sub ram

auto_sub                EQU     0x18            ;auto sub ram

red_count               EQU     0x19            ;RED LED count ram
green_count     EQU     0x1a            ;GREEN LED count ram
blue_count      EQU     0x1b            ;BLUE LED count ram

red_on          EQU     0x20            ;RED LED ON data ram
red_off         EQU     0x21            ;RED LED OFF data ram
green_on                EQU     0x22            ;GREEN LED ON data ram
green_off               EQU     0x23            ;GREEN LED OFF data ram
blue_on         EQU     0x24            ;BLUE LED ON data ram
blue_off                EQU     0x25            ;BLUE LED OFF data ram

tm0_setdata     EQU     0f3h            ;FFH-F3H=12 12*16=192usec
time0           EQU     00h             ;LED time0
time1           EQU     70h             ;LED time1
time2           EQU     20h             ;LED time2
time3           EQU     50h             ;LED time3

;**********************************************************************
                ORG     0x000             ; processor reset vector
                goto    main              ; go to beginning of program


                ORG     0x004             ; interrupt vector location
                movwf   w_temp            ; save off current W register contents
                movf    STATUS,w          ; move status register into W register
                movwf   status_temp       ; save off contents of STATUS register

                ;---------------------------------      
                bcf     INTCON,T0IF
                movlw   tm0_setdata
                movwf   TMR0

                btfss   led_mode,0
                goto    red_leds
                ;---------------------------------
                movlw   01h             ;LED mode auto shori
                addwf   acount1,1
                btfsc   STATUS,0
                incf    acount2
                btfsc   auto_sub,0
                goto    auto4
                btfsc   auto_sub,1
                goto    auto5
                btfsc   auto_sub,2
                goto    auto6
                goto    auto7
auto4           btfss   acount2,3
                goto    red_leds
                goto    autoe
auto5           btfss   acount2,4
                goto    red_leds
                goto    autoe
auto6           btfss   acount2,5
                goto    red_leds
                goto    autoe
auto7           btfss   acount2,6
                goto    red_leds
autoe           call    led_change      
                call    led_set
                movlw   00h             ;LED auto count clear
                movwf   acount1
                movwf   acount2
                ;----------------------- RED
red_leds                decfsz  red_count,1
                goto    green_leds
                btfsc   led_data,0      ;RED OFF?
                goto    red_leds_2
red_leds_3      bsf     led_data,0      ;RED ON
                movf    red_on,0
                movwf   red_count
                movf    red_count,0
                btfsc   STATUS,2
                goto    red_leds_2
                goto    green_leds
red_leds_2      bcf     led_data,0      ;RED OFF
                movf    red_off,0
                movwf   red_count
                movf    red_count,0
                btfsc   STATUS,2
                goto    red_leds_3              

                ;----------------------- GREEN
green_leds      decfsz  green_count,1
                goto    blue_leds
                btfsc   led_data,1      ;GREEN OFF?
                goto    green_leds_2
green_leds_3    bsf     led_data,1      ;GREEN ON
                movf    green_on,0
                movwf   green_count
                movf    green_count,0
                btfsc   STATUS,2
                goto    green_leds_2
                goto    blue_leds
green_leds_2    bcf     led_data,1      ;GREEN OFF
                movf    green_off,0
                movwf   green_count
                movf    green_count,0
                btfsc   STATUS,2
                goto    green_leds_3

                ;---------------------- BLUE
blue_leds               decfsz  blue_count,1
                goto    next_leds
                btfsc   led_data,2      ;BLUE OFF?
                goto    blue_leds_2
blue_leds_3     bsf     led_data,2      ;BLUE ON
                movf    blue_on,0
                movwf   blue_count
                movf    blue_count,0
                btfsc   STATUS,2
                goto    blue_leds_2
                goto    next_leds
blue_leds_2     bcf     led_data,2      ;BLUE OFF
                movf    blue_off,0
                movwf   blue_count
                movf    blue_count,0
                btfsc   STATUS,2
                goto    blue_leds_3
                ;----------------------------------

next_leds
                ;----------------------------------

                movf    status_temp,w     ; retrieve copy of STATUS register
                movwf   STATUS            ; restore pre-isr STATUS register contents
                swapf   w_temp,f
                swapf   w_temp,w          ; restore pre-isr W register contents
                retfie                    ; return from interrupt



main
                call    port_set
                movlw   0ffh
                movwf   PORTA
                movlw   00h
                movwf   PORTB

                call    timer0_set

                movlw   00h
                movwf   key_count
                movwf   key_sub
                movwf   led_mode        ;LED mode=manual
                call    auto_clear

                movlw   04h
                movwf   auto_sub        ;auto time = 3sec

                movlw   00h
                movwf   led_data        ;RED OFF,GREEN OFF,BLUE OFF
                call    set_lo_red
                call    set_lo_green
                call    set_lo_blue
                movf    red_on,0
                movwf   red_count       ;RED count data set
                movf    green_on,0
                movwf   green_count     ;GREEN count data set
                movf    blue_on,0
                movwf   blue_count      ;BLUE count data set

main_loop
;-------------------------------------- key scan
sw1_key         btfsc   PORTA,3
                goto    sw2_key
                call    wait_1
                btfsc   PORTA,3
                goto    sw2_key
                                        ;sw1 on
                btfsc   led_mode,0
                call    auto_time
                bcf     INTCON,GIE
                call    led_change      
                call    led_set
                bsf     INTCON,GIE
                
sw1_end         movf    led_data,0
                movwf   PORTB
                btfss   PORTA,3
                goto    sw1_end
;-------------------------------------
sw2_key         btfsc   PORTA,4
                goto    next_key
                call    wait_1
                btfsc   PORTA,4
                goto    next_key
                                        ;sw2 on
                btfsc   led_mode,0
                goto    sw2_key2
                bsf     led_mode,0      ;LED mode=auto
                call    auto_clear
                goto    sw2_key3
sw2_key2                bcf     led_mode,0      ;LED mode=manual
sw2_key3                btfss   PORTA,4
                goto    sw2_key3
;--------------------------------------
next_key

;-------------------------------------- led tentou              
                movf    led_data,0
                movwf   PORTB

                goto    main_loop
;-------------------------------------------------------------------------------
wait_1          
                movlw   0ffh
                movwf   wait_data
wait_2
                decfsz  wait_data,1
                goto    wait_2
                return
;-------------------------------------------------------------------------------
auto_clear      movlw   00h
                movwf   acount1         ;LED auto count clear
                movwf   acount2
                return
;-------------------------------------------------------------------------------
auto_time       btfss   auto_sub,3
                goto    auto_time2
                movlw   01h
                movwf   auto_sub
                return
auto_time2      rlf     auto_sub,1
                bcf     auto_sub,0
                return
;-------------------------------------------------------------------------------
led_change      movf    key_count,0
                btfsc   STATUS,2
                goto    led_change1
                btfss   key_count,7
                goto    led_change2
                incf    key_sub,1
led_change1     movlw   01h
                movwf   key_count
                return
led_change2     rlf     key_count,1
                bcf     key_count,0     
                return
;-------------------------------------------------------------------------------
led_set         btfsc   key_count,0
                goto    miracle0
                btfsc   key_count,1
                goto    miracle1
                btfsc   key_count,2
                goto    miracle2
                btfsc   key_count,3
                goto    miracle3
                btfsc   key_count,4
                goto    miracle4
                btfsc   key_count,5
                goto    miracle5
                btfsc   key_count,6
                goto    miracle6
                btfsc   key_count,7
                goto    miracle7
                return
miracle0                btfsc   key_sub,0
                goto    miracle10
                call    set_hi_red
                call    set_lo_green
                call    set_lo_blue
                return
miracle1                btfsc   key_sub,0
                goto    miracle11
                call    set_lo_red
                call    set_hi_green
                call    set_lo_blue
                return
miracle2                btfsc   key_sub,0
                goto    miracle12
                call    set_lo_red
                call    set_lo_green
                call    set_hi_blue
                return
miracle3        btfsc   key_sub,0
                goto    miracle13
                call    set_hi_red
                call    set_hi_green
                call    set_lo_blue
                return
miracle4                btfsc   key_sub,0
                goto    miracle14
                call    set_lo_red
                call    set_hi_green
                call    set_hi_blue
                return
miracle5                btfsc   key_sub,0
                goto    miracle15
                call    set_hi_red
                call    set_lo_green
                call    set_hi_blue
                return
miracle6                btfsc   key_sub,0
                goto    miracle16
                call    set_hi_red
                call    set_hi_green
                call    set_hi_blue
                return
miracle7                btfsc   key_sub,0
                goto    miracle17
                call    set_hi_red
                call    set_mid_green
                call    set_lo_blue
                return

miracle10               call    set_hi_red
                call    set_lo_green
                call    set_mid_blue
                return
miracle11               call    set_mid_red
                call    set_hi_green
                call    set_lo_blue
                return
miracle12               call    set_lo_red
                call    set_hi_green
                call    set_mid_blue
                return
miracle13               call    set_mid_red
                call    set_lo_green
                call    set_hi_blue
                return
miracle14               call    set_lo_red
                call    set_mid_green
                call    set_hi_blue
                return
miracle15               call    set_mid_red
                call    set_mid_green
                call    set_lo_blue
                return
miracle16               call    set_lo_red
                call    set_mid_green
                call    set_mid_blue
                return
miracle17               call    set_mid_red
                call    set_lo_green
                call    set_mid_blue
                return

;-------------------------------------------------------------------------------
set_hi_red      movlw   time1
                movwf   red_on
                movlw   time0
                movwf   red_off
                return

set_mid_red     movlw   time2
                movwf   red_on
                movlw   time3
                movwf   red_off
                return

set_lo_red      movlw   time0
                movwf   red_on
                movlw   time1
                movwf   red_off
                return
;-------------------------------------------------------------------------------
set_hi_green    movlw   time1
                movwf   green_on
                movlw   time0
                movwf   green_off
                return

set_mid_green   movlw   time2
                movwf   green_on
                movlw   time3
                movwf   green_off
                return

set_lo_green    movlw   time0
                movwf   green_on
                movlw   time1
                movwf   green_off
                return
;-------------------------------------------------------------------------------
set_hi_blue     movlw   time1
                movwf   blue_on
                movlw   time0
                movwf   blue_off
                return

set_mid_blue    movlw   time2
                movwf   blue_on
                movlw   time3
                movwf   blue_off
                return

set_lo_blue     movlw   time0
                movwf   blue_on
                movlw   time1
                movwf   blue_off
                return
;-------------------------------------------------------------------------------
port_set                                ;RA0,RA1,RA2(OUT)
                                        ;RA3,RA4(IN)
                                        ;RB0,RB1,RB2,RB3,RB4,RB5,RB6,RB7(OUT)
                bsf     STATUS,RP0
                clrf    TRISA
                bsf     TRISA,3
                nop
                bsf     TRISA,4 
                clrf    TRISB
                bcf     STATUS,RP0
                return

timer0_set

                bsf     STATUS,RP0
                movlw   083h            ;(1)PORTB NOT PULLUP
                                        ;(0)INTEDG
                                        ;(0)TOCS
                                        ;(0)TOSE
                                        ;(0)PSA
                                        ;(011)1/16
                movwf   OPTION_REG
                bcf     STATUS,RP0
                
                movlw   tm0_setdata
                movwf   TMR0
                bsf     INTCON,T0IE
                bsf     INTCON,GIE
                return


                END                     ; directive 'end of program'
ミラクルライトの全HEXコード
HEXコードです。メモ帳などのテキストエディタにコピー&ペーストしてファイルすればPICに書き込めるデータに出来ます。
:020000005F2877
:080008008C0003088D000B11B0
:10001000F3308100121C2728013093070318940A3B
:100020001818172898181A2818191D282028941D00
:1000300027282228141E27282228941E2728222811
:10004000141F2728A720B320003093009400990B99
:10005000382811183228111420089900190803199A
:1000600032283828111021089900190803192B2863
:100070009A0B492891184328911422089A001A08CB
:10008000031943284928911023089A001A080319D4
:100090003C289B0B5A2811195428111524089B0041
:1000A0001B08031954285A28111125089B001B0806
:1000B00003194D280D0883008C0E0C0E09004121F8
:1000C000FF308500003086004921003096009700FF
:1000D00092009B2004309800003091001E212D21B9
:1000E0003C212008990022089A0024089B008519C9
:1000F000862896208519862812189F208B13A72002
:10010000B3208B1711088600851D8228051A9328B5
:100110009620051A93281218902812149B209128D3
:100120001210051E9128110886007728FF309500CF
:10013000950B982808000030930094000800981D43
:10014000A428013098000800980D1810080016081F
:100150000319AD28961FB028970A013096000800B1
:10016000960D161008001618C4289618CA281619D5
:10017000D0289619D628161ADC28961AE228161BBB
:10018000E828961BEE2808001718F42814212D21C2
:100190003C2108001718F8281E2123213C210800C3
:1001A0001718FC281E212D213221080017180029BC
:1001B000142123213C210800171804291E21232182
:1001C000322108001718082914212D213221080096
:1001D00017180C291421232132210800171810297F
:1001E000142128213C21080014212D213721080049
:1001F000192123213C2108001E2123213721080039
:1002000019212D21322108001E2128213221080028
:10021000192128213C2108001E212821372108000E
:1002200019212D21372108007030A0000030A100D5
:1002300008002030A0005030A10008000030A000CD
:100240007030A10008007030A2000030A300080048
:100250002030A2005030A30008000030A20070300F
:10026000A30008007030A4000030A5000800203072
:10027000A4005030A50008000030A4007030A50094
:100280000800831685018515000005168601831276
:1002900008008316833081008312F33081008B16AF
:0402A0008B170800B0
:02400E00F13F80
:00000001FF
(miracle.hex)
ミラクルライトの各処理概要
ソースリストの赤い部分が割り込み処理、青い部分がメイン処理です。
後半の黒い部分が各種サブルーチンです。
割り込み処理
STATUSレジスタ退避
オート時間カウント処理
赤LED処理
緑LED処理
青LED処理
STATUSレジスタ復帰
メイン処理
初期設定
SW1処理
SW2処理
LED処理
ミラクルライトの完成
完成の16色をご覧下さい。よく似た色もあります。
赤色 緑色 青色 黄色 水色 紫色
白色 橙色 桃色 黄緑色 水緑色 青紫色
   
  薄青色 深黄色 深水色 深紫色  
いかがですか。こうやって16色並べてみると圧巻ですね。
これらの色にこだわらず、皆さんでいろんな色が出るように試みてください。
私の場合は色がチェンジするようにしましたが、色が徐々に変化していくようにも出来るはずです。改造してみるのも楽しいのではないでしょうか。
これからも益々面白くなる「PIC16F84電子遊戯」をよろしくお願いいたします。

不明点などあればメールお願いいたします。nonchansoft@nifty.com