Commit ce5af639 by vielex

Final version of MeasurementOfDistance.c

parent 65bcb57a
......@@ -50,16 +50,40 @@ unsigned long Flag; // 1 means valid Distance, 0 means Distance is empty
// Input: sample 12-bit ADC sample
// Output: 32-bit distance (resolution 0.001cm)
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
void SysTick_Init(unsigned long period){
NVIC_ST_CTRL_R = 0;
NVIC_ST_RELOAD_R = period;
NVIC_ST_CURRENT_R = 0;
NVIC_SYS_PRI3_R = (NVIC_SYS_PRI3_R&0x00FFFFFF)|0x40000000;
NVIC_ST_CTRL_R = 0x00000007;
}
unsigned long PF1_Configure(void){
unsigned long volatile delay;
SYSCTL_RCGC2_R |= 0x00000020; // activate port A
delay = SYSCTL_RCGC2_R; //
GPIO_PORTF_AMSEL_R &= ~0x02; // no analog
GPIO_PORTF_PCTL_R &= ~0x00000F0; // regular function
GPIO_PORTF_DIR_R |= 0x02; // make PA2 out
GPIO_PORTF_PDR_R |= 0x02; // make PA2 out
GPIO_PORTF_DR8R_R |= 0x02; // can drive up to 8mA out
GPIO_PORTF_AFSEL_R &= ~0x02; // disable alt funct on PA5
GPIO_PORTF_DEN_R |= 0x02; // enable digital I/O on PA5
return 0;
}
// executes every 25 ms, collects a sample, converts and stores in mailbox
void SysTick_Handler(void){
//GPIO_PORTF_DATA_R ^= 0x02;
ADCdata = ADC0_In();
Distance = Convert(ADCdata);
Flag = 1;
//GPIO_PORTF_DATA_R ^= 0x02;
}
//-----------------------UART_ConvertDistance-----------------------
......@@ -74,8 +98,24 @@ void SysTick_Handler(void){
// 2210 to "2.210 cm"
//10000 to "*.*** cm" any value larger than 9999 converted to "*.*** cm"
void UART_ConvertDistance(unsigned long n){
// as part of Lab 11 you implemented this function
// as part of Lab 11 you implemented this function
if(n < 10000) {
String[0] = n/1000 + 0x30;
String[1] = '.';
String[2] = (n/100)%10 + 0x30;
String[3] = (n/10)%10 + 0x30;
String[4] = n%10 + 0x30;
} else {
String[0] = '*';
String[1] = '.';
String[2] = '*';
String[3] = '*';
String[4] = '*';
}
String[5] = ' ';
String[6] = 'c';
String[7] = 'm';
String[8] = 0;
}
// main1 is a simple main program allowing you to debug the ADC interface
......@@ -106,16 +146,28 @@ int main2(void){
int main(void){
volatile unsigned long delay;
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope);
// initialize ADC0, channel 1, sequencer 3
// initialize Nokia5110 LCD (optional)
// initialize SysTick for 40 Hz interrupts
// initialize profiling on PF1 (optional)
// wait for clock to stabilize
// 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