Skip to content

Instantly share code, notes, and snippets.

@jsmith
Last active January 29, 2019 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmith/75cfe20ed3da05507820feea73b7c76a to your computer and use it in GitHub Desktop.
Save jsmith/75cfe20ed3da05507820feea73b7c76a to your computer and use it in GitHub Desktop.
A2
#include "fsl_device_registers.h"
#include "console.h";
/**
* Set the baud rate to 9600
* Date bits -> 7
* Stop bits -> 1
* Disabled Parity & Flor control
*/
void UARTx_Interface_Init() {
// Enable the clock module
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
// Enable port E and B
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK | SIM_SCGC5_PORTB_MASK;
// Connect the UART0 to the PORTE bits PC4 -TX, PC5 –RX
PORTB_PCR17 |= PORT_PCR_MUX(3); // PB17 TX
PORTB_PCR16 |= PORT_PCR_MUX(3); // PB16 RX
// Disable transmit enable and receive enable
UART0_C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
// Configure UART3 for 8 bits, parity disabled
UART0_C1 = 0b00;
// Set the baud rate to 9600
// Must be 1/16 the clock rate
UART0_BDH = 0;
UART0_BDL = 0x88;
// Enable transmit and receive
UART0_C2 |= UART_C2_TE_MASK;
UART0_C2 |= UART_C2_RE_MASK;
}
void UARTx_Putchar(char c) {
// Poll the TDRE (transmit data register empty)
while(!((UART0_S1 >> 7) & 1)) {}
// Write to output
UART0_D = c;
}
char UARTx_Getchar() {
// Poll the RDRF (transmit data register empty)
while(!((UART0_S1 >> 5) & 1)) {}
// Read from output
return UART0_D;
}
void UARTx_Putstring(char* string) {
while(*string) {
UARTx_Putchar(*string);
string++;
}
}
#ifndef HEADER_CONSOLE
#define HEADER_CONSOLE
void UARTx_Interface_Init();
void UARTx_Putchar(char c);
char UARTx_Getchar();
void UARTx_Putstring(char* string);
#endif
/**
* Jacob Smith 3635295
* Jan. 29th, 2019
* Assignment 2
*/
#include <stdlib.h>
// The functions from lab 3 were included in a header .c and .h file.
// They weren't included in the submission since they were handed in with the lab.
#include "console.h";
typedef unsigned short int16;
typedef struct {
int16 min;
int16 max;
} Result;
Result* MinMax(int16* values, int16 n) {
if (n <= 0) {
return NULL;
}
// Allocate the result, then store the first
Result* result = malloc(sizeof(Result));
result->min = values[0];
result->max = values[0];
for(int i = 1; i < n; i++) {
if (values[i] < result->min) {
result->min = values[i];
} else if (values[i] > result->max) {
result->max = values[i];
}
}
return result;
}
void Tester(int16* values, int16 n) {
Result* result = MinMax(values, n);
if (result == NULL) {
UARTx_Putstring("result -> NULL");
return;
}
int16 Min_result = result->min;
int16 Max_result = result->max;
char* Range;
if (Max_result <= 0x7FFF) {
Range = "too low";
} else if(Max_result <= 0xBFFF) {
Range = "acceptable";
} else {
Range = "good";
}
char str[100]; // Allocate enough space. 100 is definitely enough.
sprintf(str, "MIN -> %d, MAX -> %d, Range -> %s\n\r", Min_result, Max_result, Range);
UARTx_Putstring(str);
free(result);
}
int main(void) {
UARTx_Interface_Init();
UARTx_Putstring("\n\rSTART\n\r-----------------\n\r");
int16 values1[] = {2, 1000, 889, 7, 49101};
Tester(values1, 4); // Test low range
Tester(values1, 5); // Test acceptable range
int16 values2[] = {65505, 2, 60035, 7, 49101};
Tester(values2, 5); // Test high range
int16 values3[] = {65505};
Tester(values3, 1); // Test array of length 1
Tester(values3, 0); // Test array of length 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment