基于MSP430單片機,TI公司采用的是模塊化的內部結構,每個模塊,在各個不同型號的單片機內都是相同的,相同的尋址,相同的操作方式,模塊有限,于是一個一個開始進行整理。 第一個模塊:基礎定時器 參考資料:數據手冊 ,使用手冊 ,示例程序,以及那份特別特別有用的頭文件。 Exampli Code: 進入中斷示例程序 /************************************************************/ 1;時鐘源為ACLK,為單片機提供1/4S定時中斷,LCD提供512HZ刷新頻率 BTCTL=BT_ADLY_250+BT_fLCD_512; //250MS延時加512HZ刷新頻率。 備注:MSP430上電復位后,BT的寄存器值并不會恢復成一個默認值 而是保持不變,因此,每次上電的時,均要進行配置,而且最好直接采用賦值 語句。一條語句足矣 /************************************************************/ /************************************************************/ /************************************************************/ /************************************************************/ Void main(void) { WDTCTL=WDTPW+WDTHOLD; //close the wdtdog FLL_CTL0&= ~XTS_FLL; //可省略,默認選擇低頻晶振 FLL_CTL0|=XCAP18PF; //配置內部晶振 P1DIR|=BIT0; //OUTPUT BTCTL=BT_ADLY_250+BT_fLCD_512; //250MS延時加512HZ刷新頻率。 IE2|=BTIE; //打開中斷 __EINT(); While(1) {;} } /************************************************************/ #pragma vector=BASICTIMER_VECTOR __interrupt void basic_timer_ISR(void) { P1OUT ^= 0x01; // Toggle P1.0 } /************************************************************ 參考的頭文件: * BASIC TIMER 基礎定時器的功能概述 BASIC TIMER 能在無需CPU干擾的情況下產生2的N次方個定時周期 如果我們采用32768KHZ =1/2^15次方,所以,最長的定時時間可以達到 2S鐘 ************************************************************/ #define __MSP430_HAS_BT__ /* Definition to show that Module is available */ #define BTCTL_ (0x0040u) /* Basic Timer Control * /SFR DEFC( BTCTL , BTCTL_) /* 位定義The bit names have been prefixed with "BT" */ #define BTIP0 (0x01) #define BTIP1 (0x02) #define BTIP2 (0x04) #define BTFRFQ0 (0x08) #define BTFRFQ1 (0x10) #define BTDIV (0x20) /* fCLK2 = ACLK:256 */ #define BTHOLD (0x40) /* BT1 is held if this bit is set *,如果這個位置1,則暫停/ 利用 BTCTL|=BTHOLD; //可以使其暫停。 #define BTSSEL (0x80) /* fBT = fMCLK (main clock) 位定義*/ 備注:BTSSEL ,與BTDIV 確定是否對信號源進行分頻。分頻后它的最長延時如果使用32768KHZ的話,可以達到2S 注:為LCD提供的刷新頻率沒有使用分頻。 快捷定義: #define BTCNT1_ (0x0046u) /* Basic Timer Count 1 */ DEFC( BTCNT1 , BTCNT1_) #define BTCNT2_ (0x0047u) /* Basic Timer Count 2 */ DEFC( BTCNT2 , BTCNT2_) /* Frequency of the BTCNT2 coded with Bit 5 and 7 in BTCTL */ #define BT_fCLK2_ACLK (0x00) #define BT_fCLK2_ACLK_DIV256 (BTDIV) #define BT_fCLK2_MCLK (BTSSEL) /* Interrupt interval time fINT coded with Bits 0-2 in BTCTL */ #define BT_fCLK2_DIV2 (0x00) /* fINT = fCLK2:2 (default) */ #define BT_fCLK2_DIV4 (BTIP0) /* fINT = fCLK2:4 */ #define BT_fCLK2_DIV8 (BTIP1) /* fINT = fCLK2:8 */ #define BT_fCLK2_DIV16 (BTIP1+BTIP0) /* fINT = fCLK2:16 */ #define BT_fCLK2_DIV32 (BTIP2) /* fINT = fCLK2:32 */ #define BT_fCLK2_DIV64 (BTIP2+BTIP0) /* fINT = fCLK2:64 */ #define BT_fCLK2_DIV128 (BTIP2+BTIP1) /* fINT = fCLK2:128 */ #define BT_fCLK2_DIV256 (BTIP2+BTIP1+BTIP0) /* fINT = fCLK2:256 */ /* Frequency of LCD coded with Bits 3-4 */ #define BT_fLCD_DIV32 (0x00) /* fLCD = fACLK:32 (default) */ #define BT_fLCD_DIV64 (BTFRFQ0) /* fLCD = fACLK:64 */ #define BT_fLCD_DIV128 (BTFRFQ1) /* fLCD = fACLK:128 */ #define BT_fLCD_DIV256 (BTFRFQ1+BTFRFQ0) /* fLCD = fACLK:256 */ /* LCD frequency values with fBT=fACLK */ #define BT_fLCD_1K (0x00) /* fACLK:32 (default) */ #define BT_fLCD_512 (BTFRFQ0) /* fACLK:64 */ #define BT_fLCD_256 (BTFRFQ1) /* fACLK:128 */ #define BT_fLCD_128 (BTFRFQ1+BTFRFQ0) /* fACLK:256 */ //ACLK提供時鐘源,來提供LCD的刷新頻率。 /* LCD frequency values with fBT=fMCLK */ #define BT_fLCD_31K (BTSSEL) /* fMCLK:32 */ #define BT_fLCD_15_5K (BTSSEL+BTFRFQ0) /* fMCLK:64 */ #define BT_fLCD_7_8K (BTSSEL+BTFRFQ1+BTFRFQ0) /* fMCLK:256 */ ////MCLK提供時鐘源,來提供LCD的刷新頻率。 /* with assumed vlues of fACLK=32KHz, fMCLK=1MHz */ /* fBT=fACLK is thought for longer interval times */ #define BT_ADLY_0_064 (0x00) /* 0.064ms interval (default) */ #define BT_ADLY_0_125 (BTIP0) /* 0.125ms " */ #define BT_ADLY_0_25 (BTIP1) /* 0.25ms " */ #define BT_ADLY_0_5 (BTIP1+BTIP0) /* 0.5ms " */ #define BT_ADLY_1 (BTIP2) /* 1ms " */ #define BT_ADLY_2 (BTIP2+BTIP0) /* 2ms " */ #define BT_ADLY_4 (BTIP2+BTIP1) /* 4ms " */ #define BT_ADLY_8 (BTIP2+BTIP1+BTIP0) /* 8ms " */ #define BT_ADLY_16 (BTDIV) /* 16ms " */ #define BT_ADLY_32 (BTDIV+BTIP0) /* 32ms " */ #define BT_ADLY_64 (BTDIV+BTIP1) /* 64ms " */ #define BT_ADLY_125 (BTDIV+BTIP1+BTIP0) /* 125ms " */ #define BT_ADLY_250 (BTDIV+BTIP2) /* 250ms " */ #define BT_ADLY_500 (BTDIV+BTIP2+BTIP0) /* 500ms " */ #define BT_ADLY_1000 (BTDIV+BTIP2+BTIP1) /* 1000ms " */ #define BT_ADLY_2000 (BTDIV+BTIP2+BTIP1+BTIP0) /* 2000ms " */ //注:ACLK提供定時頻率,利用宏定義進行設置延時。 /* fCLK2=fMCLK (1MHz) is thought for short interval times */ /* the timing for short intervals is more precise than ACLK */ /* NOTE */ /* Be sure that the SCFQCTL-Register is set to 01Fh so that fMCLK=1MHz */ /* Too low interval time results in interrupts too frequent for the processor to handle! */ #define BT_MDLY_0_002 (BTSSEL) /* 0.002ms interval *** interval times */ #define BT_MDLY_0_004 (BTSSEL+BTIP0) /* 0.004ms " *** too short for */ #define BT_MDLY_0_008 (BTSSEL+BTIP1) /* 0.008ms " *** interrupt */ #define BT_MDLY_0_016 (BTSSEL+BTIP1+BTIP0) /* 0.016ms " *** handling */ #define BT_MDLY_0_032 (BTSSEL+BTIP2) /* 0.032ms " */ #define BT_MDLY_0_064 (BTSSEL+BTIP2+BTIP0) /* 0.064ms " */ #define BT_MDLY_0_125 (BTSSEL+BTIP2+BTIP1) /* 0.125ms " */ #define BT_MDLY_0_25 (BTSSEL+BTIP2+BTIP1+BTIP0)/* 0.25ms " */ //選擇fCLK2=fMCLK (1MHz)來提供時鐘源而產生的時鐘延時。適合比較短的延時。但是單片機就不能進入LPM3狀態了。 /* Reset/Hold coded with Bits 6-7 in BT(1)CTL */ /* this is for BT */ //#define BTRESET_CNT1 (BTRESET) /* BTCNT1 is reset while BTRESET is set */ //#define BTRESET_CNT1_2 (BTRESET+BTDIV) /* BTCNT1 .AND. BTCNT2 are reset while ~ is set */ /* this is for BT1 */ #define BTHOLD_CNT1 (BTHOLD) /* BTCNT1 is held while BTHOLD is set */ #define BTHOLD_CNT1_2 (BTHOLD+BTDIV) /* BT1CNT1 .AND. BT1CNT2 are held while ~ is set */ /* INTERRUPT CONTROL BITS */ /* #define BTIE 0x80 */ 位于IE2中斷控制位 /* #define BTIFG 0x80 */ |