closed

Loginbox

Please enter your username and password into the following fields to log in.


  • Username:
  • Password:
  •  
  • Auto log in on every visit.


  •  

Problems with ADC?



Merlin offline
Schreiberling
Avatar
Gender: male
Location: UNITED KINGDOM 
Age:
Posts: 842
Registered: 03 / 2005
Private message
Subject: Problems with ADC?  -  Posted: 19.11.2018 - 13:07   -  
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?
Merlin.

:magic:

Software is a black art.
This post has been edited 1-times. Last edit: 19.11.2018 - 13:08 by Merlin.
go down go up
rh offline
Administrator
Avatar
Gender: male
Location: GERMANY  Germany
Age:
Posts: 5378
Registered: 03 / 2002
Homepage Private message
Subject: Re: Problems with ADC?  -  Posted: 19.11.2018 - 15:14   -  
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
Cars don't buy cars -- Autos kaufen keine Autos.
Henry Ford
go down go up
Merlin offline
Schreiberling
Avatar
Gender: male
Location: UNITED KINGDOM 
Age:
Posts: 842
Registered: 03 / 2005
Private message
Subject: Re: Problems with ADC?  -  Posted: 19.11.2018 - 15:28   -  
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
Merlin.

:magic:

Software is a black art.
go down go up
 


Registered users in this topic
Currently no registered users in this section

Delete cookies of this forum  •  FAQ / Help  •  Team page  •  Imprint   |  Local time: 10.12.2018 - 18:54