×
单片机 > 单片机程序设计 > 详情

LM3S1138入门2,跑马灯

发布时间:2020-06-08 发布时间:
|
很简单了哈,这里不罗嗦了。

LED.h头文件

#ifndef  _LED_H_
#define  _LED_H_


//  定义LED名称
#define  LED1       0x01
#define  LED2       0x02
#define  LED3       0x04


//  初始化指定的LED
extern  void  LED_Init(unsigned char  ucLED);


//  点亮指定的LED
extern  void  LED_On(unsigned char  ucLED);


//  熄灭指定的LED
extern  void  LED_Off(unsigned char  ucLED);


//  反转指定的LED
extern  void  LED_Toggle(unsigned char  ucLED);


#endif  //  _LED_H_

LED.c文件:

//  包含必要的头文件
#include  "LED.H"
#include 
#include 
#include 
#include 
#include 
#include 


//  将较长的标识符定义成较短的形式
#define  SysCtlPeriEnable       SysCtlPeripheralEnable
#define  SysCtlPeriDisable      SysCtlPeripheralDisable
#define  GPIOPinTypeOut         GPIOPinTypeGPIOOutput


/***************************************************************************************************
功能:初始化指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_Init(unsigned char  ucLED)
{
    if ( ucLED & LED1 )
    {
        SysCtlPeriEnable(SYSCTL_PERIPH_GPIOD);                  //  使能GPIOD端口
        GPIOPinTypeOut(GPIO_PORTD_BASE , GPIO_PIN_0);           //  设置PD0为输入类型
        LED_Off(LED1);                                          //  熄灭LED1
    }

    if ( ucLED & LED2 )
    {
        SysCtlPeriEnable(SYSCTL_PERIPH_GPIOG);                  //  使能GPIOG端口
        GPIOPinTypeOut(GPIO_PORTG_BASE , GPIO_PIN_2);           //  设置PG2输入类型
        LED_Off(LED2);                                          //  熄灭LED2
    }

    if ( ucLED & LED3 )
    {
        SysCtlPeriEnable(SYSCTL_PERIPH_GPIOG);                  //  使能GPIOG端口
        GPIOPinTypeOut(GPIO_PORTG_BASE , GPIO_PIN_3);           //  设置PG3输入类型
        LED_Off(LED3);                                          //  熄灭LED3
    }
}


/***************************************************************************************************
功能:点亮指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_On(unsigned char  ucLED)
{
    if ( ucLED & LED1 )
    {
        GPIOPinWrite(GPIO_PORTD_BASE , GPIO_PIN_0 , 0x00);      //  PD0输出低电平
    }

    if ( ucLED & LED2 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_2 , 0x00);      //  PG2输出低电平
    }

    if ( ucLED & LED3 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_3 , 0x00);      //  PG3输出低电平
    }
}


/***************************************************************************************************
功能:熄灭指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_Off(unsigned char  ucLED)
{
    if ( ucLED & LED1 )
    {
        GPIOPinWrite(GPIO_PORTD_BASE , GPIO_PIN_0 , 0x01);          //  PD0输出高电平
    }

    if ( ucLED & LED2 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_2 , 0x01 << 2);     //  PG2输出高电平
    }

    if ( ucLED & LED3 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_3 , 0x01 << 3);     //  PG3输出高电平
    }
}


/***************************************************************************************************
功能:反转指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_Toggle(unsigned char  ucLED)
{
    unsigned char  ucVal;

    if ( ucLED & LED1 )
    {
        ucVal   =  GPIOPinRead(GPIO_PORTD_BASE , GPIO_PIN_0);   //  读取PD0的输出状态
        ucVal  ^=  0x01;                                        //  状态反转
        GPIOPinWrite(GPIO_PORTD_BASE , GPIO_PIN_0 , ucVal);     //  更新PD0的输出状态
    }

    if ( ucLED & LED2 )
    {
        ucVal   =  GPIOPinRead(GPIO_PORTG_BASE , GPIO_PIN_2);   //  读取PG2的输出状态
        ucVal  ^=  0x01 << 2;                                   //  状态反转
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_2 , ucVal);     //  更新PG2的输出状态
    }

    if ( ucLED & LED3 )
    {
        ucVal   =  GPIOPinRead(GPIO_PORTG_BASE , GPIO_PIN_3);   //  读取PG3的输出状态
        ucVal  ^=  0x01 << 3;                                   //  状态反转
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_3 , ucVal);     //  更新PG3的输出状态
    }
}

主函数main.c

//  基于《Stellaris外设驱动库》的例程:两只LED交替闪烁


//  包含必要的头文件
#include  "LED.H"
#include 
#include 
#include 
#include 
#include 
#include 


//  将较长的标识符定义成较短的形式
#define  SysCtlPeriEnable       SysCtlPeripheralEnable
#define  SysCtlPeriDisable      SysCtlPeripheralDisable
#define  GPIOPinTypeIn          GPIOPinTypeGPIOInput
#define  GPIOPinTypeOut         GPIOPinTypeGPIOOutput


//  定义KEY
#define  KEY_PERIPH             SYSCTL_PERIPH_GPIOG
#define  KEY_PORT               GPIO_PORTG_BASE
#define  KEY_PIN                GPIO_PIN_5


//  防止JTAG失效
void  JTAG_Wait(void)
{
    SysCtlPeriEnable(KEY_PERIPH);                               //  使能KEY所在的GPIO端口
    GPIOPinTypeIn(KEY_PORT , KEY_PIN);                          //  设置KEY所在管脚为输入

    if ( GPIOPinRead(KEY_PORT , KEY_PIN)  ==  0x00 )            //  如果复位时按下KEY,则进入
    {
        for (;;);                                               //  死循环,以等待JTAG连接
    }

    SysCtlPeriDisable(KEY_PERIPH);                              //  禁止KEY所在的GPIO端口
}


//  定义全局的系统时钟变量
unsigned long  TheSysClock  =  12000000UL;


//  延时
void  Delay(unsigned long  ulVal)
{
    while ( --ulVal  !=  0 );
}


//  系统初始化
void  SystemInit(void)
{
    SysCtlLDOSet(SYSCTL_LDO_2_50V);                             //  设置LDO输出电压

    SysCtlClockSet(SYSCTL_USE_OSC |                             //  系统时钟设置,采用主振荡器
                   SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ |
                   SYSCTL_SYSDIV_1);

/*
    SysCtlLDOSet(SYSCTL_LDO_2_75V);                             //  配置PLL前将LDO电压设置为2.75V

    SysCtlClockSet(SYSCTL_USE_PLL |                             //  系统时钟设置,采用PLL
                   SYSCTL_OSC_MAIN |                            //  主振荡器
                   SYSCTL_XTAL_6MHZ |                           //  外接6MHz晶振
                   SYSCTL_SYSDIV_10);                           //  分频结果为20MHz
*/

    TheSysClock  =  SysCtlClockGet();                           //  获取系统时钟,单位:Hz

    LED_Init(LED1 | LED2|LED3);                                      //  初始化LED1和LED2
}


//  主函数(程序入口)
int  main(void)
{
    JTAG_Wait();                                                //  防止JTAG失效,重要!

    SystemInit();                                               //  系统初始化

    for (;;)
    {
        LED_On(LED1);
        Delay(200 * (TheSysClock / 4000));                      //  延时约200ms
        LED_Off(LED1);
        LED_On(LED2);
        Delay(200 * (TheSysClock / 4000));                      //  延时约200ms
        LED_Off(LED2);
        LED_On(LED3);
        Delay(200 * (TheSysClock / 4000));                      //  延时约200ms
        LED_Off(LED3);
    }
}




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
8051单片机的函数发生器的设计