;****************************************************************************************** ; PIC12F1501 Sample Program ; 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] : PWM2 ; Ra2[O] : PWM1 ; Ra4[O] : PWM3 ; Ra5[O] : PWM4 ;****************************************************************************************** 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 ; ; 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 ORG 4h GOTO START 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'11001010' MOVWF TRISA ; (4)Initialized PWM CALL INITPWM ; (5)Set PWM Value SELBANK 12 MOVLW H'19' ; Duty 10 % ( 0xff * 0.1 ==> 0x19) MOVWF PWM1DCH MOVLW H'3F' ; Duty 25 % ( 0xff * 0.25 ==> 0x3F) MOVWF PWM2DCH MOVLW H'7F' ; Duty 50 % ( 0xff * 0.50 ==> 0x7F) MOVWF PWM3DCH MOVLW H'BF' ; Duty 75 % ( 0xff * 0.75 ==> 0xBF) MOVWF PWM4DCH ; ***************************** ; 2.Program main ; ***************************** ; (1)Main loop MAIN GOTO MAIN ;****************************************************************************************** ; Program : INITPWM ; Title : ; Input : ; Output : ;****************************************************************************************** INITPWM ; ******************************************************* ; 1.Define T2CON: TIMER2 CONTROL REGISTER ; T2CON[7] Unimplemented: Read as e0f ; T2CON[6-3] T2OUTPS<3:0>: Timer2 Output Postscaler Select bits ; 0000 = 1:1 Postscaler ; 0001 = 1:2 Postscaler ; 0010 = 1:3 Postscaler ; 0011 = 1:4 Postscaler ; 0100 = 1:5 Postscaler ; 0101 = 1:6 Postscaler ; 0110 = 1:7 Postscaler ; 0111 = 1:8 Postscaler ; 1000 = 1:9 Postscaler ; 1001 = 1:10 Postscaler ; 1010 = 1:11 Postscaler ; 1011 = 1:12 Postscaler ; 1100 = 1:13 Postscaler ; 1101 = 1:14 Postscaler ; 1110 = 1:15 Postscaler ; 1111 = 1:16 Postscaler ; T2CON[2] TMR2ON: Timer2 On bit ; 1 = Timer2 is on, 0 = Timer2 is off ; T2CON[1-0] T2CKPS<1:0>: Timer2 Clock Prescale Select bits ; 00 = Prescaler is 1 ; 01 = Prescaler is 4 ; 10 = Prescaler is 16 ; 11 = Prescaler is 64 ; ******************************************************* SELBANK 0 MOVLW B'00000100' MOVWF T2CON ; ******************************************************* ; 2.Define PR2: Timer2 Module Period Register ; OSC:16MHz / 0xFF / 4 / 1(Prescaler) ==> 15.625[KHz] ; ******************************************************* SELBANK 0 MOVLW H'FF' MOVWF PR2 ; ******************************************************* ; 3.Define PWMxCON: PWM CONTROL REGISTER ; PWMxCON[7] PWMxEN: PWM Module Enable bit ; 1 = PWM module is enabled, 0 = PWM module is disabled ; PWMxCON[6] PWMxOE: PWM Module Output Enable bit ; 1 = Output to PWMx pin is enabled, 0 = Output to PWMx pin is disabled ; PWMxCON[5] PWMxOUT: PWM Module Output Value bit ; PWMxCON[4] PWMxPOL: PWMx Output Polarity Select bit ; 1 = PWM output is active-low, 0 = PWM output is active-high ; PWMxCON[3-0] Unimplemented: Read as e0f ; ******************************************************* SELBANK 12 MOVLW B'11100000' MOVWF PWM1CON MOVLW B'11100000' MOVWF PWM2CON MOVLW B'11100000' MOVWF PWM3CON MOVLW B'11100000' MOVWF PWM4CON ; ******************************************************* ; 4.Define PWMxDCH: PWM DUTY CYCLE HIGH BITS ; PWMxDCH[7-0]: PWM Duty Cycle Most Significant bits ; These bits are the MSbs of the PWM duty cycle. The two LSbs are found in the PWMxDCL register. ; ******************************************************* SELBANK 12 MOVLW H'7F' MOVWF PWM1DCH MOVLW H'7F' MOVWF PWM2DCH MOVLW H'7F' MOVWF PWM3DCH MOVLW H'7F' MOVWF PWM4DCH ; ******************************************************* ; 5.Define PWMxDCL: PWM DUTY CYCLE LOW BITS ; PWMxDCL[7:6]: PWM Duty Cycle Least Significant bits ; These bits are the LSbs of the PWM duty cycle. The MSbs are found in the PWMxDCH register. ; PWMxDCL[5-0]: Unimplemented: Read as e0f ; ******************************************************* SELBANK 12 MOVLW H'00' MOVWF PWM1DCL MOVLW H'00' MOVWF PWM2DCL MOVLW H'00' MOVWF PWM3DCL MOVLW H'00' MOVWF PWM4DCL SELBANK 0 ; SET BANK0 RETURN END