TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
adc.c
Go to the documentation of this file.
1#include <avr/io.h>
2#include <stdio.h>
3#include <util/delay.h>
4
5#include "adc.h"
6#include "log.h"
7
9
10#define ADC_BASE_ADR ((volatile uint8_t *)0x1000) // Adress offset for ADC
11
12void ADC_init(void) {
13
14 // XMEM controls the ADC and is enabled elsewhere (xmem_init())
15 // Do NOT touch DDR/PORTA (PORTA is the external data bus and is handeled
16 // trough writing to and reading to xmem) No need to set up PD6/PD7; XMEM
17 // controls WR/RD automatically when we read or write to memory allocted to
18 // the ADC
19}
20
21void ADC_start_conv(void) {
22
23 // Write to any adress on ADC to trigger WR
24 *ADC_BASE_ADR = 0x00;
25 // Delay to ensure ADC conversion is done, checking BUSY pin insead.
26 while ((PORTD & PD4)) {
27 }
28}
29
30uint8_t ADC_read_ch(void) {
31
32 // Read from any adress in ADC allocated memory adressarea to
33 return *ADC_BASE_ADR;
34}
35
36void ADC_read_all(struct ADC_meas *output) {
37
39
40 volatile uint8_t adc_value[4];
41
42 // Send a pulse then read for each channel
43 for (uint8_t i = 0; i < 4; i++) {
44 _delay_ms(5);
45 adc_value[i] = ADC_read_ch();
46 output->channel[i] = adc_value[i];
47 }
48}
49
50/*
51void ADC_test(void) {
52
53 struct ADC_meas data;
54
55 ADC_read_all(&data);
56
57 printf("--ADC Test--\n\r");
58 printf("CH0=%3d CH1=%3d CH2=%3d CH3=%3d\r\n", data.channel[0] - 128,
59 data.channel[1] - 128, data.channel[2] - 128, data.channel[3] - 128);
60
61 _delay_ms(100);
62}
63*/
#define ADC_BASE_ADR
Definition adc.c:10
ADC memory-mapped interface.
void ADC_init(void)
Initialize the external memory–mapped ADC interface.
Definition adc.c:12
uint8_t ADC_read_ch(void)
Read the current ADC channel sample.
Definition adc.c:30
void ADC_read_all(struct ADC_meas *output)
Perform a full four-channel ADC conversion and return all results.
Definition adc.c:36
void ADC_start_conv(void)
Start a conversion on the external ADC and wait for completion.
Definition adc.c:21
#define LOG_MODULE_DEFINE(name)
Definition log.h:39
Logging interface for debug messages.
Container for ADC measurement results.
Definition adc.h:20
uint8_t channel[4]
Definition adc.h:21