Académique Documents
Professionnel Documents
Culture Documents
What/Why
--------
#define clrScrn()
printf("\x1b[2J")
#define ALL_BITS (0xff<<2)
/****************************************************************************
Function
pause
Parameters
int: t, a number stands for the wait time
Returns
nothing
Description
pause function, take an int input, with unit of uS, upper limit is
30000 uS
EX: wait 10uS, pause(10)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void pause(int t)
{
// multiply t by 10, as 10 dummy loop needs about 1 uS
t*=10;
// execute dummy loops
for (int i =0;i<t;i++);
}
/****************************************************************************
Function
write
Parameters
Take 3 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
third is the value indicate if the pin will be set as high or low
Returns
nothing
Description
if third param is 0, write this pin low, otherwise write hi, assuming
the pin intialized before
EX: make PF4 hi, write('F',BIT4HI,1)
EX: make PC2 lo, write('C',BIT2HI,0)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void write(char port, uint8_t pin, uint8_t level)
{
// if port is B
if (port == 'B')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is C
else if (port == 'C')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is D
else if (port == 'D')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is E
else if (port == 'E')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is F
else if (port == 'F')
{
// if level is not 0
if (level != 0)
{
// write this pin to high
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
}
else
{
// write this pin to low
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
}
}
/****************************************************************************
Function
read
Parameters
Take 2 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
Returns
nothing
Description
read logical value from the corresponding pin
EX: read from PF4, read('F',BIT4HI)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
uint8_t read(char port, uint8_t pin)
{
// if port is B
if (port == 'B')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is C
else if (port == 'C')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is D
else if (port == 'D')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is E
else if (port == 'E')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is F
else if (port == 'F')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
& pin) ==
& pin) ==
& pin) ==
& pin) ==
& pin) ==
}
// return error number
else return 2;
}
/****************************************************************************
Function
pulse
Parameters
Take 2 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
Returns
nothing
Description
Create a pulse output on indicated pin
EX: pulse PF4, pulse('F',BIT4HI)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void pulse(char port, uint8_t pin)
{
// if port is B
if (port == 'B')
{
// write this pin to high
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
// wait 20 uS
pause(20);
// write this pin to low
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
// wait 20 uS
pause(20);
}
// if port is C
else if(port == 'C')
{
// write this pin to high
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
// wait 20 uS
pause(20);
// write this pin to low
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
// wait 20 uS
pause(20);
}
// if port is D
else if(port == 'D')
{
// write this pin to high
HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
// wait 20 uS
pause(20);
|= pin;
&= ~pin;
|= pin;
&= ~pin;
{
// print the error message
puts("Error: you indicate a wrong port for pulse \n");
}
}
/****************************************************************************
Function
delay
Parameters
int: t, a number stands for the wait time
Returns
nothing
Description
delay function, take an int input, with unit of mS, upper limit is
30000 mS
EX: wait 10mS, delay(10)
!!!: blocking code, don't use in the state machine or event checker
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void delay(int t)
{
int dummy=HWREG(SYSCTL_RCGCGPIO);
}
// if port is F
else if (port == 'F')
{
// Activate the system clock for port F
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R5;
// dummy code to wait some time for execution
int dummy=HWREG(SYSCTL_RCGCGPIO);
}
else
{
// print the error message
puts("Try to activate system clock for an invalid port");
}
}
/****************************************************************************
Function
InitPinIO
Parameters
Take 2 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
Returns
nothing
Description
Initialize the indicated pin as IO pin
// EX: activate PF4 as IO pin, InitPinIO('F',BIT4HI)
// EX: to make PF2,PF4 IO in one time, InitPinIO('F',BIT2HI|BIT4HI)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void InitPinIO(char port, uint8_t pin)
{
// if port is B
if (port == 'B')
{
//activate pin to IO
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= pin;
}
// if port is C
else if (port == 'C')
{
//activate pin to IO
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= pin;
}
// if port is D
else if (port == 'D')
{
//activate pin to IO
HWREG(GPIO_PORTD_BASE+GPIO_O_DEN) |= pin;
}
// if port is E
else if (port == 'E')
{
//activate pin to IO
HWREG(GPIO_PORTE_BASE+GPIO_O_DEN) |= pin;
}
// if port is F
else if (port == 'F')
{
//activate pin to IO
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN) |= pin;
}
else
{
//print the error message
puts("try to set an invalid pin as IO");
}
}
/****************************************************************************
Function
InitPinDir
Parameters
Take 3 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
third is the value indicate if the pin will be set as output or input
Returns
nothing
Description
if third param is 0, write this pin input, otherwise output, assuming
the pin intialized before
// EX: make PF4 output, InitPinDir('F',BIT4HI,1)
// EX: make PC2 input, InitPinDir('C',BIT2HI,0)
// EX: to make PF2,PF4 in output in one time,
InitPinDir('F',BIT2HI|BIT4HI,1)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void InitPinDir(char port, uint8_t pin, uint8_t level)
{
// if port is B
if (port == 'B')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= pin;
else
// set this pin to input
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) &= ~pin;
}
// if port is C
else if (port == 'C')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR)
}
// if port is D
else if (port == 'D')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTD_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTD_BASE+GPIO_O_DIR)
}
// if port is E
else if (port == 'E')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTE_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTE_BASE+GPIO_O_DIR)
}
// if port is F
else if (port == 'F')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR)
}
|= pin;
&= ~pin;
|= pin;
&= ~pin;
|= pin;
&= ~pin;
|= pin;
&= ~pin;
}
// test harness
#ifdef test
int main(void)
{
_HW_Timer_Init(ES_Timer_RATE_1mS);
TERMIO_Init();
clrScrn();
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;
int dummy=HWREG(SYSCTL_RCGCGPIO);
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (BIT6HI|BIT7HI);
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= (BIT6HI|BIT7HI);
for(;;)
pulse('B',BIT6HI);
}
#endif