collab/PineapplePad
git clone https://git.julia.blue/collab/PineapplePad
0e8c935
master
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 ) { 10SYSCTL_RCGCGPIO_R |=0x08 ;// Turn on port d clock 11for (volatile int dummy = 0 ;dummy < 0x1337 ;dummy ++ ) {} 12GPIO_PORTD_DIR_R &= ~(1 <<2 );// Set to input 13GPIO_PORTD_AFSEL_R |=1 <<2 ;// Turn on alternate func 14GPIO_PORTD_DEN_R &= ~(1 <<2 );// Turn of digital 15GPIO_PORTD_AMSEL_R |=1 <<2 ;// Turn on analog 16SYSCTL_RCGC0_R |=1 <<16 ;// Enable adc clock 17for (volatile int dummy = 0 ;dummy < 0x1337 ;dummy ++ ) {} 18SYSCTL_RCGC0_R &= ~0x00000300 ;// Set to lowest speed 19ADC0_SSPRI_R = 0x0123 ;// Set sequencer priorities 20ADC0_ACTSS_R &= ~0x0008 ;// Disable sequencer 21ADC0_EMUX_R &= ~0xF000 ;// Set software trigger for seq3 22ADC0_SSMUX3_R &= ~0x000F ;// Deselect analog channels 23ADC0_SSMUX3_R += 5 ;// Analog channel 5 = PD2 24ADC0_SSCTL3_R = 0x0006 ;// Magical control bits 25ADC0_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 ) { 34ADC0_PSSI_R |=0x08 ;// Initiate ADC poll 35while (!(ADC0_RIS_R & 0x08 ));// Wait for it to finish 36uint32_t temp = ADC0_SSFIFO3_R & 0x0FFF ;// Get result 37ADC0_ISC_R |=0x08 ;// Ack 38return temp ; 39}