You are on page 1of 2

//#define TEST

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdint.h>
<stdbool.h>
<stdio.h>
"inc/hw_types.h"
"inc/hw_memmap.h"
"driverlib/sysctl.h"
"driverlib/gpio.h"
"driverlib/interrupt.h"
"utils/uartstdio.h"

#include
#include
#include
#include

"ES_Configure.h"
"ES_Framework.h"
"ES_Port.h"
"termio.h"

#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "ShiftRegister.h"
static uint8_t Last_Write=0;
// Initialize the hardware running the shift register
// Tiva Port B Pins 0-2 will be set to output
// Pin 0 to Serial input, Pin 1 to Shift Clock, Pin 2 to Register clock
// Takes: nothing
// Returns: nothing
void SR_Init(void)
{
// Initialize Port B
// Enable clock on Port B
HWREG(SYSCTL_RCGCGPIO)|=SYSCTL_RCGCGPIO_R1;
while((HWREG(SYSCTL_PRGPIO)&SYSCTL_PRGPIO_R1)!=SYSCTL_PRGPIO_R1)
{
}
// Set Port B Pins 0-2 to digital output
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN)|=(GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2);
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR)|=(GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2);
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))&=~(GPIO_PIN_0|GPIO_PIN_1);
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))|=GPIO_PIN_2;
}
// Allow external programs to determine the last value written to the shift
register
// Takes: nothing
// Returns uint8_t Last_Write: last value written to register
uint8_t SR_GetCurrentRegister(void)
{
// Return value of last write to register
return Last_Write;
}
// Write an input value to the shift register as a binary number
// MSB of input will appear at Q7 of the shift register
// LSB of input will appear at Q0 of the shift register
// Takes: uint8_t NewValue: the value to be written to the shift register
// Returns: nothing
void SR_Write(uint8_t NewValue)
{
// Update Last_Write
Last_Write=NewValue;
// Create mask with only 7th bit HI

uint8_t mask=128;
// set the register clock low
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))&=~GPIO_PIN_2;
for(int i=0;i<8;i++)
{
// compare the input against a mask and write the MSB to the
register
if ((mask&NewValue)==mask)
{
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))|=GPIO_PIN_0;
}
else
{
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))&=~GPIO_PIN_0;
}
// Pulse the shift clock
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))|=GPIO_PIN_1;
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))&=~GPIO_PIN_1;
//shift mask to check next most significant bit
mask=mask>>1;
}
// shift the register
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))|=GPIO_PIN_2;
}
#ifdef TEST
// test harness
int main(void)
{
SR_Init();
TERMIO_Init();
puts("testing shift register");
uint8_t tester=7;
SR_Write(tester);
return 0;
}
#endif

You might also like