Last active
October 27, 2023 19:57
-
-
Save ajarmst/6e420fe8f461a3e93a639fdb63808177 to your computer and use it in GitHub Desktop.
ADC Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////////////////////////// | |
// HC12 Program: Demo 5 - ADC | |
// Processor: MC9S12XDP512 | |
// Bus Speed: 20 MHz (Requires Active PLL) | |
// Author: AJ Armstrong | |
// Details: Demo of voltage measurement | |
// Date: Oct 2023 | |
// Revision History : | |
///////////////////////////////////////////////////////////////////////////// | |
#include <stdio.h> // For sprintf, etc | |
#include <ctype.h> // For character utilities | |
#include <string.h> // Various string utilities | |
#include <hidef.h> // Common defines and macros | |
#include "derivative.h" // Derivative-specific definitions | |
#include "pll.h" // Phase-locked loop clock control | |
#include "swled.h" // GPIO switches and LEDs | |
#include "timer.h" // Enhanced capture timer | |
#include "rti.h" // Realtime interrupts | |
#include "segs.h" // 7-Segment Displays | |
#include "lcd.h" // LCD Display | |
#include "sci.h" // Serial communications interface (EIA/TIA-232) | |
///////////////////////////////////////////////////////////////////////////// | |
// Local Prototypes | |
///////////////////////////////////////////////////////////////////////////// | |
void blinky(void); //Toggle yellow led | |
void displayVolts(void); // Update LCD with most recent voltages. | |
///////////////////////////////////////////////////////////////////////////// | |
// Global Variables | |
///////////////////////////////////////////////////////////////////////////// | |
volatile uint ADVal[8] = {0}; | |
///////////////////////////////////////////////////////////////////////////// | |
// Constants | |
///////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////// | |
// Main Entry | |
///////////////////////////////////////////////////////////////////////////// | |
void main(void) | |
{ | |
_DISABLE_COP(); //No watchdog | |
///////////////////////////////////////////////////////////////////////////// | |
// One-Time Initializations | |
///////////////////////////////////////////////////////////////////////////// | |
PLL_To20MHz(); // Configure the main clock to 20 MHz (implications for timers) | |
SWL_Init(); // Standard initialization for switches and LEDs | |
SEG_Init(); // Standard initializaton for 7-seg displays | |
RTI_Init(); // Initialize RTI timer | |
///////////////////////////////////////////////////////////////////////////// | |
// Initialize ADC | |
///////////////////////////////////////////////////////////////////////////// | |
// Power up - Fast flag clear, Enable interrupts | |
ATD0CTL2 = ATD0CTL2_ADPU_MASK | ATD0CTL2_AFFC_MASK | ATD0CTL2_ASCIE_MASK; | |
// I need to wait at least 20 uSec for ADC to power up and stabilize | |
// LCD initialization takes like, forever, so I moved it here. | |
LCD_Init(); // Set up LCD (takes a while) | |
// Convert all 8 pins, finish then freeze | |
ATD0CTL3 = ATD0CTL3_S8C_MASK | ATD0CTL3_FRZ1_MASK; | |
// 10-bit resolution, 4 A/D coversion clock periods, | |
// prescaler to 10 -> 2MHZ ADC-Clock (w/ 20 MHz bus) | |
ATD0CTL4 = ATD0CTL4_SMP0 | ATD0CTL4_PRS2_MASK; | |
// Wrap at 5. | |
ATD0CTL0 = 5; | |
//And set it to continuously convert, unsigned, right justified, multiple chans | |
ATD0CTL5 = ATD0CTL5_DJM_MASK | ATD0CTL5_SCAN_MASK | ATD0CTL5_MULT_MASK; | |
///////////////////////////////////////////////////////////////////////////// | |
// Main Program Loop | |
///////////////////////////////////////////////////////////////////////////// | |
EnableInterrupts; // Ensure our board will respond to interrupt signals. | |
LCD_StringRC(0, 0, "ADC Demo"); | |
RTI_RegisterEvent(0,blinky,100); //Das blinkenlichten | |
RTI_RegisterEvent(3,displayVolts,250); //Quarter second updates | |
for (;;) | |
{ | |
//Not going to WAI, because ADC interrupts happen fast | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////// | |
// Functions | |
///////////////////////////////////////////////////////////////////////////// | |
void blinky(void){SWL_ToggleLEDs(eYellow);} //Toggle yellow led | |
void displayVolts(void) //Update display of ADC voltages | |
{ | |
char szBuff[256] = ""; // Buffer for creating strings | |
//Channel 5, at Pin 77 | |
SEG_16H(ADVal[5],0); //Top segs | |
sprintf(szBuff, "Voltage 5: %5.3f V", ADVal[5]/1023.0*5.0); | |
LCD_StringRC(2, 2, szBuff); | |
//Channel 0, at Pin 67 | |
SEG_16H(ADVal[0],1); //Bottom segs | |
sprintf(szBuff, "Voltage 0: %5.3f V", ADVal[0]/1023.0*5.0); | |
LCD_StringRC(3, 2, szBuff); | |
} | |
///////////////////////////////////////////////////////////////////////////// | |
// Interrupt Service Routines | |
///////////////////////////////////////////////////////////////////////////// | |
// ISR for ADC 0 | |
interrupt VectorNumber_Vatd0 void INT_AD0(void) | |
{ | |
// read channel values | |
ADVal[0] = ATD0DR0; | |
//ADVal[1] = ATD0DR1; | |
//ADVal[2] = ATD0DR2; | |
//ADVal[3] = ATD0DR3; | |
//ADVal[4] = ATD0DR4; | |
ADVal[5] = ATD0DR5; | |
//ADVal[6] = ATD0DR6; | |
//ADVal[7] = ATD0DR7; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment