以下是学习Lm3s811开发板时写的一个小程序,本人菜鸟,请各位同仁多多指教!
一、main.c
#include "systemInit.h"
// 主函数(程序入口)
void main(void)
{
SysClockInit_6M(); // 时钟初始化:晶振,6MHz
while (1)
{
if(KeyScan())
{
LedControl_D2(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_D2(0);
LedControl_L3(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_L3(0);
LedControl_L4(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_L4(0);
LedControl_L5(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_L5(0);
LedControl_L6(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_L6(0);
LedControl_L7(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_L7(0);
LedControl_L8(1);
SysCtlDelay(500*(TheSysClock/3000)); //延时500ms
LedControl_L8(0);
}
}
}
二、systemInit.c
#include "systemInit.h"
// 定义全局的系统时钟变量
unsigned long TheSysClock = 6000000UL;//默认系统时钟频率为6MHZ
// 系统时钟初始化
void SysClockInit_50M(void)//设置时钟频率为50MHZ,ADC工作时必须配置始终频率42M(14*3)以上,54M(18*3)以下,默认是16.667*3=50M
{
SysCtlLDOSet(SYSCTL_LDO_2_75V); // 配置PLL前须将LDO设为2.75V
SysCtlDelay(1*(TheSysClock/3000)); //延时1ms,等待LDO输出稳定
SysCtlClockSet(SYSCTL_USE_PLL | // 系统时钟设置,采用PLL
SYSCTL_OSC_MAIN | // 主振荡器
SYSCTL_XTAL_6MHZ | // 外接6MHz晶振
SYSCTL_SYSDIV_4); // 分频结果为50MHz
SysCtlDelay(1*(TheSysClock/3000)); //延时1ms,等待时钟稳定
TheSysClock = SysCtlClockGet(); // 获取当前的系统时钟频率
}
void SysClockInit_6M(void)//设置时钟频率为6MHZ
{
SysCtlLDOSet(SYSCTL_LDO_2_50V); // 设置LDO输出电压
SysCtlDelay(1*(TheSysClock/3000)); //延时1ms,等待LDO输出稳定
SysCtlClockSet(SYSCTL_USE_OSC | // 系统时钟设置
SYSCTL_OSC_MAIN | // 采用主振荡器
SYSCTL_XTAL_6MHZ | // 外接6MHz晶振
SYSCTL_SYSDIV_1); // 不分频
SysCtlDelay(1*(TheSysClock/3000)); //延时1ms,等待时钟稳定
TheSysClock = SysCtlClockGet(); // 获取当前的系统时钟频率
}
/*********************KeyScan*********************************************************/
unsigned char KeyScan(void)//检测按键S2有没有被按下,按下返回1,否则返回0
{
unsigned char KeyVal = 0x00;
SysCtlPeriEnable(KEY_S2_PERIPH);
GPIOPinTypeIn(KEY_S2_PORT, KEY_S2_PIN);
if(GPIOPinRead(KEY_S2_PORT,KEY_S2_PIN)==0)//DVD解码板压到位状态
{
SysCtlDelay(20 * (TheSysClock / 3000)); //延时20ms消抖
if(GPIOPinRead(KEY_S2_PORT,KEY_S2_PIN)==0)
{
KeyVal= 1;
}
}
return KeyVal;
}
/****************************************************************************************/
/*********************LedControl_D2**********************************************************/
void LedControl_D2(unsigned char control)//控制LED D2亮灭
{
SysCtlPeriEnable(LED_D2_PERIPH);
GPIOPinTypeOut(LED_D2_PORT, LED_D2_PIN);
if(control)
{
GPIOPinWrite(LED_D2_PORT, LED_D2_PIN,LED_D2_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_D2_PORT, LED_D2_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
/*********************LedControl_L3**********************************************************/
void LedControl_L3(unsigned char control)//控制LED L3亮灭
{
SysCtlPeriEnable(LED_L3_PERIPH);
GPIOPinTypeOut(LED_L3_PORT, LED_L3_PIN);
if(control)
{
GPIOPinWrite(LED_L3_PORT, LED_L3_PIN,LED_L3_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_L3_PORT, LED_L3_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
/*********************LedControl_L4**********************************************************/
void LedControl_L4(unsigned char control)//控制LED L4亮灭
{
SysCtlPeriEnable(LED_L4_PERIPH);
GPIOPinTypeOut(LED_L4_PORT, LED_L4_PIN);
if(control)
{
GPIOPinWrite(LED_L4_PORT, LED_L4_PIN,LED_L4_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_L4_PORT, LED_L4_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
/*********************LedControl_L5**********************************************************/
void LedControl_L5(unsigned char control)//控制LED L5亮灭
{
SysCtlPeriEnable(LED_L5_PERIPH);
GPIOPinTypeOut(LED_L5_PORT, LED_L5_PIN);
if(control)
{
GPIOPinWrite(LED_L5_PORT, LED_L5_PIN,LED_L5_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_L5_PORT, LED_L5_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
/*********************LedControl_L6**********************************************************/
void LedControl_L6(unsigned char control)//控制LED L6亮灭
{
SysCtlPeriEnable(LED_L6_PERIPH);
GPIOPinTypeOut(LED_L6_PORT, LED_L6_PIN);
if(control)
{
GPIOPinWrite(LED_L6_PORT, LED_L6_PIN,LED_L6_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_L6_PORT, LED_L6_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
/*********************LedControl_L7**********************************************************/
void LedControl_L7(unsigned char control)//控制LED L7亮灭
{
SysCtlPeriEnable(LED_L7_PERIPH);
GPIOPinTypeOut(LED_L7_PORT, LED_L7_PIN);
if(control)
{
GPIOPinWrite(LED_L7_PORT, LED_L7_PIN,LED_L7_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_L7_PORT, LED_L7_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
/*********************LedControl_L8**********************************************************/
void LedControl_L8(unsigned char control)//控制LED L8亮灭
{
SysCtlPeriEnable(LED_L8_PERIPH);
GPIOPinTypeOut(LED_L8_PORT, LED_L8_PIN);
if(control)
{
GPIOPinWrite(LED_L8_PORT, LED_L8_PIN,LED_L8_PIN); //点亮LED
}
else
{
GPIOPinWrite(LED_L8_PORT, LED_L8_PIN,0); //熄灭LED
}
}
/****************************************************************************************/
三、systemInit.h
#ifndef __SYSTEM_INIT_H__
#define __SYSTEM_INIT_H__
// 包含必要的头文件
#include <hw_types.h>
#include <hw_memmap.h>
#include <hw_ints.h>
#include <interrupt.h>
#include <sysctl.h>
#include <gpio.h>
// 将较长的标识符定义成较短的形式
#define SysCtlPeriEnable SysCtlPeripheralEnable
#define SysCtlPeriDisable SysCtlPeripheralDisable
#define GPIOPinTypeIn GPIOPinTypeGPIOInput
#define GPIOPinTypeOut GPIOPinTypeGPIOOutput
#define GPIOPinTypeOD GPIOPinTypeGPIOOutputOD
//定义按键 S2 控制引脚
#define KEY_S2_PERIPH SYSCTL_PERIPH_GPIOC
#define KEY_S2_PORT GPIO_PORTC_BASE
#define KEY_S2_PIN GPIO_PIN_4
//定义LED灯 控制引脚
#define LED_D2_PERIPH SYSCTL_PERIPH_GPIOC
#define LED_D2_PORT GPIO_PORTC_BASE
#define LED_D2_PIN GPIO_PIN_5
#define LED_L3_PERIPH SYSCTL_PERIPH_GPIOD
#define LED_L3_PORT GPIO_PORTD_BASE
#define LED_L3_PIN GPIO_PIN_0
#define LED_L4_PERIPH SYSCTL_PERIPH_GPIOD
#define LED_L4_PORT GPIO_PORTD_BASE
#define LED_L4_PIN GPIO_PIN_1
#define LED_L5_PERIPH SYSCTL_PERIPH_GPIOB
#define LED_L5_PORT GPIO_PORTB_BASE
#define LED_L5_PIN GPIO_PIN_0
#define LED_L6_PERIPH SYSCTL_PERIPH_GPIOB
#define LED_L6_PORT GPIO_PORTB_BASE
#define LED_L6_PIN GPIO_PIN_1
#define LED_L7_PERIPH SYSCTL_PERIPH_GPIOE
#define LED_L7_PORT GPIO_PORTE_BASE
#define LED_L7_PIN GPIO_PIN_0
#define LED_L8_PERIPH SYSCTL_PERIPH_GPIOE
#define LED_L8_PORT GPIO_PORTE_BASE
#define LED_L8_PIN GPIO_PIN_1
// 声明全局的系统时钟变量
extern unsigned long TheSysClock;
// 系统时钟初始化
extern void SysClockInit_6M(void);
extern void SysClockInit_50M(void);
//检测按键S2有没有被按下,按下返回1,否则返回0
extern unsigned char KeyScan(void);
//控制LED D2亮灭
extern void LedControl_D2(unsigned char control);
//控制LED L3亮灭
extern void LedControl_L3(unsigned char control);
//控制LED L4亮灭
extern void LedControl_L4(unsigned char control);
//控制LED L5亮灭
extern void LedControl_L5(unsigned char control);
//控制LED L6亮灭
extern void LedControl_L6(unsigned char control);
//控制LED L7亮灭
extern void LedControl_L7(unsigned char control);
//控制LED L8亮灭
extern void LedControl_L8(unsigned char control);
#endif // __SYSTEM_INIT_H__