Skip to content

Commit

Permalink
added ADC driver
Browse files Browse the repository at this point in the history
  • Loading branch information
1brahimmohamed committed Oct 8, 2022
1 parent 871995b commit 8789dde
Show file tree
Hide file tree
Showing 10 changed files with 794 additions and 0 deletions.
115 changes: 115 additions & 0 deletions ADC/.cproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="de.innot.avreclipse.configuration.app.debug.1641364492">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="de.innot.avreclipse.configuration.app.debug.1641364492" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="de.innot.avreclipse.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=de.innot.avreclipse.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" description="" id="de.innot.avreclipse.configuration.app.debug.1641364492" name="Debug" parent="de.innot.avreclipse.configuration.app.debug">
<folderInfo id="de.innot.avreclipse.configuration.app.debug.1641364492." name="/" resourcePath="">
<toolChain id="de.innot.avreclipse.toolchain.winavr.app.debug.2082027393" name="AVR-GCC Toolchain" superClass="de.innot.avreclipse.toolchain.winavr.app.debug">
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.debug.1414800951" name="Generate HEX file for Flash memory" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.debug.999508324" name="Generate HEX file for EEPROM" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.debug.1257019916" name="Generate Extended Listing (Source + generated Assembler)" superClass="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.size.app.debug.528984928" name="Print Size" superClass="de.innot.avreclipse.toolchain.options.toolchain.size.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.debug.947801746" name="AVRDude" superClass="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.debug"/>
<targetPlatform id="de.innot.avreclipse.targetplatform.winavr.app.debug.588036398" name="AVR Cross-Target" superClass="de.innot.avreclipse.targetplatform.winavr.app.debug"/>
<builder buildPath="${workspace_loc:/ADC}/Debug" id="de.innot.avreclipse.target.builder.winavr.app.debug.1084446751" managedBuildOn="true" name="AVR GNU Make Builder.Debug" superClass="de.innot.avreclipse.target.builder.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.assembler.winavr.app.debug.776950715" name="AVR Assembler" superClass="de.innot.avreclipse.tool.assembler.winavr.app.debug">
<option id="de.innot.avreclipse.assembler.option.debug.level.1649108865" superClass="de.innot.avreclipse.assembler.option.debug.level"/>
</tool>
<tool id="de.innot.avreclipse.tool.compiler.winavr.app.debug.1456580512" name="AVR Compiler" superClass="de.innot.avreclipse.tool.compiler.winavr.app.debug">
<option id="de.innot.avreclipse.compiler.option.debug.level.1698234212" superClass="de.innot.avreclipse.compiler.option.debug.level"/>
<option id="de.innot.avreclipse.compiler.option.optimize.1384634233" superClass="de.innot.avreclipse.compiler.option.optimize"/>
</tool>
<tool id="de.innot.avreclipse.tool.cppcompiler.app.debug.2132210789" name="AVR C++ Compiler" superClass="de.innot.avreclipse.tool.cppcompiler.app.debug">
<option id="de.innot.avreclipse.cppcompiler.option.debug.level.1608454976" superClass="de.innot.avreclipse.cppcompiler.option.debug.level"/>
<option id="de.innot.avreclipse.cppcompiler.option.optimize.512523834" superClass="de.innot.avreclipse.cppcompiler.option.optimize"/>
</tool>
<tool id="de.innot.avreclipse.tool.linker.winavr.app.debug.1987609095" name="AVR C Linker" superClass="de.innot.avreclipse.tool.linker.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.cpplinker.app.debug.1783259311" name="AVR C++ Linker" superClass="de.innot.avreclipse.tool.cpplinker.app.debug"/>
<tool id="de.innot.avreclipse.tool.archiver.winavr.base.1894461529" name="AVR Archiver" superClass="de.innot.avreclipse.tool.archiver.winavr.base"/>
<tool id="de.innot.avreclipse.tool.objdump.winavr.app.debug.1928375831" name="AVR Create Extended Listing" superClass="de.innot.avreclipse.tool.objdump.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.objcopy.flash.winavr.app.debug.135969819" name="AVR Create Flash image" superClass="de.innot.avreclipse.tool.objcopy.flash.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.debug.1164421237" name="AVR Create EEPROM image" superClass="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.size.winavr.app.debug.127801099" name="Print Size" superClass="de.innot.avreclipse.tool.size.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.avrdude.app.debug.2002323317" name="AVRDude" superClass="de.innot.avreclipse.tool.avrdude.app.debug"/>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
<cconfiguration id="de.innot.avreclipse.configuration.app.release.2015382769">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="de.innot.avreclipse.configuration.app.release.2015382769" moduleId="org.eclipse.cdt.core.settings" name="Release">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="de.innot.avreclipse.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=de.innot.avreclipse.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" description="" id="de.innot.avreclipse.configuration.app.release.2015382769" name="Release" parent="de.innot.avreclipse.configuration.app.release">
<folderInfo id="de.innot.avreclipse.configuration.app.release.2015382769." name="/" resourcePath="">
<toolChain id="de.innot.avreclipse.toolchain.winavr.app.release.600611098" name="AVR-GCC Toolchain" superClass="de.innot.avreclipse.toolchain.winavr.app.release">
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release.274095678" name="Generate HEX file for Flash memory" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release.247594976" name="Generate HEX file for EEPROM" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release.1236016901" name="Generate Extended Listing (Source + generated Assembler)" superClass="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.size.app.release.1237954502" name="Print Size" superClass="de.innot.avreclipse.toolchain.options.toolchain.size.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release.1259431780" name="AVRDude" superClass="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release"/>
<targetPlatform id="de.innot.avreclipse.targetplatform.winavr.app.release.2098745557" name="AVR Cross-Target" superClass="de.innot.avreclipse.targetplatform.winavr.app.release"/>
<builder buildPath="${workspace_loc:/ADC}/Release" id="de.innot.avreclipse.target.builder.winavr.app.release.982072941" managedBuildOn="true" name="AVR GNU Make Builder.Release" superClass="de.innot.avreclipse.target.builder.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.assembler.winavr.app.release.1692747073" name="AVR Assembler" superClass="de.innot.avreclipse.tool.assembler.winavr.app.release">
<option id="de.innot.avreclipse.assembler.option.debug.level.1294593602" superClass="de.innot.avreclipse.assembler.option.debug.level" value="de.innot.avreclipse.assembler.option.debug.level.none" valueType="enumerated"/>
<inputType id="de.innot.avreclipse.tool.assembler.input.1551130451" superClass="de.innot.avreclipse.tool.assembler.input"/>
</tool>
<tool id="de.innot.avreclipse.tool.compiler.winavr.app.release.2100446050" name="AVR Compiler" superClass="de.innot.avreclipse.tool.compiler.winavr.app.release">
<option id="de.innot.avreclipse.compiler.option.debug.level.1920471851" superClass="de.innot.avreclipse.compiler.option.debug.level" value="de.innot.avreclipse.compiler.option.debug.level.none" valueType="enumerated"/>
<option id="de.innot.avreclipse.compiler.option.optimize.1111952400" superClass="de.innot.avreclipse.compiler.option.optimize" value="de.innot.avreclipse.compiler.optimize.size" valueType="enumerated"/>
<inputType id="de.innot.avreclipse.compiler.winavr.input.2141191257" name="C Source Files" superClass="de.innot.avreclipse.compiler.winavr.input"/>
</tool>
<tool id="de.innot.avreclipse.tool.cppcompiler.app.release.1079699682" name="AVR C++ Compiler" superClass="de.innot.avreclipse.tool.cppcompiler.app.release">
<option id="de.innot.avreclipse.cppcompiler.option.debug.level.835813044" superClass="de.innot.avreclipse.cppcompiler.option.debug.level" value="de.innot.avreclipse.cppcompiler.option.debug.level.none" valueType="enumerated"/>
<option id="de.innot.avreclipse.cppcompiler.option.optimize.176516110" superClass="de.innot.avreclipse.cppcompiler.option.optimize" value="de.innot.avreclipse.cppcompiler.optimize.size" valueType="enumerated"/>
</tool>
<tool id="de.innot.avreclipse.tool.linker.winavr.app.release.466553647" name="AVR C Linker" superClass="de.innot.avreclipse.tool.linker.winavr.app.release">
<inputType id="de.innot.avreclipse.tool.linker.input.1142257567" name="OBJ Files" superClass="de.innot.avreclipse.tool.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
<tool id="de.innot.avreclipse.tool.cpplinker.app.release.477423109" name="AVR C++ Linker" superClass="de.innot.avreclipse.tool.cpplinker.app.release"/>
<tool id="de.innot.avreclipse.tool.archiver.winavr.base.1686899394" name="AVR Archiver" superClass="de.innot.avreclipse.tool.archiver.winavr.base"/>
<tool id="de.innot.avreclipse.tool.objdump.winavr.app.release.70742447" name="AVR Create Extended Listing" superClass="de.innot.avreclipse.tool.objdump.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release.1979185218" name="AVR Create Flash image" superClass="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release.1268270059" name="AVR Create EEPROM image" superClass="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.size.winavr.app.release.2113322471" name="Print Size" superClass="de.innot.avreclipse.tool.size.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.avrdude.app.release.1343084296" name="AVRDude" superClass="de.innot.avreclipse.tool.avrdude.app.release"/>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="ADC.de.innot.avreclipse.project.winavr.elf_2.1.0.383701953" name="AVR Cross Target Application" projectType="de.innot.avreclipse.project.winavr.elf_2.1.0"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="de.innot.avreclipse.configuration.app.release.2015382769;de.innot.avreclipse.configuration.app.release.2015382769.;de.innot.avreclipse.tool.compiler.winavr.app.release.2100446050;de.innot.avreclipse.compiler.winavr.input.2141191257">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="de.innot.avreclipse.core.AVRGCCManagedMakePerProjectProfileC"/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
</cproject>
27 changes: 27 additions & 0 deletions ADC/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ADC</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>de.innot.avreclipse.core.avrnature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions ADC/.settings/de.innot.avreclipse.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
avrtarget/ClockFrequency=16000000
avrtarget/ExtRAMSize=0
avrtarget/ExtendedRAM=false
avrtarget/MCUType=atmega32
avrtarget/UseEEPROM=false
avrtarget/UseExtendedRAMforHeap=true
eclipse.preferences.version=1
2 changes: 2 additions & 0 deletions ADC/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
78 changes: 78 additions & 0 deletions ADC/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 */
}
64 changes: 64 additions & 0 deletions ADC/src/adc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/******************************************************************************
*
* 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"



/*******************************************************************************
* 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_ */
34 changes: 34 additions & 0 deletions ADC/src/common_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******************************************************************************
*
* Module: Common - Macros
* File Name: Common_Macros.h
* Description: Commonly used Macros
* Author: Ibrahim Mohamed
*
*******************************************************************************/

#ifndef COMMON_MACROS
#define COMMON_MACROS

/* Set a certain bit in any register */
#define SET_BIT(REG,BIT) (REG|=(1<<BIT))

/* Clear a certain bit in any register */
#define CLEAR_BIT(REG,BIT) (REG&=(~(1<<BIT)))

/* Toggle a certain bit in any register */
#define TOGGLE_BIT(REG,BIT) (REG^=(1<<BIT))

/* Rotate right the register value with specific number of rotates */
#define ROR(REG,num) ( REG= (REG>>num) | (REG<<(8-num)) )

/* Rotate left the register value with specific number of rotates */
#define ROL(REG,num) ( REG= (REG<<num) | (REG>>(8-num)) )

/* Check if a specific bit is set in any register and return true if yes */
#define BIT_IS_SET(REG,BIT) ( REG & (1<<BIT) )

/* Check if a specific bit is cleared in any register and return true if yes */
#define BIT_IS_CLEAR(REG,BIT) ( !(REG & (1<<BIT)) )

#endif
Loading

0 comments on commit 8789dde

Please sign in to comment.