;****************************************************************************************** ; PIC12F1501 Sample Program ; This sample program, LED blinks every 0.5 seconds ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H26.04.10 ; History : ; Laungage : MPLAB MPASM ; ;****************************************************************************************** ; Description ; OSC : Internal OSC 16MHz ; Assign I/O pins ; Ra0[O] : LED0 ; Ra1[O] : LED1 ; Ra2[O] : LED2 ;****************************************************************************************** LIST P=PIC12F1501,ST=OFF,R=DEC INCLUDE "P12F1501.inc" __CONFIG _CONFIG1 , _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF __CONFIG _CONFIG2 , _WRT_OFF & _STVREN_OFF & _BORV_HI & _LPBOR_OFF & _LVP_OFF ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) 20h-6fh ; TMP_G EQU 20H ; ; Define Common RAM AREA (16 bytes) 70h-7fh ; TMP_C EQU 70H C_TMP1 EQU 71H INTWRG EQU 72H INTSTS EQU 73H C_CNTER EQU 74H C_FLAG EQU 75H ; ; Define Macro ; SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ; The active bank is selected by writing the bank number into the Bank Select Register (BSR) ENDM ; ; Program Start ; ORG 0h GOTO START ; ***************************** ; 0.Interrupts ; ***************************** ORG 4h MOVWF INTWRG ; Save W-REG SWAPF STATUS,W ; Save STATUS Register MOVWF INTSTS ; ; CALL INTRPT_TIM1 ; SWAPF INTSTS,W ; Restore STATUS Register MOVWF STATUS ; MOVFW INTWRG ; Restore W-REG RETFIE START ; ***************************** ; 1.Initialized ; ***************************** ; (1)Define OSC (16MHz, IntOSC) SELBANK 1 ; SET BANK1 MOVLW B'01111010' MOVWF OSCCON ; (2)Define ANSELA Register SELBANK 3 CLRF ANSELA ; All Degital Port ; (3)Define I/O port SELBANK 1 ; SET BANK1 MOVLW B'11111000' MOVWF TRISA ; (4)Define interrupt CLRF C_CNTER CLRF C_FLAG CALL INIT_TIMER1 ; (5)Clear I/O port SELBANK 0 CLRF PORTA ; ***************************** ; 2.Program main ; ***************************** ; (1)Main loop MAIN GOTO MAIN ;****************************************************************************************** ; Function Name : Init_Timer1 ; Title : ; Input : ; Output : ;****************************************************************************************** INIT_TIMER1 ; ******************************************************* ; 1.Define T1CON(TIMER1 CONTROL REGISTER) Register ; T1CON(7-6) TMR1CS<1:0>: Timer1 Clock Source Select bits ; 11 =Timer1 clock source is LFINTOSC ; 10 =Timer1 clock source is T1CKI pin (on rising edge) ; 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) Unimplemented: Read as e0f ; T1CON(2) T1SYNC: Timer1 Synchronization Control bit ; 1 = Do not synchronize asynchronous clock input ; 0 = Synchronize asynchronous clock input with system clock (FOSC) ; T1CON(1) Unimplemented: Read as e0f ; T1CON(0) TMR1ON: Timer1 On bit ; 1 = Enables Timer1 ; 0 = Stops Timer1 and clears Timer1 gate flip-flop ; ******************************************************* SELBANK 0 ; SET BANK0 MOVLW B'00110001' 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 ; 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 ; 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 ; 11 = Reserved ; 10 = Comparator 1 optionally synchronized output (sync_C1OUT) ; 01 = Timer0 overflow output ; 00 = Timer1 gate pin ; ******************************************************* MOVLW B'00000000' MOVWF T1GCON ; ******************************************************* ; 3.Define TMR1 Register ; ******************************************************* ; TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.01[S];100Hz ) ; 10mS(100Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 100(100Hz)==> 0d5,000(Count) ==> 0x1388(Count) ; TMR1 = 0xFFFF - 0x1388 ==> 0xEC77 ; TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.1[S];10Hz ) ; 100mS(10Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 10(10Hz) ==> 0d50,000(Count) ==> 0xC350(Count) ; TMR1 = 0xFFFF - 0xC350 ==> 0x3CAF MOVLW H'3C' MOVWF TMR1H MOVLW H'AF' MOVWF TMR1L ; ******************************************************* ; 4.Define interrupt ; ******************************************************* ; (1)INTCON: INTERRUPT CONTROL REGISTER ; INTCON[7] GIE: Global Interrupt Enable bit ; 1 = Enables all active interrupts / 0 = Disables all interrupts ; INTCON[6] PEIE: Peripheral Interrupt Enable bit ; 1 = Enables all active peripheral interrupts / 0 = Disables all peripheral interrupts ; INTCON[5] TMR0IE: Timer0 Overflow Interrupt Enable bit ; 1 = Enables the Timer0 interrupt / 0 = Disables the Timer0 interrupt ; INTCON[4] INTE: INT External Interrupt Enable bit ; 1 = Enables the INT external interrupt / 0 = Disables the INT external interrupt ; INTCON[3] IOCIE: Interrupt-on-Change Enable bit ; 1 = Enables the interrupt-on-change / 0 = Disables the interrupt-on-change ; INTCON[2] TMR0IF: Timer0 Overflow Interrupt Flag bit ; 1 = TMR0 register has overflowed / 0 = TMR0 register did not overflow ; INTCON[1] INTF: INT External Interrupt Flag bit ; 1 = The INT external interrupt occurred / 0 = The INT external interrupt did not occur ; INTCON[0] IOCIF: Interrupt-on-Change Interrupt Flag bit ; 1 = When at least one of the interrupt-on-change pins changed state / 0 = None of the interrupt-on-change pins have changed state MOVLW B'11000000' MOVWF INTCON ; CORE REGISTERS ; (2)PIE1: PERIPHERAL INTERRUPT ENABLE REGISTER 1 ; PIE1[7] TMR1GIE: Timer1 Gate Interrupt Enable bit ; 1 = Enables the Timer1 Gate Acquisition interrupt / 0 = Disables the Timer1 Gate Acquisition interrupt ; PIE1[6] ADIE: A/D Converter (ADC) Interrupt Enable bit ; 1 = Enables the ADC interrupt / 0 = Disables the ADC interrupt ; PIE1[5-2] Unimplemented: Read as e0f ; PIE1[1] TMR2IE: TMR2 to PR2 Match Interrupt Enable bit ; 1 = Enables the Timer2 to PR2 match interrupt / 0 = Disables the Timer2 to PR2 match interrupt ; PIE1[0] TMR1IE: Timer1 Overflow Interrupt Enable bit ; 1 = Enables the Timer1 overflow interrupt / 0 = Disables the Timer1 overflow interrupt SELBANK 1 MOVLW B'00000001' MOVWF PIE1 ; (3)PIR1: PERIPHERAL INTERRUPT REQUEST REGISTER 1 ; PIR1[7] TMR1GIF: Timer1 Gate Interrupt Flag bit ; 1 = Interrupt is pending / 0 = Interrupt is not pending ; PIR1[6] ADIF: A/D Converter Interrupt Flag bit ; 1 = Interrupt is pending / 0 = Interrupt is not pending ; PIR1[5-2] Unimplemented: Read as e0f ; PIR1[1] TMR2IF: Timer2 to PR2 Interrupt Flag bit ; 1 = Interrupt is pending / 0 = Interrupt is not pending ; PIR1[0] TMR1IF: Timer1 Overflow Interrupt Flag bit ; 1 = Interrupt is pending / 0 = Interrupt is not pending SELBANK 0 ; SET BANK0 MOVLW B'11111110' MOVWF PIR1 RETURN ;****************************************************************************************** ; Function Name : Intrpt_tim1 ; Title : interrupt function ; Input : ; Output : ;****************************************************************************************** INTRPT_TIM1 ; **************************************************** ; 1.Initialize ; **************************************************** SELBANK 1 ; SET BANK0 MOVLW B'00000000' MOVWF PIE1 SELBANK 0 ; SET BANK0 MOVLW B'11111110' MOVWF PIR1 ; **************************************************** ; 2.Check counter ; **************************************************** MOVLW H'01' ADDWF C_CNTER,F MOVF C_CNTER,W XORLW H'05' ; 0x05(0d5) BTFSC STATUS,Z GOTO INTRPT_TIM1_2 CALL INIT_TIMER1 RETURN ; **************************************************** ; 3.Main process ; (This process operates every time the count reaches 5) ; **************************************************** INTRPT_TIM1_2 MOVF C_FLAG,W XORLW H'00' BTFSS STATUS,Z GOTO INTRPT_TIM1_2A GOTO INTRPT_TIM1_2B INTRPT_TIM1_2A CLRF C_CNTER CLRF C_FLAG MOVLW B'00000111' MOVWF PORTA CALL INIT_TIMER1 RETURN INTRPT_TIM1_2B CLRF C_CNTER MOVLW B'00000001' MOVWF C_FLAG MOVLW B'00000000' MOVWF PORTA CALL INIT_TIMER1 RETURN END