Your subroutine "Output_A2D_High_Byte" has only one instruction: movwf PORTB Move WHAT to PortB? The stuff in W is gabarge from the delay routine your have! Better read the ADC value from ADRESH to W first thomas >From: Graham North >Reply-To: pic microcontroller discussion list >To: PICLIST@MITVMA.MIT.EDU >Subject: [PIC]: 16F877 ADC Probs >Date: Mon, 23 Apr 2001 20:13:28 -0000 > >Hi I'm having some problems with the ADC on the 16F877. Am I correct in >thinking that I can connect the sampling pin straight to a voltage (is this >limited to 0 -> 5 Volts). Is some sort of protection recomended. > >I can not find reference to either of these questions in the data sheet. > >My code at the moment is reading pin A 0 and then displaying the highest 8 >bits to port B, but the value on port B will not change! > >I have included the code, I don't usually like including large amounts of >code, but this has got me stumped! > >Thanks for any help, > >Graham North > > >; ************************** Main Code ************************** > >Main > > call Startup_Init > call InitPorts > call InitPWM > call Init_ADC > >Loop > > call ADC_Read > > bsf PORTE,0 ; Used for testing > > call Delay > > call Output_A2D_High_Byte > > bcf PORTE,0 ; Used for testing > > call Delay > > goto Loop > >; ************************* Subroutines ************************** > > >Output_A2D_High_Byte > >; Outputs the 8bit value returned from the A2D > > movwf PORTB > > return > > >InitPWM > >; Sets up PWM output > > BANK1 ; Select bank 1. > > bcf TRISC,2 ; Set pin as output by clearing > ; TRISC<2> bit. > > movlw B'00111111' ; Set PWM period. > movwf PR2 ; Period = [(PR2)+1] * 4 * > ; TOSC * (TMR2 prescale value) > ; Page 61 data sheet. > > BANK0 ; Select bank 0. > > movlw B'00100000' ; Set PWM duty cycle. > movwf CCPR1L ; Duty Cycle = (CCPR1L:CCP1CON > bsf CCP1CON,5 ; <5:4>) * TOSC * (TMR2 > bsf CCP1CON,4 ; prescale value) > ; Page 61 data sheet. > > movlw B'00000110' ; Start TIMER2 and set TMR2 > movwf T2CON ; prescale value to 1:16 > ; (bit 1 = 1, bit 0 = x). > ; Page 55 data sheet. > > movlw B'00001100' ; Start PWM (bits 3-0 = 11xx) > movwf CCP1CON ; Page 58 data sheet. > > return > > >InitPorts > >; INITIALISE PORTS >; binary used to see individual pin level > > movlw b'00000000' ; all port pins = low > movwf PORTA > movlw b'00000000' > movwf PORTB > movlw b'00000000' > movwf PORTC > movlw b'00000000' > movwf PORTD > movlw b'00000000' > movwf PORTE > > BANK1 ; Select Bank 1 for TRIS registers > >; binary used to see individual pin IO status MSB -> LSB > > movlw b'00000001' ; Port A Bit 0 = input > movwf TRISA > movlw b'00000000' ; Port B = outputs > movwf TRISB > movlw b'01000000' ; Port C Bit 6 = input > movwf TRISC > movlw b'11111111' ; Port D = inputs > movwf TRISD > movlw b'00000000' ; Port E = outputs > movwf TRISE > > movlw b'00000110' ; all analog pins = digital > movwf ADCON1 > > BANK0 ; Select Bank 0 > > return > > >Startup_Init > >; Setting up the Special Function Registers > > movlw b'10010000' > movwf INTCON > > movwf b'11111111' > movwf OPTION_REG > > return > > >Init_ADC > >; Initialize ADC to use Fosc/2 and input line 000 >; input line can be selected on a per conversion basis >; using GetADC > > bsf STATUS,RP0 > > clrf TRISA ; setup PORT A for input > comf TRISA > > bcf PIE1,ADIE ; disable ADC interrupt > > bcf STATUS,RP0 > > movlw B'00000001' ; initialize ADC > movwf ADCON0 > > bsf STATUS,RP0 > clrf ADCON1 > bcf STATUS,RP0 > > bcf PIR1,ADIF ; clear conversion complete flag > > return > > >ADC_Read > >; Read a value from the internal 10 bit A2D and return it in the working >register. > > btfsc ADCON0,GO ; wait for conversion to finish > goto ADC_Read > > bcf PIR1,ADIF ; clear conversion complete flag > > movf ADRESH,W ; return upper 8 bits of result > > return > >Delay > >; 1/2 SEC DELAY SUBROUTINE WITH 4MHz CLOCK > > movlw 01h > movwf NbHi > movlw 03h > movwf NbLo > movlw 8Ah > movwf NaHi > movlw 5Bh > movwf NaLo > >DeLoop0 decfsz NaLo,F > goto DeLoop0 > decfsz NaHi,F > goto DeLoop0 > decfsz NbLo,F > goto DeLoop0 > decfsz NbHi,F > goto DeLoop0 > return > > > >_________________________________________________________________________ >Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. > >-- >http://www.piclist.com hint: The list server can filter out subtopics >(like ads or off topics) for you. See http://www.piclist.com/#topics > > _________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics