Problem with manipulation of IO ports in external file

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

Hello everyone.

I have made an external file to make all my port manipulations, in an ATTINY85 mcu, and it does't seem to work while if I put the code in the main it does.

I am posting the code I am testing with other parts excluded for ease of reading.

My main file is 

#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "setUp.h"
#include "inPuts.h"

#define tap (1<<PB3)
#define divSel (1<<PB4)
#define beatSel (1<<PB2)
#define  beat2 (1<<PB1)
#define  beat4 (1<<PB0)

int main(void)
{
	setUp();

	while (1)
	{
		inPuts();

	}

Setup.h
 

#include <avr/io.h>

#ifndef SETUP_H_
#define SETUP_H_

void setUp(void);

#endif /* SETUP_H_ */

setUp.c

#include <avr/io.h>
#include <avr/interrupt.h>

#define tap (1<<PB3)
#define divSel (1<<PB4)
#define beatSel (1<<PB2)
#define  beat2 (1<<PB1)
#define  beat4 (1<<PB0)

void setUp(){

	//inputs-outputs
	DDRB|=beatSel | beat4 | beat2;						//outputs
	DDRB &= ~(divSel) & ~(tap);						// inputs

	PORTB |= divSel | tap;							//pullup resistors inputs

}

inPuts.h

include <avr/io.h>
#include <avr/interrupt.h>

#ifndef INPUTS_H_
#define INPUTS_H_

void inPuts(void);

#endif /* INPUTS_H_ */

inPuts.c

#define F_CPU 8000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "setUp.h"
#include "inPuts.h"

#define tap (1<<PB5)
#define divSel (1<<PB3)
#define beatSel (1<<PB4)
#define  beat2 (1<<PB2)
#define  beat4 (1<<PB1)
#define beat8 (1<<PB0)

void inPuts (void){
    PORTB^=beatSel;
}

Thanks in advace

This topic has a solution.
Last Edited: Sun. Dec 10, 2017 - 06:31 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is beatSel PB2 or PB4? You use both in your code.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

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

Anytime you see the same thing repeated multiple times it screams out that you should do things once and correctly. Combine those three copies of #define s into one header file. By convention, #defined names are upper case.

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

Brian Fairchild wrote:

Is beatSel PB2 or PB4? You use both in your code.

 

pffff.... You are right... Probably that is the mistake.... I have read my code so many times and still didn't see that.

 

Last Edited: Wed. Dec 6, 2017 - 08:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Anytime you see the same thing repeated multiple times it screams out that you should do things once and correctly. Combine those three copies of #define s into one header file. By convention, #defined names are upper case.

Thank you Kartman for the advice. I was hoping for that kind of info since I am a little new to this kind of multiple file programming.

Last Edited: Wed. Dec 6, 2017 - 08:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dataPanik wrote:
 this kind of multiple file programming.

as opposed to what other kind(s) of multiple file programming?

 

Note that this is standard 'C' stuff - not specific to Atmel or AVR.

 

You might take a look at this - in the Tutorials section:

 

[TUT] Modularizing C Code: Managing large projects

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

awneil wrote:

dataPanik wrote:

 this kind of multiple file programming.

 

 

as opposed to what other kind(s) of multiple file programming?

 

Note that this is standard 'C' stuff - not specific to Atmel or AVR.

 

You might take a look at this - in the Tutorials section:

 

[TUT] Modularizing C Code: Managing large projects

Yes I mean this kind of programming with multiple files, which is something new to me. I am more used to classic infinite loop style with everything dropped there.

I have seen this tutorial but haven't found the time to study it thoroughly yet.

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

Tell us more about what:

dataPanik wrote:
it does't seem to work
actually means.

 

Oh and how are you building this code? Is this in an IDE like Atmel Studio 7 or is it some "homemade" building system ?

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

clawson wrote:

Tell us more about what:

 

dataPanik wrote:

it does't seem to work

 

actually means.

 

Oh and how are you building this code? Is this in an IDE like Atmel Studio 7 or is it some "homemade" building system ?

It is Atmel Studio 7. It means PORT^= bitmask; does't toggle the specified output pin. 

I think the first answer will fix my problem combined with the header file including all the #define s will save me the next time from such mistakes.

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

Run the code in the AS7 simulator. Does execution reach inPuts() ?

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

dataPanik wrote:
 I am more used to classic infinite loop style with everything dropped there.

The "infinite loop" style has nothing to do with organising your project across multiple files!

 

Note that it makes things a whole lot clearer if you just quote the part you're specifically replying to - not the entire post!

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

awneil wrote:

Note that it makes things a whole lot clearer if you just quote the part you're specifically replying to - not the entire post!

Got it

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

awneil wrote:
dataPanik wrote:
this kind of multiple file programming.
 

as opposed to what other kind(s) of multiple file programming?   Note that this is standard 'C' stuff - not specific to Atmel or AVR.

Come on, Andy.. Of-course it's "as opposed to single file programming".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Wed. Dec 6, 2017 - 03:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to close the topic. The problem was in different definition between files. As it was pointed here I made a header file containing all my #define s and everything works as it is supposed to.

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

Excellent - thanks for reporting back.

 

Please mark the solution - instructions here: http://www.avrfreaks.net/comment...

 

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

Brian Fairchild wrote:

Is beatSel PB2 or PB4? You use both in your code.

So my main problem was using different #define s in different files. 

 

Kartman wrote:

 Combine those three copies of #define s into one header file.

If I had used from the beginning this strategy things would had never gone the wrong way.

Thanks everyone.