Delays and writing to ports (What am I seeing here?)

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello AVR Freaks,

  I've recently been porting a larger arduino project to atmel studio. It needed some fine tuning and optimization.

  I'm working on driving an external multiplexer at the moment, and I'm doing some LED test on the counter first. 
  The actual code will be using the ADC interrupt as the source for incrementing the muxCounter variable, but for now I was just making sure I'm shifting into the right place.
  I've got 3 LEDs hooked up to my Arduino Mega R3, on PL 7,6, and 5. 

  

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

uint8_t count = 0;

int main(void)
{
	
	DDRL = 0B11100000; //declare the last 3 pins on the L bus as outputs
	
	
	
    while (1) 
    {
		//this if is un-necessary
		//we can just let this number overflow, and go back into 0. 
		//if(count>8)
		//{
		//	count = 0;
		//}
		PINL = (count << 4);
		_delay_ms(1000);
		count++;
    }
}

  The problem that I was not expecting to see, is that after every increment of my count variable, the LED's go blank. 
  This program counts fine, but only every other second. 
  I'm not sure if this is happening because of the delay, but if it does happen in the ADC_VECT interrupt, we'll have a problem. 
  I'm not really sure what could be causing it. 
  Is a "feature" of _delay_ms() that it turns all pins that were high low?
  I might post up a video if this is abnormal behavior.

  Thanks!

This topic has a solution.
Last Edited: Sat. Jun 29, 2019 - 08:49 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

halfordC wrote:

		PINL = (count << 4);

You mean:

		PORTL = (count << 4);

 

Writing 1 bits to to PINx will toggle the corresponding bits in PORTx, effectively XORing, so:

 

count  |  count << 4   |  PORT
-------|---------------|----------
 ----  |   ---- ----   | 0000 0000
 0000  |   0000 0000   | 0000 0000
 0001  |   0001 0000   | 0001 0000
 0010  |   0010 0000   | 0011 0000
 0011  |   0011 0000   | 0000 0000
 0100  |   0100 0000   | 0100 0000
 0101  |   0101 0000   | 0001 0000
 0110  |   0110 0000   | 0111 0000
 0111  |   0111 0000   | 0000 0000
 1000  |   1000 0000   | 1000 0000
 1001  |   1001 0000   | 0001 0000
 1010  |   1010 0000   | 1011 0000
 1011  |   1011 0000   | 0000 0000
 1100  |   1100 0000   | 1100 0000
 1101  |   1101 0000   | 0001 0000
 1110  |   1110 0000   | 1111 0000
 1111  |   1111 0000   | 0000 0000

You're only using the three MSB for LEDs, so the pattern you'll see is:

count  |  count << 4   |  LEDS
-------|---------------|----------
 ----  |   ---- ----   | 000
 0000  |   0000 0000   | 000
 0001  |   0001 0000   | 000
 0010  |   0010 0000   | 001
 0011  |   0011 0000   | 000
 0100  |   0100 0000   | 010
 0101  |   0101 0000   | 000
 0110  |   0110 0000   | 011
 0111  |   0111 0000   | 000
 1000  |   1000 0000   | 100
 1001  |   1001 0000   | 000
 1010  |   1010 0000   | 101
 1011  |   1011 0000   | 000
 1100  |   1100 0000   | 110
 1101  |   1101 0000   | 000
 1110  |   1110 0000   | 111
 1111  |   1111 0000   | 000

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
		PINL = (count << 4);

You want PORTL.  And perhaps "<< 5"

Writing to PINL will toggle the bits, and since you're writing the same bits twice (because of 4 vs 5), you'll turn off the bits you set in the previous loop.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

haha, the easy things you miss when programming while sleepy.

Thanks both of you, everything is working great now. 
Should I delete this thread, or tag it as solved?
I couldn't find any rules sticky on a quick perusing of these boards.

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tag it as solved - pick either post (Joey was first so I guess his) and [Mark as Solution]

 

Moderator