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.

PIC ADC

Status
Not open for further replies.
In my PIC ADC part of the program (8 bits), i want to check the result, if it is more than 0000 0100 then it will send instruction1. If it is more than 0000 1000 then it will send instruction2 and so on.

I have no idea how to do this in PIC assembly language because there is no IF command or arithmetic operators. How do i get around doing this?
 
Spectacular Butter said:
In my PIC ADC part of the program (8 bits), i want to check the result, if it is more than 0000 0100 then it will send instruction1. If it is more than 0000 1000 then it will send instruction2 and so on.

I have no idea how to do this in PIC assembly language because there is no IF command or arithmetic operators. How do i get around doing this?

By subtraction, and checking of the results flags - check the PICList for an entire section about comparisons.
 
From what you have said u are interested in comapring two values. So in ur case i think u can start from the max value.

Say for example your 8bit data from the ADC is stored in a register called "DATA". then


MOVLW b'10000000
ANDWF DATA, W
BTFSS STATUS, Z
CALL sub routine 1 ; calls ur sub routine1 if the data is in the
range 1xxx xxxx

MOVLW b'01000000
ANDWF DATA, W
BTFSS STATUS, Z
CALL sub routine 2 ; calls ur sub routine2 if the data is in the
range 01xx xxxx

MOVLW b'00100000
ANDWF DATA, W
BTFSS STATUS, Z
CALL sub routine 3 ; calls ur sub routine3 if the data is in
the range 001x xxxx
.
.
.
.

MOVLW b'00000001
ANDWF DATA, W
BTFSS STATUS, Z
CALL sub routine 8 ; calls ur sub routine8 if the data is
equal to 0000 0001
CALL sub routine 9 ; calls ur sub routine9 if the data is
equal to 0000 0000


Try out
 
Do i need to place a current limiting capacitor at the AD input?

I was testing my ADC by using a potentiometer between 5v and analog input of PIC. The program seems to work at first but after awhile i saw smoke coming from the PIC.

Then i placed a 390 ohm resistor between the potentiometer and analog input and everything seems ok. I still dunno what was the problem but luckily the PIC is still working.

Does that mean i can't feed a 5v directly (when potentiometer is 0 ohm) to an analog input without using resistor? According to a diagram i saw online, there is no such resistor.
 
Spectacular Butter said:
Do i need to place a current limiting capacitor at the AD input?

I was testing my ADC by using a potentiometer between 5v and analog input of PIC. The program seems to work at first but after awhile i saw smoke coming from the PIC.

Then i placed a 390 ohm resistor between the potentiometer and analog input and everything seems ok. I still dunno what was the problem but luckily the PIC is still working.

Does that mean i can't feed a 5v directly (when potentiometer is 0 ohm) to an analog input without using resistor? According to a diagram i saw online, there is no such resistor.

You CAN feed from 0V to 5V directly into a PIC pin, but it's not really recommended - for example if the port is accidently set as an output it could short the pin to either supply rail. As well, if it should exceed the 5V rail, or drop below the 0V line, for any reason the PIC protection diodes will conduct to try and protect the chip - without a series resistor there's no current limiting and it's likely to damage the PIC (or the external circuitry).

Check my tutorials to see how I use the analogue inputs!.

Incidently, the previous post by 'marxmax' doesn't do what you were asking, it simply checks each bit in turn. You need to use either SUBLW or XORLW (or the WF varients) to check for what you want.
 
Maybe i am not correct, i am quite new in this.

I thought he want to check whether the value are in between 0 to 2, 2 to 4, 4 to 8, ..... 64 to 128. But if he want to check within any other rage of values this code is not going to help.

By the way i have gone through your PIC tutorials previously while searching through google, but did not visit this forum. It helped me a lot in A/D conversion and serial communication when i build an interface for PDA to monitor ECG waveform.
 
Spectacular Butter said:
Do i need to place a current limiting capacitor at the AD input?

I was testing my ADC by using a potentiometer between 5v and analog input of PIC. The program seems to work at first but after awhile i saw smoke coming from the PIC.

Then i placed a 390 ohm resistor between the potentiometer and analog input and everything seems ok. I still dunno what was the problem but luckily the PIC is still working.

Does that mean i can't feed a 5v directly (when potentiometer is 0 ohm) to an analog input without using resistor? According to a diagram i saw online, there is no such resistor.

There is no problem running a PIC straight to 0 to Vdd. You would not use a current limiting resistor, it is unnecessary and may even decrease the speed/accuracy of the ADC if your pot's resistance goes too high in the midrange.

You generally don't want to add resistors because you're afraid of a port direction getting screwed up. Otherwise, you'd be adding resistors to every digital input pin too.

And even shorting a pin defined as an output to the other voltage would be hard pressed to cause smoke. PICs are fairly well tolerant of output shorts.

Are you sure you did not hook that pot up to 12v or something above Vdd? Anything much over 5v will let the magic smoke out quite consistently.
 
Oznog said:
Spectacular Butter said:
Do i need to place a current limiting capacitor at the AD input?

I was testing my ADC by using a potentiometer between 5v and analog input of PIC. The program seems to work at first but after awhile i saw smoke coming from the PIC.

Then i placed a 390 <a href="#">ohm</a> resistor between the potentiometer and analog input and everything seems ok. I still dunno what was the problem but luckily the PIC is still working.

Does that mean i can't feed a 5v directly (when potentiometer is 0 <a href="#">ohm</a>) to an analog input without using resistor? According to a diagram i saw online, there is no such resistor.

There is no problem running a PIC straight to 0 to Vdd. You would not use a current limiting resistor, it is unnecessary and may even decrease the speed/accuracy of the ADC if your pot's resistance goes too high in the midrange.

I would disagree, depending on the exact input circuit used!. Often you would be feeding the input from an opamp, if the feed from the opamp has the capability of exceeding Vdd or dropping below Vss, you SHOULD add a current limiting resistor. The maximum value for this is specified in the datasheet, and is usually about 2.5Kohms.

The only real effect of too large a value is to slow the reading time, due to the time taken for the sample and hold capacitor to charge. If you don't wait long enough, it will obviously affect accuracy, as the capacitor won't have charged to the final voltage. This is mostly a concern when switching between different inputs, if using just one input the capacitor is already charged to something like it's next reading.
 
dun worry i found a way to do it already.

process1
btfsc RESULT,7 ; skip next instruction if MSB is 0
retlw 0x2F ; return with instruction to move forward with speed 10
btfsc RESULT,6 ; skip next instruction if bit is 0
retlw 0x2B ; return with instruction to move forward with speed 6
btfsc RESULT,5 ; skip next instruction if bit is 0
retlw 0x28 ; return with instruction to move forward with speed 3
btfsc RESULT,4 ; skip next instruction if bit is 0
retlw 0x30 ; return with instruction to brake
btfsc RESULT,3 ; skip next instruction if bit is 0
retlw 0x30 ; return with instruction to brake
btfsc RESULT,2 ; skip next instruction if bit is 0
retlw 0x3A ; return with instruction to reverse with speed 5
retlw 0x3A ; return with instruction to reverse with speed 5

When i turn the pot to one side, it will go forward with full speed. Then when i turn to the other side, it will reverse.
 
Spectacular Butter said:
dun worry i found a way to do it already.

process1
btfsc RESULT,7 ; skip next instruction if MSB is 0
retlw 0x2F ; return with instruction to move forward with speed 10
btfsc RESULT,6 ; skip next instruction if bit is 0
retlw 0x2B ; return with instruction to move forward with speed 6
btfsc RESULT,5 ; skip next instruction if bit is 0
retlw 0x28 ; return with instruction to move forward with speed 3
btfsc RESULT,4 ; skip next instruction if bit is 0
retlw 0x30 ; return with instruction to brake
btfsc RESULT,3 ; skip next instruction if bit is 0
retlw 0x30 ; return with instruction to brake
btfsc RESULT,2 ; skip next instruction if bit is 0
retlw 0x3A ; return with instruction to reverse with speed 5
retlw 0x3A ; return with instruction to reverse with speed 5

When i turn the pot to one side, it will go forward with full speed. Then when i turn to the other side, it will reverse.

That's essentially the same as the version posted by 'marxmax', and has the same drawbacks - but if that's all you require, fair enough!.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top