draw sample graph

This commit is contained in:
Andy Killorin 2025-03-27 12:33:39 -04:00
parent c2a0fdd210
commit 6bfbf2366b
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
4 changed files with 60 additions and 8 deletions

View file

@ -28,6 +28,7 @@ uint32_t gADCSamplingRate; // [Hz] actual ADC sampling rate
// imported globals
extern uint32_t gSystemClock; // [Hz] system clock frequency
extern volatile uint32_t gTime; // time in hundredths of a second
extern volatile uint8_t drawRequested;
// initialize all button and joystick handling hardware
void ButtonInit(void)
@ -189,6 +190,9 @@ void ButtonISR(void) {
if (presses & 2) { // EK-TM4C1294XL button 2 pressed
gTime = 0;
}
if (presses & 4) { // boosterpack button 1 pressed
drawRequested = 1;
}
if (running) {
if (tic) gTime++; // increment time every other ISR call
tic = !tic;

49
main.c
View file

@ -15,18 +15,20 @@
#include "driverlib/interrupt.h"
#include "Crystalfontz128x128_ST7735.h"
#include <stdio.h>
#include "buttons.h"
#include "grlib/grlib.h"
#include "sampling.h"
#include <math.h>
#include <string.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"
#include "sampling.h"
#include "buttons.h"
#define PWM_FREQUENCY 20000 // PWM frequency = 20 kHz
uint32_t gSystemClock; // [Hz] system clock frequency
volatile uint32_t gTime = 0; // time in hundredths of a second
volatile uint8_t drawRequested = 1;
// start a pwm test signal
void start_signal() {
@ -87,8 +89,8 @@ int main(void) {
GrContextForegroundSet(&sContext, ClrBlue);
uint8_t xy_pos;
// draw gridlines from the center out
uint8_t xy_pos;
for (xy_pos = HEIGHT/2; xy_pos < HEIGHT; xy_pos += PIXELS_PER_DIV) {
GrLineDrawV(&sContext, xy_pos, 0, 128); // right
GrLineDrawV(&sContext, HEIGHT - xy_pos, 0, 128); // left
@ -98,7 +100,48 @@ int main(void) {
}
// display graph
#define LOCAL_BUF_LEN 128
uint16_t local_adc_buffer[LOCAL_BUF_LEN]; // copy of adc buffer
int32_t adc_current_index;
int32_t j;
if (drawRequested) {
IntMasterDisable(); // critical section
adc_current_index = gADCBufferIndex - 128;
for (j=0; j<LOCAL_BUF_LEN; j++) {
local_adc_buffer[j] = gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + j)];
}
IntMasterEnable();
drawRequested = 0;
}
GrContextForegroundSet(&sContext, ClrPink);
for(j=0; j<LOCAL_BUF_LEN; j++) {
#define TRANSPOSE(x) (HEIGHT/2) - (x >> 8)
//GrPixelDraw(&sContext, j, TRANSPOSE(local_adc_buffer[j]));
uint32_t upper,lower, current, last;
if (j==0) {
upper = TRANSPOSE(local_adc_buffer[j]);
lower = upper;
last = upper;
} else {
current = TRANSPOSE(local_adc_buffer[j]);
// couldn't find these anywhere
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
upper = MAX(current, last);
lower = MIN(current, last);
last = current;
}
GrLineDrawV(&sContext, j, lower, upper);
}
// display time
time = gTime; // read shared global only once
uint32_t hundredths = time % 100;

View file

@ -13,12 +13,7 @@
#include "buttons.h"
#include "sampling.h"
// index wrapping macro
#define ADC_BUFFER_WRAP(i) ((i) & (ADC_BUFFER_SIZE - 1))
// latest sample index
volatile int32_t gADCBufferIndex = ADC_BUFFER_SIZE - 1;
volatile uint16_t gADCBuffer[ADC_BUFFER_SIZE]; // circular buffer
volatile uint32_t gADCErrors = 0; // number of missed ADC deadlines
void ADC_ISR(void)
{
// clear ADC1 sequence0 interrupt flag in the ADCISC register
@ -37,6 +32,9 @@ void ADC_ISR(void)
void start_sampler() {
gADCBufferIndex = ADC_BUFFER_SIZE - 1;
gADCErrors = 0;
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE,
GPIO_PIN_0); // GPIO setup for analog input AIN3

View file

@ -5,6 +5,13 @@
#define ADC_BUFFER_SIZE 2048 // size must be a power of 2
// index wrapping macro
#define ADC_BUFFER_WRAP(i) ((i) & (ADC_BUFFER_SIZE - 1))
volatile int32_t gADCBufferIndex;
volatile uint16_t gADCBuffer[ADC_BUFFER_SIZE]; // circular buffer
volatile uint32_t gADCErrors; // number of missed ADC deadlines
// initialize ADC and ISR
void start_sampler(void);