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.

newbie in using PIC16F877

Status
Not open for further replies.
Looking at your earlier post you need to left justify and use the top 3 bits of adresh.
Code:
main:
    [COLOR="blue"]ADCON1.7 = 0	'left justify[/COLOR] 
    ADCON0.2 = 1	'start A/D conversion
    while(ADCON0.2=1)	'wait for it to complete
    wend
    output1 = adres[COLOR="blue"]h[/COLOR] / 32
    WaitMs 100
    Goto main

A better way to write this is,
Code:
main:
    ADCON1.7 = 0	'left justify
    ADCON0.2 = 1	'start A/D conversion
    while(ADCON0.2=1)	'wait for it to complete
    wend
    output1 = adresh[COLOR="Blue"]>>5[/COLOR]
    WaitMs 100
    Goto main

Mike.
 
tnx for the help pommie. this is the result when i tried your program
Code:
INPUT            EXPECTED OUTPUT                           ACTUAL OUTPUT
0V		0	0	0			0	0	0
0.5V		0	0	1			0	0	0
1V		0	1	0			0	0	1
1.5V		0	1	1			0	1	0
2V		1	0	0			0	1	1
2.5V		1	0	1			0	1	1
3V		1	1	0			1	0	0
3.5V		1	1	1			1	0	1

i followed the program and loaded it in the pic and simulate it.
 
Last edited:
To get full output with 3.5V input you need to make Vref equal to 3.5V as well.

Or, you can do it in code,
Code:
Dim temp as word

main:
    ADCON1.7 = 0	'left justify 
    ADCON0.2 = 1	'start A/D conversion
    while(ADCON0.2=1)	'wait for it to complete
    wend
    temp=adresh
    temp=temp*7		'2*3.5
    temp=temp/10	'2*5    = adresh*3.5/5
    output1 = temp / 32
    WaitMs 100
    Goto main
Note, due to using integer maths we can't do *3.5/5 and so we do *7/10.

Mike.
 
i'm getting hard time to follow the commands. why multiply temp by 7 and divide it by 10? Vref+ = 3.5V and Vref-=0V???
 
I'm guessing that you have 5V as Vref and so to get the maximum ADC value with only 3.5V in you need to multiply the ADC value by 5/3.5 (woops, got that wrong way around above).

Try,
Code:
Dim temp as word

main:
    ADCON1.7 = 0	'left justify 
    ADCON0.2 = 1	'start A/D conversion
    while(ADCON0.2=1)	'wait for it to complete
    wend
    temp=adresh
    temp=temp*10	'2*5
    temp=temp/7		'2*3.5    = adresh*5/3.5
    output1 = temp / 32
    WaitMs 100
    Goto main

Mike.
 
hi Mike,
If the OP is using picsim ide Basic [oshonsoft???] then if he:
DIM output1 as BYTE then the Basic sets ADC as left justify, 8 bit only.
DIM output1 as WORD its right justified full 10 bit.
 
Looking at your diagram, the voltmeter should not go from the potentiometer to the input pin. Remove the voltmeter and connect the potentiometer slider to the pic pin. If you want to see the voltage then connect a voltmeter from the slider/pic pin to ground.

Mike.
 
Looking at your diagram, the voltmeter should not go from the potentiometer to the input pin. Remove the voltmeter and connect the potentiometer slider to the pic pin. If you want to see the voltage then connect a voltmeter from the slider/pic pin to ground.

Mike.

FINALLY! thanks for the HELP :D i see, i guess i need to go back to basic electronics! :(

btw, can you explain what this line do?

temp=temp*10 '2*5
temp=temp/7 '2*3.5= adresh*5/3.5

i dont get this part of the code...
 
FINALLY! thanks for the HELP :D i see, i guess i need to go back to basic electronics! :(

btw, can you explain what this line do?

temp=temp*10 '2*5
temp=temp/7 '2*3.5= adresh*5/3.5

i dont get this part of the code...

That code is doing temp=temp/0.7 (or temp=temp*1.44) but as we are using integer maths the way we do it is to multiply by 10 and then divide by 7. Now your diagram is corrected you wont need this part.

Mike.
 
Last edited:
That code is doing temp=temp/0.7 (or temp=temp*1.44) but as we are using integer maths the way we do it is to multiply by 10 and then divide by 7. Now your diagram is corrected you wont need this part.

Mike.

i see, but how do you come up 0.7 to divide it? btw, what do you mean by 'you wont need this part' coz when i omitted the line the output is not the same. since it was program for 3bits which is divided by 32, meaning to say i can divide it with 16 to obtain 4bits of output?

btw, is there a thread here regarding on how to use the timer module of 877 using basic? 'coz i would like to try using its timer by this simple operation:

1. start timer running
2. wait for input (e.g RA0 = 1)
3. store the value of the timer when RA0 is low while continuing the time
4. store another value of the time when RA0 is pressed again
5. compare the the 2 values and compute for the difference.

or any sample program that uses timer module of 877 that uses basic will do.. i got nose bleed when reading the datasheet.. :(


thnx :)
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top