Defining port ?

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

Guys,

I have a question on defining port, please correct me if I'm wrong...

#define VS1003B_PORT    PORTF
#define VS1003B_DDR      DDRF
#define VS1003B_PIN      PINF

#define VS1003B_XCS     7
#define VS1003B_XRESET  6
#define VS1003B_DREQ    5
#define VS1003B_XDCS    4

#define PORT_INI()        VS1003B_DDR = (1<<VS1003B_XCS) | (1<<VS1003B_XRESET) |(1<<VS1003B_XDCS) | (0<<VS1003B_DREQ)

   loop_until_bit_is_set(VS1003B_PIN,VS1003B_DREQ);

I want VS1003B_XCS,VS1003B_XRESET,VS1003B_XDCS = output
and VS1003B_DREQ = input,

am I right defining like this :

#define PORT_INI()        VS1003B_DDR = (1<<VS1003B_XCS) | (1<<VS1003B_XRESET) |(1<<VS1003B_XDCS) | (0<<VS1003B_DREQ)

or

#define PORT_INI()        VS1003B_DDR = 0xDF

is better ?

I tried to check

loop_until_bit_is_set(VS1003B_PIN,VS1003B_DREQ);

but never work, even I put 5V on pin 5 as a test for H condition, do I miss something here ?

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void PORT_INI()       
{ VS1003B_DDR = (1<<VS1003B_XCS) | (1<<VS1003B_XRESET) | (0<<VS1003B_DREQ)|(1<<VS1003B_XDCS) ;
}

or

void PORT_INI()       
{ VS1003B_DDR = 0xDF ;
} 

Both untested

EDIT: You would have to call either from your MAIN as I did these as chip initializations.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

I agree with Jim. Why hide this stuff in pre-processor macros? Why not C callable functions? (only don't give them all upper case names as most people expect that only for macros).

If you are concerned about the CALL/RET overhead of using functions and that's why you are using #define then just arrange for the functions to be inlined. By default the optimisation probably already calls for -finline-small-functions anyway but if you want to be absolutely sure then:

__attribute__((always_inline))
void Port_init(void) { 
  VS1003B_DDR = (1<<VS1003B_XCS) | (1<<VS1003B_XRESET) | (0<<VS1003B_DREQ)|(1<<VS1003B_XDCS) ;
} 

As to the question as to whether this is the "right version" or simply setting 0xDF. Pretend you don't know this code (or you are coming back to it after a year). Which of the two versions helps you understand more about what is being configured? (if you said the anonymous 0xDF version I'm afraid that's the wrong answer!).

Oh and unless you program in C++ never do what Jim did:

void PORT_INI() 

it should always be:

void PORT_INI(void) 

(but then my further points about name case and so on).

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

OOPS!! :oops:

Good thing I put the disclaimer in.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Jim / Bianchi,

FYI the reason to use (void) is shown by the following example:

uint16_t read_adc1() {
  return ADC;
}

uint16_t read_adc2(void) {
  return ADC;
}

int main(void) {
  OCR1A = read_adc1(3);
  OCR1A = read_adc2(3);
}
		.././test.c: In function 'main':
C:\Documents and Settings\asl\My Documents\Atmel Studio\test\test\test.c(15,2): too many arguments to function 'read_adc2'
		  OCR1A = read_adc2(3);
		  ^
C:\Documents and Settings\asl\My Documents\Atmel Studio\test\test\test.c(9,10): declared here
		 uint16_t read_adc2(void) {
		          ^

read_adc2() causes an error while read_adc1() goes by unnoticed.

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

I was in a rush Cliff, but yes I see what you mean. My bad for not remembering.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

I'm guilty of leaving out the void myself. I'm too used to programming in C++. Even worse, lately I have been doing a lot of Actionscript. With that you can leave out parameters or add extra ones and the compiler doesn't care one bit. About the only thing that really matters is that the parameters you do provide match the types that the signature specifies. But then it does not care that the value that you pass is of the correct type. If you have an anonymous variable that is physically a string and pass it to a function that takes a number, nothing complains. And once it gets into the function you have a 50/50 chance that it will be treated as a string. I have had many instances of "1 + 1 = 11".

Regards,
Steve A.

The Board helps those that help themselves.