新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > MSP430單片機學習小記1--基礎定時器

        MSP430單片機學習小記1--基礎定時器

        作者: 時間:2016-08-23 來源:網絡 收藏

          基于單片機,公司采用的是模塊化的內部結構,每個模塊,在各個不同型號的單片機內都是相同的,相同的尋址,相同的操作方式,模塊有限,于是一個一個開始進行整理。

        本文引用地址:http://www.104case.com/article/201608/295905.htm

          第一個模塊:基礎定時器

          參考資料:數據手冊 ,使用手冊 ,示例程序,以及那份特別特別有用的頭文件。

          Exampli Code:

          進入中斷示例程序

          /************************************************************/

          1;時鐘源為ACLK,為單片機提供1/4S定時中斷,LCD提供512HZ刷新頻率

          BTCTL=BT_ADLY_250+BT_fLCD_512; //250MS延時加512HZ刷新頻率。

          備注:上電復位后,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|=BE; //打開中斷

          __EINT();

          While(1)

          {;}

          }

          /************************************************************/

          #pragma vector=BASICMER_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 ___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 */



        關鍵詞: TI MSP430

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 乌什县| 邛崃市| 青阳县| 乌兰县| 东安县| 全椒县| 离岛区| 青神县| 吉木乃县| 光泽县| 文山县| 天柱县| 离岛区| 长治县| 华安县| 呼伦贝尔市| 罗甸县| 邵阳县| 诸城市| 柳河县| 休宁县| 融水| 云阳县| 琼海市| 鱼台县| 突泉县| 山阳县| 平阴县| 昂仁县| 马鞍山市| 精河县| 高州市| 唐山市| 韶山市| 迭部县| 克拉玛依市| 重庆市| 大连市| 繁昌县| 阳江市| 织金县|