diff --git a/ADC/src/adc.h b/ADC/src/adc.h index a003239..3cff8c5 100644 --- a/ADC/src/adc.h +++ b/ADC/src/adc.h @@ -14,6 +14,12 @@ #include "std_types.h" +/******************************************************************************* + * Configurations * + *******************************************************************************/ + +#define ADC_MAXIMUM_VALUE 1023 +#define ADC_REF_VOLT_VALUE 2.56 /******************************************************************************* * Types Declaration * @@ -44,8 +50,6 @@ typedef struct{ *******************************************************************************/ - - /* * Description : * Initialize the ADC: diff --git a/LM 35 - Temperature Sensor/src/adc.c b/LM 35 - Temperature Sensor/src/adc.c new file mode 100644 index 0000000..2fe13e3 --- /dev/null +++ b/LM 35 - Temperature Sensor/src/adc.c @@ -0,0 +1,78 @@ + /****************************************************************************** + * + * Module: Analog to Digital Converter + * File Name: adc.h + * Description: source file for the ADC driver + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#include +#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<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 */ +} diff --git a/LM 35 - Temperature Sensor/src/adc.h b/LM 35 - Temperature Sensor/src/adc.h new file mode 100644 index 0000000..3cff8c5 --- /dev/null +++ b/LM 35 - Temperature Sensor/src/adc.h @@ -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_ */