Académique Documents
Professionnel Documents
Culture Documents
Module
ShiftRegisterWrite.c
Revision
1.0.1
Description
This module acts as the low level interface to a write only shift register.
Notes
History
When
-------------10/26/16 00:00
10/11/15 19:55
Who
--rdb
jec
What/Why
--------
first pass
****************************************************************************/
// 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"
#include "ShiftRegisterWrite.h"
#define SR_SIZE
/****************************************************************************
Function
SR_Init
Parameters
Returns
Description
Initializes hardware for the shift register
Notes
Author
R. Davis Born, 10/26/16, 00:00
****************************************************************************/
void SR_Init(void){
printf("Initializing Shift Register service\n\r");
// Initialize PORT A
HWREG(SYSCTL_RCGCGPIO) |= SR_PORT_A_INIT;
while((HWREG(SYSCTL_RCGCGPIO) & SR_PORT_A_WAIT_CLK) != SR_PORT_A_WAIT_CLK)
{
// Wait for clock to be ready
}
// Initialize SHIFT REGISTER STRING 0
printf("Initializing Shift Register 0\n\r");
// Enable Pins to be Digital I/O
HWREG(SR_PORT_A + GPIO_O_DEN) |= (SR_DATA_A0 | SR_SCLK_A0 | SR_RCLK_A0);
// Set Pin 0 on Port B to be a Digital Output
HWREG(SR_PORT_A + GPIO_O_DIR) |= (SR_DATA_A0 | SR_SCLK_A0 | SR_RCLK_A0);
// Initialize SHIFT REGISTER STRING 1
if(NUM_SR_STRINGS > 1)
{
printf("Initializing Shift Register 1\n\r");
// Enable Pins to be Digital I/O
HWREG(SR_PORT_A + GPIO_O_DEN) |= (SR_DATA_A1 | SR_SCLK_A1 |
SR_RCLK_A1);
// Set Pin 0 on Port B to be a Digital Output
HWREG(SR_PORT_A + GPIO_O_DIR) |= (SR_DATA_A1 | SR_SCLK_A1 |
SR_RCLK_A1);
}
// Initialize SHIFT REGISTER STRING 2 (requires PORT E to be initialized)
if(NUM_SR_STRINGS > 2)
{
printf("Initializing Shift Register 2\n\r");
HWREG(SYSCTL_RCGCGPIO) |= SR_PORT_B_INIT;
while((HWREG(SYSCTL_RCGCGPIO) & SR_PORT_B_WAIT_CLK) !=
SR_PORT_B_WAIT_CLK)
{
// Wait for clock to be ready
}
// Enable Pins to be Digital I/O
HWREG(SR_PORT_B + GPIO_O_DEN) |= (SR_DATA_B0 | SR_SCLK_B0 |
SR_RCLK_B0);
// Set Pin 0 on Port B to be a Digital Output
HWREG(SR_PORT_B + GPIO_O_DIR) |= (SR_DATA_B0 | SR_SCLK_B0 |
SR_RCLK_B0);
}
// Initialize SHIFT REGISTER STRING 3
if(NUM_SR_STRINGS > 3)
{
printf("Initializing Shift Register 3\n\r");
// Enable Pins to be Digital I/O
HWREG(SR_PORT_B + GPIO_O_DEN) |= (SR_DATA_B1 | SR_SCLK_B1 |
SR_RCLK_B1);
// Set Pin 0 on Port B to be a Digital Output
HWREG(SR_PORT_B + GPIO_O_DIR) |= (SR_DATA_B1 | SR_SCLK_B1 |
SR_RCLK_B1);
}
}
/****************************************************************************
Function
SR_GetCurrentRegister
Parameters
Returns
uint8_t last value written to the shift register
Description
Notes
Author
R. Davis Born, 10/26/16, 00:00
****************************************************************************/
uint8_t SR_GetCurrentRegister(uint8_t SR_STRING_X){
// Return the last thing written to shift register depending on
SR_STRING_X
switch(SR_STRING_X)
{
case SR_STRING_0:
return LocalRegisterImage_0;
case SR_STRING_1:
return LocalRegisterImage_1;
case SR_STRING_2:
return LocalRegisterImage_2;
case SR_STRING_3:
return LocalRegisterImage_3;
default:
printf("Invalid SR_STRING specified\n\r");
return LocalRegisterImage_1;
}
}
/****************************************************************************
Function
SR_Write
Parameters
Returns
Description
Shifts the data into the shift register
Notes
Author
R. Davis Born, 10/26/16, 00:00
****************************************************************************/
void SR_Write(uint8_t SR_STRING_X, uint8_t NewValue){
uint8_t BitCounter;
uint8_t SCLK = SR_SCLK_A0;
uint32_t SR_PORT = SR_PORT_A;
// Set the shift clock and the port depending on the SR_STRING_X
switch(SR_STRING_X)
{
case SR_STRING_0:
SCLK = SR_SCLK_A0;
SR_PORT = SR_PORT_A;
LocalRegisterImage_0 = NewValue; //
break;
case SR_STRING_1:
SCLK = SR_SCLK_A1;
SR_PORT = SR_PORT_A;
LocalRegisterImage_1 = NewValue; //
break;
case SR_STRING_2:
SCLK = SR_SCLK_B0;
SR_PORT = SR_PORT_B;
LocalRegisterImage_1 = NewValue; //
break;
case SR_STRING_3:
SCLK = SR_SCLK_B1;
SR_PORT = SR_PORT_B;
LocalRegisterImage_3 = NewValue; //
break;
default:
printf("Invalid SR_STRING specified
break;
}
// shift out the data while pulsing the serial clock
// FOR EACH of the bits in NewValue
for(BitCounter = 0; BitCounter < SR_SIZE; BitCounter++)
{
// Isolate the MSB of NewValue, put it into the LSB position and
output
SR_setDS(SR_STRING_X, GET_MSB_IN_LSB(NewValue));
// Pulse shift clock
SR_pulse(SR_PORT, SCLK);
}
/****************************************************************************
Function
SR_Post
Parameters
Returns
Description
Pulses the register clock of the given shift register to upload data to the
output of the shift register
Notes
Author
R. Davis Born, 10/26/16, 00:00
****************************************************************************/
void SR_Post(uint8_t SR_STRING_X)
{
uint8_t RCLK = SR_RCLK_A0;
uint32_t SR_PORT = SR_PORT_A;
// Determine the register clock and the port depending on SR_STRING_X
switch(SR_STRING_X)
case SR_STRING_0:
RCLK = SR_RCLK_A0;
SR_PORT = SR_PORT_A;
break;
case SR_STRING_1:
RCLK = SR_RCLK_A1;
SR_PORT = SR_PORT_A;
break;
case SR_STRING_2:
RCLK = SR_RCLK_B0;
SR_PORT = SR_PORT_B;
break;
case SR_STRING_3:
RCLK = SR_RCLK_B1;
SR_PORT = SR_PORT_B;
break;
default:
printf("Invalid SR_STRING specified in SR_Post()\n\r");
break;
}
// Pulse the given register clock to output the data
SR_pulse(SR_PORT, RCLK);
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
SR_setDS
Parameters
uint8_t DSVal either 1 or 0 to set the data bit HIGH or LOW
Returns
Description
Notes
Author
R. Davis Born, 10/26/16, 00:00
****************************************************************************/
static void SR_setDS(uint8_t SR_STRING_X, uint8_t DSVal)
{
uint8_t DATA = SR_DATA_A0;
uint32_t SR_PORT = SR_PORT_A;
// Determines the data line and the port depending on SR_STRING_X
switch(SR_STRING_X)
{
case SR_STRING_0:
DATA = SR_DATA_A0;
SR_PORT = SR_PORT_A;
break;
case SR_STRING_1:
DATA = SR_DATA_A1;
SR_PORT = SR_PORT_A;
break;
case SR_STRING_2:
DATA = SR_DATA_B0;
SR_PORT = SR_PORT_B;
break;
case SR_STRING_3:
//printf("Set Data Audio\n\r");
DATA = SR_DATA_B1;
SR_PORT = SR_PORT_B;
break;
default:
printf("Invalid SR_STRING specified in SR_SetDS()\n\r");
break;
pin
port low %d \n\r", DATA);
&= ~DATA;
/****************************************************************************
Function
SR_pulse
Parameters
uint8_t PulseBIT the bit to pulse high then low
Returns
Description
Notes
Author
R. Davis Born, 10/26/16, 00:00
****************************************************************************/
static void SR_pulse(uint32_t SR_PORT_X, uint8_t SR_XCLK_X)
{
// set the clock pin HI
HWREG(SR_PORT_X + (GPIO_O_DATA + ALL_BITS) ) |= SR_XCLK_X;