creating header files

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

This is the code that i wrote for Adc with two functions Adc_init() and Adc_read().
This file is to be named 'adc.c'.I would like to know how i can create a header file 'adc.h' and for that how should i define these two functions inside it.

#include 
void Adc_init(void)
{
	ADMUX|=(1<<REFS0)|(1<<MUX0)|(1<<ADLAR);//adc1 seleceted with avcc as reference
	ADCSRA|=(1<<ADEN)|(1<<ADSC)|(1<<ADPS2);//prescale by 16

}

int Adc_read()
{int_8 adc_val;
	adc_val=ADCH;
   return(adc_val);
}

*** ***

*********************

the more is see,less i know

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void Adc_init();
int Adc_read();

Sid

Life... is a state of mind

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

so it will be like

Quote:

#if
void Adc_init();
int Adc_read();
#endif

My doubt is that what kind of 'type cast' must be given to read adc values.As it will be sensitive to the cast given in c language.

*** ***

*********************

the more is see,less i know

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

Type casts are only required when it wont be clear to the compiler how to make a type conversion.

However C already has implicit rules (integer promotion) that will automatically handle such conversion.

For example, as written above your read function reads ADCH which is uint8_t but returns int. We could question why the function itself doesn't also return uint8_t (nothing is really added by making it int, it will still be 0..255) but anyway the unsigned char (uint8_t) will automatically be promoted to int without requiring a type cast.

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

arun_nr wrote:
so it will be like
Quote:

#if
void Adc_init();
int Adc_read();
#endif

What exactly do you mean by the empty #if?

Normally it is a good idea to include a condition to protect the header from multiple inclusion.
Something like:

#ifndef _ADC_H__INCLUDED_
  #define _ADC_H__INCLUDED_

  void Adc_init();
  int Adc_read(); 

#endif

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

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

In the cse at hand, static inline fucntions in the header file are probably the way to go.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int Adc_read() 
{int_8 adc_val; 
   adc_val=ADCH; 
   return(adc_val); 
} 

No need for the intermediate variable here. Simply use:

int Adc_read() 
{
   return ADCH; 
} 

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:

int Adc_read() 
{int_8 adc_val; 
   adc_val=ADCH; 
   return(adc_val); 
} 

No need for the intermediate variable here. Simply use:

int Adc_read() 
{
   return ADCH; 
} 

I would expect that 'int_8' is a signed type, and then there is a difference. His version does a sign extension, yours not. If the sign extension was intended, then the direct return must look like this:

return (int8_t)ADCH;

Stefan Ernst

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

Hi

i am having a similar problem by using files at the same time.
I am using main.c adc.c and adc.h
i am not able to compile this as it shows
compiler avr-gcc
the file has been added to Pwm.rar.I trust including the header file is the problem.

undefined reference to Adc_init() and Adc_read

[u]

Attachment(s): 

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

To be strict:

void Adc_init (void); 
int Adc_read (void);

avrfreaks does not support Opera. Profile inactive.

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

Quote:
I trust including the header file is the problem.
No, the problem is that you have not added adc.c to makefile. Add it here:
Quote:
SRC = $(TARGET).c adc.c

Regards,
Steve A.

The Board helps those that help themselves.

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

well i have a different problem

in the main.c if i gave simply

  void Adc_init();
        int Adc_read();
	void Adc1_init();
        int Adc_read();

it wont give an error
but when i give variable to the read function
like v=int Adc_read();
i=int Adc_read();
it shows compile error.The screenshot of this is available here in the attachment.

Attachment(s): 

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

arookie wrote:
well i have a different problem

in the main.c if i gave simply

  void Adc_init();
        int Adc_read();
	void Adc1_init();
        int Adc_read();

it wont give an error
but when i give variable to the read function
like v=int Adc_read();
i=int Adc_read();
it shows compile error.The screenshot of this is available here in the attachment.


That's not valid syntax. This is a function prototype or declaration:

int Adc_read();

It tells the compiler what types the inputs (parameters) and output (return) of the function are. To call the function you need to take off the return type and replace the parameter types with arguments; in this case there are no parameters:

Adc_read();

You can save the return value to a variable like this:

int i = Adc_read();

I recommend you read through some C books and/or tutorials until you get a better handle on its syntax.

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

Thanks
You solved my headache

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

Also, study "...Managing large projects...", in the tutorial forum.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1