pwm pic16f873

Discussion in 'Embedded Systems and Microcontrollers' started by chipus, Mar 9, 2010.

  1. chipus

    Thread Starter New Member

    Mar 9, 2010
    6
    0
    hello
    i need some help in constructing pwm program.. i need to vary duty cycle from 0% to 100%.. interruption will occur before the duty cycle is changed.. can any one fix this program???.. :(:(

    ; oscillator : 4 mHZ


    List p=16f8773
    #include <p16f873.inc>
    ;***declare registeraion

    ORG 20H
    COUNT RES 1
    AN0 RES 1
    AN1 RES 1

    ;***port initialiization

    MAIN BCF STATUS,RP0
    CLRF PORTA ;clear content of port A
    CLRF PORTB ;clear content of port B
    CLRF PORTC ;clear content of port C
    BSF STATUS,RP0 ;change to bank 1
    MOVLW B'11111111'
    MOVWF TRISA ;set port A as input
    CLRF TRISB ;set port B as output

    ;***A/D conversion

    BCF STATUS,RP0 ;change to bank 0
    MOVLW B'01000001'
    MOVWF ADCON0 ;fosc/8, channel 0=RA0/AN0, A/D converter=on
    BSF STATUS,RP0 ;change to bank 1
    MOVLW B'00000000'
    MOVWF ADCON1 ;set port A as analog input port

    ;***PWM initialization
    ;**step 1: set the PWM period by writting to the PR2 register

    MOVLW D'49'
    MOVWF PR2 ;PR2=49

    ;**step 2: set the PWM duty cycle by writting duty cycle by writing to the CCPR1RL register and CCP1CON<5:4> bits

    BCF STATUS,RP0
    MOVLW B'10110100'
    MOVWF CCPR1L ;load 180 in W to CCPR1L
    BSF CCP1CON,CCP1Y ;set bit 1 of the duty cycle
    BSF CCP1CON,CCP1X ;set bit 0 of the duty cycle


    ;**step 3: make the CCP1 pin output by clearing the TRISC<2> bit

    MOVLW B'11111011'
    ANDWF TRISC ;clear TRISC<2>

    ;**step 4: set the TMR2 prescale value and enable Timer2 by writting to T2CON

    CLRF TMR2
    BSF T2CON,TMR2ON

    ;**step 5: configure the CCP1 module for PWM operation

    BSF CCP1CON,CCP1M3
    BSF CCP1CON,CCP1M2

    BTFSS PIR1,TMR2IF
    GOTO $-1
    BCF PIR1,TMR2IF

    ;***1st loop

    LOOP1 BTFSS INTCON,T0IF
    GOTO LOOP1

    BCF INTCON,T0IF

    BCF INTCON,T0IF
    BSF ADCON0,GO_DONE
    BTFSC ADCON0,GO_DONE
    GOTO $-1

    MOVF ADRESH,W
    MOVWF AN0
    MOVWF PORTB

    MOVLW B'01001001'
    MOVWF ADCON0

    ;***2nd loop

    LOOP2 BTFSS INTCON,T0IF
    GOTO LOOP2

    BCF INTCON,T0IF
    BCF INTCON,T0IF

    BSF ADCON0,GO_DONE
    BTFSC ADCON0,GO_DONE
    GOTO $-1

    MOVF ADRESH,W
    MOVWF AN1
    MOVWF PORTB

    SUBWF AN0,0
    BTFSS STATUS,Z
    MOVWF CCPR1L
    GOTO MAIN

    END
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Have you turned the watchdog timer off? There are no config settings so it probably takes default settings from MPLAB.
    I've also spotted a couple of things.
     
    chipus likes this.
Loading...