0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-12-31

はじめに

本読んでLチカくらいは余裕でしょ、と意気込んだが記法とかだいぶ変わっていた(MPASMからpic-asというのに変わったらしい)ので備忘録です。テクノロジーは日々進化を続けており、5年前の内容では対応できないウンヌン…
半年前くらいにやってたのでちょー曖昧。

回路図

余裕があれば追記します。

サンプルプログラム1

電源オンでLED点灯



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 <xc.inc>
; ***** 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


サンプルプログラム2

Lチカ1s間隔

※コンフィグビット等は上記と同様の設定につき省略



; ***** 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


サンプルプログラム3

SWオン時LED点灯

※コンフィグビット等は上記と同様の設定につき省略



; ***** 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


参考文献

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?