Skip to content

Instantly share code, notes, and snippets.

@xiupos
Created March 20, 2018 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiupos/1ea44fd9f3cc07e0533092fa847bb244 to your computer and use it in GitHub Desktop.
Save xiupos/1ea44fd9f3cc07e0533092fa847bb244 to your computer and use it in GitHub Desktop.
16F84A+Servo Motor
; サーボモータ
; ha2zakura
list p=16F84A ; 使用するPICの種類の定義
#include <p16F84A.inc> ; 設定ファイルの指定
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC
; _CP_OFF プログラムコードプロテクト無効
; _WDT_OFF ウォッチドドック・タイマ無効
; _PWRTE_ON パワーアップタイム有効
; _HS_OSC 外部クロック(4MHz-20MHz)
;***** 定数の定義 **
w_temp EQU 0x0C ; Wレジスタ保存用
status_temp EQU 0x0D ; STATUSレジスタ保存用
CNT1 EQU 0x0E ; カウント用
TIM1 EQU 0x0F ; DELAY用
TIM2 EQU 0x10 ; DELAY用
delay_ms EQU 0x11 ; DELAY用
;**********************************************************************
ORG 0x000 ; リセット
goto MAIN ; MAINへ移動
;**********************************************************************
ORG 0x004 ; 割り込み処理
goto INTR
;**********************************************************************
; メイン
MAIN bsf STATUS, RP0 ; バンク1へ
movlw B'00000010'
movwf TRISA ; 0 : 出力, 1 : 入力
clrf TRISB ; bit0 -> RA0(RB0)
movlw B'01010111' ; プリスケーラの設定
movwf OPTION_REG
bcf STATUS, RP0 ; バンク0へ
clrf PORTA
clrf PORTB
movlw D'156' ; カウント値の設定
movwf TMR0
bsf INTCON, T0IE ; TMR0割り込みを許可
bsf INTCON, GIE ; 割り込みを許可
LOOP btfsc PORTA, 1
goto S_OFF
S_ON movlw D'100' ; RA1がHIGHの時1ms
movwf delay_ms
goto LOOP
S_OFF movlw D'200' ; RA1がLOWの時1ms
movwf delay_ms
goto LOOP
DELAY movf delay_ms, w
movwf TIM2
DLP10ns movlw D'2' ; 1
movwf TIM1 ; 2
DLP3ns decfsz TIM1, f ; 3,6,7
goto DLP3ns ; 4,5
decfsz TIM2, f ; 8
goto DLP10ns ; 9,10
return
;**********************************************************************
; 割り込み処理
INTR movwf w_temp ; Wレジスタを保存しておく
movf STATUS, w ; STATUSレジスタも保存しておく
movwf status_temp
bcf INTCON, T0IF ; TMR0オーバーフローフラグのクリア
bsf INTCON, T0IE ; TMR0オーバーフローフラグのクリア
movlw D'156' ; カウント値の設定
movwf TMR0
bsf PORTB, 4
call DELAY ; 一定時間だけHIGHに
bcf PORTB, 4
ENDINTR movf status_temp, w ; STATUSを復帰
movwf STATUS
swapf w_temp, f
swapf w_temp, w ; Wレジスタを復帰
retfie ; 割り込みから復帰
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment