PDA

View Full Version : Good ol' 877



Ioannis
- 8th February 2018, 15:30
Good ol' 16F877 but getting on my nerves...

Why this does work:


ADCON0 = %11000001 ' Set RC clock and enable ADC
ADCON1 = %00001110 ' Set PORTA.0 to analog,left justify, rest digital
TRISA = %00000101 ' RA0:Analog input 0
adcin 0,level


and this does NOT?


ADCON0 = %11000001 ' Set RC clock and enable ADC
ADCON1 = %00001110 ' Set PORTA.0 to analog,left justify, rest digital
TRISA = %00000101 ' RA0:Analog input 0
high adcon0.2 ' start conversion
while adcon0.2:wend ' wait to finish
level=adresh ' get 8-bit value to level variable


Ioannis

pedja089
- 8th February 2018, 15:42
I'm not 100% sure but HIGH set 2 registers, tris and port. Who know what is happening when you try to set tris of ADCON0...
HIGH and LOW should be used only with PIN on uC.
You should look at disassembly to know what is going on.
I would try this:

ADCON0 = %11000001 ' Set RC clock and enable ADC
ADCON1 = %00001110 ' Set PORTA.0 to analog,left justify, rest digital
TRISA = %00000101 ' RA0:Analog input 0
adcon0.2=1 ' start conversion
while adcon0.2:wend ' wait to finish
level=adresh

picster
- 8th February 2018, 20:47
Not sure, but I seem to recall having an issue with putting if/then/else/endif on the same line. Might be a syntax thing where you need to put wend on its own?

Worth a shot?

Picster

Ioannis
- 8th February 2018, 23:16
Well, the adcon0.2=1 was the solution!

I guess last night I was not very wake to see it.

While/Wend can be just fine on the same line, there is no problem.

Thank you for the help,
Ioannis

mpgmike
- 9th February 2018, 02:55
Not sure, but I seem to recall having an issue with putting if/then/else/endif on the same line.

Picster
Adding a colon, the compiler treats the next line as a new line. For example:


IF Box = 4 THEN : GOSUB Square : ELSEIF Triangle = 3 THEN : LOW LED : ENDIF

Would be the same as:


IF Box = 4 THEN
GOSUB Square
ELSEIF Triangle = 3 THEN
LOW LED
ENDIF

HenrikOlsson
- 9th February 2018, 07:09
HIGH/LOW are really only meant to be used on PORT registers.
Those commands also clear the corresponding TRIS bit which is at certain offset from the register (PORT) in question. That offset is 80h (at least for the '877, don't know if other parts/familes might be different).

So when you do HIGH ADCON0.2 the compiler generates code to also clear bit 2 at the memory addess 80h "away" from ADCON0 which in this case happens to be ADCON1 and unexpected things may occur.

Ioannis
- 9th February 2018, 08:49
Very good explanation Henrik. Thank you.

Ioannis

richard
- 9th February 2018, 09:03
from the user manual

The HIGH command may only be used on output pins. If used on bits within
variables or bits within registers (other than PORT or GPIO), unexpected behavior
may result.

Ioannis
- 9th February 2018, 11:07
Who reads the manual...? :o

Ioannis

richard
- 9th February 2018, 11:36
its best to wait till all else fails :D