Anda di halaman 1dari 6

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

//
// uart_echo.c - Example for reading data from and writing data to the UART in
//
an interrupt driven fashion.
//
// Copyright (c) 2005-2013 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 10007 of the EK-LM3S8962 Firmware Package.
//
//*****************************************************************************
#include <string.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/pin_map.h"
#include "Glcd_st7565.h"
long ulBuffer[16];
unsigned long SWnSelect=2;
unsigned long ulFrame0[10]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0xFF};
unsigned long ulFrame1[10]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x0B,0x0A,0xFF};
unsigned long ulFrame2[10]={0x01,0x02,0x03,0x04,0x05,0x0D,0x0C,0x0B,0x0A,0xFF};
unsigned long ulFrame3[10]={0x01,0x02,0x03,0x10,0x0F,0x0D,0x0C,0x0B,0x0A,0xFF};
unsigned long iCount;
unsigned char iUart=2;
unsigned char iLine=7, iColumn=0;
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>UART Echo (uart_echo)</h1>
//!
//! This example application utilizes the UART to echo text. The first UART
//! (connected to the FTDI virtual serial port on the evaluation board) will be
//! configured in 115,200 baud, 8-n-1 mode. All characters received on the
//! UART are transmitted back to the UART.
//
//*****************************************************************************
//*****************************************************************************

//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
void
UART0IntHandler(void)
{
unsigned long ulStatus;
ulStatus = UARTIntStatus(UART0_BASE, true);
UARTIntClear(UART0_BASE, ulStatus);
if (UARTRxErrorGet(UART0_BASE)==0)
{
while(UARTCharsAvail(UART0_BASE))
{
ulBuffer[iCount]=UARTCharGetNonBlocking(UART0_BASE);
iCount++;
}
//ulBuffer[iCount-1]=iCount-3;
iUart=0;
}
}
//******************************************************************************
*****************
//
//
//
//******************************************************************************
*****************
void
UART1IntHandler(void)
{
unsigned long ulStatus;
ulStatus = UARTIntStatus(UART1_BASE, true);
UARTIntClear(UART1_BASE, ulStatus);
if (UARTRxErrorGet(UART1_BASE)==0)
{
while(UARTCharsAvail(UART1_BASE))
{
ulBuffer[iCount]=UARTCharGetNonBlocking(UART1_BASE);
iCount++;
}
//ulBuffer[iCount-1]=iCount-3;
iUart=1;
}
}

//***************************************************************************
//
//
//
//***************************************************************************
void
UART0Init (void)
{
UARTDisable(UART0_BASE);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 9600,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX4_8, UART_FIFO_RX4_8);
IntEnable(INT_UART0);
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
UARTEnable(UART0_BASE);
}
//*****************************************************************************
//
//
//
//*****************************************************************************
void
UART1Init (void)
{
UARTDisable(UART1_BASE);// uart1 disable active
GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3);
UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 9600,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
UARTFIFOLevelSet(UART1_BASE, UART_FIFO_TX4_8, UART_FIFO_RX4_8);
IntEnable(INT_UART1);
UARTIntEnable(UART1_BASE, UART_INT_RX | UART_INT_RT);
UARTEnable(UART1_BASE);
}
//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
//
// Loop while there are more characters to send.
//
while(ulCount--)
{
//
// Write the next character to the UART.
//
UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
}
}
//*************************************************************************

//
//error check frame
//
//***********************************************************************
void
ErrorCheck (void)
{
if (((iCount-3)!=ulBuffer[iCount-1])||(ulBuffer[0]!=0x01)||(ulBuffer[iCo
unt]!=0xFF))
{
UARTSend("\n Error Frame", strlen("\n Error Frame"));
switch(iUart)
{
case 0:
UARTCharPutNonBlocking(UART0_BASE, '2');
while (UARTBusy(UART0_BASE)){}
break;
case 1:
UARTCharPutNonBlocking(UART1_BASE, '2');
while (UARTBusy(UART1_BASE)){}
break;
}
iCount=0;
iUart=2;
}
}
//*************************************************************************
//
//display data on the terminal
//
//*************************************************************************
void
UARTNumberDisplay(void)
{
unsigned char j;
if (iCount!=0)
{
//ErrorCheck();
switch(iUart)
{
case 0:
for (j=0; j<iCount; j++)
UARTCharPutNonBlocking(UART0_BASE, ulBuf
fer[j]);
while (UARTBusy(UART0_BASE)){}
break;
case 1:
for (j=0; j<iCount; j++)
UARTCharPutNonBlocking(UART1_BASE, ulBuf
fer[j]);
while (UARTBusy(UART1_BASE)){}
break;
}
}
iCount=0;
iUart=2;
}

//******************************************************************************
//
//display GLCD
//
//******************************************************************************
void
UARTNumberDisplayGLCD(void)
{
unsigned char j;
if (iCount!=0)
{
for (j=0; j<iCount; j++)
{
//ulBuffer[j]=-48;
GLCD_PutChar57(iLine, iColumn,ulBuffer[j]);
iColumn+=6;
if (iColumn>=128)
{
iColumn=0;
if (iLine==0)
{
iLine=8;
clear_display();
}
iLine--;
}
}
iCount=0;
iUart=2;
}
}
//
//
//
//
//
void
ButtonInput (void)
{
//unsigned char iVal;
if (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0)==0)
{
while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0)==0) {};
}
else if (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1)==0)
{
while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1)==0) {};
}
else if (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)==0)
{
while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)==0) {};
}
else if (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0)

{
while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0) {};
}
}
//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
int
main(void)
{
//
// Set the clocking to run directly from the crystal.
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_8MHZ);
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 |
GPIO_PIN_2 | GPIO_PIN_3);
GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2
| GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_S
TD_WPU);
//
// Enable processor interrupts.
//
IntMasterEnable();
UART0Init();
UART1Init();
Enable_GLCD();
//UARTCharPutNonBlocking(UART0_BASE, 222);
while(1)
{
UARTNumberDisplayGLCD();
//ButtonInput();
}
}

Anda mungkin juga menyukai