Created
December 1, 2015 04:04
-
-
Save michaellin/c754c2109baabad3c779 to your computer and use it in GitHub Desktop.
This file contains 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
/********************************************************* | |
Wrench Helper Functions | |
Aaron Manheim 11/07/15 | |
**********************************************************/ | |
// the common headers for C99 types | |
#include <stdint.h> | |
#include <stdbool.h> | |
// the headers to access the GPIO subsystem | |
#include "inc/hw_memmap.h" | |
#include "inc/hw_types.h" | |
#include "inc/hw_gpio.h" | |
#include "inc/hw_sysctl.h" | |
// the headers to access the TivaWare Library | |
#include "driverlib/sysctl.h" | |
#include "driverlib/pin_map.h" | |
#include "driverlib/gpio.h" | |
#include "driverlib/timer.h" | |
#include "driverlib/interrupt.h" | |
#include "BITDEFS.H" | |
//The header for analog input | |
#include "ADMulti.h" | |
// readability defines | |
#define DATA GPIO_PIN_0 | |
#define SCLK GPIO_PIN_1 | |
#define SCLK_HI BIT1HI | |
#define SCLK_LO BIT1LO | |
#define RCLK GPIO_PIN_2 | |
#define RCLK_LO BIT2LO | |
#define RCLK_HI BIT2HI | |
#define PORT_E BIT4HI | |
#define POT GPIO_PIN_0 | |
#define GET_MSB_IN_LSB(x) ((x & 0x80)>>7) | |
#define ALL_BITS (0xff<<2) | |
/************************************************************* | |
Varaiables to adjust to calibrate Pot | |
*************************************************************/ | |
#define BinSize 570 | |
#define NumberOfBins 8 | |
#define PotValueOffset 0 | |
// an image of the last 8 bits written to the shift register | |
static uint8_t LocalRegisterImage=0; | |
#define HowMany 1; | |
static uint32_t results[1]; | |
static uint16_t PotValue; | |
static int PotBin=0; | |
// Funtion to initialize shift register | |
void Wrench_SR_Init(void){ | |
// set up port B by enabling the peripheral clock and setting the direction | |
// of PB0, PB1 & PB2 to output | |
//Enable GPIO Port B | |
HWREG(SYSCTL_RCGCGPIO) |= BIT1HI ; | |
//Wait a few cycles for clock | |
while ((HWREG(SYSCTL_PRGPIO) & BIT1HI) != BIT1HI); | |
//Assign Digital Port | |
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (DATA | SCLK | RCLK ); | |
//Assign Port B pin PB0, PB1, and PB2 to be output | |
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= (DATA | SCLK | RCLK ); | |
// start with the data & sclk lines low and the RCLK line high | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(DATA); | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) |= (RCLK); | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(SCLK); | |
} | |
// Function to get current register | |
uint8_t Wrench_SR_GetCurrentRegister(void){ | |
return LocalRegisterImage; | |
} | |
// Function to write to shift register | |
void Wrench_SR_Write(uint8_t NewValue){ | |
LocalRegisterImage = NewValue; // save a local copy | |
// lower the register clock | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(RCLK); | |
//loop 8 times | |
for (int i=0; i<8; i=i+1){ | |
// shift out the data while pulsing the serial clock | |
// Isolate the MSB of NewValue, put it into the LSB position and output | |
if ((NewValue & BIT7HI)== BIT7HI) { | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) |= (DATA); | |
} | |
else{ | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(DATA); | |
} | |
// raise SCLK | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) |= (SCLK); | |
// lower SCLK | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(SCLK); | |
//Shift over MSB | |
NewValue = NewValue<<1; | |
} | |
// raise the register clock to latch the new data | |
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) |= (RCLK); | |
} | |
// Funtion to initialize Pot | |
void Wrench_Pot_Init(void){ | |
ADC_MultiInit(1); | |
} | |
// Function to read pot value | |
uint16_t Wrench_Pot_Read( void){ | |
ADC_MultiRead(results); | |
PotValue = results[0]; | |
return PotValue; | |
} | |
//Function to put Pot reading into bin between 1 and 15 | |
int Wrench_Pot_Bin( void){ | |
uint16_t CurrentPotValue = Wrench_Pot_Read() + PotValueOffset; | |
for (int k=0; k<NumberOfBins; k=k+1){ | |
if ((CurrentPotValue >= k*BinSize) && (CurrentPotValue < (k+1)*BinSize)){ | |
int PotBin = k+1; | |
return PotBin; | |
} | |
} | |
return PotBin; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment