Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic18f4550 adc

Status
Not open for further replies.

adrianvon

Member
Hi all,
I am doing an analog to digital program. The analog input is set to AN0 and the digital output is port D. The program worked, but bit 7 is starting from bit 3 i.e. shifted by 4 bits (see attached pic). Does someone know what is causing this? The coding is set to left justified since i only need to use the first 8bits.

Below is the coding:

Code:
;REGISTER DECLERATIONS

REG1 EQU 60H ; HERE WE ARE NAMING THE GENERAL PURPOUSE REGISTER


ORG 0000H

;ALL INTIALIZATION HERE OF ALL PORTS AND PERIPHERALS HERE

MOVLW B'00000001' ; SET AN0 AS ANALOG INPUT AND SWITCH ON ADC MODULE
MOVWF ADCON0

MOVLW B'00111110' ;SET AN2 AND AN3 AS REFERENCE VOLTAGES, AND SET AN0 AS ANALOG INPUT
MOVWF ADCON1

MOVLW B'00101100' ;SET AS LEFT JUSTIFIED, SET AS 12 TAD, SET AS FOSC/4
MOVWF ADCON2

CLRF PORTD ;DECLARE PORT D AS OUTPUT FOR THE DIGITAL OUTPUT
CLRF TRISD


MOVLW B'111111' ;DECLARE PORT A AS INPUT FOR THE ANALOG INPYT
MOVWF TRISA

;START MAIN PROGRAM HERE


MAIN


CALL ACQ_DELAY ;CALL DELAY
BSF ADCON0,1 ; CONVERSION IN PROCESS (START CONVERTING)
LINE1
BTFSC ADCON0,1 ;check if ADCON0 bit 1 if clear,if yes skip the next instruction
BRA LINE1
SWAPF ADRESH,W ;MOVE VALUE FROM AN0 TO WORKING REGISTER
MOVWF PORTD  ;MOVE VALUE IN WORKING REGISTER TO PORTD 
GOTO MAIN   

ACQ_DELAY
MOVLW .3
MOVWF REG1
ACQ1
DECFSZ REG1
BRA ACQ1
RETURN		


END_OF_PROGRAME

And can someone explain to me what exactly T[SUB]AD[/SUB] means and how to determine which one to use?? and how to determine the conversion clock select bit? (see attached pic)

Thanks in advance,
Adrian
 

Attachments

  • Untitled.png
    Untitled.png
    5.8 KB · Views: 241
  • Capture.JPG
    Capture.JPG
    76.4 KB · Views: 345
Hi Adrian,

I have no idea why your result is shifted but I can hopefully explain what TAD and the conversion clock are.

TAD is your A/D converter clock source. If your TAD (AD Clock source) is too fast, then you will have inaccuracies in the last few bits of your conversion. If your TAD is too slow, then the internal sample-and-hold capacitor will slightly discharge before the conversion is completed, resulting in a lower reading than it should be.

So 1TAD is one cycle of the AD Clock and therefore 20TAD is 20 cycles of the AD Clock. The conversion internally takes 12 cycles (12 TAD) to complete. There are minimum TAD times for AD modules also.

You can work out your TAD based on your AD Clock source. For example a PIC32MX has a minimum TAD time of 83.3ns which is equal to a 12MHz AD Clock source.

Just a little on the acquisition time too... If your acquisition time is too short, or your input resistance too high, then the internal sample-and-hold capacitor will not have enough time to completely charge, resulting in a lower than normal reading. The acquisition time is a function of the input resistance and the internal capacitance.

HTH.
 
Last edited:
hi
Check what the swapf instruction is doing.
 
Hi,

Thanks for your reply.
I changed the coding as the one below and it worked.

Code:
;REGISTER DECLERATIONS

REG1 EQU 60H ; HERE WE ARE NAMING THE GENERAL PURPOUSE REGISTER
ADC EQU 61H


ORG 0000H

;ALL INTIALIZATION HERE OF ALL PORTS AND PERIPHERALS HERE

MOVLW B'00000001' ; SET AN0 AS ANALOG INPUT AND SWITCH ON ADC MODULE
MOVWF ADCON0

MOVLW B'00111110' ;SET AN2 AND AN3 AS REFERENCE VOLTAGES, AND SET AN0 AS ANALOG INPUT
MOVWF ADCON1

MOVLW B'00101100' ;SET AS LEFT JUSTIFIED, SET AS 12 TAD, SET AS FOSC/4
MOVWF ADCON2

CLRF PORTD ;DECLARE PORT D AS OUTPUT FOR THE DIGITAL OUTPUT
CLRF TRISD


MOVLW B'111111' ;DECLARE PORT A AS INPUT FOR THE ANALOG INPYT
MOVWF TRISA

CLRF ADC ;CLEAR REGISTER ON START UP ONLY

;START MAIN PROGRAM HERE


MAIN


CALL ACQ_DELAY ;CALL DELAY
BSF ADCON0,1 ; CONVERSION IN PROCESS (START CONVERTING)
LINE1
BTFSC ADCON0,1 ;check if ADCON0 bit 1 if clear,if yes skip the next instruction
BRA LINE1
MOVFF ADRESH,ADC ;MOVE VALUE FROM AN0 TO ADRESH REGISTER
MOVFF ADRESH,PORTD  ;MOVE VALUE FROM ADRESH REGISTER TO PORTD 
GOTO MAIN   

ACQ_DELAY
MOVLW .3
MOVWF REG1
ACQ1
DECFSZ REG1
BRA ACQ1
RETURN		


END_OF_PROGRAME
 
No but i will try it.

Can i set the +ve reference temperature (pin 5) to a voltage of more than 5V ?? or will i damage the PIC if i increase it more than 5V??

Thanks
 

Attachments

  • PIC Pins.JPG
    PIC Pins.JPG
    66.2 KB · Views: 284
Last edited:
I tried it with movff ADRESH,PORTD and its working fine.

Can i use MOVFF for the register decelerations as well ?? for instance instead of writing:

Code:
MOVLW B'00000001' 
MOVWF ADCON0

can i write:
Code:
MOVFF B'00000001'. ADCON0
 
Hi Adrian,

Generally VDD (or AVDD) is the upper limit for Vref+. You can damage the PIC by using a higher voltage.
 
Hi,

Thanks for the reply. Since i cannot increase the ref voltage above 5V, is it possible to set the ADC to 7 bits instead of 8 Bits?? The reason for this is that i want to represent 128 bits for an analog input which will vary from 0V to 4000mV (i.e. 31.25mV = 1 bit)

The diagram below may help you understand what im trying to do.

Thanks
 

Attachments

  • Untitled.png
    Untitled.png
    13.7 KB · Views: 216
Last edited:
...Since i cannot increase the ref voltage above 5V, is it possible to set the ADC to 7 bits instead of 8 Bits??...

Yeh sure. Just shift your AD Result right until you have the 7 upper bits.

So for a 10-bit A/D shift right 3 times and you are left with a 7-bit result.

Dividing your 10-bit result by 8 will do the same thing (or divide your 8-bit result by 2).
 
Last edited:
Dividing your 10-bit result by 8 will do the same thing (or divide your 8-bit result by 2).

Good idea...

Can i divide using assembly? if yes, do you know the instruction set please?

Thanks in advance
 
Last edited:
Can i divide using assembly? if yes, do you know the instruction set please?

'RRF' will shift right and divide by 2 every time you shift.
 
I tried it with movff ADRESH,PORTD and its working fine.

Can i use MOVFF for the register decelerations as well ?? for instance instead of writing:

Code:
MOVLW B'00000001' 
MOVWF ADCON0

can i write:
Code:
MOVFF B'00000001'. ADCON0

hi,
No, movff reg1,reg2 ; is a Register Contents move, not a Literal.

Your MOVFF B'00000001', ADCON0;, would move the Contents of B'00000001 Register to ADCON0
 
Last edited:
hi,
No, movff reg1,reg2 ; is a Register Contents move, not a Literal.

Your MOVFF B'00000001', ADCON0;, would move the Contents of B'00000001 Register to ADCON0

Hi,

Ok, thanks for explaining :)
 
Last edited:
'RRF' will shift right and divide by 2 every time you shift.

RRF is not being recognized as an instruction. I thing its RRCF.

Can someone help me in using this instruction to set the ADC to 7 bit please ?? i tried several times but its still not working. I think im using the RRCF instruction incorrectly...

Thanks
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top