Commit ce5af639 by vielex

Final version of MeasurementOfDistance.c

parent 65bcb57a
// MeasurementOfDistance.c // MeasurementOfDistance.c
// Runs on LM4F120/TM4C123 // Runs on LM4F120/TM4C123
// Use SysTick interrupts to periodically initiate a software- // Use SysTick interrupts to periodically initiate a software-
// triggered ADC conversion, convert the sample to a fixed- // triggered ADC conversion, convert the sample to a fixed-
// point decimal distance, and store the result in a mailbox. // point decimal distance, and store the result in a mailbox.
// The foreground thread takes the result from the mailbox, // The foreground thread takes the result from the mailbox,
// converts the result to a string, and prints it to the // converts the result to a string, and prints it to the
// Nokia5110 LCD. The display is optional. // Nokia5110 LCD. The display is optional.
// January 15, 2016 // January 15, 2016
/* This example accompanies the book /* This example accompanies the book
"Embedded Systems: Introduction to ARM Cortex M Microcontrollers", "Embedded Systems: Introduction to ARM Cortex M Microcontrollers",
ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2015 ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2015
Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file You may use, edit, run or distribute this file
as long as the above copyright notice remains as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/ http://users.ece.utexas.edu/~valvano/
*/ */
// Slide pot pin 3 connected to +3.3V // Slide pot pin 3 connected to +3.3V
// Slide pot pin 2 connected to PE2(Ain1) and PD3 // Slide pot pin 2 connected to PE2(Ain1) and PD3
// Slide pot pin 1 connected to ground // Slide pot pin 1 connected to ground
#include "ADC.h" #include "ADC.h"
#include "..//tm4c123gh6pm.h" #include "..//tm4c123gh6pm.h"
#include "Nokia5110.h" #include "Nokia5110.h"
#include "TExaS.h" #include "TExaS.h"
void EnableInterrupts(void); // Enable interrupts void EnableInterrupts(void); // Enable interrupts
unsigned char String[10]; // null-terminated ASCII string unsigned char String[10]; // null-terminated ASCII string
unsigned long Distance; // units 0.001 cm unsigned long Distance; // units 0.001 cm
unsigned long ADCdata; // 12-bit 0 to 4095 sample unsigned long ADCdata; // 12-bit 0 to 4095 sample
unsigned long Flag; // 1 means valid Distance, 0 means Distance is empty unsigned long Flag; // 1 means valid Distance, 0 means Distance is empty
//********Convert**************** //********Convert****************
// Convert a 12-bit binary ADC sample into a 32-bit unsigned // Convert a 12-bit binary ADC sample into a 32-bit unsigned
// fixed-point distance (resolution 0.001 cm). Calibration // fixed-point distance (resolution 0.001 cm). Calibration
// data is gathered using known distances and reading the // data is gathered using known distances and reading the
// ADC value measured on PE1. // ADC value measured on PE1.
// Overflow and dropout should be considered // Overflow and dropout should be considered
// Input: sample 12-bit ADC sample // Input: sample 12-bit ADC sample
// Output: 32-bit distance (resolution 0.001cm) // Output: 32-bit distance (resolution 0.001cm)
unsigned long Convert(unsigned long sample){ unsigned long Convert(unsigned long sample){
return 0; // replace this line with real code return (2 * 1000 * sample) / 4095.0; // replace this line with real code
} }
// Initialize SysTick interrupts to trigger at 40 Hz, 25 ms // Initialize SysTick interrupts to trigger at 40 Hz, 25 ms
void SysTick_Init(unsigned long period){ void SysTick_Init(unsigned long period){
NVIC_ST_CTRL_R = 0;
} NVIC_ST_RELOAD_R = period;
// executes every 25 ms, collects a sample, converts and stores in mailbox NVIC_ST_CURRENT_R = 0;
void SysTick_Handler(void){ NVIC_SYS_PRI3_R = (NVIC_SYS_PRI3_R&0x00FFFFFF)|0x40000000;
NVIC_ST_CTRL_R = 0x00000007;
}
}
//-----------------------UART_ConvertDistance-----------------------
// Converts a 32-bit distance into an ASCII string unsigned long PF1_Configure(void){
// Input: 32-bit number to be converted (resolution 0.001cm) unsigned long volatile delay;
// Output: store the conversion in global variable String[10] SYSCTL_RCGC2_R |= 0x00000020; // activate port A
// Fixed format 1 digit, point, 3 digits, space, units, null termination delay = SYSCTL_RCGC2_R; //
// Examples GPIO_PORTF_AMSEL_R &= ~0x02; // no analog
// 4 to "0.004 cm" GPIO_PORTF_PCTL_R &= ~0x00000F0; // regular function
// 31 to "0.031 cm" GPIO_PORTF_DIR_R |= 0x02; // make PA2 out
// 102 to "0.102 cm" GPIO_PORTF_PDR_R |= 0x02; // make PA2 out
// 2210 to "2.210 cm" GPIO_PORTF_DR8R_R |= 0x02; // can drive up to 8mA out
//10000 to "*.*** cm" any value larger than 9999 converted to "*.*** cm" GPIO_PORTF_AFSEL_R &= ~0x02; // disable alt funct on PA5
void UART_ConvertDistance(unsigned long n){ GPIO_PORTF_DEN_R |= 0x02; // enable digital I/O on PA5
// as part of Lab 11 you implemented this function return 0;
}
}
// executes every 25 ms, collects a sample, converts and stores in mailbox
// main1 is a simple main program allowing you to debug the ADC interface void SysTick_Handler(void){
int main1(void){ //GPIO_PORTF_DATA_R ^= 0x02;
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope); ADCdata = ADC0_In();
ADC0_Init(); // initialize ADC0, channel 1, sequencer 3 Distance = Convert(ADCdata);
EnableInterrupts(); Flag = 1;
while(1){ //GPIO_PORTF_DATA_R ^= 0x02;
ADCdata = ADC0_In(); }
}
} //-----------------------UART_ConvertDistance-----------------------
// once the ADC is operational, you can use main2 to debug the convert to distance // Converts a 32-bit distance into an ASCII string
int main2(void){ // Input: 32-bit number to be converted (resolution 0.001cm)
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_NoScope); // Output: store the conversion in global variable String[10]
ADC0_Init(); // initialize ADC0, channel 1, sequencer 3 // Fixed format 1 digit, point, 3 digits, space, units, null termination
Nokia5110_Init(); // initialize Nokia5110 LCD // Examples
EnableInterrupts(); // 4 to "0.004 cm"
while(1){ // 31 to "0.031 cm"
ADCdata = ADC0_In(); // 102 to "0.102 cm"
Nokia5110_SetCursor(0, 0); // 2210 to "2.210 cm"
Distance = Convert(ADCdata); //10000 to "*.*** cm" any value larger than 9999 converted to "*.*** cm"
UART_ConvertDistance(Distance); // from Lab 11 void UART_ConvertDistance(unsigned long n){
Nokia5110_OutString(String); // output to Nokia5110 LCD (optional) // as part of Lab 11 you implemented this function
} if(n < 10000) {
} String[0] = n/1000 + 0x30;
// once the ADC and convert to distance functions are operational, String[1] = '.';
// you should use this main to build the final solution with interrupts and mailbox String[2] = (n/100)%10 + 0x30;
int main(void){ String[3] = (n/10)%10 + 0x30;
volatile unsigned long delay; String[4] = n%10 + 0x30;
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope); } else {
// initialize ADC0, channel 1, sequencer 3 String[0] = '*';
// initialize Nokia5110 LCD (optional) String[1] = '.';
// initialize SysTick for 40 Hz interrupts String[2] = '*';
// initialize profiling on PF1 (optional) String[3] = '*';
// wait for clock to stabilize String[4] = '*';
}
EnableInterrupts(); String[5] = ' ';
// print a welcome message (optional) String[6] = 'c';
while(1){ String[7] = 'm';
// read mailbox String[8] = 0;
// output to Nokia5110 LCD (optional) }
}
} // main1 is a simple main program allowing you to debug the ADC interface
int main1(void){
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope);
ADC0_Init(); // initialize ADC0, channel 1, sequencer 3
EnableInterrupts();
while(1){
ADCdata = ADC0_In();
}
}
// once the ADC is operational, you can use main2 to debug the convert to distance
int main2(void){
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_NoScope);
ADC0_Init(); // initialize ADC0, channel 1, sequencer 3
Nokia5110_Init(); // initialize Nokia5110 LCD
EnableInterrupts();
while(1){
ADCdata = ADC0_In();
Nokia5110_SetCursor(0, 0);
Distance = Convert(ADCdata);
UART_ConvertDistance(Distance); // from Lab 11
Nokia5110_OutString(String); // output to Nokia5110 LCD (optional)
}
}
// once the ADC and convert to distance functions are operational,
// you should use this main to build the final solution with interrupts and mailbox
int main(void){
volatile unsigned long delay;
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope);
// initialize ADC0, channel 1, sequencer 3
ADC0_Init();
// initialize Nokia5110 LCD (optional)
Nokia5110_Init();
// initialize SysTick for 40 Hz interrupts
SysTick_Init(1999999);
// initialize profiling on PF1 (optional)
PF1_Configure();
EnableInterrupts();
// print a welcome message (optional)
Nokia5110_Clear();
//Nokia5110_SetCursor(0, 0);
Nokia5110_OutString((unsigned char *)"");
while(1){
// read mailbox
// output to Nokia5110 LCD (optional)
if(Flag) {
UART_ConvertDistance(Distance);
Nokia5110_SetCursor(0, 0);
Nokia5110_OutString(String);
Flag = 0;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment