Sei sulla pagina 1di 10

//*****************************************************************************

// Copyright (c) 2014 Texas Instruments Incorporated. All rights reserved.


// Software License Agreement
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This file was automatically generated by the Tiva C Series PinMux Utility
// Version: 1.0.4
//
//*****************************************************************************
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "utils/uartstdio.h"
#include "my_dice.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "driverlib/interrupt.h"
#include "driverlib/timer.h"
#include "driverlib/gpio.h"
#include "inc/tm4c123gh6pm.h"
#include "driverlib/uart.h"

//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

volatile unsigned int number1;


volatile unsigned int number2;

void ClearAllLeds(void);
void RedDigit(void);
void BlueDigit(void);
void Spiral(void);
void Loading(void);
void Introduction(void);
void Border(void);

void
PortFunctionInit(void)
{
//
// Enable Peripheral Clocks
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

//
// Enable pin PA7 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_7);

//
// Enable pin PA5 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_5);

//
// Enable pin PA3 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);

//
// Enable pin PA6 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6);

//
// Enable pin PA2 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2);

//
// Enable pin PA4 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4);
//
// Enable pin PC7 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_7);

//
// Enable pin PC6 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_6);

//
// Enable pin PC5 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_5);

//
// Enable pin PC4 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4);

//
// Enable pin PD6 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6);

//
// Enable pin PD2 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_2);

//
// Enable pin PD3 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);

//
// Enable pin PD1 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1);

//
// Enable pin PD7 for GPIOOutput
//

//
//First open the lock and select the bits we want to modify in the GPIO commit
register.
//
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x80;

//
//Now modify the configuration of the pins that we unlocked.
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7);

//
// Enable pin PE1 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_1);

//
// Enable pin PE3 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_3);

//
// Enable pin PE2 for GPIOOutput
//
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_2);

//
// Enable pin PF0 for GPIOInput
//

//
//First open the lock and select the bits we want to modify in the GPIO commit
register.
//
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1;

//
//Now modify the configuration of the pins that we unlocked.
//
MAP_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0);

GPIO_PORTF_PUR_R |= 0x01;
}

//Globally enable interrupts


void IntGlobalEnable(void)
{
__asm(" cpsie i\n");
}

//Globally disable interrupts


void IntGlobalDisable(void)
{
__asm(" cpsid i\n");
}

void Timer0A_Init(unsigned long period)


{
//
// Enable Peripheral Clocks
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); // configure for 32-bit
timer mode
TimerLoadSet(TIMER0_BASE, TIMER_A, period -1); //reload value
IntPrioritySet(INT_TIMER0A, 0x00);
// configure Timer0A interrupt priority as 0
IntEnable(INT_TIMER0A);
// enable interrupt 19 in NVIC (Timer0A)
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // arm timeout interrupt
TimerEnable(TIMER0_BASE, TIMER_A); //
enable timer0A
}

void Timer0A_Handler(void)
{

TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // acknowledge


flag for Timer0A timeout

Loading();
//Trigger a Loading LED
Animation

void
uart_Init(void) {

//SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |


SYSCTL_XTAL_16MHZ);

SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,


(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
}

void
Interrupt_Init(void)
{
IntEnable(INT_GPIOF); // enable
interrupt 30 in NVIC (GPIOF)
IntPrioritySet(INT_GPIOF, 0x02); // configure GPIOF interrupt
priority as 2
GPIO_PORTF_IM_R |= 0x01; // arm interrupt on PF0
GPIO_PORTF_IS_R &= ~0x01; // PF0 is edge-sensitive
GPIO_PORTF_IBE_R &= ~0x01; // PF0 not both edges trigger

GPIO_PORTF_IEV_R &= ~0x01; // PF0 falling edge event


IntMasterEnable(); // globally enable
interrupt
}

void GPIOPortF_Handler(void){

//Prevents Debounce
NVIC_EN0_R &= ~0x40000000;
SysCtlDelay(53333); //
Delay for a while
NVIC_EN0_R |= 0x40000000;

GPIO_PORTF_ICR_R |= 0x01; // acknowledge flag for PF0


char num1[1];
char num2[1];

if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0)==0x00) //If PF0 is pressed


{

Border();
ClearAllLeds();
//Clear the LEDs turned on by the Periodic Interrupt
Spiral();
//Show a Spiral LED animation
ClearAllLeds();
//Clear the LEDs turned on by the Spiral

number1 = rand()%6 + 1;
//Get a random number from 1-6
RedDigit();
//Trigger the LEDs that's correlated with the number

number2 = rand()%6 + 1;
//Get a random number from 1-6
BlueDigit();
//Trigger the LEDs that's correlated with the number

//Test
UARTCharPut(UART0_BASE, '\n');
UARTCharPut(UART0_BASE, '|');
UARTCharPut(UART0_BASE, '\t');

sprintf(num1, "%d", number1);


UARTCharPut(UART0_BASE, num1[0]); //Print the number
for Dice 1

UARTCharPut(UART0_BASE, '\t');
UARTCharPut(UART0_BASE, '|');
UARTCharPut(UART0_BASE, '\t');

sprintf(num2, "%d", number2);


UARTCharPut(UART0_BASE, num2[0]); //Print the number
for Dice 2

UARTCharPut(UART0_BASE, '\t');
UARTCharPut(UART0_BASE, '|');
UARTCharPut(UART0_BASE, '\n');
UARTCharPut(UART0_BASE, '\r');
UARTCharPut(UART0_BASE, '\n');

Border();
UARTCharPut(UART0_BASE, '\n');
SysCtlDelay(10000000);
//Delay for 3 Seconds
ClearAllLeds();
//Clear the LEDs that represented Dice 1 and 2
}

}
int main(void)
{

//srand(clock()*100);

unsigned long period = 20000000; //reload value to Timer0A to


generate a second delay

PortFunctionInit();
//Initialize the GPIO ports
uart_Init();
//Initialize the UART

Introduction();

Timer0A_Init(period); //Initialize
Timer0A and configure the interrupt
Interrupt_Init();
//Initialize the Interrupt

//
// Loop forever.
//
while(1)
{

}
}

void Introduction(){

char msg[] = "\tYou Feel'n Lucky?\n\rPress The Button to Roll The Dice\n\r";
int i = 0;

while(msg[i]!='\0'){
UARTCharPut(UART0_BASE, msg[i]);
i++;
}
}

void Border(){
char msg[] = "*********************************\n\r";
int i = 0;

while(msg[i]!='\0'){
UARTCharPut(UART0_BASE, msg[i]);
i++;
}
}

void ClearAllLeds()
{
GPIO_PORTA_DATA_R &= ~0xFC;
GPIO_PORTD_DATA_R &= ~0xCE;
GPIO_PORTE_DATA_R &= ~0x0E;
GPIO_PORTC_DATA_R &= ~0xF0;
}
void RedDigit()
{
switch(number1){
case 1 :
GPIO_PORTD_DATA_R |= 0x04;

break;
case 2 :
GPIO_PORTA_DATA_R |= 0x20;
GPIO_PORTE_DATA_R |= 0x08;
break;
case 3 :
GPIO_PORTA_DATA_R |= 0x20;
GPIO_PORTD_DATA_R |= 0x04;
GPIO_PORTE_DATA_R |= 0x08;

break;
case 4 :
GPIO_PORTA_DATA_R |= 0x20;
GPIO_PORTA_DATA_R |= 0x80;
GPIO_PORTE_DATA_R |= 0x02;
GPIO_PORTE_DATA_R |= 0x08;
break;
case 5 :
GPIO_PORTA_DATA_R |= 0x20;
GPIO_PORTA_DATA_R |= 0x80;
GPIO_PORTD_DATA_R |= 0x04;
GPIO_PORTE_DATA_R |= 0x02;
GPIO_PORTE_DATA_R |= 0x08;
break;
case 6 :
GPIO_PORTA_DATA_R |= 0x20;
GPIO_PORTA_DATA_R |= 0x80;
GPIO_PORTD_DATA_R |= 0x02;
GPIO_PORTD_DATA_R |= 0x08;
GPIO_PORTE_DATA_R |= 0x02;
GPIO_PORTE_DATA_R |= 0x08;
break;
}
}

void BlueDigit()
{
switch(number2){

case 1 :
GPIO_PORTD_DATA_R |= 0x40;

break;
case 2 :
GPIO_PORTC_DATA_R |= 0x10;
GPIO_PORTA_DATA_R |= 0x04;
break;
case 3 :
GPIO_PORTC_DATA_R |= 0x10;
GPIO_PORTD_DATA_R |= 0x40;
GPIO_PORTA_DATA_R |= 0x04;
break;
case 4 :
GPIO_PORTC_DATA_R |= 0x50;
GPIO_PORTA_DATA_R |= 0x14;
break;
case 5 :
GPIO_PORTC_DATA_R |= 0x50;
GPIO_PORTD_DATA_R |= 0x40;
GPIO_PORTA_DATA_R |= 0x14;
break;
case 6 :
GPIO_PORTC_DATA_R |= 0xD0;
GPIO_PORTD_DATA_R |= 0x80;
GPIO_PORTA_DATA_R |= 0x14;
break;
}
}

void Spiral()
{
GPIO_PORTA_DATA_R |= 0x20;
GPIO_PORTC_DATA_R |= 0x40;
SysCtlDelay(350000); //Generate a 0.10s Delay
GPIO_PORTA_DATA_R |= 0x40;
GPIO_PORTC_DATA_R |= 0x20;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTA_DATA_R |= 0x80;
GPIO_PORTC_DATA_R |= 0x10;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTD_DATA_R |= 0x08;
GPIO_PORTC_DATA_R |= 0x80;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTE_DATA_R |= 0x08;
GPIO_PORTA_DATA_R |= 0x10;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTE_DATA_R |= 0x04;
GPIO_PORTA_DATA_R |= 0x08;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTE_DATA_R |= 0x02;
GPIO_PORTA_DATA_R |= 0x04;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTD_DATA_R |= 0x02;
GPIO_PORTD_DATA_R |= 0x80;
SysCtlDelay(350000); //Generate a 0.10s Delay

GPIO_PORTD_DATA_R |= 0x04;
GPIO_PORTD_DATA_R |= 0x08;
SysCtlDelay(350000); //Generate a 0.10s Delay
}

void Loading()
{
GPIO_PORTA_DATA_R ^= 0x20;
GPIO_PORTD_DATA_R ^= 0x02;
GPIO_PORTE_DATA_R ^= 0x02;
SysCtlDelay(500000); //Generate a 0.15s Delay
GPIO_PORTA_DATA_R ^= 0x40;
GPIO_PORTD_DATA_R ^= 0x04;
GPIO_PORTE_DATA_R ^= 0x04;
SysCtlDelay(500000); //Generate a 0.15s Delay
GPIO_PORTA_DATA_R ^= 0x80;
GPIO_PORTD_DATA_R ^= 0x08;
GPIO_PORTE_DATA_R ^= 0x08;
SysCtlDelay(500000); //Generate a 0.15s Delay
GPIO_PORTC_DATA_R ^= 0x10;
GPIO_PORTC_DATA_R ^= 0x80;
GPIO_PORTA_DATA_R ^= 0x10;
SysCtlDelay(500000); //Generate a 0.15s Delay
GPIO_PORTC_DATA_R ^= 0x20;
GPIO_PORTD_DATA_R ^= 0x40;
GPIO_PORTA_DATA_R ^= 0x08;
SysCtlDelay(500000); //Generate a 0.15s Delay
GPIO_PORTC_DATA_R ^= 0x40;
GPIO_PORTD_DATA_R ^= 0x80;
GPIO_PORTA_DATA_R ^= 0x04;
SysCtlDelay(500000); //Generate a 0.15s Delay
}

Potrebbero piacerti anche