+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Newbie level 3
    Points: 63, Level: 1

    Join Date
    Aug 2013
    Posts
    4
    Helped
    0 / 0
    Points
    63
    Level
    1

    pwm generation depending on analog input

    i have to generate a pwm using ccp module from an analog signal varying between 0-5volt. i have combined adc and ccp programs... there is a problem wen i checked it in proteus... e.g if i give 2.5 volt, i will get 50%duty cycle, but after that if i change voltage to 1.25volt, i am not getting 25%duty cycle... if i reset , then i will get 25%. that means progrm is stuck in some loop... here is the program... plz help...
    Code:
    LIST P=PIC18F452, F=INHX32, N=0, ST=OFF, R=HEX
    #include P18F452.INC
    CONFIG OSC=HS, OSCS=OFF
    CONFIG WDT=OFF
    CONFIG BORV=45, PWRT=ON, BOR=ON
    CONFIG DEBUG=OFF, LVP=OFF, STVR=OFF
    
    COUNT EQU 25
    ORG 00H
    CLRF TRISC
    CLRF TRISD
    BSF TRISA,0
    MOVLW 0X81 ;Fosc/64,channel 0,A/D on
    MOVWF ADCON0
    MOVLW 0X4E ;left justified,Fosc/64,AN0=analog
    MOVWF ADCON1
    OVER	CALL DELAY	
    BSF ADCON0,GO
    BACK	BTFSC ADCON0,DONE
    BRA BACK
    MOVFF ADRESL,PORTC
    MOVFF ADRESH,PORTD
    CALL DELAY
    BRA OVER
    DELAY MOVLW 35
    MOVWF COUNT
    AGAIN	DECFSZ COUNT
    BRA AGAIN	
    RETURN	
    
    CLRF CCP1CON	;16Mhz xtal, 4Khz pwm
    MOVLW D'249'
    MOVWF PR2
    MOVFF PORTD,CCPR1L	;digital output moved to ccpr1l
    BCF TRISC,CCP1
    MOVLW 0X01
    MOVWF T2CON	 ;prescalar 4
    MOVLW 0X0C
    MOVWF CCP1CON	;decimal neglected,pwm option
    CLRF TMR2
    BSF T2CON,TMR2ON
    REPEAT BCF PIR1,TMR2IF
    LOOP BTFSS PIR1,TMR2IF
    BRA LOOP
    GOTO REPEAT
    END
    Last edited by keith1200rs; 27th October 2013 at 08:38. Reason: added CODE tags

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 5
    Points: 25,337, Level: 38

    Join Date
    Dec 2010
    Location
    Southampton and holiday cottage in Wensleydale (UK)
    Posts
    4,863
    Helped
    1314 / 1314
    Points
    25,337
    Level
    38

    Re: pwm generation depending on analog input

    Don't understand your programming language, BUT you have a repeat. ...... then a goto repeat, so it repeats the generator but not looking for a new input, point the goto (urh!) to a point before the analogue is read.
    Frank (FORTRAN 77)



    •   AltAdvertisment

        
       

  3. #3
    Newbie level 3
    Points: 63, Level: 1

    Join Date
    Aug 2013
    Posts
    4
    Helped
    0 / 0
    Points
    63
    Level
    1

    Re: pwm generation depending on analog input

    yes i had returned control to the start of the program where analog input can b fetched... here it is
    Code:
    	    LIST P=PIC18F452, F=INHX32, N=0, ST=OFF, R=HEX
            #include P18F452.INC
    		CONFIG OSC=HS, OSCS=OFF
    		CONFIG WDT=OFF
    		CONFIG BORV=45, PWRT=ON, BOR=ON
    		CONFIG DEBUG=OFF, LVP=OFF, STVR=OFF
    	
    		COUNT EQU 25
    		ORG 00H
    		CLRF TRISC
    		CLRF TRISD
    		BSF TRISA,0
    		MOVLW 0X81    ;Fosc/64,channel 0,A/D on
    		MOVWF ADCON0
    		MOVLW 0X4E    ;left justified,Fosc/64,AN0=analog
    		MOVWF ADCON1
    OVER		CALL DELAY	
    		BSF ADCON0,GO
    BACK		BTFSC ADCON0,DONE
    		BRA BACK
    		MOVFF ADRESL,PORTC
    		MOVFF ADRESH,PORTD
    	
    
    	    	CLRF CCP1CON	;16Mhz xtal, 4Khz pwm
    		MOVLW D'249'
    		MOVWF PR2
    		MOVFF PORTD,CCPR1L	;digital output moved to ccpr1l
    		BCF TRISC,CCP1
    		MOVLW 0X01
    		MOVWF T2CON		;prescalar 4
    		MOVLW 0X0C
    		MOVWF CCP1CON	;decimal neglected,pwm option
    		CLRF TMR2
    		BSF T2CON,TMR2ON
     		BCF PIR1,TMR2IF
    LOOP    	BTFSS PIR1,TMR2IF
    		BRA LOOP
    				  
    
    DELAY   
    		MOVLW 35
    	   	MOVWF COUNT
    AGAIN		DECFSZ COUNT
    	    	BRA AGAIN	
    	    	RETURN	
    	    	CALL DELAY
    		BRA OVER
    
    			  END
    - - - Updated - - -

    still the pulses go more distorted...
    Last edited by keith1200rs; 27th October 2013 at 08:36. Reason: added CODE tags



--[[ ]]--