Adpwm - A/D converter and Pulse Width Modulation control


This program is based on SAD but with the addition of a pulse width modulated output on PORTB Pin0. Pulse width modulation varies the "mark space ratio" or "duty cycle" of a digital output, ie, the percentage of time that the pin is on. When repeated at a high rate, it controls the power to a pin and can be used to control the power to output devices, to dim lights and control motor speed for example.

The main program can be broken up into five main areas:



;**************************************************************************
;
;TITLE   PWM on AN2 controled by AD on channel AN1 and displayed on RB0-7
;This program is a simple implementation of the PIC16C71's A/D
;
;	  The A/D is configured as follows:
;       Vref = +5V internal.
;       A/D Osc. =  internal RC
;     
;

;**************************************************************************

#include 	"p16c711.inc"
        	LIST P=16C711

        __CONFIG _XT_OSC & _PWRTE_OFF & _WDT_OFF & _BODEN_OFF & _CP_OFF

;--------------------------------------------------------------------------
; Variables
;--------------------------------------------------------------------------

RAM	  EQU	  	0x0C
TEMP      EQU  	  	RAM+0
RESULT	  EQU	  	RAM+1
COUNT1	  EQU	  	RAM+2
COUNT2	  EQU	  	RAM+3
COUNT3	  EQU	  	RAM+4

;--------------------------------------------------------------------------
; Program Code
;--------------------------------------------------------------------------

          ORG       	0x00       
          GOTO      	Start
;
;--------------------------------------------------------------------------
; Main Program
;--------------------------------------------------------------------------

          ORG       	0x10
Start
  	  MOVLW   	B'00000000'     	;set port b as outputs
          BSF     	STATUS, RP0     
          MOVWF   	TRISB           
          BCF     	STATUS, RP0    	
  	  CALL    	INITAD
  
update	  CALL		GETAD      
	  MOVF		ADRES,W         	;get a/d value
	  MOVWF		RESULT
	  MOVWF		PORTB
	
	  BSF		PORTB,0			;turn pin on
	  CLRF		COUNT1

LOOP1	  MOVF		COUNT1,W
	  SUBWF		RESULT,W
	  BTFSS		STATUS,C		;if less than then skip
	  BCF		PORTB,0			;turn pin off
 
	  CLRF		COUNT2
LOOP2	  INCFSZ	COUNT2,F
	  GOTO 		LOOP2

	  INCFSZ	COUNT1,F
	  GOTO		LOOP1
	  GOTO		update

;
; This routine gets the result of an A->D convestion from AN1
; Result is available in ADRES;
;

GETAD	  MOVLW		D'03'		    	;setup time >= 10uS.
	  MOVWF		TEMP
SDLOOP	  DECFSZ  	TEMP, F
	  GOTO    	SDLOOP
    
	  BCF     	ADCON0,ADIF     	;clear int flag
	  BSF     	ADCON0,GO       	;start new conversion
GETADLOOP
	  BTFSC   	ADCON0,NOT_DONE 	;a/d done?
	  GOTO		GETADLOOP       	;no then keep checking
	  RETURN	                	;yes then update new value.
;
;
;InitializeAD, initializes and sets up the A/D hardware.
;

INITAD	  BSF     	STATUS,RP0      	;Configure AN1 & AN0 as input
	  MOVLW   	B'00000010'     	;Vref as Reference
	  MOVWF   	ADCON1          	;
	  BCF     	STATUS,RP0      	;
	  MOVLW   	B'11000001'     	;RC, AN0, A/D
	  MOVWF   	ADCON0          	;
	  RETURN


	  END





Back Download Home Page

Contacts: