-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8789dde
commit 46ca25d
Showing
3 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/****************************************************************************** | ||
* | ||
* Module: Analog to Digital Converter | ||
* File Name: adc.h | ||
* Description: source file for the ADC driver | ||
* Author: Ibrahim Mohamed | ||
* | ||
*******************************************************************************/ | ||
|
||
#include <avr/io.h> | ||
#include "adc.h" | ||
#include "common_macros.h" | ||
#include "gpio.h" | ||
|
||
|
||
/******************************************************************************* | ||
* Functions Definitions * | ||
*******************************************************************************/ | ||
|
||
|
||
|
||
/* | ||
* Description : | ||
* Initialize the ADC: | ||
*/ | ||
void ADC_init(const ADC_ConfigType * Config_Ptr){ | ||
|
||
/* ADMUX Register Bits Description: | ||
* REFS1:0 = 00 to choose to connect external reference, internal reference or AVCC | ||
* ADLAR = 0 right adjusted | ||
* MUX4:0 = 00000 to choose channel 0 as initialization | ||
*/ | ||
|
||
ADMUX = 0; | ||
|
||
/* | ||
* insert the required voltage value in the last 2 bits bits (REFS0 and REFS1) | ||
* of ADMUX Register | ||
*/ | ||
ADMUX = (ADMUX & 0x3F) | (Config_Ptr->ref_volt); | ||
|
||
|
||
/* ADCSRA Register Bits Description: | ||
* ADEN = 1 Enable ADC | ||
* ADIE = 0 Disable ADC Interrupt | ||
* ADATE = 0 Disable Auto Trigger | ||
* ADPS2:0 = XXX to choose ADC_Clock (F_CPU/X where X is 2,4,8,16,32 or 64) --> ADC must operate in range 50-200Khz | ||
*/ | ||
|
||
ADCSRA = (1<<ADEN) | (1<<ADPS1); | ||
ADCSRA = (ADCSRA & 0xF4) | (Config_Ptr->prescaler); | ||
|
||
} | ||
|
||
|
||
|
||
/* | ||
* Description : | ||
* reads the channel from the ADC: | ||
*/ | ||
uint16 ADC_readChannel(uint8 ch_num){ | ||
|
||
ch_num &= 0x07; /* Input channel number must be from 0 --> 7 */ | ||
ADMUX &= 0X0E; /* Clear first 5 bits in the ADMUX (channel number MUX4:0 bits) before set the required channel */ | ||
ADMUX = ADMUX | ch_num; /* Choose the correct channel by setting the channel number in MUX4:0 bits */ | ||
|
||
/* start conversion */ | ||
SET_BIT(ADCSRA,ADSC); | ||
|
||
/* Polling */ | ||
while(BIT_IS_CLEAR(ADCSRA,ADIF)); /* Wait for conversion to complete, ADIF becomes '1' */ | ||
|
||
/* clear the flag */ | ||
SET_BIT(ADCSRA,ADIF); | ||
|
||
|
||
return ADC; /* return the value of ADC register that holds the converted value */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/****************************************************************************** | ||
* | ||
* Module: Analog to Digital Converter | ||
* File Name: adc.h | ||
* Description: header file for the ADC driver | ||
* Author: Ibrahim Mohamed | ||
* | ||
*******************************************************************************/ | ||
|
||
|
||
#ifndef SRC_ADC_H_ | ||
#define SRC_ADC_H_ | ||
|
||
#include "std_types.h" | ||
|
||
|
||
/******************************************************************************* | ||
* Configurations * | ||
*******************************************************************************/ | ||
|
||
#define ADC_MAXIMUM_VALUE 1023 | ||
#define ADC_REF_VOLT_VALUE 2.56 | ||
|
||
/******************************************************************************* | ||
* Types Declaration * | ||
*******************************************************************************/ | ||
|
||
typedef enum{ | ||
AREF_OFF = 0, | ||
AVCC = 1, | ||
INTERNAL_VOLTAGE = 4 | ||
} ADC_ReferenceVolatge; | ||
|
||
|
||
typedef enum{ | ||
F_CPU_2 = 0, | ||
F_CPU_2 = 1, | ||
F_CPU_4, F_CPU_8, F_CPU_16, F_CPU_32, F_CPU_64, F_CPU_128 | ||
} ADC_Prescaler; | ||
|
||
|
||
typedef struct{ | ||
ADC_ReferenceVolatge ref_volt; | ||
ADC_Prescaler prescaler; | ||
} ADC_ConfigType; | ||
|
||
|
||
/******************************************************************************* | ||
* Functions Prototypes * | ||
*******************************************************************************/ | ||
|
||
|
||
/* | ||
* Description : | ||
* Initialize the ADC: | ||
*/ | ||
void ADC_init(const ADC_ConfigType * Config_Ptr); | ||
|
||
|
||
|
||
/* | ||
* Description : | ||
* reads the channel from the ADC: | ||
*/ | ||
uint16 ADC_readChannel(uint8 ch_num); | ||
|
||
|
||
#endif /* SRC_ADC_H_ */ |