;****************************************************************************************** ; PIC12F1822 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H25.08.04 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; Description ; Oscillator : Internal Oscillator 31.25KH ; Output : P1A(RA2), P1B(RA0) ;****************************************************************************************** LIST P=PIC12F1822,ST=OFF,R=DEC INCLUDE "P12F1822.inc" __CONFIG _CONFIG1 , _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF __CONFIG _CONFIG2 , _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_HI & _LVP_OFF ; ; Define GENERAL PURPOSE RAM AREA (80 bytes maximum) ; TMP_G EQU 20H ; ; Define Common RAM AREA (16 bytes) ; TMP_C EQU 70H FRE_C EQU 71H ; frequency DUT_C EQU 72H ; duty cycle DLY_C EQU 73H ; delay ; Define Macro SELBANK MACRO #BANK_NO MOVLW #BANK_NO MOVWF BSR ENDM ORG 0h GOTO START ORG 4h GOTO START START ; ***************************** ; 1.Initialized ; ***************************** ; (1)Define OSC (31.25KHz, IntOSC) SELBANK 1 ; SET BANK1 ; MOVLW B'11111010' ; 16MHz, IntOSC MOVLW B'00010010' ; 31.25KHz, IntOSC MOVWF OSCCON ; (2)Define ANSELA (ALL Digital Port) SELBANK 3 ; SET BANK3 CLRF ANSELA ; (3)Define I/O port SELBANK 1 ; SET BANK1 MOVLW B'00111010' MOVWF TRISA ; (4)Initialized PWM CALL INITPWM SELBANK 0 ; SET BANK0 ; ***************************** ; 2.Program main ; ***************************** ; (1)There is no need to do anything MAIN GOTO MAIN ;****************************************************************************************** ; Program : INITPWM ; Title : This function outputs a pseudo sine wave of 50 Hz ; Input : ; Output : ;****************************************************************************************** INITPWM ; ******************************************************* ; 1.ALTERNATE PIN FUNCTION CONTROL REGISTER ; CCP1SEL ; 0 = CCP1/P1A function is on RA2 ; 1 = CCP1/P1A function is on RA5 ; P1BSEL ; 0 = P1B function is on RA0 ; 1 = P1B function is on RA4 ; ******************************************************* SELBANK 2 ; SET BANK2 BCF APFCON,CCP1SEL BCF APFCON,P1BSEL ; ******************************************************* ; 2.Define CCP1CON: CCP1 CONTROL REGISTER ; CCP1CON[7-6] P1M<1:0>: Enhanced PWM Output Configuration bits(1) ; Capture mode: Unused ; Compare mode: Unused ; PWM mode: ; If CCP1M<3:2> = 00, 01, 10: ; xx = P1A assigned as Capture/Compare input; P1B, P1C, P1D assigned as port pins ŠPIC16F/LF1823 only ; If CCP1M<3:2> = 11: ; 00 = Single output; P1A modulated; P1B, P1C, P1D assigned as port pins ; 01 = Full-Bridge output forward; P1D modulated; P1A active; P1B, P1C inactive ŠPIC16F/LF1823 only ; 10 = Half-Bridge output; P1A, P1B modulated with dead-band control; P1C, P1D assigned as port pins ; 11 = Full-Bridge output reverse; P1B modulated; P1C active; P1A, P1D inactive ŠPIC16F/LF1823 only ; CCP1CON[5-4] DC1B<1:0>: PWM Duty Cycle Least Significant bits ; Capture mode: Unused ; Compare mode: Unused ; PWM mode: ; These bits are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPR1L. ; CCP1CON[3-0] CCP1M<3:0>: ECCP1 Mode Select bits ; 0000 = Capture/Compare/PWM off (resets ECCP1 module) ; 0001 = Reserved ; 0010 = Compare mode: toggle output on match ; 0011 = Reserved ; 0100 = Capture mode: every falling edge ; 0101 = Capture mode: every rising edge ; 0110 = Capture mode: every 4th rising edge ; 0111 = Capture mode: every 16th rising edge ; 1000 = Compare mode: initialize ECCP1 pin low; set output on compare match (set CCP1IF) ; 1001 = Compare mode: initialize ECCP1 pin high; clear output on compare match (set CCP1IF) ; 1010 = Compare mode: generate software interrupt only; ECCP1 pin reverts to I/O state ; 1011 = Compare mode: Special Event Trigger (CCP1 resets TMR1, sets CCP1IF bit, and starts A/D conversion if A/D module is enabled) ; PWM mode: ; 1100 = PWM mode: P1A, P1C active-high; P1B, P1D active-high ; 1101 = PWM mode: P1A, P1C active-high; P1B, P1D active-low ; 1110 = PWM mode: P1A, P1C active-low; P1B, P1D active-high ; 1111 = PWM mode: P1A, P1C active-low; P1B, P1D active-low ; ******************************************************* SELBANK 5 ; SET BANK5 MOVLW B'10001100' MOVWF CCP1CON ; ******************************************************* ; 3.T2CON: TIMER2 CONTROL REGISTER ; T2CON[7] Unimplemented: Read as e0f ; T2CON[6-3] TOUTPS<3:0>: Timer 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 ; SET BANK0 ; MOVLW B'00000100' MOVLW B'00000000' MOVWF T2CON ; ******************************************************* ; 4.PSTR1CON: PWM STEERING CONTROL REGISTER ; PSTR1CON[7-5] Unimplemented: Read as e0f ; PSTR1CON[4] STR1SYNC: Steering Sync bit ; 1 = Output steering update occurs on next PWM period ; 0 = Output steering update occurs at the beginning of the instruction cycle boundary ; PSTR1CON[3] STR1D: Steering Enable bit D(2) ; 1 = P1D pin has the PWM waveform with polarity control from CCP1M<1:0> ; 0 = P1D pin is assigned to port pin ; PSTR1CON[2] STR1C: Steering Enable bit C(2) ; 1 = P1C pin has the PWM waveform with polarity control from CCP1M<1:0> ; 0 = P1C pin is assigned to port pin ; PSTR1CON[1] STR1B: Steering Enable bit B ; 1 = P1B pin has the PWM waveform with polarity control from CCP1M<1:0> ; 0 = P1B pin is assigned to port pin ; PSTR1CON[0] STR1A: Steering Enable bit A ; 1 = P1A pin has the PWM waveform with polarity control from CCP1M<1:0> ; 0 = P1A pin is assigned to port pin ; ******************************************************* SELBANK 5 ; SET BANK5 MOVLW B'00010011' MOVWF PSTR1CON ; ******************************************************* ; 5.Set the frequency (PR2 Register) ; ******************************************************* SELBANK 0 ; SET BANK0 MOVLW H'9B' MOVWF PR2 MOVWF FRE_C ; ******************************************************* ; 6.Set the delay (PWM1CON : ENHANCED PWM CONTROL Register) ; PWM1CON[7] P1RSEN: PWM Restart Enable bit ; 1 = Upon auto-shutdown, the CCP1ASE bit clears automatically once the shutdown event goes ; away; the PWM restarts automatically ; 0 = Upon auto-shutdown, CCP1ASE must be cleared in software to restart the PWM ; PWM1CON[6-0] P1DC<6:0>: PWM Delay Count bits ; P1DC1 = Number of FOSC/4 (4 * TOSC) cycles between the scheduled time when a PWM signal ; should transition active and the actual time it transitions active ; ******************************************************* SELBANK 5 ; SET BANK5 MOVF FRE_C,W MOVWF DLY_C RRF DLY_C ; PWM1CON <= PR2 / 4 RRF DLY_C MOVF DLY_C,W ANDLW B'00111111' MOVWF PWM1CON ; ******************************************************* ; 7.Set the duty cycle (CCPR1L Register) ; This makes 50 percent duty cycle ; ******************************************************* SELBANK 5 ; SET BANK5 MOVF FRE_C,W MOVWF DUT_C RRF DUT_C ; CCPR1L <= PR2 / 2 MOVF DUT_C,W ANDLW B'01111111' MOVWF CCPR1L ; ******************************************************* ; 8.Start ; ******************************************************* SELBANK 0 ; SET BANK0 MOVLW B'00000100' MOVWF T2CON RETURN END