アセンブラでPIC16F1847のLチカを行う備忘録

<h2>はじめに</h2>
<p>
本読んでLチカくらいは余裕でしょ、と意気込んだが記法とかだいぶ変わっていた（MPASMからpic-asというのに変わったらしい）ので備忘録です。テクノロジーは日々進化を続けており、５年前の内容では対応できないウンヌン…<br>
半年前くらいにやってたのでちょー曖昧。
</p>
<h2>回路図</h2>
<p>
余裕があれば追記します。
</p>
<h2>サンプルプログラム１</h2>
<h3>電源オンでLED点灯</h3>
<pre>
<code>
PROCESSOR 16F1847

; PIC16F1847 Configuration Bit Settings
; Assembly source line config statements
; CONFIG1
  CONFIG  FOSC = HS             ; Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
  CONFIG  WDTE = OFF            ; Watchdog Timer Enable (WDT disabled)
  CONFIG  PWRTE = OFF           ; Power-up Timer Enable (PWRT enabled)
  CONFIG  MCLRE = ON            ; MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
  CONFIG  CP = OFF              ; Flash Program Memory Code Protection (Program memory code protection is disabled)
  CONFIG  CPD = OFF             ; Data Memory Code Protection (Data memory code protection is disabled)
  CONFIG  BOREN = ON            ; Brown-out Reset Enable (Brown-out Reset disabled)
  CONFIG  CLKOUTEN = OFF        ; Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
  CONFIG  IESO = ON             ; Internal/External Switchover (Internal/External Switchover mode is enabled)
  CONFIG  FCMEN = ON            ; Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

; CONFIG2
  CONFIG  WRT = OFF             ; Flash Memory Self-Write Protection (Write protection off)
  CONFIG  PLLEN = ON            ; PLL Enable (4x PLL enabled)
  CONFIG  STVREN = ON           ; Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
  CONFIG  BORV = LO             ; Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
  CONFIG  LVP = ON              ; Low-Voltage Programming Enable (Low-voltage programming enabled)

// config statements should precede project file includes.
#include &lt;xc.inc&gt;

; ***** main ***********************
PSECT   LedOn,abs,local,class=CODE,delta=2
	
MAIN:
	BANKSEL(PORTB)		; 
	clrf	BANKMASK(PORTB)	; Initialize PORTB
	BANKSEL(ANSELB)		; 
	clrf	BANKMASK(ANSELB); Make RB<7:0> digital(0)
	BANKSEL(TRISB)		; 
	clrf	BANKMASK(TRISB)	; Set RB<7:0> as outputs(0)
	BANKSEL(PORTB)		; 
	bsf	RB0		; Set RB0 as High
WAIT:
	goto	WAIT
	
	end
</code>
</pre>

<h2>サンプルプログラム２</h2>
<h3>Lチカ1s間隔</h3>
<p>
※コンフィグビット等は上記と同様の設定につき省略
</p>
<pre>
<code>
; ***** variables ***********************
CNT1	equ 20h
CNT2	equ 21h
CNT3	equ 22h
CNT4	equ 23h

; ***** main ***********************
PSECT   LedOnOff,abs,local,class=CODE,delta=2
	
MAIN:
	BANKSEL(PORTB)		; 
	clrf	BANKMASK(PORTB)	; Initialize PORTB
	BANKSEL(ANSELB)		; 
	clrf	BANKMASK(ANSELB); Make RB<7:0> digital(0)
	BANKSEL(TRISB)		; 
	clrf	BANKMASK(TRISB)	; Set RB<7:0> as outputs(0)

REPEAT:
	call	TIMER3
	BANKSEL(PORTB)		; 
	bsf	RB0		; Set RB0 as High
	call	TIMER3
	BANKSEL(PORTB)		; 
	bcf	RB0		; Set RB0 as Low
	goto	REPEAT
	
TIMER1:
	movlw	62
	movwf	CNT1
LOOP1:
	nop
	decfsz	CNT1,f
	goto	LOOP1
	RETURN
	
TIMER2:
	movlw	100
	movwf	CNT2
LOOP2:
	nop
	call	TIMER1
	decfsz	CNT2,f
	goto	LOOP2
	RETURN
	
TIMER3:
	movlw	100
	movwf	CNT3
LOOP3:
	nop
	call	TIMER2
	decfsz	CNT3,f
	goto	LOOP3
	RETURN
	
	end
</code>
</pre>

<h2>サンプルプログラム３</h2>
<h3>SWオン時LED点灯</h3>
<p>
※コンフィグビット等は上記と同様の設定につき省略
</p>
<pre>
<code>
; ***** main ***********************
PSECT   LedSWInput,abs,local,class=CODE,delta=2
	
MAIN:
	BANKSEL(PORTA)		; 
	clrf	BANKMASK(PORTA)	; Initialize PORTA
	BANKSEL(ANSELA)		; 
	clrf	BANKMASK(ANSELA); Make RB<7:0> digital(0)
	movlw	0x0F		; Set 1 on W register
	BANKSEL(TRISA)		; 
	movwf	BANKMASK(TRISA)	; Set RB<3:0> as outputs(1)

	BANKSEL(PORTB)		; 
	clrf	BANKMASK(PORTB)	; Initialize PORTB
	BANKSEL(ANSELB)		; 
	clrf	BANKMASK(ANSELB); Make RB<7:0> digital(0)
	BANKSEL(TRISB)		; 
	clrf	BANKMASK(TRISB)	; Set RB<7:0> as outputs(0)

INPUT:
	
	clrw
	BANKSEL(PORTA)
	btfsc	PORTA, 0
	movlw	0x01
	movwf	BANKMASK(PORTB)

	goto	INPUT

	end
</code>
</pre>

<h2>参考文献</h2>
<p>
図解PICマイコン実習(第2版) ゼロからわかる電子制御<br>
<a href="https://www.morikita.co.jp/books/mid/078332">
https://www.morikita.co.jp/books/mid/078332
</a>
<br>
新しいアセンブラの公式ガイド<br>
<a href="https://microchipdeveloper.com/swtools:pic-asm">
https://microchipdeveloper.com/swtools:pic-asm
</a>
</p>