Skip to content

Commit 054f182

Browse files
committed
New test for the ADC module, made refactoring.
1 parent 4435aa4 commit 054f182

File tree

4 files changed

+99
-40
lines changed

4 files changed

+99
-40
lines changed

MCAL/ADC/ADC_interface.h

+40-19
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,77 @@
77
* @copyright Copyright (c) 2022
88
*
99
*/
10-
/**
11-
* @addtogroup MCAL_drivers
12-
* @{
13-
* @addtogroup ADC_drivers
14-
* @{
15-
*/
10+
1611
#ifndef _ADC_INTERFACE_H_
1712
#define _ADC_INTERFACE_H_
1813
#include "../../LIB/STD_TYPES.h"
14+
15+
16+
/**
17+
* @brief Specify the ADC channel
18+
*/
19+
typedef enum
20+
{
21+
adc0, /**< ADC channel 0, single ended input */
22+
adc1, /**< ADC channel 1, single ended input */
23+
adc2, /**< ADC channel 2, single ended input */
24+
adc3, /**< ADC channel 3, single ended input */
25+
adc4, /**< ADC channel 4, single ended input */
26+
adc5, /**< ADC channel 5, single ended input */
27+
adc6, /**< ADC channel 6, single ended input */
28+
adc7 /**< ADC channel 7, single ended input */
29+
} ADC_channel;
30+
31+
typedef enum
32+
{
33+
free_running_mode,
34+
analog_comparator,
35+
int0,
36+
timer0_compare_match,
37+
timer0_overflow,
38+
timer1_compare_match_channelB,
39+
timer1_overflow,
40+
timer1_capture_event
41+
}ADC_triggeringSource;
42+
43+
1944
/**
2045
* @brief Initialize the ADC peripheral
21-
*
2246
*/
2347
void ADC_init(void);
2448

2549
/**
2650
* @brief Start Synchronous conversion from a specific channel, the value of the conversion is
2751
* stored in the memory location addressed by `reading`
28-
*
2952
* @param channel
3053
* @param reading
3154
*/
32-
void ADC_StartSynchConversion(uint8_t channel, uint16_t *reading);
55+
void ADC_startSynchConversion(ADC_channel channel, uint16_t * const reading);
3356

3457
/**
3558
* @brief Start Asynchronous conversion from a specific channel, the value of the conversion is
3659
* stored in a memory location addressed ny `reading`
37-
* @note After the conversion, the function addressed by `callBackFunction` will be executed
38-
*
3960
* @param channel
4061
* @param reading
4162
* @param callBackFunction
63+
* @note After the conversion, the function addressed by `callBackFunction` will be executed
4264
*/
43-
void ADC_StartAsynchConversion(uint8_t channel, uint16_t *reading, void (*callBackFunction)(void));
65+
void ADC_startAsynchConversion(ADC_channel channel, uint16_t * const reading, void (*callBackFunction)(void));
66+
67+
68+
69+
void ADC_autoTrigger(ADC_channel channel, ADC_triggeringSource source, uint16_t * const reading);
70+
4471

45-
/**
46-
* @addtogroup ADC_PRESCALERS
47-
* @{
48-
*/
4972
#define DIV_BY_2 1
5073
#define DIV_BY_4 2
5174
#define DIV_BY_8 3
5275
#define DIV_BY_16 4
5376
#define DIV_BY_32 5
5477
#define DIV_BY_64 6
5578
#define DIV_BY_128 7
56-
/* @}*/
5779

58-
#endif /* _ADC_INTERFACE_H_ */
5980

81+
#endif /* _ADC_INTERFACE_H_ */
6082

6183

62-
/* @}@}*/

MCAL/ADC/ADC_program.c

+24-21
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77

88

99
static uint8_t ADC_BusyState=IDLE;
10+
static uint16_t *ptrToReading=NULL;
11+
static void (*ADC_callBackFunction)(void)=NULL;
1012

11-
void ADC_init(void)
12-
{
1313

14-
/*AVCC with external capacitor at AREF pin >> MADE: ADMUX_REFS0= 1 &ADMUX_REFS1= 0 */
15-
16-
/*AREF, Internal Vref turned off*/
17-
CLEAR_BIT(ADMUX, ADMUX_REFS0);
18-
CLEAR_BIT(ADMUX, ADMUX_REFS0);
1914

20-
/*Activate Left Adjust Result*/
21-
//SET_BIT(ADMUX, ADMUX_ADLAR);
15+
void ADC_init(void)
16+
{
2217

18+
/*AVCC, With external capacitor at AREF pin*/
19+
SET_BIT(ADMUX, ADMUX_REFS0);
20+
CLEAR_BIT(ADMUX, ADMUX_REFS1);
2321

2422
/*Set Prescaler to divide by 128*/
2523
ADCSRA&=0b11111000;
@@ -31,15 +29,22 @@ void ADC_init(void)
3129
}
3230

3331

34-
void ADC_StartSynchConversion(uint8_t channel, uint16_t *reading)
32+
33+
void ADC_selectChannel(ADC_channel channel)
34+
{
35+
/*CLEAR the MUX bits in ADMUX register and Specify a Channel*/
36+
ADMUX &=0b11100000;
37+
ADMUX |=channel;
38+
}
39+
40+
void ADC_startSynchConversion(ADC_channel channel, uint16_t * const reading)
3541
{
3642
if(ADC_BusyState==BUSY)
3743
return;
3844
ADC_BusyState=BUSY;
39-
40-
/*CLEAR the MUX bits in ADMUX register and Specify a Channel*/
41-
ADMUX &=0b11100000;
42-
ADMUX |=channel;
45+
46+
/*Select the desired channel */
47+
ADC_selectChannel(channel) ;
4348

4449
/*Start Conversion*/
4550
SET_BIT(ADCSRA, ADCSRA_ADSC);
@@ -54,9 +59,7 @@ void ADC_StartSynchConversion(uint8_t channel, uint16_t *reading)
5459
ADC_BusyState=IDLE;
5560
}
5661

57-
static uint16_t *ptrToReading=NULL;
58-
static void (*ADC_callBackFunction)(void)=NULL;
59-
void ADC_StartAsynchConversion(uint8_t channel, uint16_t *reading, void (*callBackFunction)(void))
62+
void ADC_startAsynchConversion(ADC_channel channel, uint16_t * const reading, void (*callBackFunction)(void))
6063
{
6164
if(ADC_BusyState==BUSY)
6265
return;
@@ -70,9 +73,8 @@ void ADC_StartAsynchConversion(uint8_t channel, uint16_t *reading, void (*callBa
7073
ptrToReading=reading;
7174
ADC_callBackFunction=callBackFunction;
7275

73-
/*CLEAR the MUX bits in ADMUX register and Specify a Channel*/
74-
ADMUX &=0b11100000;
75-
ADMUX |=channel;
76+
/*Select the desired channel */
77+
ADC_selectChannel(channel) ;
7678

7779
/*Start Conversion*/
7880
SET_BIT(ADCSRA, ADCSRA_ADSC);
@@ -85,7 +87,8 @@ void ADC_StartAsynchConversion(uint8_t channel, uint16_t *reading, void (*callBa
8587
void __vector_16(void) __attribute__((signal));
8688
void __vector_16(void)
8789
{
88-
*ptrToReading=ADC; /*This assigns the reading of the ADC to the `*reading` in the `ADC_StartAsynchConversion`*/
90+
/*This assigns the reading of the ADC to the `*reading` in the `ADC_StartAsynchConversion`*/
91+
*ptrToReading=ADC;
8992

9093
/*Invoke the callbackfunction*/
9194
ADC_callBackFunction();

tests/ADC_output.gif

73.5 KB
Loading

tests/ADC_test.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
* @file ADC_test.c
4+
* @brief Contains test for ADC
5+
* @author Abdulrahman Aboghanima
6+
* @date Fri Aug 26 17:18:26 2022
7+
* @copyright Copyright (c) 2022
8+
* @version 0.1
9+
*
10+
*/
11+
12+
#include "../MCAL/ADC/ADC_interface.h"
13+
#include "../MCAL/DIO/DIO_interface.h"
14+
#include <util/delay.h>
15+
16+
int main()
17+
{
18+
/*Setting the Pin0 of the PORTA to ne input for the ADC*/
19+
DIO_SetPinDirection(DIO_PORTA, DIO_PIN0, DIO_PIN_INPUT);
20+
/*Setting both PORT C, D as the output of the converted analog value*/
21+
DIO_SetPortDirection(DIO_PORTC, DIO_PORT_OUTPUT);
22+
DIO_SetPortDirection(DIO_PORTD, DIO_PORT_OUTPUT);
23+
24+
ADC_init();
25+
26+
uint16_t reading;
27+
28+
while(1){
29+
ADC_startSynchConversion(adc0, &reading);
30+
DIO_SetPortValue(DIO_PORTC, reading);
31+
DIO_SetPortValue(DIO_PORTD, reading>>8);
32+
_delay_ms(500);
33+
}
34+
return 0;
35+
}

0 commit comments

Comments
 (0)