70 lines
2.7 KiB
C
70 lines
2.7 KiB
C
#include <stdint.h>
|
|
#include "tm4c123gh6pm.h"
|
|
|
|
#define RED_LED (1 << 1) // PF1 for the red LED
|
|
#define ADC_30MV_THRESHOLD (30 * 4096 / 3300) // 30mV to ADC value
|
|
#define ADC_10MV_THRESHOLD (10 * 4096 / 3300) // 10mV to ADC value
|
|
|
|
void PortF_Init(void);
|
|
void ADC0_Init(void);
|
|
uint16_t ADC0_Read(void);
|
|
|
|
int main(void) {
|
|
int adc_value;
|
|
int high_state = 0; // 0: Below 10mV, 1: Above 30mV
|
|
|
|
PortF_Init(); // Initialize LED
|
|
ADC0_Init(); // Initialize ADC
|
|
|
|
// Ensure LED is off by default
|
|
GPIO_PORTF_DATA_R &= ~RED_LED;
|
|
|
|
while (1) {
|
|
adc_value = ADC0_Read(); // Read ADC value
|
|
|
|
if (adc_value >= ADC_30MV_THRESHOLD && high_state == 0) {
|
|
GPIO_PORTF_DATA_R |= RED_LED; // Turn on red LED
|
|
high_state = 1; // Signal is above 30mV
|
|
} else if (adc_value < ADC_10MV_THRESHOLD && high_state == 1) {
|
|
GPIO_PORTF_DATA_R &= ~RED_LED; // Turn off red LED
|
|
high_state = 0; // Reset to allow new detection
|
|
} else if (adc_value < ADC_10MV_THRESHOLD) {
|
|
GPIO_PORTF_DATA_R &= ~RED_LED; // Ensure LED is off when no signal
|
|
}
|
|
}
|
|
}
|
|
|
|
void PortF_Init(void) {
|
|
SYSCTL_RCGCGPIO_R |= (1U << 5); // Enable clock for Port F // SYSCTL_RCGC2_R |= 0x00000020;
|
|
while ((SYSCTL_PRGPIO_R & (1U << 5)) == 0); // Wait for Port F to be ready
|
|
|
|
GPIO_PORTF_DIR_R |= 0x01; // Set PF1 as output
|
|
GPIO_PORTF_DEN_R |= 0x01; // Enable digital function for PF1
|
|
|
|
GPIO_PORTF_DATA_R &= ~0x01; // Ensure LED is off initially
|
|
}
|
|
|
|
void ADC0_Init(void) {
|
|
SYSCTL_RCGCADC_R |= 0x01; // Enable ADC0 clock
|
|
SYSCTL_RCGCGPIO_R |= 0x10; // Enable clock for Port E
|
|
while ((SYSCTL_PRGPIO_R & (1U << 4)) == 0); // Wait for Port E to be ready
|
|
|
|
GPIO_PORTE_AFSEL_R |= (1U << 3); // Enable alternate function on PE3
|
|
GPIO_PORTE_DEN_R &= ~(1U << 3); // Disable digital I/O on PE3
|
|
GPIO_PORTE_AMSEL_R |= (1U << 3); // Enable analog function on PE3
|
|
|
|
ADC0_ACTSS_R &= ~8; // Disable sample sequencer 3
|
|
ADC0_EMUX_R = (ADC0_EMUX_R & 0xFFFF0FFF); // Software trigger for SS3
|
|
ADC0_SSMUX3_R = 0; // Set channel AIN0 (PE3)
|
|
ADC0_SSCTL3_R = 6; // End of sequence and enable interrupt
|
|
ADC0_ACTSS_R |= 8; // Enable sample sequencer 3
|
|
}
|
|
|
|
uint16_t ADC0_Read(void) {
|
|
ADC0_PSSI_R = 8; // Start sampling on SS3
|
|
while ((ADC0_RIS_R & 8) == 0); // Wait for conversion to complete
|
|
uint16_t result = ADC0_SSFIFO3_R & 0xFFF; // Read 12-bit ADC value
|
|
ADC0_ISC_R = 8; // Clear the completion flag
|
|
return result;
|
|
}
|