Skip to content

Commit

Permalink
Passed lab 14 simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
rosterloh committed Apr 16, 2014
1 parent fbc869d commit 1fc70ce
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
25 changes: 22 additions & 3 deletions MeasurementOfDistance/ADC.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,22 @@
// SS3 triggering event: software trigger
// SS3 1st sample source: channel 1
// SS3 interrupts: enabled but not promoted to controller
void ADC0_Init(void){

void ADC0_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000010; // 1) activate clock for Port E
delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize
GPIO_PORTE_DIR_R &= ~0x04; // 2) make PE2 input
GPIO_PORTE_AFSEL_R |= 0x04; // 3) enable alternate function on PE2
GPIO_PORTE_DEN_R &= ~0x04; // 4) disable digital I/O on PE2
GPIO_PORTE_AMSEL_R |= 0x04; // 5) enable analog function on PE2
SYSCTL_RCGC0_R |= 0x00010000; // 6) activate ADC0
delay = SYSCTL_RCGC2_R;
SYSCTL_RCGC0_R &= ~0x00000300; // 7) configure for 125K
ADC0_SSPRI_R = 0x0123; // 8) Sequencer 3 is highest priority
ADC0_ACTSS_R &= ~0x0008; // 9) disable sample sequencer 3
ADC0_EMUX_R &= ~0xF000; // 10) seq3 is software trigger
ADC0_SSMUX3_R = (ADC0_SSMUX3_R&0xFFFFFFF0)+1; // 11) channel Ain1 (PE2)
ADC0_SSCTL3_R = 0x0006; // 12) no TS0 D0, yes IE0 END0
ADC0_ACTSS_R |= 0x0008; // 13) enable sample sequencer 3
}


Expand All @@ -40,5 +54,10 @@ void ADC0_Init(void){
// Input: none
// Output: 12-bit result of ADC conversion
unsigned long ADC0_In(void){
return 0; // replace this line with proper code
unsigned long result;
ADC0_PSSI_R = 0x0008; // 1) initiate SS3
while((ADC0_RIS_R&0x08)==0){}; // 2) wait for conversion done
result = ADC0_SSFIFO3_R&0xFFF; // 3) read result
ADC0_ISC_R = 0x0008; // 4) acknowledge completion
return result;
}
93 changes: 58 additions & 35 deletions MeasurementOfDistance/MeasurementOfDistance.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,27 @@ 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
unsigned long result;
result = (2000*sample)/4095;
return result;
}

// Initialize SysTick interrupts to trigger at 40 Hz, 25 ms
void SysTick_Init(unsigned long period){

NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_RELOAD_R = period; // reload value (80Mhz/40Hz)-1
NVIC_ST_CURRENT_R = 0; // any write to current clears it
NVIC_SYS_PRI3_R = (NVIC_SYS_PRI3_R&0x00FFFFFF)|0x20000000; // priority 1
NVIC_ST_CTRL_R = 0x00000007; // enable with core clock and interrupts
}
// executes every 25 ms, collects a sample, converts and stores in mailbox
void SysTick_Handler(void){

//NVIC_ST_RELOAD_R = 1999999; // reload value
GPIO_PORTF_DATA_R |= 0x04; // profile
ADCdata = ADC0_In();
GPIO_PORTF_DATA_R &= ~0x04;
Distance = Convert(ADCdata);
Flag = 1;
}

//-----------------------UART_ConvertDistance-----------------------
Expand All @@ -75,44 +86,56 @@ void SysTick_Handler(void){
//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

}
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

EnableInterrupts();
// print a welcome message (optional)
while(1){
// read mailbox
// output to Nokia5110 LCD (optional)
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[5] = ' ';
String[6] = 'c';
String[7] = 'm';
String[8] = 0;
}

int main1(void){
int main(void){
volatile unsigned long delay;
//TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_NoScope);
TExaS_Init(ADC0_AIN1_PIN_PE2, SSI0_Real_Nokia5110_Scope);
ADC0_Init(); // initialize ADC0, channel 1, sequencer 3
EnableInterrupts();
while(1){
ADCdata = ADC0_In();
}
}
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
ADC0_Init(); // initialize ADC0, channel 1, sequencer 3
Nokia5110_Init(); // initialize Nokia5110 LCD
SysTick_Init(1999999); // initialize SysTick for 40 Hz interrupts
// initialize profiling on PF1 (optional)
// wait for clock to stabilize
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF; // activate port F
delay = SYSCTL_RCGC2_R;
GPIO_PORTF_DIR_R |= 0x04; // make PF2 out (built-in LED)
GPIO_PORTF_AFSEL_R &= ~0x04; // disable alt funct on PF2
GPIO_PORTF_DEN_R |= 0x04; // enable digital I/O on PF2
// configure PF2 as GPIO
GPIO_PORTF_PCTL_R = (GPIO_PORTF_PCTL_R&0xFFFFF0FF)+0x00000000;
GPIO_PORTF_AMSEL_R = 0; // disable analog functionality on PF
EnableInterrupts();
Nokia5110_Clear();
Nokia5110_OutString((unsigned char *)"UT.6.01x Lab 14 Start");
while(1){
ADCdata = ADC0_In();
Nokia5110_SetCursor(0, 0);
Distance = Convert(ADCdata);
UART_ConvertDistance(Distance); // from Lab 11
Nokia5110_OutString(String);
if(Flag) {
UART_ConvertDistance(Distance);
Nokia5110_SetCursor(0, 3);
Nokia5110_OutString(String);
Flag = 0;
}
}
}

6 changes: 3 additions & 3 deletions MeasurementOfDistance/Nokia5110.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,14 @@ void Nokia5110_OutString(unsigned char *ptr){
// assumes: LCD is in default horizontal addressing mode (V = 0)
void Nokia5110_OutUDec(unsigned short n){
if(n < 10){
Nokia5110_OutString(" ");
Nokia5110_OutString((unsigned char *)" ");
Nokia5110_OutChar(n+'0'); /* n is between 0 and 9 */
} else if(n<100){
Nokia5110_OutString(" ");
Nokia5110_OutString((unsigned char *)" ");
Nokia5110_OutChar(n/10+'0'); /* tens digit */
Nokia5110_OutChar(n%10+'0'); /* ones digit */
} else if(n<1000){
Nokia5110_OutString(" ");
Nokia5110_OutString((unsigned char *)" ");
Nokia5110_OutChar(n/100+'0'); /* hundreds digit */
n = n%100;
Nokia5110_OutChar(n/10+'0'); /* tens digit */
Expand Down

0 comments on commit 1fc70ce

Please sign in to comment.