Problems with ADC?

Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1372
Registered: 03 / 2005
Subject:

Problems with ADC?

 · 
Posted: 19.11.2018 - 13:07  ·  #1
I am experiencing a very strange problem, and I wonder if anyone has a similar experience.

In a application I am using ADC input and port pin outputs on the same port (Port C on a MEGA328).

I have a boolean function, let us call it test, and a pin defined thus

Code
  MyOut    [@PortC, 1] : bit;


Now if I set

Code
MyOut := not Test;


the processor misbehaves, consistent with it resetting (I do not have watchdog or brownout detect) but I cannot say for sure that that is what is happening.

However, if I put

Code

if Test then
  MyOut := FALSE;
else
  MyOut := TRUE;
endif;


everything behaves as it should.

Looking at the assembler, after optimisation timings should be about the same. The difference between these two pieces of code is that the one that does not work uses read-modify-write of the C port (IN and OUT), whereas the one that does work uses direct port pin manipulation (CBI and SBI). But I honestly don't see why that would make a difference unless ADC detection gets upset, and I don't see why it should.

There is nothing logically wrong with the code generated as far as I can see.

Has anyone experienced anything similar?
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Problems with ADC?

 · 
Posted: 19.11.2018 - 15:14  ·  #2
Hello Merlin
in the past I found several Infos about ADC Problems with some AVRs when ADC Ports are mixed with digital IOs (AVRfreaks).
Btw
The SBI/CBI are internal atomic Instructions.
Maybe you can prevent the avrco rmw by enclosing it in a disable interrupt
rolf
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1372
Registered: 03 / 2005
Subject:

Re: Problems with ADC?

 · 
Posted: 19.11.2018 - 15:28  ·  #3
Thanks for info, Rolf. That gives me confidence.

I don't need to worry about read-modify-write. I am happy with atomic instructions. I know how to generate them so that is fine. I thought about disable interrupts, but felt that properly documented the if-then-else construct was cleaner and shorter (in asm).

In fact I tend to use this type of construct in interrupt routines anyway, because although the source is longer, the optimised asm is shorter and runs almost 3 x as fast in my case!

Thanks again
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   64   78 · Page-Gen-Time: 0.022875s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI