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.
Thanks for the reply Ian Rogers. It is working with the instruction set you gave me, but the problem is that the last bit is being shifted to the first bit and i dont want that to happen when the last bit is 1. Is it possible to shift it and replace the last bit with 0 instaad of replacing it with the fist bit??

the reason why i want it to be like this is because if lets say in 8 bit mode, the value is 10000000, when shifted it will end up greater than the value in the 8 bit mode. (see attached pic)

Thanks.
 

Attachments

  • shift.png
    shift.png
    6.2 KB · Views: 155
Last edited:
In your attached picture you have the MSB and LSB mixed up but that's neither here nor there as long as you know which bit is what in the program...

0b11000000 = 192
0b10000001 = 129

is the conventional way.

0b11000000 = 192. rotate right...
0b01100000 = 96. rotate right...
0b00110000 = 48. rotate right...
0b00011000 = 24.

Everytime you shift the bits once to the right you halve the original number. If you are using 'RRNCF' then your carry bit (the LSB which gets shifted out) will not set your MSB. Perhaps paste your code so we can have a look. My assembly is a bit rusty but it still works ;)
 
Hi.

Thanks for the reply. Below is the coding. At the moment i am not using the ADC, just for testing. Instead in moving 00000111 (7dec) to REG1, shift it and output the result in Port D. The result is showing as 100000011 = 131dec. The problem is that the first bit is shifting to the last bit. Is there a way so to keep the last bit 0 ??

Thanks

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

REG1 EQU 60H

MOVLW B'00000000' ;DECLARE BITS AS output
MOVWF TRISD

;START MAIN PROGRAM HERE
MAIN

CLRF REG1

MOVLW B'00000111' 
MOVWF REG1
RRNCF REG1 
MOVFF REG1,PORTD
LINE1
GOTO LINE1

END_OF_PROGRAME
 
Last edited:
Ok i found a way for this problem :) after the 'RRNCF REG1' command i wrote 'BCF REG1,7'. That way if the shifted bit is 1 it will be cleared. Tomorrow i will put this coding in the actual code and will let you know if it worked fine . Thanks very much for your help guys ;)
 
Last edited:
Ok i found a way for this problem :) after the 'RRNCF REG1' command i wrote 'BCF REG1,7'. That way if the shifted bit is 1 is will be cleared. Tomorrow i will put this coding in the actual code and will let you know if it worked fine . Thanks very much for your help guys ;)

Nice I was going to suggest masking it but that works just the same :)
 
Hi,

The ADC seems to be working fine. I included the ADC coding to another program. The aim of the program is to give it a reference value (which is saved in register REF) and compare it with the A2D result. If greater, an LED will turn on at port c,2 otherwise it will turn off.

Now i have a problem with the compare routine:

Code:
;COMPARE ADC RESULT WITH REGISTER 'REF'
MOVFF ADRESH, F            
MOVLW REF            
CPFSGT ADRESH 
BRA LINE7
BRA LINE5
LINE7
BSF PORTC,2
GOTO MAIN
LINE5
BCF PORTC,2

It is not comparing the value of the REF register to the A2D value, but when i changed REF with a value it worked fine:
Code:
;COMPARE ADC RESULT WITH REGISTER 'REF'
MOVFF ADRESH, F            
MOVLW .40           
CPFSGT ADRESH 
BRA LINE7
BRA LINE5
LINE7
BSF PORTC,2
GOTO MAIN
LINE5
BCF PORTC,2

Can someone help me solve this problem please??
Below is the full coding:

Code:
;REGISTER DECLERATIONS

REG1 EQU 60H ; HERE WE ARE NAMING THE GENERAL PURPOUSE REGISTER
ADC EQU 61H
REF EQU 62H
REG2 EQU 63H
REG3 EQU 64H
REG4 EQU 65H
F EQU 66H


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

SETF PORTB ;DECLARE PORT B AS INTUP FOR THE REFERENCE SWITCHES
SETF TRISB

CLRF PORTC
CLRF TRISC


CLRF ADC ;CLEAR REGISTER ON START UP ONLY
CLRF PORTC
CLRF REF

;START MAIN PROGRAM HERE


MAIN

;TEST SWITCHES ... DECREMENT AND INCREMENT REFERENCE REGISTER (REF)

BTFSS PORTB,6
BRA LINE9
INCF REF
CLRF PORTD
MOVFF REF, PORTD
CALL DELAY

LINE9
BTFSS PORTB,7
BRA LINE8
DECF REF
CLRF PORTD
MOVFF REF, PORTD
CALL DELAY
LINE8

;ADC ROUTINE

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

RRNCF ADRESH  ;SHIFT RESULT TO THE RIGHT. THIS WAY IT WILL HAVE 7 BITS INSTEAD OF 8 BITS
BCF ADRESH,7  ;CLEAR THE LAST BIT OF REG1. THIS IS DONE SO THAT IF THE SHIFTED BIT IS 1, IT WILL BE CLEARED (0)

MOVFF ADRESH,PORTD ;MOVE VALUE FROM ADRESH REGISTER TO PORTD 


;COMPARE ADC RESULT WITH REGISTER 'REF'
MOVFF ADRESH , F            
MOVLW REF          
CPFSGT ADRESH 
BRA LINE7
BRA LINE5
LINE7
BSF PORTC,2
GOTO MAIN
LINE5
BCF PORTC,2

GOTO MAIN   

DELAY
CLRF REG2
CLRF REG3
MOVLW 30
MOVWF REG4
BSF PORTC,2
LINE1_DELAY
DECFSZ REG2
BRA LINE1_DELAY
DECFSZ REG3
BRA LINE1_DELAY
DECFSZ REG4
BRA LINE1_DELAY
RETURN

ACQ_DELAY
MOVLW REF
MOVWF REG1
ACQ1
DECFSZ REG1
BRA ACQ1
RETURN		


END_OF_PROGRAME

Thanks in advance :)
 
Last edited:
hi adrian,
It helps a lot if you can post the full program code, post it as a *.asm , using "Advanced Reply", "Manage Attachments"
 
Code:
Did notice this error
LIST P=18F4550
#include <P18F4455.INC>

Yes i also noticed that before attaching it, but its still not working.
 
Code:
Did notice this error
LIST P=18F4550
#include <P18F4455.INC>

Yes i also noticed that before attaching it, but its still not working.

I didn't suggest that it would be the cause of the problem, its just a typo that needed correcting.

In your asm file, you had cut out the compare instructions, so I have had to assume that the compare code in your previous posts, is the one you are debugging, so I have pasted that compare routine into your latest posting, will let you know how it goes.
 
Last edited:
Hi,

The compare coding is suppose to be in the coding attached. It is under ";COMPARE ADC RESULT WITH REGISTER 'REF'" routine.

Here is the compare code:
Code:
MOVFF ADRESH , F            
MOVLW REF          
CPFSGT ADRESH 
BRA LINE7
BRA LINE5
LINE7
BSF PORTC,2
GOTO MAIN
LINE5
BCF PORTC,2

Thanks ericgibbs.
 
Last edited:
adrian,
This code now works.

I have preloaded REG with '64 decimal', this is equal to 4.0Vin to the adc, adc counts 818.

Used PORTA1,2 as outputs to show the compare result.

Also simplified the Header for testing.
 

Attachments

  • A2D.ASM
    2 KB · Views: 126
  • AAesp01.gif
    AAesp01.gif
    30.5 KB · Views: 142
  • AAesp02.gif
    AAesp02.gif
    30.4 KB · Views: 161
Last edited:
Hi,

Thanks alot for the reply and coding, but isn't that a fixed reference (64 DEC)? or am i not understanding the coding?

The way i want it is to set the reference from 2 switches which are connected to port B, 6,7. When port b,6 is set (switch pressed) the ref value is decremented and when port b,7 is set (switch pressed) the ref value is incremented. the value is then saved in register REF. This can be seen in the coding i have attached.
Now is problem is when it comes to compare the reference value which is saved in register REF with the A2D value (which is saved in register ADRESH).

I will try to take a video of it working on the breadboard.
 
Last edited:
Hi, the link below is a video of the circuit.

https://youtu.be/EtsipdQFnNA

At the beginning of the video i am showing:
1) the potentiometer which vary the the analog input
2) a green led which is suppose to turn off when the reference value is exceeded
3) the digital output (last led not being used since the ADC was set to 7 bit). Note that these leds show the ADC result but when the reference switches are pressed, the reference value is shown for few seconds.
4) two switched which are used to increment and decrement the reference value

After then i an varying the pot and the digital output is changing accordingly. Now notice that the green led is turning off when digital output = 98 dec (2+32+64), but this led is suppose to turn of only when the reference value is exceeded and not when value 98 dec is exceeded.

After that i an showing the switches being pressed which are changing the reference value. when both switches are depressed, the digital output leds will show the ADC result again.

Everything is working fine except when it comes to compare the two values (i.e. reference value with the A2D value), since the green led is not turning off. Instead it is turning off when 98 dec is exceeded.
 
hi
I have watched your video OK,

The problem I thought you had was in the compare, thats why I changed the earlier code.

The point you should consider is, when you have read the high byte of only the ADRESH of the adc registers
its already divided by 4, so a max count of 1023 would be now 255, you are then dividing by 2, so thats 127 max equals 5Vinp.

Is this what you really want to do, let 0 to 127 represent 0 to +5Vin.???

Post your latest asm, with comments and I will run it thru Oshonsoft Sim,
 
Last edited:
Hi,

I think im not explaining myself correctly.

Is this what you really want to do, let 0 to 127 represent 0 to +5Vin.???

That was done and it worked.
The problem is with the green led which should turn off when the value in register ADRESH exceed the value in register REF. But as seen in the video this is not happening, instead the green led is turning off at value 98 DEC.
 
Hi,

I think im not explaining myself correctly.



That was done and it worked.
The problem is with the green led which should turn off when the value in register ADRESH exceed the value in register REF. But as seen in the video this is not happening, instead the green led is turning off at value 98 DEC.

hi,
OK,no problem.
Please post your full asm I will help debug it with the Sim.
 
Hi ericgibbs,
Attached is the .asm file of this program. I have included comments to the coding.

Thanks alot for your help.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top