Skip to content

Commit

Permalink
modified ADC driver
Browse files Browse the repository at this point in the history
  • Loading branch information
1brahimmohamed committed Oct 8, 2022
1 parent 8789dde commit 46ca25d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ADC/src/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#include "std_types.h"


/*******************************************************************************
* Configurations *
*******************************************************************************/

#define ADC_MAXIMUM_VALUE 1023
#define ADC_REF_VOLT_VALUE 2.56

/*******************************************************************************
* Types Declaration *
Expand Down Expand Up @@ -44,8 +50,6 @@ typedef struct{
*******************************************************************************/




/*
* Description :
* Initialize the ADC:
Expand Down
78 changes: 78 additions & 0 deletions LM 35 - Temperature Sensor/src/adc.c
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 */
}
68 changes: 68 additions & 0 deletions LM 35 - Temperature Sensor/src/adc.h
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_ */

0 comments on commit 46ca25d

Please sign in to comment.