106 lines
3.3 KiB
C
106 lines
3.3 KiB
C
#include <stdint.h>
|
|
#include "tm4c123gh6pm.h"
|
|
|
|
// Function prototypes
|
|
void Timer0A_Init(void);
|
|
void ADC0_Init(void);
|
|
void ADC0_Start(void);
|
|
|
|
|
|
uint32_t ADC0_Read(void);
|
|
void Delay(uint32_t time);
|
|
|
|
volatile uint32_t pulse_count = 0;
|
|
volatile uint32_t pulse_detected = 0;
|
|
|
|
int main(void)
|
|
{
|
|
Timer0A_Init(); // Initialize Timer0A for 1-minute interval
|
|
ADC0_Init(); // Initialize ADC0
|
|
|
|
while (1)
|
|
{
|
|
if (pulse_detected)
|
|
{
|
|
pulse_count++; // Increment pulse count
|
|
pulse_detected = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Timer0A Initialization: Configures Timer0A to generate an interrupt every 1 minute
|
|
void Timer0A_Init(void)
|
|
{
|
|
SYSCTL_RCGCTIMER_R |= 0x01; // Enable clock to Timer0
|
|
TIMER0_CTL_R = 0; // Disable Timer0A during configuration
|
|
TIMER0_CFG_R = 0; // Set 32-bit timer configuration
|
|
TIMER0_TAMR_R = 0x02; // Configure Timer0A in periodic mode
|
|
TIMER0_TAILR_R = 48000000 * 60; // Load value for 1-minute interval
|
|
TIMER0_IMR_R |= 0x01; // Enable Timer0A timeout interrupt
|
|
TIMER0_CTL_R |= 0x01; // Enable Timer0A
|
|
NVIC_EN0_R |= (1 << 19); // Enable IRQ19 for Timer0A
|
|
}
|
|
|
|
// ADC0 Initialization: Configures ADC0 for single-channel, single-conversion
|
|
void ADC0_Init(void)
|
|
{
|
|
SYSCTL_RCGCADC_R |= 0x01; // Enable ADC0
|
|
SYSCTL_RCGCGPIO_R |= 0x10; // Enable clock for Port E
|
|
GPIO_PORTE_AFSEL_R |= 0x08; // Enable alternate function for PE3
|
|
GPIO_PORTE_DEN_R &= ~0x08; // Disable digital I/O on PE3
|
|
GPIO_PORTE_AMSEL_R |= 0x08; // Enable analog function on PE3
|
|
ADC0_ACTSS_R &= ~0x08; // Disable SS3 during configuration
|
|
ADC0_EMUX_R &= ~0xF000; // Configure SS3 for software trigger
|
|
ADC0_SSMUX3_R = 0; // Set channel AIN0 (PE3)
|
|
ADC0_SSCTL3_R = 0x06; // Set END0 and IE0 bits for single sample
|
|
ADC0_ACTSS_R |= 0x08; // Enable SS3
|
|
}
|
|
|
|
// Start ADC0 Conversion
|
|
void ADC0_Start(void)
|
|
{
|
|
ADC0_PSSI_R |= 0x08; // Initiate SS3
|
|
}
|
|
|
|
|
|
// Read ADC0 Value
|
|
uint32_t ADC0_Read(void)
|
|
{
|
|
while ((ADC0_RIS_R & 0x08) == 0); // Wait for conversion to complete
|
|
uint32_t result = ADC0_SSFIFO3_R & 0xFFF; // Read 12-bit ADC result
|
|
ADC0_ISC_R = 0x08; // Clear completion flag
|
|
return result;
|
|
}
|
|
|
|
// Timer0A Interrupt Handler: Executes every 1 minute
|
|
void Timer0A_Handler(void)
|
|
{
|
|
TIMER0_ICR_R = 0x01; // Clear the timer interrupt flag
|
|
pulse_count = 0; // Reset pulse count for the next interval
|
|
}
|
|
|
|
// Sample ADC and check for pulse presence
|
|
void check_pulse(void)
|
|
{
|
|
ADC0_Start();
|
|
uint32_t adc_value = ADC0_Read();
|
|
|
|
// Convert ADC value to voltage (assuming 3.3V reference)
|
|
float voltage = (adc_value * 3.3) / 4095;
|
|
|
|
if (voltage >= 0.4 && voltage <= 0.6)
|
|
{
|
|
pulse_detected = 1; // Signal a valid pulse
|
|
}
|
|
}
|
|
|
|
|
|
// Simple delay function
|
|
void Delay(uint32_t time)
|
|
{
|
|
volatile uint32_t count;
|
|
for(count = 0; count < time; count++);
|
|
}
|