;****************************************************************************************** ; PIC16F1939 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H23.08.10 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; (1)Description ; OSC : Internal 16MHz ;****************************************************************************************** LIST P=PIC16F1939,ST=OFF,R=DEC INCLUDE "P16F1939.inc" __CONFIG _CONFIG1 , _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF __CONFIG _CONFIG2 , _WRT_OFF & _VCAPEN_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_25 & _LVP_OFF ; __CONFIG _CONFIG2 , _WRT_OFF & _VCAPEN_OFF & _PLLEN_ON & _STVREN_OFF & _BORV_25 & _LVP_OFF ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) ; TMP_G EQU 20H WAT1MLP EQU 21H ; ; Define Common RAM AREA (16 bytes) ; TMP_C EQU 70H INTWRG EQU 71H INTSTS EQU 72H LED_LST EQU 73H INT_CNT EQU 74H ; ; Define Macro ; SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ENDM MWAIT MACRO #WAITN MOVLW #WAITN CALL WAIT1M ENDM ; ; Program Start ; ORG 0h GOTO START ; ***************************** ; 0.Interrupts ; ***************************** ORG 4h MOVWF INTWRG ; W-REGの値を保存 SWAPF STATUS,W ; STATUSレジスタの値を保存 MOVWF INTSTS ; STATUSレジスタの値を保存 ; CALL INTR ; SWAPF INTSTS,W ; STATUSレジスタの値を復元 MOVWF STATUS ; STATUSレジスタの値を復元 MOVFW INTWRG ; W-REGレジスタの値を復元 RETFIE START ; ***************************** ; 1.Initialized Segment ; ***************************** ; (1)Define OSC (16MHz, IntOSC) SELBANK 1 ; SET BANK1 MOVLW B'11111010' MOVWF OSCCON ; (2)Define I/O port SELBANK 1 ; SET BANK1 MOVLW B'11111111' MOVWF TRISA MOVLW B'00000000' MOVWF TRISB MOVLW B'11111111' MOVWF TRISC MOVLW B'11111111' MOVWF TRISD MOVLW B'11111111' MOVWF TRISE ; (3)Initialized Timer1 CLRF INT_CNT CALL INTTIM SELBANK 0 ; SET BANK0 ; ***************************** ; 2.Program main ; ***************************** MOVLW H'01' MOVWF LED_LST MAIN GOTO MAIN ;****************************************************************************************** ; Program : MOVLED ; Title : ; Input : ; Output : ; Description : ;****************************************************************************************** MOVLED MOVFW LED_LST XORLW H'80' BTFSS STATUS,Z GOTO MOVLED1 MOVLW H'01' MOVWF LED_LST RETURN MOVLED1 RLF LED_LST,F RETURN ;****************************************************************************************** ; Program : INTTIM ; Title : Initialized Timer1 (100mS) ; Input : ; Output : ; Description : Fosc:16MHz ;****************************************************************************************** INTTIM ; ******************************************************* ; 1.Define T1CON(TIMER1 CONTROL REGISTER) Register ; T1CON(7-6) TMR1CS<1:0>: Timer1 Clock Source Select bits ; 11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC) ; 10 =Timer1 clock source is pin or oscillator: ; If T1OSCEN = 0: External clock from T1CKI pin (on the rising edge) ; If T1OSCEN = 1: Crystal oscillator on T1OSI/T1OSO pins ; 01 =Timer1 clock source is system clock (FOSC) ; 00 =Timer1 clock source is instruction clock (FOSC/4) ; T1CON(5-4) T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits ; 11 = 1:8 Prescale value ; 10 = 1:4 Prescale value ; 01 = 1:2 Prescale value ; 00 = 1:1 Prescale value ; T1CON(3) T1OSCEN: LP Oscillator Enable Control bit ; 1 = Dedicated Timer1 oscillator circuit enabled ; 0 = Dedicated Timer1 oscillator circuit disabled ; T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Control bit ; TMR1CS<1:0> = 1X ; 1 = Do not synchronize external clock input ; 0 = Synchronize external clock input with system clock (FOSC) ; TMR1CS<1:0> = 0X ; This bit is ignored. Timer1 uses the internal clock when TMR1CS<1:0> = 1X. ; T1CON(1) Unimplemented: Read as ‘0’ ; T1CON(0) TMR1ON: Timer1 On bit ; 1 = Enables Timer1 ; 0 = Stops Timer1 ; Clears Timer1 Gate flip-flop ; ******************************************************* SELBANK 0 ; SET BANK0 ; MOVLW B'00000101' MOVLW B'00110101' MOVWF T1CON ; ******************************************************* ; 2.Define T1GCON(TIMER1 GATE CONTROL REGISTER) Register ; T1GCON(7) TMR1GE: Timer1 Gate Enable bit ; If TMR1ON = 0: This bit is ignored ; If TMR1ON = 1: ; 1 = Timer1 counting is controlled by the Timer1 gate function ; 0 = Timer1 counts regardless of Timer1 gate function ; T1GCON(6) T1GPOL: Timer1 Gate Polarity bit ; 1 = Timer1 gate is active-high (Timer1 counts when gate is high) ; 0 = Timer1 gate is active-low (Timer1 counts when gate is low) ; T1GCON(5) T1GTM: Timer1 Gate Toggle Mode bit ; 1 = Timer1 Gate Toggle mode is enabled ; 0 = Timer1 Gate Toggle mode is disabled and toggle flip-flop is cleared ; Timer1 gate flip-flop toggles on every rising edge. ; T1GCON(4) T1GSPM: Timer1 Gate Single-Pulse Mode bit ; 1 = Timer1 gate Single-Pulse mode is enabled and is controlling Timer1 gate ; 0 = Timer1 gate Single-Pulse mode is disabled ; T1GCON(3) T1GGO/DONE: Timer1 Gate Single-Pulse Acquisition Status bit ; 1 = Timer1 gate single-pulse acquisition is ready, waiting for an edge ; 0 = Timer1 gate single-pulse acquisition has completed or has not been started ; This bit is automatically cleared when T1GSPM is cleared. ; T1GCON(2) T1GVAL: Timer1 Gate Current State bit ; Indicates the current state of the Timer1 gate that could be provided to TMR1H:TMR1L. ; Unaffected by Timer1 Gate Enable (TMR1GE). ; T1GCON(1-0) T1GSS<1:0>: Timer1 Gate Source Select bits ; 00 = Timer1 Gate pin ; 01 = Timer0 overflow output ; 10 = Comparator 1 optionally synchronized output (SYNCC1OUT) ; 11 = Comparator 2 optionally synchronized output (SYNCC2OUT) ; ******************************************************* SELBANK 0 ; SET BANK0 MOVLW B'00000000' MOVWF T1GCON ; ******************************************************* ; 3.Define TMR1 Register ; ******************************************************* ; TMR1 data ( OSC:16MHz, 1:1 Prescale value, 0.001[S];1000Hz ) ; 10mS(100Hz): 16MHz / 4(FOSC/4) / 100(100Hz)==> 0d40,000(Count) ==> 0x9C40(Count) ; TMR1 = 0xFFFF - 0x9C40 ==> 0x63BF ; 100mS(10Hz): 16MHz / 4(FOSC/4) / 8(1:8 Prescale value) / 10(10Hz)==> 0d50,000(Count) ==> 0xC350(Count) ; TMR1 = 0xFFFF - 0xC350 ==> 0x3CAF SELBANK 0 ; SET BANK0 MOVLW H'3C' MOVWF TMR1H MOVLW H'AF' MOVWF TMR1L ; ******************************************************* ; 4.Interrupts ; ******************************************************* SELBANK 0 ; SET BANK0 BCF PIR1,TMR1IF ; Timer1 Overflow Interrupt Flag bit ※Timer1 BSF INTCON,GIE ; Grobal Interrupt Ebable (->1) ※Total BSF INTCON,PEIE ; Peripheral Interrupt Ebable (->1) ※Timer etc. SELBANK 1 ; SET BANK1 BSF PIE1,TMR1IE ; Timer1 Overflow Interrupt Enable bit(->1) ※Timer1 SELBANK 0 ; SET BANK0 RETURN ;****************************************************************************************** ; Program : INTR ; Title : Interrupts ; Input : ; Output : ; Description : ;****************************************************************************************** INTR CALL INTTIM MOVFW INT_CNT XORLW H'09' ; 100[mS] * 10 BTFSC STATUS,Z GOTO INTR1 MOVLW H'01' ADDWF INT_CNT,F RETURN INTR1 CLRF INT_CNT CALL MOVLED MOVFW LED_LST MOVWF PORTB RETURN END