📄 timerdemo.c
字号:
#include <LPC213x.H> // Philips Peripheral Registers
#include <LPC213xDEF.H> //ARM菜鸟HotPower创建定义文件
/*----------------------------------------------------
文 件 名: LPC213XDEF.H
创 建 人: ARM菜鸟 HotPower@126.com
创 建 日 期: 2005.7.23 0:38
最近修改日期: 2006.1.11 2:38
创 建 地 点 : 西安大雁塔村队部
说 明 : 已经过硬件调试
实 验 项 目 : 蜂鸣器实验
------------------------------------------------------*/
/*-----------------------------------------
LED数码字符定义
-------------------------------------------*/
//共阳极数码管(左移)
#define LedSegA 0x01
#define LedSegB 0x02
#define LedSegC 0x04
#define LedSegD 0x08
#define LedSegE 0x10
#define LedSegF 0x20
#define LedSegG 0x40
#define LedSegH 0x80
//
//共阳极数码管(右移)
/*
#define LedSegA 0x80
#define LedSegB 0x40
#define LedSegC 0x20
#define LedSegD 0x10
#define LedSegE 0x08
#define LedSegF 0x04
#define LedSegG 0x02
#define LedSegH 0x01
*/
//
#define LedChar0 (LedSegA + LedSegB + LedSegC + LedSegD + LedSegE + LedSegF )
#define LedChar1 ( LedSegB + LedSegC )
#define LedChar2 (LedSegA + LedSegB + LedSegD + LedSegE + LedSegG)
#define LedChar3 (LedSegA + LedSegB + LedSegC + LedSegD + LedSegG)
#define LedChar4 ( LedSegB + LedSegC + LedSegF + LedSegG)
#define LedChar5 (LedSegA + LedSegC + LedSegD + LedSegF + LedSegG)
#define LedChar6 (LedSegA + LedSegC + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedChar7 (LedSegA + LedSegB + LedSegC )
#define LedChar8 (LedSegA + LedSegB + LedSegC + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedChar9 (LedSegA + LedSegB + LedSegC + LedSegD + LedSegF + LedSegG)
#define LedCharA (LedSegA + LedSegB + LedSegC + LedSegE + LedSegF + LedSegG)
#define LedCharB ( LedSegC + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedCharC (LedSegA + LedSegD + LedSegE + LedSegF )
#define LedCharD ( LedSegB + LedSegC + LedSegD + LedSegE + LedSegG)
#define LedCharE (LedSegA + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedCharF (LedSegA + LedSegE + LedSegF + LedSegG)
static const unsigned char LedTab[] =
{
~LedChar0,
~LedChar1,
~LedChar2,
~LedChar3,
~LedChar4,
~LedChar5,
~LedChar6,
~LedChar7,
~LedChar8,
~LedChar9,
~LedCharA,
~LedCharB,
~LedCharC,
~LedCharD,
~LedCharE,
~LedCharF
};
#define RCLK P0_29
//#define SCK0 P0_28 //<LPC213xDEF.H>已定义
//#define MOSI0 P0_25 //<LPC213xDEF.H>已定义
#define BEEP P0_7// P0.7控制蜂鸣器,低电平蜂鸣
#define LED1 P1_18//
#define LED2 P1_19//
#define LED3 P1_20//
#define LED4 P1_21//
#define LED5 P1_22//
#define LED6 P1_23//
#define LED7 P1_24//
#define LED8 P1_25//
/* System configuration .Fosc、Fcclk、Fcco、Fpclk must be defined */
/* 系统设置, Fosc、Fcclk、Fcco、Fpclk必须定义*/
#define Fosc 11059200 //Crystal frequence,10MHz~25MHz,should be the same as actual status.
//应当与实际一至晶振频率,10MHz~25MHz,应当与实际一至
#define Fcclk (Fosc * 4) //System frequence,should be (1~32)multiples of Fosc,and should be equal or less than 60MHz.
//系统频率,必须为Fosc的整数倍(1~32),且<=60MHZ
#define Fcco (Fcclk * 4) //CCO frequence,should be 2、4、8、16 multiples of Fcclk, ranged from 156MHz to 320MHz.
//CCO频率,必须为Fcclk的2、4、8、16倍,范围为156MHz~320MHz
#define Fpclk (Fcclk / 4) * 1 //VPB clock frequence , must be 1、2、4 multiples of (Fcclk / 4).
//VPB时钟频率,只能为(Fcclk / 4)的1、2、4倍
//函数声明
void Timer0Init(void);
void PortInit(void);
void VicIntSetup(void);
//函数声明
void LedDisplay(unsigned char);
void MSPI_SendData(unsigned char);
void DelayNS (uint32 dly);
/*
*********************************************************************************************************
** 函数名称 :IRQ_Timer0()
** 函数功能 :定时器0中断服务程序,取反LED9控制口。
** 入口参数 :无
** 出口参数 :无
*********************************************************************************************************
*/
void IRQ_Timer0 (void) __irq
{
static unsigned char val = 0;
static unsigned int Count = 0;
if (Count < 2) {
Count ++;
}
else {
Count = 0;
IO1PIN ^= (1 << LED1)
| (1 << LED2)
| (1 << LED3)
| (1 << LED4)
| (1 << LED5)
| (1 << LED6)
| (1 << LED7)
| (1 << LED8);//LED交替闪烁
LedDisplay(LedTab[val]);
val ++;
val &= 0x0f;
}
IO0PIN ^= (1 << BEEP);//BEEP蜂鸣
T0IR = 0x01; /* 清除中断标志 */
VICVectAddr = 0x00; /* 通知VIC中断处理结束 */
}
void VicIntSetup(void)
{
/* 设置定时器0中断IRQ */
VICIntSelect = 0x00; /* 所有中断通道设置为IRQ中断 */
VICVectCntl0 = 0x20 | 0x04; /* 设置定时器0中断通道分配最高优先级 */
VICVectAddr0 = (uint32)IRQ_Timer0; /* 设置中断服务程序地址 */
VICIntEnable = 1 << 0x04; /* 使能定时器0中断 */
}
void PortInit(void)
{
PINSEL0 = 0x00000000; // 设置管脚连接GPIO
PINSEL1 = 0x00000000; // 设置管脚连接GPIO
IO0DIR = 0x00000000; // 设置P0口为输入
IO1DIR = 0x00000000; // 设置P1口为输入
// PINSEL0 |= (P0_29_GPIO << P0_29_PINSEL) | (P0_4_SCK0 << P0_4_PINSEL) | (P0_6_MOSI0 << P0_6_PINSEL);
// PINSEL0 |= (P0_7_GPIO << P0_7_PINSEL);
/*
PINSEL1 |= (P1_18_GPIO << P1_18_PINSEL)
| (P1_19_GPIO << P1_19_PINSEL)
| (P1_20_GPIO << P1_20_PINSEL)
| (P1_21_GPIO << P1_21_PINSEL)
| (P1_22_GPIO << P1_22_PINSEL)
| (P1_23_GPIO << P1_23_PINSEL)
| (P1_24_GPIO << P1_24_PINSEL)
| (P1_25_GPIO << P1_25_PINSEL);
*/
IO0DIR |= (1 << BEEP); // 设置BEEP控制口为输出
IO0DIR |= (1 << RCLK); // 设置RCLK控制口为输出
IO0DIR |= (1 << SCK0); // 设置SCK0控制口为输出
IO0DIR |= (1 << MOSI0); // 设置MOSI0控制口为输出
IO1DIR |= (1 << LED1) // 设置LED1为输出
| (1 << LED2) // 设置LED2为输出
| (1 << LED3) // 设置LED3为输出
| (1 << LED4) // 设置LED4为输出
| (1 << LED5) // 设置LED5为输出
| (1 << LED6) // 设置LED6为输出
| (1 << LED7) // 设置LED7为输出
| (1 << LED8); // 设置LED8为输出
IO0PIN = (0 << RCLK)
| (1 << SCK0)
| (1 << MOSI0)
| (1 << BEEP);
IO1PIN = (1 << LED1)
| (1 << LED3)
| (1 << LED5)
| (1 << LED7);
}
void Timer0Init(void)
{
/* 定时器0初始化 */
T0TC = 0; /* 定时器设置为0 */
T0PR = 0; /* 时钟不分频 */
T0MCR = 0x03; /* 设置T0MR0匹配后复位T0TC,并产生中断标志 */
T0MR0 = Fpclk / 2; /* 0.5秒钟定时 */
T0TCR = 0x01; /* 启动定时器 */
}
void LedDisplay(unsigned char data)
{
MSPI_SendData(data);
IO0SET = (1 << RCLK);
IO0CLR = (1 << RCLK);
}
void MSPI_SendData(unsigned char data)
{
unsigned char i;
for (i = 0; i < 8; i ++) {
IO0CLR = (1 << SCK0);
if (data & 0x80) {
IO0SET = (1 << MOSI0);
}
else {
IO0CLR = (1 << MOSI0);
}
data <<= 1;
IO0SET = (1 << SCK0);
}
}
/*
*********************************************************************************************************
** 函数名称 :DelayNS()
** 函数功能 :长软件延时
** 入口参数 :dly 延时参数,值越大,延时越久
** 出口参数 :无
*********************************************************************************************************
*/
void DelayNS (uint32 dly)
{
uint32 i;
for ( ; dly>0; dly--)
for (i=0; i<50000; i++);
}
/*
*********************************************************************************************************
** 函数名称 :main()
** 函数功能 :用P0.7控制BEEP,让BEEP鸣叫。
** 调试说明 :需将跳线JP5和LED短接。
*********************************************************************************************************
*/
int main (void)
{
PortInit();
Timer0Init();
VicIntSetup();
while (1)
{
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -