;****************************************************************************************** ; PIC18F14K50 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H21.12.20 ; History : ; Laungage : MPASM ;****************************************************************************************** ; (1)Description ; ;****************************************************************************************** LIST P=PIC18F14K50,ST=OFF,R=DEC INCLUDE "P18F14K50.inc" config CPUDIV = NOCLKDIV ;CPU System Clock Selection bit(No CPU System Clock divide) config USBDIV = OFF ;USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) config FOSC = HS ;Oscillator Selection bits(HS oscillator) config PLLEN = ON ;X PLL Enable bit(Oscillator multiplied by 4) config PCLKEN = OFF ;Primary Clock Enable Bit(Primary clock is under software control) config FCMEN = OFF config IESO = OFF ;Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) config PWRTEN = OFF,BOREN = OFF,BORV = 19 config WDTEN = OFF,WDTPS = 1 config MCLRE = OFF ;MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF LP0 EQU 00H LP1 EQU 01H LP2 EQU 02H LP3 EQU 03H LEDLST EQU 04H G_LP EQU 05H INTWRG EQU 06H INTSTS EQU 07H ORG 0h GOTO START ORG 8h GOTO INTFNC ORG 18h GOTO INTFNC INTFNC ; ***************************** ; 0.Interrupts ; ***************************** MOVWF INTWRG ; W-REGの値を保存 SWAPF STATUS,W ; STATUSレジスタの値を保存 MOVWF INTSTS ; STATUSレジスタの値を保存 ; CALL INTRUP ; SWAPF INTSTS,W ; STATUSレジスタの値を復元 MOVWF STATUS ; STATUSレジスタの値を復元 MOVF INTWRG,W ; W-REGレジスタの値を復元 RETFIE START ; ***************************** ; 1.Initialized Segment ; ***************************** ; (1)Define I/O Port MOVLW B'00000000' MOVWF TRISC MOVLW B'00000000' MOVWF PORTC ; (2)Initialize MOVLW B'00000001' MOVWF LEDLST CALL MOVLED CLRF G_LP ; (3)Initialized Timer CALL INITIM ; ***************************** ; 2.Program main (No action) ; ***************************** MAIN GOTO MAIN ;****************************************************************************************** ; Function Name : MOVLED ; Title : Move LED ; Input : ; Output : ;****************************************************************************************** MOVLED MOVF LEDLST,W MOVWF PORTC RLCF LEDLST ; MOVF LEDLST,W XORLW H'00' BTFSS STATUS,Z RETURN MOVLW B'00000000' MOVWF LEDLST RLCF LEDLST RETURN ;****************************************************************************************** ; Function Name : INITIM ; Title : Initialize Timer ; Input : ; Output : ;****************************************************************************************** INITIM: ; ******************************************************* ; 1.Define Interrupts ; Timer1 interrupt, if enabled, is generated on overflow, which is latched in the TMR1IF ; interrupt flag bit of the PIR1 register. This interrupt can be enabled or disabled ; by setting or clearing the TMR1IE Interrupt Enable bit of the PIE1 register. ; ******************************************************* ; (1)Define INTCON(INTERRUPT CONTROL)Register ; INTCON(7) GIE/GIEH(Global Interrupt Enable bit) ; INTCON(6) PEIE/GIEL(Peripheral Interrupt Enable bit) ; 1 = Enables, 0 = Disables BSF INTCON,GIE BSF INTCON,PEIE ; (2)Define PIR1(PERIPHERAL INTERRUPT REQUEST)Register ; PIR1(0) TMR1IF(TMR1 Overflow Interrupt Flag bit) ; 1 = Enables, 0 = Disables BCF PIR1,TMR1IF ; (3)Define PIE1(PERIPHERAL INTERRUPT ENABLE)Register ; PIE1(0) TMR1IE(TMR1 Overflow Interrupt Enable bit) ; 1 = Enables, 0 = Disables BSF PIE1,TMR1IE ; (4)Define IPR1(PERIPHERAL INTERRUPT PRIORITY REGISTER 1) ; IPR1(0) TMR1IP(TMR1 Overflow Interrupt Priority bit) ; 1 = High priority, 0 = Low priority ; BSF IPR1,TMR1IP ; ******************************************************* ; 2.Define T1CON(TIMER1 CONTROL REGISTER) Register ; T1CON(7) RD16: 16-bit Read/Write Mode Enable bit ; 1 = Enables register read/write of TImer1 in one 16-bit operation ; 0 = Enables register read/write of Timer1 in two 8-bit operations ; T1CON(6) T1RUN: Timer1 System Clock Status bit ; 1 = Main system clock is derived from Timer1 oscillator ; 0 = Main system clock is derived from another source ; 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: Timer1 Oscillator Enable bit ; 1 = Timer1 oscillator is enabled ; 0 = Timer1 oscillator is shut off ; The oscillator inverter and feedback resistor are turned off to eliminate power drain. ; T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit ; When TMR1CS = 1: ; 1 = Do not synchronize external clock input ; 0 = Synchronize external clock input ; When TMR1CS = 0: ; This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. ; T1CON(1) TMR1CS: Timer1 Clock Source Select bit ; 1 = External clock from the T13CKI pin (on the rising edge) ; 0 = Internal clock (FOSC/4) ; T1CON(0) bit 0 TMR1ON: Timer1 On bit ; 1 = Enables Timer1 ; 0 = Stops Timer1 ; ******************************************************* MOVLW B'10110001' ; 16-bit Read/Write Mode, 1:8 Prescale value MOVWF T1CON ; ******************************************************* ; 3.Define TMR1 Register ; Caution(16-bit Read/Write Mode) !!!!: ; (a)Writing to TMR1H does not directly affect Timer1. ; Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. ; This allows all 16 bits of Timer1 to be updated at once. ; (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. ; This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to ; determine whether a read of the high byte, followed by a read of the low byte, has become invalid due ; to a rollover or carry between reads. ; ******************************************************* ; TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) ; 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) ; TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 MOVLW H'C5' MOVWF TMR1H MOVLW H'67' MOVWF TMR1L RETURN ;****************************************************************************************** ; Function Name : INTRUP ; Title : Interrupts Function ; Input : ; Output : ;****************************************************************************************** INTRUP: ;******************* ;1.Clear Timer1 (10mS) ;******************* MOVLW H'C5' MOVWF TMR1H MOVLW H'67' MOVWF TMR1L BCF PIR1,TMR1IF ; ******************* ; 2.Move LED (1S) ; ******************* MOVLW H'01' ADDWF G_LP,F MOVF G_LP,W XORLW H'64' ; 0x64(0d100) BTFSS STATUS,Z RETURN ; CALL MOVLED CLRF G_LP RETURN END