?? 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 + -