Lesson 20: Analog to Digital Converter (ADC)
ADCs are peripherals that convert a continuous analog voltage to a discrete digital number. In order to convert to digital, the signal is sampled at higher frequencies to minimize signal loss. Then the amplitude at those sampled moments is converted with respect to their quantization level. Finally, these levels and moments are entitled to a unique code, which is simply the combinations of 0's and 1's – this is called encoding.
EK-TM4C123G LaunchPad
Registers
Keil C Sample Firmware Code
#include #include #include #include #include "TM4C123GH6PM.h" void Setup_ADC_SS3_CH9(void); void Setup_UART0(void); void Setup_GPIO(); void UART_PrintString(char * s); void DelayMs(int s); uint32_t result; int main(void) { char str[100]; uint16_t result; Setup_ADC_SS3_CH9(); Setup_UART0(); Setup_GPIO(); 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 Setup_GPIO(void) { // GPIO Initialization and Configuration // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO |= (_PORTs);) SYSCTL->RCGCGPIO |= (__); // allow time for clock to stabilize (SYSCTL->PRGPIO) while((SYSCTL->PRGPIO & (__)) != (__)){}; // 2. Unlock GPIO only PD7, PF0 on TM4C123G; PD7, PE7 on TM4C1294 (GPIOx->LOCK = 0x4C4F434B; and GPIOx->CR = _PINs;) // 3. Set Analog Mode Select bits for each Port (GPIOx->AMSEL = _PINs; 0=digital, 1=analog) // 4. Set Port Control Register for each Port (GPIOx->PCTL = PMCn << _PTCL_PINn, check the PCTL table) // 5. Set Alternate Function Select bits for each Port (GPIOx->AFSEL = _PINs; 0=regular I/O, 1=PCTL peripheral) // 6. Set Output pins for each Port (Direction of the Pins: GPIOx->DIR = _PINs; 0=input, 1=output) // 7. Set PUR bits for internal pull-up, PDR for pull-down reg, ODR for open drain (0: disable, 1=enable) // 8. Set Digital ENable register on all GPIO pins (GPIOx->DEN = _PINs; 0=disable, 1=enable) } void UART_PrintString(char * s) { // Your Code Here } void DelayMs(int s) { volatile int i, j; for (i = 0; i < s; i++) for (j = 0; j < 3180; j++) {}; }#include #include #include #include #include "TM4C123GH6PM.h" void Setup_ADC_SS3_CH9(void); void Setup_UART0(void); void Setup_GPIO(); void UART_PrintString(char * s); void DelayMs(int s); uint32_t result; int main(void) { char str[100]; uint16_t result; Setup_ADC_SS3_CH9(); Setup_UART0(); Setup_GPIO(); 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 Setup_GPIO(void) { // GPIO Initialization and Configuration // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO |= (_PORTs);) SYSCTL->RCGCGPIO |= (_PORTs); |= (__); // allow time for clock to stabilize (SYSCTL->PRGPIO) while((SYSCTL->PRGPIO & (__)) != (__)){}; // 2. Unlock GPIO only PD7, PF0 on TM4C123G; PD7, PE7 on TM4C1294 (GPIOx->LOCK = 0x4C4F434B; and GPIOx->CR = _PINs;) // 3. Set Analog Mode Select bits for each Port (GPIOx->AMSEL = _PINs; 0=digital, 1=analog) // 4. Set Port Control Register for each Port (GPIOx->PCTL = PMCn << _PTCL_PINn, check the PCTL table) // 5. Set Alternate Function Select bits for each Port (GPIOx->AFSEL = _PINs; 0=regular I/O, 1=PCTL peripheral) // 6. Set Output pins for each Port (Direction of the Pins: GPIOx->DIR = _PINs; 0=input, 1=output) // 7. Set PUR bits for internal pull-up, PDR for pull-down reg, ODR for open drain (0: disable, 1=enable) // 8. Set Digital ENable register on all GPIO pins (GPIOx->DEN = _PINs; 0=disable, 1=enable) } void UART_PrintString(char * s) { // Your Code Here } void DelayMs(int s) { volatile int i, j; for (i = 0; i < s; i++) for (j = 0; j < 3180; j++) {}; }
EK-TM4C1294 LaunchPad
Registers
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++) {}; }