collab/PineapplePad

git clone https://git.julia.blue/collab/PineapplePad

Joseph RyanAdd sfx0e8c935

master
1.5 KiB39 linesraw
1#include <stdint.h>
2#include "../inc/ADC.h"
3#include "../inc/tm4c123gh6pm.h"
4
5// ADC initialization function
6// Input: none
7// Output: none
8// measures from PD2, analog channel 5
9void ADC_Init(void) {
10    SYSCTL_RCGCGPIO_R |= 0x08; // Turn on port d clock
11    for(volatile int dummy = 0; dummy < 0x1337; dummy++) {}
12    GPIO_PORTD_DIR_R &= ~(1 << 2);  // Set to input
13    GPIO_PORTD_AFSEL_R |= 1 << 2;   // Turn on alternate func
14    GPIO_PORTD_DEN_R &= ~(1 << 2);  // Turn of digital
15    GPIO_PORTD_AMSEL_R |= 1 << 2;   // Turn on analog
16    SYSCTL_RCGC0_R |= 1 << 16;      // Enable adc clock
17    for(volatile int dummy = 0; dummy < 0x1337; dummy++) {}
18    SYSCTL_RCGC0_R &= ~0x00000300;  // Set to lowest speed
19    ADC0_SSPRI_R = 0x0123;          // Set sequencer priorities
20    ADC0_ACTSS_R &= ~0x0008;        // Disable sequencer
21    ADC0_EMUX_R &= ~0xF000;         // Set software trigger for seq3
22    ADC0_SSMUX3_R &= ~0x000F;       // Deselect analog channels
23    ADC0_SSMUX3_R += 5;             // Analog channel 5 = PD2
24    ADC0_SSCTL3_R = 0x0006;         // Magical control bits
25    ADC0_ACTSS_R |= 0x0008;         // Re-enable adc
26}
27
28//------------ADCIn------------
29// Busy-wait Analog to digital conversion
30// Input: none
31// Output: 12-bit result of ADC conversion
32// measures from PD2, analog channel 5
33uint32_t ADC_In(void) {
34    ADC0_PSSI_R |= 0x08;            // Initiate ADC poll
35    while(!(ADC0_RIS_R&0x08));      // Wait for it to finish
36    uint32_t temp = ADC0_SSFIFO3_R & 0x0FFF; // Get result
37    ADC0_ISC_R |= 0x08;             // Ack
38    return temp;
39}