;********************************************************************* ; * ; Tutorial 18 Analog to Digital Converter * ; * ;********************************************************************* ;* TARGET PIC16F877 ;********************************************************************* ; ;********************************************************************* ;* @version 0.1, 27 September 2001 ;* @author James Caska ;********************************************************************* list p=16F877 #include DELAY EQU .32 FLAGS EQU .33 USEINTERRUPT EQU .0 ORG .0 GOTO INIT ORG .4 INTERRUPTHANDLER CALL ADC.DisplayResult BCF PIR1,ADIF ;Clear the Receive Interrupt RETFIE ADC.DisplayResult MOVF ADRESH,W MOVWF PORTC BSF STATUS,RP0 MOVF ADRESL,W BCF STATUS,RP0 MOVWF PORTB RETURN ConfigureADCChannel MOVF PORTD,W ANDLW .7 ADDWF PCL,F GOTO Channel0 GOTO Channel1 GOTO Channel2 GOTO Channel3 GOTO Channel4 GOTO Channel5 GOTO Channel6 GOTO Channel7 Channel0 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BCF ADCON0,CHS0 ;000 = channel 0, (RA0/AN0) BCF ADCON0,CHS1 BCF ADCON0,CHS2 RETURN Channel1 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BSF ADCON0,CHS0 ;001 = channel 1, (RA1/AN1) BCF ADCON0,CHS1 BCF ADCON0,CHS2 RETURN Channel2 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BCF ADCON0,CHS0 ;010 = channel 2, (RA2/AN2) BSF ADCON0,CHS1 BCF ADCON0,CHS2 RETURN Channel3 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BSF ADCON0,CHS0 ;011 = channel 3, (RA3/AN3) BSF ADCON0,CHS1 BCF ADCON0,CHS2 RETURN Channel4 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BCF ADCON0,CHS0 ;100 = channel 4, (RA5/AN4) BCF ADCON0,CHS1 BSF ADCON0,CHS2 RETURN Channel5 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BSF ADCON0,CHS0 ;101 = channel 5, (RE0/AN5) BCF ADCON0,CHS1 BSF ADCON0,CHS2 RETURN Channel6 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BCF ADCON0,CHS0 ;110 = channel 6, (RE1/AN6) BSF ADCON0,CHS1 BSF ADCON0,CHS2 RETURN Channel7 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Channel Select BSF ADCON0,CHS0 ;111 = channel 7, (RE2/AN7) BSF ADCON0,CHS1 BSF ADCON0,CHS2 RETURN WaitAquisitionTime MOVLW .10 MOVWF DELAY WaitAquisitionTime.wait DECFSZ DELAY,F GOTO WaitAquisitionTime.wait RETURN INIT ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;======Begin PORT Configuration BSF STATUS, RP0 ;PORT TRIS Registers are on page 1 ;======PORT A MOVLW .63 ;PORTA Configuration Bits MOVWF TRISA ;======PORT B MOVLW .0 ;PORTB Configuration Bits MOVWF TRISB ;======PORT C MOVLW .0 ;PORTC Configuration Bits MOVWF TRISC ;======PORT D MOVLW .255 ;PORTD Configuration Bits MOVWF TRISD ;======PORT E MOVLW .7 ;PORTE Configuration Bits MOVWF TRISE BCF STATUS, RP0 ;Restore Page0 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Port Configuration BSF STATUS,RP0 ;Bank1 MOVLW .0 ;PCFG3:PCFG0(0000)AN7/RE2(A)AN6/RE1(A)AN5/RE0(A)AN4/RA5(A)AN3/RA3(A)AN2/RA2(A)AN1/RA1(A)AN0/RA0(A) MOVWF ADCON1 BSF ADCON1,ADFM ;Right Justfied Result BCF STATUS,RP0 ;Restore Bank0 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Conversion Oscillator Configuration BSF ADCON0,ADCS0 ;RC - Required if using wakeup from sleep BSF ADCON0,ADCS1 ; ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======ADC Configure Interrupt BCF PIR1,ADIF ;Clear the AD interrupt flag BSF STATUS,RP0 ;Bank1 BSF PIE1,ADIE ;Set AD Interrupt Enable flag BCF STATUS,RP0 ;Bank0 ;=======ADC Turn On ADC converter BSF ADCON0,ADON BSF INTCON,PEIE ;Enabled Peripheral Interrupt MAIN CALL ConfigureADCChannel CALL WaitAquisitionTime MOVLW .1 ;Toggle between conversion methods XORWF FLAGS,F BTFSS FLAGS,USEINTERRUPT GOTO ADC.withSleep ADC.PollingWithInterrupt BCF PIR1,ADIF ;Clear Interrupt Before Enabling BSF INTCON,GIE ;Enabled GLobal Interrupts ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======Begin ADC Conversion BSF ADCON0,GO_DONE ADC.PollingWithInterrupt.Loop BTFSC ADCON0,GO_DONE GOTO ADC.PollingWithInterrupt.Loop GOTO MAIN ADC.withSleep BCF INTCON,GIE ;Disable GLobal Interrupts ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;=======Begin ADC Conversion BSF ADCON0,GO_DONE SLEEP NOP CALL ADC.DisplayResult GOTO MAIN END