Lesson 18: Analog to Digital Converter (ADC)
EK-TM4C123G LaunchPad
Keil C Sample Firmware Code
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include "TM4C123GH6PM.h" void Setup_ADC_SS3_CH9(void); void Setup_UART0(void); void UART_PrintString(char * s); void DelayMs(int s); char str[100]; uint32_t result; int main(void) { Setup_ADC_SS3_CH9(); Setup_UART0(); // Enable GPIOA[0][1] for UART, PE[4] for ADC // Config for GPIO // 1. Enable Clock on GPIOA, GPIOE // allow time for clock to stabilize // 2. Unlock for TM4C123:PORTC[3:0],PORTD[7],and PORTF[0]; TM4C1294:GPIOD[7] // 3. Config AMSEL for PortE[4] to enable analog // 4. Config PCTL to select GPIO // 5. Enable AFSEL bits to 1 // 6. Set DIR to 0 for input, 1 for output // 7. Set PUE bits to 1 to enable internal pull-up // 8. Set DEN bits to 1 to enable data pins sprintf(str,"EE-3450: ADC Test \n\r"); UART_PrintString(str); while(1){ // Initiate SS3 ADC0->PSSI = 0x08; // Waiting for conversion done while ((ADC0->RIS & 0x08) != 0x08) {}; result = ADC0->SSFIFO3 & 0xFFF; // Read 12-bit result ADC0->ISC = 0x08; // Acknowledge completion sprintf(str,"ADC result = %d \n\r", result); UART_PrintString(str); DelayMs(100); } } void Setup_ADC_SS3_CH9(void) { // 1. Enable the ADC clock (SYSCTL_RGCGADC) SYSCTL->RCGCADC |= 0x01; while ((SYSCTL->PRADC & 0x01) != 0x01) {}; // 2. Configure max sampling rate (ADCn_PC_R) ADC0->PC = 0x01; // 0x01: 12Ksps // 3. Configure Sequencer # priority ADC0->SSPRI = 0x0123; // 4. Disable Sample Sequencer ADC0->ACTSS &= ~(1<<3); // 5. Configure triggle mode ADC0->EMUX &= ~(0xF000); // 0x00: Software triggle // 6. COnfigure ADC chanel ADC0->SSMUX3 = (ADC0->SSMUX3 & 0xFFFFFFF0) + 9; // Use chanel 9 // 7. Set flag on sample capture ADC0->SSCTL3 = 0x0006; //no TS0 D0, yes IE0 END0 // 8. COnfigure interrupt ADC0->IM &= ~0x0008; // Disable SS3 interrupt // 9. Enable Sample Sequencer ADC0->ACTSS |= 0x0008; } void Setup_UART0(void) { // Config BuadRate as 115200bps #define UARTIBRD 27 #define UARTFBRD 8 // 1. Enable clock to UART Module 0 (SYSCTL_RCGCUART) SYSCTL->RCGCUART |= 0x01; // allow time for clock to stabilize while((SYSCTL->PRUART & 0x01) != 0x01){}; // 2. Disable UART by clearing UARTEN in UARTCTL register UART0->CTL &= ~(0x00000001); // 3. Write BRD to UARTIBRD and UARTFBRD register UART0->IBRD = UARTIBRD; UART0->FBRD = UARTFBRD; // 4. Write UART Line control (UARTLCRH register) // 8bit, No parity, 1-stop bit, no FIFO = 0x00000060 UART0->LCRH = 0x60; // 5. Configure the UART clock source (UARTCC register) UART0->CC = 0x00; // Clock Source from System Clock // 6. Enable UART by clearing UARTEN in UARTCTL register UART0->CTL = 0x301; // Enable UART0, TXE, RXE } void UART_PrintString(char * s) { char ch; ch = *s; while( ch != NULL ){ while( (UART0->FR & 0x80) != 0x80) {}; UART0->DR = ch; s++; ch = *s; } } void DelayMs(int s) { volatile int i, j; for (i = 0; i < s; i++) for (j = 0; j < 3180; j++) {}; }
EK-TM4C1294 LaunchPad
Keil C Sample Firmware Code
Please follow this article to add ezTiva LIB into your project.
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include "TM4C1294NCPDT.h" #include "ez1294.h" void Setup_ADC_SS3_CH9(void); void Setup_UART0(void); void UART_PrintString(char * s); void DelayMs(int s); char str[100]; uint32_t result; int main(void) { Setup_1294_80MHz(); // Setup SystemClock to 80MHz Setup_ADC_SS3_CH9(); Setup_UART0(); // Enable GPIOA[0][1] for UART, PE[4] for ADC // Config for GPIO // 1. Enable Clock on GPIOA, GPIOE (SYSCTL_RCGCGPIO) // allow time for clock to stabilize (SYSCTL_PRGPIO) // 2. Unlock for TM4C123:PORTC[3:0],PORTD[7],and PORTF[0]; TM4C1294:GPIOD[7] // 3. Config AMSEL for PortE[4] to enable analog // 4. Config PCTL to select GPIO // 5. Enable AFSEL bits to 1 for PortA[1:0] and PortE[4] // 6. Set DIR to 0 for input, 1 for output // 7. Set PUE bits to 1 to enable internal pull-up // 8. Set DEN bits to 1 to enable digital data pins, write 0 to disable digital function sprintf(str,"EE-3450: ADC Test \n\r"); UART_PrintString(str); while(1){ // Initiate SS3 ADC0->PSSI = 0x08; // Waiting for conversion done while ((ADC0->RIS & 0x08) != 0x08) {}; result = ADC0->SSFIFO3 & 0xFFF; // Read 12-bit result ADC0->ISC = 0x08; // Acknowledge completion sprintf(str,"ADC result = %d \n\r", result); UART_PrintString(str); DelayMs(100); } } void Setup_ADC_SS3_CH9(void) { // 1. Enable the ADC clock (SYSCTL_RGCGADC) SYSCTL->RCGCADC |= 0x01; while ((SYSCTL->PRADC & 0x01) != 0x01) {}; // 2. Configure max sampling rate (ADCn_PC_R) ADC0->PC = 0x01; // 0x01: 12Ksps // 3. Configure Sequencer # priority ADC0->SSPRI = 0x0123; // 4. Disable Sample Sequencer ADC0->ACTSS &= ~(1<<3); // 5. Configure triggle mode ADC0->EMUX &= ~(0xF000); // 0x00: Software triggle // 6. COnfigure ADC chanel ADC0->SSMUX3 = (ADC0->SSMUX3 & 0xFFFFFFF0) + 9; // Use chanel 9 // 7. Set flag on sample capture ADC0->SSCTL3 = 0x0006; //no TS0 D0, yes IE0 END0 // 8. COnfigure interrupt ADC0->IM &= ~0x0008; // Disable SS3 interrupt // 9. Enable Sample Sequencer ADC0->ACTSS |= 0x0008; } void Setup_UART0(void) { // Config BuadRate as 115200bps #define UARTIBRD ?? #define UARTFBRD ?? // 1. Enable clock to UART Module 0 (SYSCTL_RCGCUART) SYSCTL->RCGCUART |= 0x01; // allow time for clock to stabilize while((SYSCTL->PRUART & 0x01) != 0x01){}; // 2. Disable UART by clearing UARTEN in UARTCTL register UART0->CTL &= ~(0x00000001); // 3. Write BRD to UARTIBRD and UARTFBRD register UART0->IBRD = UARTIBRD; UART0->FBRD = UARTFBRD; // 4. Write UART Line control (UARTLCRH register) // 8bit, No parity, 1-stop bit, no FIFO = 0x00000060 UART0->LCRH = 0x60; // 5. Configure the UART clock source (UARTCC register) UART0->CC = 0x00; // Clock Source from System Clock // 6. Enable UART by clearing UARTEN in UARTCTL register UART0->CTL = 0x301; // Enable UART0, TXE, RXE } void UART_PrintString(char * s) { char ch; ch = *s; while( ch != NULL ){ while( (UART0->FR & 0x80) != 0x80) {}; UART0->DR = ch; s++; ch = *s; } } void DelayMs(int s) { volatile int i, j; for (i = 0; i < s; i++) for (j = 0; j < 3180; j++) {}; }