msp430g2553秒表代码
MSP430G2553捕获程序案例与经验分享
MSP430G2553捕获程序案例与经验分享MSP430G2553单片机定时器A有3个捕获比较寄存器CCR0,CCR1,CCR2.。
MSP430G2553捕获程序应用很广泛,电子工程师可以多加了解。
所谓捕获,就是我们来检测外围的信号跳变时刻(此时信号理解为数字信号,即脉冲),此信号乃为我们捕获的对象,可以测量信号的脉冲宽度,即频率等。
捕获首先需要考虑的初始化工作1.设置BCS模块,确定系统时钟MCLK子系统时钟SMCLK把MCLK设置为8MHZ,SMCLK设置为1MHZ。
2.捕获输入引脚的选择选择IO引脚时应查阅器件的手册,能够快速的查阅PDF资料找到正确的答案是一个程序员的基本素质。
3.程序设计思路根据测频的原理,需要2次捕获才能测量一次输入信号的频率。
因此要定义2个变量保存2次捕获结果。
变量是无符号的整数型变量(与捕获寄存器的字长匹配)。
输入信号与CPU的工作是异步的,所以设计程序的时候是不知道什么时候才有捕获输入。
程序处理何时发生了捕获的方法有2种一是查询的方法,定时器硬件在发生捕获事件后会置捕获中断表示CCIF为1,程序在主循环里不断的查询这个标志即可判断是否有捕获事件发生。
二是定时器中断法,当发生捕获事件时必产生定时器中断,在中断中读取捕获寄存器即可。
查询的方法不是好的程序设计方法,因为查询时要占用CPU,使得CPU不能再做其他任务。
中断的方法对初学者有一定的困难。
即中断程序如何与主程序通信(交换信息)。
理解中断及设计中断服务程序要困难一些。
捕获模式捕获外部输入的信号的上升沿或下降沿或上升沿下降沿都捕捉,当捕捉发生时,把TAR 的值装载到TACCRx中,同时也可以进入中断,执行相应的操作。
这样利用捕捉上升沿或。
G2553时钟设置
时钟操作模式时钟源:1、ACLK:系统辅助时钟,来自 LFXT1CLK 为32768HZ。
2、MCLK:系统主时钟,有以下5种选择:1、32768HZ,来自ACLK。
2、1MHZ: BCSCTL1 = CALBC1_1MHZ;DCOCTL = CALDCO_1MHZ;3、8MHZ: BCSCTL1 = CALBC1_8MHZ;DCOCTL = CALDCO_8MHZ;4、12MHZ: BCSCTL1 = CALBC1_12MHZ;DCOCTL = CALDCO_12MHZ;5、16MHZ: BCSCTL1 = CALBC1_16MHZ;DCOCTL = CALDCO_16MHZ;3、SMCLK:子系统时钟,一般为16MHZ。
MSP430具有一种运行模式及5种可利用软件来选择的低功耗操作模式。
一个中断事件能够将器件从任一低功耗模式唤醒、处理请求、并在接收到来自中断程序的返回信号时恢复至低功耗模式。
以下6种操作模式可利用软件来配置:•激活模式(AM)–所有时钟处于激活状态•低功耗模式0 (LPM0)–CPU被禁用–ACLK和SMCLK仍然有效,MCLK被禁用•低功耗模式1 (LPM1)–CPU被禁用–ACLK和SMCLK仍然有效,MCLK被禁用–如果DCO不是在激活模式下被使用,则DCO的dc生成器被禁用•低功耗模式2 (LPM2)–CPU被禁用–MCLK和SMCLK被禁用–DCO的dc生成器保持启用–ACLK保持激活•低功耗模式3 (LPM3)–CPU被禁用–MCLK和SMCLK被禁用–DCO的dc生成器保持启用–ACLK保持激活•低功耗模式 4(LPM4)–CPU被禁用–ACLK被禁用–MCLK和SMCLK被禁用–DCO的dc生成器保持启用–晶体振荡器被停止时钟结构图CPU。
ADXL-MSP430G2553
#ifndef ADXL_G2553_H_#define ADXL_G2553_H_#define WORD unsigned short#define uchar unsigned char#define uint unsigned int/*#define SCL_1 P1OUT|=BIT5 //IIC时钟引脚定义#define SCL_0 P1OUT&=~BIT5#define SCL_OUT P1DIR|=BIT5#define SDA P1IN&BIT4#define SDA_1 P1OUT|=BIT4 //IIC数据引脚定义#define SDA_0 P1OUT&=~BIT4#define SDA_IN P1DIR&=~BIT4#define SDA_OUT P1DIR|=BIT4*/#define SCL_1 P1OUT|=BIT0 //IIC时钟引脚定义#define SCL_0 P1OUT&=~BIT0#define SCL_OUT P1DIR|=BIT0#define SDA P1IN&BIT1#define SDA_1 P1OUT|=BIT1 //IIC数据引脚定义#define SDA_0 P1OUT&=~BIT1#define SDA_IN P1DIR&=~BIT1#define SDA_OUT P1DIR|=BIT1#define SlaveAddress 0xA6 //定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改//ALT ADDRESS引脚接地时地址为0xA6,接电源时地址为0x3A//接收数据缓存区//====================器件宏定义======================#define DEVID 0xe5 //器件ID#define THRESH_TAP 0x1d //敲击阈值#define OFSX 0x1e //X轴偏移#define OFSY 0x1f //Y轴偏移#define OFSZ 0x20 //Z轴偏移#define DUR 0x21 //敲击持续时间#define Latent 0x22 //敲击延迟#define Window 0x23 //敲击窗口#define THRESH_ACT 0x24 //活动阈值#define THRESH_INACT 0x25 //静止阈值#define TIME_INACT 0x26 //静止时间#define ACT_INACT_CTL 0x27 //轴使能控制活动和静止检测#define THRESH_FF 0x28 //自由落体阈值#define TIME_FF 0x29 //自由落体时间#define TAP_AXES 0x2a //单击/双击轴控制#define ACT_TAP_STATUS 0x2b //单机/双击源#define BW_RATE 0x2c //数据速率及功率模式控制#define POWER_CTL 0x2d //省电特性控制#define INT_ENABLE 0x2e //中断使能控制#define INT_MAP 0x2f //中断映射控制#define INT_SOURCE 0x30 //中断源#define DATA_FORMATE 0x31 //数据格式控制#define DATAX0 0x32 //X轴数据-低八位#define DATAX1 0x33 //X轴数据-高八位#define DATAY0 0x34 //Y轴数据-低八位#define DATAY1 0x35 //Y轴数据-高八位#define DATAZ0 0x36 //Z轴数据-低八位#define DATAZ1 0x37 //Z轴数据-高八位#define FIFO_CTL 0x38 //FIFO控制#define FIFO_STATUS 0x39 //FIFO状态extern void first_Angle(void);extern void Angle(void);extern void display_Angle(void);extern void display_XYZ(void);extern void Init_ADXL345();extern void Multiple_Read_ADXL345(void);#endif /* ADXL_G2553_H_ */#include"msp430g2553.h"#include <math.h>#include"ADXL345-G2553.h"#include "HT1621IO.h"char BUF[8];uchar ge,shi,bai,qian,wan; //显示变量int data_xyz[3];void delay_ADXL345(unsigned int k);void Init_ADXL345(void); //初始化ADXL345void conversion(uint temp_data);void Single_Write_ADXL345(uchar REG_Address,uchar REG_data); //单个写入数据uchar Single_Read_ADXL345(uchar REG_Address); //单个读取内部寄存器数据void Multiple_Read_ADXL345(void); //连续的读取内部寄存器数据void Delay5us();void Delay5ms();void ADXL345_Start();void ADXL345_Stop();void ADXL345_SendACK(char ack);char ADXL345_RecvACK();void ADXL345_SendByte(char dat);char ADXL345_RecvByte();//void ADXL345_ReadPage();//void ADXL345_WritePage();//*********************************************************void conversion(uint temp_data){temp_data=temp_data%10000; //取余运算qian=temp_data/1000;temp_data=temp_data%1000; //取余运算bai=temp_data/100;temp_data=temp_data%100; //取余运算shi=temp_data/10;/* if(qian==1){bai=9;shi=9;}*/}/*******************************/void delay_ADXL345(unsigned int k){unsigned int i,j;for(i=0;i<k;i++){for(j=0;j<12;j++){;}}}/**************************************延时5微秒(STC90C52RC@12M)不同的工作环境,需要调整此函数,注意时钟过快时需要修改当改用1T的MCU时,请调整此延时函数**************************************/void Delay5us(){__delay_cycles(60);}/**************************************延时5毫秒(STC90C52RC@12M)不同的工作环境,需要调整此函数当改用1T的MCU时,请调整此延时函数**************************************/void Delay5ms(){__delay_cycles(6000);}/**************************************起始信号**************************************/ void ADXL345_Start(){SCL_OUT;SDA_OUT;SDA_1; //拉高数据线_NOP();SCL_1; //拉高时钟线Delay5us(); //延时SDA_0; //产生下降沿Delay5us(); //延时SCL_0; //拉低时钟线}/**************************************停止信号**************************************/ void ADXL345_Stop(){SDA_OUT;SDA_0; //拉低数据线_NOP();SCL_1; //拉高时钟线Delay5us(); //延时SDA_1; //产生上升沿Delay5us(); //延时}/**************************************发送应答信号入口参数:ack (0:ACK 1:NAK)**************************************/ void ADXL345_SendACK(char ack){SDA_OUT;if(ack)SDA_1;elseSDA_0;_NOP();SCL_1; //拉高时钟线Delay5us(); //延时SCL_0; //拉低时钟线// Delay5us(); //延时SDA_1;}/**************************************接收应答信号**************************************/char ADXL345_RecvACK(){char ack;SDA_IN;SCL_0; //拉高时钟线Delay5us(); //延时SCL_1;if(SDA)ack=0; //读应答信号elseack=1;SCL_0; //拉低时钟线Delay5us(); //延时SDA_OUT;return ack ;}/**************************************向IIC总线发送一个字节数据**************************************/void ADXL345_SendByte(char dat){char i;SDA_OUT;for (i=0; i<8; i++) //8位计数器{Delay5us(); //延时if((dat&0x80)==0x80) //发送数据的最高位{SDA_1;}else{SDA_0;}//送数据口_NOP();SCL_1; //拉高时钟线Delay5us(); //延时SCL_0; //拉低时钟线dat<<=1;}ADXL345_RecvACK();}/**************************************从IIC总线接收一个字节数据**************************************/char ADXL345_RecvByte(){char i;char dat = 0;SDA_IN;_NOP();for (i=0; i<8; i++) //8位计数器{dat <<= 1;SCL_1; //拉高时钟线Delay5us(); //延时if(SDA)dat++;SCL_0; //拉低时钟线Delay5us(); //延时}return dat;}//******单字节写入*******************************************void Single_Write_ADXL345(uchar REG_Address,uchar REG_data){ADXL345_Start(); //起始信号ADXL345_SendByte(SlaveAddress); //发送设备地址+写信号ADXL345_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf22页ADXL345_SendByte(REG_data); //内部寄存器数据,请参考中文pdf22页ADXL345_Stop(); //发送停止信号}//********单字节读取*****************************************uchar Single_Read_ADXL345(uchar REG_Address){ uchar REG_data;ADXL345_Start(); //起始信号ADXL345_SendByte(SlaveAddress); //发送设备地址+写信号ADXL345_SendByte(REG_Address); //发送存储单元地址,从0开始ADXL345_Start(); //起始信号ADXL345_SendByte(SlaveAddress+1); //发送设备地址+读信号REG_data=ADXL345_RecvByte(); //读出寄存器数据ADXL345_SendACK(1);ADXL345_Stop(); //停止信号return REG_data;}//*********************************************************////连续读出ADXL345内部加速度数据,地址范围0x32~0x37////*********************************************************//*********************************************************************** void Multiple_Read_ADXL345(void){uchar i;ADXL345_Start(); //起始信号ADXL345_SendByte(SlaveAddress); //发送设备地址+写信号ADXL345_SendByte(0x32); //发送存储单元地址,从0x32开始ADXL345_Start(); //起始信号ADXL345_SendByte(SlaveAddress+1); //发送设备地址+读信号for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF {BUF[i] = ADXL345_RecvByte(); //BUF[0]存储0x32地址中的数据if (i == 5){ADXL345_SendACK(1); //最后一个数据需要回NOACK }else{ADXL345_SendACK(0); //回应ACK}}ADXL345_Stop(); //停止信号Delay5ms();}//*****************************************************************//初始化ADXL345,根据需要请参考pdf进行修改************************void Init_ADXL345(){Single_Write_ADXL345(0x31,0x0B); //测量范围,正负16g,13位模式。
MSP430G2553学习笔记(数据手册)
MSP430G2553学习笔记(数据手册)MSP430G2553性能参数(DIP—20) 工作电压范围:1.8~3。
6V。
5种低功耗模式。
16位的RISC结构,62。
5ns指令周期.超低功耗:运行模式—230µA;待机模式—0.5µA;关闭模式—0.1µA;可以在不到1µs的时间里超快速地从待机模式唤醒.基本时钟模块配置:具有四种校准频率并高达16MHz的内部频率;内部超低功耗LF振荡器;32。
768KHz晶体;外部数字时钟源。
两个16 位Timer_A,分别具有三个捕获/比较寄存器。
用于模拟信号比较功能或者斜率模数(A/D)转换的片载比较器。
带内部基准、采样与保持以及自动扫描功能的10位200—ksps 模数(A/D)转换器。
16KB闪存,512B的RAM。
16个I/O口。
注意:MSP430G2553无P3口!MSP430G2553的时钟基本时钟系统的寄存器DCOCTL—DCO控制寄存器DCOxDCO频率选择控制1MODxDCO频率校正选择,通常令MODx=0注意:在MSP430G2553上电复位后,默认RSEL=7,DCO=3,通过数据手册查得DCO频率大概在0.8~1。
5MHz之间。
BCSCTL1—基本时钟控制寄存器1XT2OFF不用管,因为MSP430G2553内部没有XT2提供的HF时钟XTS不用管,默认复位后的0值即可DIV Ax设置ACLK的分频数00 /101 /210 /411 /8RSELxDCO频率选择控制2BCSCTL2-基本时钟控制寄存器2SELMxMCLK的选择控制位00 DCOCLK01 DCOCLK10 LFXT1CLK或者VLOCLK11 LFXT1CLK或者VLOCLK DIVMx设置MCLK的分频数00 /101 /210 /411 /8SELSSMCLK的选择控制位0 DCOCLK1 LFXT1CLK或者VLOCLK DIVSx设置SMCLK的分频数00 /101 /210 /411 /8DCORDCO直流发生电阻选择,此位一般设00 内部电阻1 外部电阻BCSCTL3—基本时钟控制寄存器3XT2Sx不用管LFXT1Sx00 LFXT1选为32。
MSP430G2553寄存器资料讲解
MSP430G2553学习笔记常用赋值运算符:清除:&=~ ,置位:|= ,测试:&= ,取反:^= ,看门狗模块:WDT(看门狗)WDTCTL 看门狗定时器控制寄存器15--8 7 6 5 4 3 2 1 0口令HOLD NMIES NMI TMSEL CNTCL SSEL IS1 IS0IS1,IS0 选择看门狗定时器的定时输出,T是WDTCNT的输入时钟源周期0 T x 2(15)1 T x 2(13)2 T x 2(9)3 T x 2(6)SSEL 选择WDTCNT的时钟源0 SMCLK1 ACLKIS0、IS1、SSEL可确定WDT定时时间,WDT只能定时8种和时钟源相关的时间WDT可选的定时时间(晶体为32768HZ,SMCLK=1MHZ)SSEL IS1 IS0 定时时间/ms0 1 1 0.056 Tsmclk x 2(6)0 1 0 0.5 Tsmclk x 2(9)1 1 1 1.9 Taclk x 2(6)0 0 1 8 Tsmclk x 2(13)1 1 0 16 Taclk x 2(9)0 0 0 32 Tsmclk x 2(15)(PUC复位后的值)1 0 1 250 Taclk x 2(13)1 0 0 1000 Taclk x 2(15)CNTCL当该位为1时,清除WDTCNTTMSEL 工作模式选择0 看门狗模式1 定时器模式NMI 选择RST/NMI引脚功能,在PUC后被复位0 RST/NMI引脚为复位端1 RST/NMI引脚为边沿触发的非屏蔽中断输入NMIES 选择中断的边沿触发方式0 上升沿触发NMI中断1 下降沿触发NMI中断HOLD 停止看门狗定时器工作,降低功耗0 WDT功能激活1 时钟禁止输入,计数停止WDT(看门狗)配置语句WDTCTL=WDTPW+WDTHOLD;//将WDTPW+WDTHOLD赋值给WDTCTL,关闭看门狗定时器控制寄存器(Stop watchdogtimer)IE1 |= WDTIE;//使能WDT中断WDTCTL = WDT_ADL Y_1000;//WDT 1 s / 4间隔计时器WDTCTL = WDTPW + WDTHOLD + WDTNMI + WDTNMIES;//WDTCTL 由高8位口令和低8位控制命令组成,要写入操作WDT的控制命令,出于安全原因必须先正确写入高字节看门狗口令。
MSP430单片机秒表程序(完整)
MSP430单片机"秒表" 程序(完整)/*******************************************************基于MSP430F449单片机的秒表*功能:秒计时,8位数码管显示,包括小时、分钟、秒和毫秒*此程序同样适用于其他系列单片机*by:duyunfu1987******************************************************/#include "msp430x44x.h"#define DPYOUT P3OUT //数码管的段选输出口#define DPYCOM P2OUT //38译码器的ABC输入#define OPENOUT P2OUT |= BIT3 //74HC573使能锁存段选#define CLOSEOUT P2OUT &= ~BIT3//74HC573无效int hour,min,sec,ms; //缓冲区定义,小时、分钟、秒、毫秒int count = 0; //2ms计数,计到5时ms增1//共“阴”极数码管的码表unsigned char LED7CC[] ={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};//延时n(us)void delay_us(int n){ while(n-- >0)_NOP();}//延时n(ms)void delay_ms(int dms){ int i;while(dms-- >0){ for(i=0;i<250;i++);}}//初始化缓冲区与IO口void Init(){hour = 0;min = 0;sec = 0;ms = 0;P2SEL = 0;P3SEL = 0;P2DIR |= BIT0+BIT1+BIT2+ BIT3;//A B C 使能位P2DIR &= ~(BIT4+BIT5+BIT6); //按键P3DIR = 0xff;P3OUT = 0x00;}//8位数码管动态显示函数void display(){DPYOUT = 0;_NOP();DPYOUT = LED7CC[ms%10]; DPYCOM = 7;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[ms/10]; DPYCOM = 6;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[sec%10]|0x80; DPYCOM = 5;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[sec/10]; DPYCOM = 4;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[min%10]|0x80; DPYCOM = 3;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[min/10]; DPYCOM = 2;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[hour%10]|0x80; DPYCOM = 1;OPENOUT;CLOSEOUT;DPYOUT = LED7CC[hour/10]; DPYCOM = 0;OPENOUT;CLOSEOUT;}//按键处理函数void key_deal(int key){switch(key){case 0x60: //START--开始计时{ BTCTL = BT_ADL Y_2;IE2 |= BTIE;_EINT();}break;case 0x50: //STOP -- 停止BTCTL |= BTHOLD; break;case 0x30: //CLEAR--缓冲区清零{ hour = 0;min = 0;sec = 0;ms = 0;}break;default : break;}display();}//主函数void main( void ){int key;// Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD;FLL_CTL0 |= XCAP18PF;Init();while(1){if((key = P2IN & 0x70)!=0x70){delay_ms(10);if((key = P2IN & 0x70)!=0x70){ key_deal(key);}}display();}}//BT中断服务程序,2ms计时#pragma vector = BASICTIMER_VECTOR__interrupt void BT_ISR(){count ++;if(count == 5){ count = 0;ms ++;}if(ms == 100){ms = 0;sec ++;if(sec == 60){sec = 0;min ++;if(min == 60){min = 0;hour ++;if(hour == 24)hour = 0;}}}}。
msp430g2553例程大全
MSPG2553 例程1.//************************************************************************* *****// LaunchPad Lab2 - Software Toggle P1.0,//// MSP430G2xx2// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |// | P1.0|-->LED////************************************************************************* *****#include <msp430g2553.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerif (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF){while(1); // If calibration constants erased, trap CPU!!}// Configure Basic ClockBCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ; // Set DCO step + modulationBCSCTL3 |= LFXT1S_2; // Set LFXT1P1DIR = BIT6; // P1.6 output (green LED)P1OUT = 0; // LED offIFG1 &= ~OFIFG; // Clear OSCFault flagBCSCTL2 |=SELM_1 + DIVM_0; // Set MCLKfor(;;){P1OUT = BIT6; // P1.6 on (green LED)_delay_cycles(100);P1OUT = 0; // green LED off_delay_cycles(5000);}}2.//************************************************************************* *****// LaunchPad Lab3 - Software Port Interrupt Service//// MSP430G2xx2// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// /|\ | |// --o--|P1.3 P1.0|-->LED// \|/////************************************************************************* *****#include <msp430g2553.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerP1DIR |= BIT0; // Set P1.0 to output directionP1IES |= BIT3; // P1.3 Hi/lo edgeP1IFG &= ~BIT3; // P1.3 IFG clearedP1IE |= BIT3; // P1.3 interrupt enabled_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt }// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port_1(void){if (P1IFG & BIT3){P1OUT ^= BIT0; // P1.0 = toggleP1IFG &= ~BIT3; // P1.3 IFG cleared }}3.//************************************************************************* *****// LaunchPad Lab5 - ADC10, Sample A10 Temp and Convert to oC and oF//// MSP430G2452// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |// |A10 |////************************************************************************* *****#include "msp430g2553.h"long temp;long IntDegF;long IntDegC;void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDT//Configure ADC10ADC10CTL1 = INCH_10 + ADC10DIV_3; // Choose ADC Channel as Temp SensorADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;//Choose ADC Ref source__enable_interrupt(); // Enable interrupts.TACCR0 = 30; // Delay to allow Ref to settleTACCTL0 |= CCIE; // Compare-mode interrupt.TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode.LPM0; // Wait for delay.TACCTL0 &= ~CCIE; // Disable timer Interrupt__disable_interrupt();while(1){ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start__bis_SR_register(LPM0_bits + GIE); // LPM0 with interrupts enabled// oF = ((A10/1024)*1500mV)-923mV)*1/1.97mV = A10*761/1024 - 468temp = ADC10MEM;IntDegF = ((temp - 630) * 761) / 1024;// oC = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278temp = ADC10MEM;IntDegC = ((temp - 673) * 423) / 1024;__no_operation(); // SET BREAKPOINT HERE}}// ADC10 interrupt service routine#pragma vector=ADC10_VECTOR__interrupt void ADC10_ISR (void){__bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from 0(SR)}#pragma vector=TIMER0_A0_VECTOR__interrupt void ta0_isr(void){TACTL = 0;__bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from 0(SR)}4.//************************************************************************* *****// MSP430F20xx Demo - Basic Clock, Output Buffered SMCLK, ACLK and MCLK/10 //// Description: Buffer ACLK on P2.0, default SMCLK(DCO) on P1.4 and MCLK/10 on // P1.5.// ACLK = LFXT1 = VLO, MCLK = SMCLK = default DCO// //* External watch crystal installed on XIN XOUT is required for ACLK *////// MSP430F20xx// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |// | P1.4/SMCLK|-->SMCLK = Default DCO// | P1.5|-->MCLK/10 = DCO/10// | P1.0/ACLK|-->ACLK = VLO//// M. Buccini / L. Westlund// Texas Instruments Inc.// October 2005// Built with IAR Embedded Workbench Version: 3.40A//************************************************************************* *****#include <msp430x20x3.h>unsigned char s;void main(void){WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog TimerBCSCTL3 |= LFXT1S_2; // LFXT1 = VLO//DCOCTL = 0;//BCSCTL1 = CALBC1_16MHZ;//DCOCTL = CALBC1_16MHZ;P1DIR |= 0x31; // P1.0,5 and P1.4 outputsP1SEL |= 0x11; // P1.0,4 ACLK/VLO, SMCLK/DCO output//SMCLK Sub-System Main Clk,ACLK和SMCLK可以通过复用引脚输出,MCLK 不能直接输出体现, MCLK可以配置为VLO或者DCOwhile(1){P1OUT |= 0x20; // P1.5 = 1, 通过开关P1.5来体现MCLK,这两条指令的周期大概为SMCLK的1/10P1OUT &= ~0x20;//20;}}5.//************************************************************************* *****// MSP430xG46x Demo - FLL+, Runs Internal DCO at 8MHz// Description: This program demonstrates setting the internal DCO to run at// 8MHz with auto-calibration by the FLL+.// ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = DCO = (121+1) x 2 x ACLK = 7995392Hz// //* An external watch crystal between XIN & XOUT is required for ACLK *////// MSP430xG461x// -----------------// /|\| XIN|-// | | | 32kHz// --|RST XOUT|-// | |// | P1.1|--> MCLK = 8MHz// | |// | P1.5|--> ACLK = 32kHz// | |//// K. Quiring/ M. Mitchell// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* ****#include <msp430xG46x.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerFLL_CTL0 |= DCOPLUS + XCAP18PF; // DCO+ set, freq = xtal x D x N+1 SCFI0 |= FN_4; // x2 DCO freq, 8MHz nominal DCOSCFQCTL = 121; // (121+1) x 32768 x 2 = 7.99 MHzP1DIR = 0x22; // P1.1 & P1.5 to output directionP1SEL = 0x22; // P1.1 & P1.5 to output MCLK & ACLKwhile(1); // Loop in place}6.//************************************************************************* ***// MSP430xG46x Demo - Flash In-System Programming, Copy SegA to SegB//// Description: This program first erases flash seg A, then it increments all// values in seg A, then it erases seg B, then copies seg A to seg B.// Assumed MCLK 550kHz - 900kHz.// //* Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash *////// MSP430xG461x// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |//// M. Mitchell// Texas Instruments Inc.// Feb 2005// Built with IAR Embedded Workbench Version: 3.21A//************************************************************************* *****#include <msp430xG46x.h>char value; // 8-bit value to write to segment A// Function prototypesvoid write_SegA (char value);void copy_A2B (void);void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerFCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generatorvalue = 0; // Initialize valuewhile(1) // Repeat forever{write_SegA(value++); // Write segment A, increment valuecopy_A2B(); // Copy segment A to B_NOP(); // SET BREAKPOINT HERE}}void write_SegA (char value){char *Flash_ptr; // Flash pointerunsigned int i;Flash_ptr = (char *) 0x1080; // Initialize Flash pointerFCTL1 = FWKEY + ERASE; // Set Erase bitFCTL3 = FWKEY; // Clear Lock bit*Flash_ptr = 0; // Dummy write to erase Flash segmentFCTL1 = FWKEY + WRT; // Set WRT bit for write operationfor (i=0; i<128; i++){*Flash_ptr++ = value; // Write value to flash}FCTL1 = FWKEY; // Clear WRT bitFCTL3 = FWKEY + LOCK; // Set LOCK bit}void copy_A2B (void){char *Flash_ptrA; // Segment A pointerchar *Flash_ptrB; // Segment B pointerunsigned int i;Flash_ptrA = (char *) 0x1080; // Initialize Flash segment A pointerFlash_ptrB = (char *) 0x1000; // Initialize Flash segment B pointerFCTL1 = FWKEY + ERASE; // Set Erase bitFCTL3 = FWKEY; // Clear Lock bit*Flash_ptrB = 0; // Dummy write to erase Flash segment B FCTL1 = FWKEY + WRT; // Set WRT bit for write operationfor (i=0; i<128; i++){*Flash_ptrB++ = *Flash_ptrA++; // Copy value segment A to segment B}FCTL1 = FWKEY; // Clear WRT bitFCTL3 = FWKEY + LOCK; // Set LOCK bit}7.//************************************************************************* *****// MSP430xG46x Demo - Software Port Interrupt on P1.0 from LPM4//// Description: A hi/low transition on P1.0 will trigger P1_ISR which,// toggles P2.1. Normal mode is LPM4 ~ 0.1uA. LPM4 current can be measured// with the LED removed, all unused P1.x/P2.x configured as output or inputs// pulled high or low, and ensure the P2.0 interrupt input does not float.// ACLK = 32.768kHz, MCLK = SMCLK = default DCO//// MSP430xG461x// -----------------// /|\| |// | | |// --|RST |// /|\ | |// --o--|P1.0 P2.1|-->LED// \|///// K. Quiring/ M. Mitchell// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* *****#include <msp430xG46x.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTFLL_CTL0 |= XCAP14PF; // Configure load capsP2DIR = BIT1; // Set P2.1 to output directionP1IES = BIT0; // H-L transitionP1IE = BIT0; // Enable interrupt_BIS_SR(LPM4_bits + GIE); // LPM4, enable interrupts}// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port1_ISR (void){unsigned volatile int i;for (i=10000; i>0; i--); // Debounce delayP1IFG &= ~BIT0; // Clear P1IFGif ((P1IN & 0x01) == 0)P2OUT ^= 0x02; // Toggle P2.1 using exclusive-OR}8.//************************************************************************* *****// MSP430xG46x Demo - Software Port Interrupt on P1.0 from LPM4//// Description: A hi/low transition on P1.0 will trigger P1_ISR which,// toggles P2.1. Normal mode is LPM4 ~ 0.1uA. LPM4 current can be measured// with the LED removed, all unused P1.x/P2.x configured as output or inputs// pulled high or low, and ensure the P2.0 interrupt input does not float.// ACLK = 32.768kHz, MCLK = SMCLK = default DCO//// MSP430xG461x// -----------------// /|\| |// | | |// --|RST |// /|\ | |// --o--|P1.0 P2.1|-->LED// \|///// K. Quiring/ M. Mitchell// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* *****#include <msp430xG46x.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTFLL_CTL0 |= XCAP14PF; // Configure load capsP2DIR = BIT1; // Set P2.1 to output directionP1IES = BIT0; // H-L transitionP1IE = BIT0; // Enable interrupt_BIS_SR(LPM4_bits + GIE); // LPM4, enable interrupts}// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port1_ISR (void){unsigned volatile int i;for (i=10000; i>0; i--); // Debounce delayP1IFG &= ~BIT0; // Clear P1IFGif ((P1IN & 0x01) == 0)P2OUT ^= 0x02; // Toggle P2.1 using exclusive-OR}9.//************************************************************************* *****// MSP430xG46x Demo - USCI_A0, 115200 UART Echo ISR, DCO SMCLK// (modified code example "msp430xG46x_uscia0_uart_01_115k.c")//// Description: Echo a received character, RX ISR used. Normal mode is LPM0.// USCI_A0 RX interrupt triggers TX Echo.// Baud rate divider with 1048576hz = 1048576/115200 = ~9.1 (009h|01h)// ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = default DCO = 32 x ACLK = 1048576Hz// //* An external watch crystal between XIN & XOUT is required for ACLK *////// MSP430FG4619// -----------------// /|\| XIN|-// | | | 32kHz// --|RST XOUT|-// | |// | P2.5/UCA0RXD|<------------// | | 115200 - 8N1// | P2.4/UCA0TXD|------------>//// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* *****#include "msp430xG46x.h"void main(void){volatile unsigned int i;WDTCTL = WDTPW+WDTHOLD; // Stop WDTFLL_CTL0 |= XCAP14PF; // Configure load capsdo{IFG1 &= ~OFIFG; // Clear OSCFault flagfor (i = 0x47FF; i > 0; i--); // Time for flag to set}while ((IFG1 & OFIFG)); // OSCFault flag still set?P2SEL |= 0x030; // P2.4,5 = USCI_A0 RXD/TXDUCA0CTL1 |= UCSSEL_2; // SMCLKUCA0BR0 = 18;0x09; // 1MHz 115200UCA0BR1 = 0;0x00; // 1MHz 115200UCA0MCTL = 0;0x02; // ModulationUCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt_BIS_SR(LPM0_bits + GIE); // Enter LPM0, interrupts enabled}// Echo back RXed character, confirm TX buffer is ready first#pragma vector=USCIAB0RX_VECTOR__interrupt void USCIA0RX_ISR (void){while(!(IFG2&UCA0TXIFG));UCA0TXBUF = UCA0RXBUF; // TX -> RXed character}10./************************************************************************** ***** MSP-EXP430G2-LaunchPad User Experience Application** 1. Device starts up in LPM3 + blinking LED to indicate device is alive* + Upon first button press, device transitions to application mode* 2. Application Mode* + Continuously sample ADC Temp Sensor channel, compare result against* initial value* + Set PWM based on measured ADC offset: Red LED for positive offset, Green* LED for negative offset* + Transmit temperature value via TimerA UART to PC* + Button Press --> Calibrate using current temperature* Send character '� via UART, notifying PC******************************************************************************/ #include "msp430g2553.h"#define LED0 BIT0#define LED1 BIT6#define LED_DIR P1DIR#define LED_OUT P1OUT#define BUTTON BIT3#define BUTTON_OUT P1OUT#define BUTTON_DIR P1DIR#define BUTTON_IN P1IN#define BUTTON_IE P1IE#define BUTTON_IES P1IES#define BUTTON_IFG P1IFG#define BUTTON_REN P1REN#define TXD BIT1 // TXD on P1.1 #define RXD BIT2 // RXD on P1.2#define APP_STANDBY_MODE 0#define APP_APPLICATION_MODE 1#define TIMER_PWM_MODE 0#define TIMER_UART_MODE 1#define TIMER_PWM_PERIOD 2000#define TIMER_PWM_OFFSET 20#define TEMP_SAME 0#define TEMP_HOT 1#define TEMP_COLD 2#define TEMP_THRESHOLD 5// Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz#define Bitime_5 0x05*4 // ~ 0.5 bit length + small adjustment#define Bitime 13*4//0x0D#define UART_UPDA TE_INTERV AL 1000unsigned char BitCnt;unsigned char applicationMode = APP_STANDBY_MODE;unsigned char timerMode = TIMER_PWM_MODE;unsigned char tempMode;unsigned char calibrateUpdate = 0;unsigned char tempPolarity = TEMP_SAME;unsigned int TXByte;/* Using an 8-value moving average filter on sampled ADC values */long tempMeasured[8];unsigned char tempMeasuredPosition=0;long tempAverage;long tempCalibrated, tempDifference;void InitializeLeds(void);void InitializeButton(void);void PreApplicationMode(void); // Blinks LED, waits for button pressvoid ConfigureAdcTempSensor(void);void ConfigureTimerPwm(void);void ConfigureTimerUart(void);void Transmit(void);void InitializeClocks(void);void main(void){unsigned int uartUpdateTimer = UART_UPDATE_INTERV AL;unsigned char i;WDTCTL = WDTPW + WDTHOLD; // Stop WDTInitializeClocks();InitializeButton();InitializeLeds();PreApplicationMode(); // Blinks LEDs, waits for button press/* Application Mode begins */applicationMode = APP_APPLICATION_MODE;ConfigureAdcTempSensor();ConfigureTimerPwm();__enable_interrupt(); // Enable interrupts./* Main Application Loop */while(1){ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start__bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled/* Moving average filter out of 8 values to somewhat stabilize sampled ADC */tempMeasured[tempMeasuredPosition++] = ADC10MEM;if (tempMeasuredPosition == 8)tempMeasuredPosition = 0;tempAverage = 0;for (i = 0; i < 8; i++)tempAverage += tempMeasured[i];tempAverage >>= 3; // Divide by 8 to get averageif ((--uartUpdateTimer == 0) || calibrateUpdate ){ConfigureTimerUart();if (calibrateUpdate){TXByte = 248; // A character with high value, outside of temp rangeTransmit();calibrateUpdate = 0;TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 );Transmit();uartUpdateTimer = UART_UPDATE_INTERV AL;ConfigureTimerPwm();}tempDifference = tempAverage - tempCalibrated;if (tempDifference < -TEMP_THRESHOLD){tempDifference = -tempDifference;tempPolarity = TEMP_COLD;LED_OUT &= ~ LED1;}elseif (tempDifference > TEMP_THRESHOLD){tempPolarity = TEMP_HOT;LED_OUT &= ~ LED0;}else{tempPolarity = TEMP_SAME;TACCTL0 &= ~CCIE;TACCTL1 &= ~CCIE;LED_OUT &= ~(LED0 + LED1);}if (tempPolarity != TEMP_SAME){tempDifference <<= 3;tempDifference += TIMER_PWM_OFFSET;TACCR1 = ( (tempDifference) < (TIMER_PWM_PERIOD-1) ? (tempDifference) : (TIMER_PWM_PERIOD-1) );TACCTL0 |= CCIE;TACCTL1 |= CCIE;}}void PreApplicationMode(void){LED_DIR |= LED0 + LED1;LED_OUT |= LED0; // To enable the LED toggling effect LED_OUT &= ~LED1;BCSCTL1 |= DIV A_1; // ACLK/2BCSCTL3 |= LFXT1S_2; // ACLK = VLOTACCR0 = 1200; //TACTL = TASSEL_1 | MC_1; // TACLK = SMCLK, Up mode. TACCTL1 = CCIE + OUTMOD_3; // TACCTL1 Capture Compare TACCR1 = 600;__bis_SR_register(LPM3_bits + GIE); // LPM0 with interrupts enabled}void ConfigureAdcTempSensor(void){unsigned char i;/* Configure ADC Temp Sensor Channel */ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;__delay_cycles(1000); // Wait for ADC Ref to settleADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled tempCalibrated = ADC10MEM;for (i=0; i < 8; i++)tempMeasured[i] = tempCalibrated;tempAverage = tempCalibrated;}void ConfigureTimerPwm(void){timerMode = TIMER_PWM_MODE;TACCR0 = TIMER_PWM_PERIOD; //TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode. TACCTL0 = CCIE;TACCTL1 = CCIE + OUTMOD_3; // TACCTL1 Capture Compare TACCR1 = 1;}void ConfigureTimerUart(void){timerMode = TIMER_UART_MODE; // Configure TimerA0 UART TXCCTL0 = OUT; // TXD Idle as MarkTACTL = TASSEL_2 + MC_2 + ID_3; // SMCLK/8, continuous modeP1SEL |= TXD + RXD; //P1DIR |= TXD; //}// Function Transmits Character from TXBytevoid Transmit(){BitCnt = 0xA; // Load Bit counter, 8data + ST/SP while (CCR0 != TAR) // Prevent async captureCCR0 = TAR; // Current state of TA counterCCR0 += Bitime; // Some time till first bitTXByte |= 0x100; // Add mark stop bit to TXByteTXByte = TXByte << 1; // Add space start bitCCTL0 = CCIS0 + OUTMOD0 + CCIE; // TXD = mark = idlewhile ( CCTL0 & CCIE ); // Wait for TX completion}// Timer A0 interrupt service routine#pragma vector=TIMER0_A0_VECTOR__interrupt void Timer_A (void){if (timerMode == TIMER_UART_MODE){CCR0 += Bitime; // Add Offset to CCR0if (CCTL0 & CCIS0) // TX on CCI0B?{if ( BitCnt == 0)CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt else{CCTL0 |= OUTMOD2; // TX Spaceif (TXByte & 0x01)CCTL0 &= ~ OUTMOD2; // TX MarkTXByte = TXByte >> 1;BitCnt --;}}}else{if (tempPolarity == TEMP_HOT)LED_OUT |= LED1;if (tempPolarity == TEMP_COLD)LED_OUT |= LED0;TACCTL0 &= ~CCIFG;}}#pragma vector=TIMER0_A1_VECTOR__interrupt void ta1_isr(void){TACCTL1 &= ~CCIFG;if (applicationMode == APP_APPLICATION_MODE)LED_OUT &= ~(LED0 + LED1);elseLED_OUT ^= (LED0 + LED1);}void InitializeClocks(void){BCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ;BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO / 8 = 1MHz }void InitializeButton(void) // Configure Push Button{BUTTON_DIR &= ~BUTTON;BUTTON_OUT |= BUTTON;BUTTON_REN |= BUTTON;BUTTON_IES |= BUTTON;BUTTON_IFG &= ~BUTTON;BUTTON_IE |= BUTTON;}void InitializeLeds(void){LED_DIR |= LED0 + LED1;LED_OUT &= ~(LED0 + LED1);}/* ************************************************************** Port Interrupt for Button Press* 1. During standby mode: to exit and enter application mode* 2. During application mode: to recalibrate temp sensor* *********************************************************** */#pragma vector=PORT1_VECTOR__interrupt void PORT1_ISR(void){BUTTON_IFG = 0;BUTTON_IE &= ~BUTTON; /* Debounce */WDTCTL = WDT_ADL Y_250;IFG1 &= ~WDTIFG; /* clear interrupt flag */IE1 |= WDTIE;if (applicationMode == APP_APPLICATION_MODE){tempCalibrated = tempAverage;calibrateUpdate = 1;}else{applicationMode = APP_APPLICATION_MODE; // Switch from STANDBY to APPLICATION MODE__bic_SR_register_on_exit(LPM3_bits);}}#pragma vector=WDT_VECTOR__interrupt void WDT_ISR(void){IE1 &= ~WDTIE; /* disable interrupt */IFG1 &= ~WDTIFG; /* clear interrupt flag */WDTCTL = WDTPW + WDTHOLD; /* put WDT back in hold state */BUTTON_IE |= BUTTON; /* Debouncing complete */ }// ADC10 interrupt service routine#pragma vector=ADC10_VECTOR__interrupt void ADC10_ISR (void){__bic_SR_register_on_exit(CPUOFF); // Return to active mode}。
MSP430G2553时钟,MSP430G2553时钟配置
MSP430G2553 时钟,MSP430G2553 时钟配置MSP430G2553 系统时钟和振荡器时钟系统由基本时钟模块提供支持,此时钟模块支持一个32768Hz 手表晶体振荡器、一个内部超低功耗低频振荡器和一个内部数字控制振荡器(DCO)。
基本时钟模块专为同时满足低系统成本及低功耗要求而设计。
内部DCO 提供了一个快速接通时钟源并可在不到1µs 的时间里实现稳定。
基本时钟模块提供了以下时钟信号:•辅助时钟(ACLK),此时钟由一个32768Hz 手表晶振或内部LF 振荡器提供信号源。
•主时钟(MCLK),CPU 所采用的系统时钟。
•系统子时钟(SMCLK),外设模块所采用的子系统时钟。
用于校准DCO 输出频率的DCO 设定值存储于信息内存的A 段中。
主DCO 特性MSP430G2553 时钟1,MSP430G2553 能做到超低功耗,合理的时钟模块是功不可没的。
但是功能强大的时钟模块设置起来也相对复杂一些。
2,MSP430G2553 的时钟源有:(1),外接低频晶振LFXT1CLK:低频模式接手表晶体32768Hz,高频模式450KHz~8MHz;(2),外接高速晶振XT2CLK:8MHz;(3),内部数字控制振荡器DCO:是一个可控的RC 振荡器,频率在0~16MHz;(4),超低功耗低频振荡器VLO:不可控,4~20KHz 典型值为12KHz;3,时钟模块:430 的时钟模块有MCLK SMCLK ACLK :(1),主系统时钟MCLK:提供给MSP430 的CPU 时钟。
可以来自LFXT1CLK XT2CLK DCO VLO 可选,默认为DCO。
(2),子系统时钟SMCLK:提供给高速外设。
可以来自LFXT1CLK XT2CLK DCO VLO 可选,默认为DCO。
(3),辅助系统时钟ACLK:提供给低速外设。
MSP430G2553_
ZeroMemory(&wrOverlapped,sizeof(wrOverlapped)); if (wrOverlapped.hEvent != NULL) { ResetEvent(wrOverlapped.hEvent); wrOverlapped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); } PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR); txIndex=0; rxIndex=0; } virtual ~ComPort(void) { if(bComOpened) CloseHandle(hCom); } unsigned char rxIndex; unsigned char lpInBuffer[1024]; int Sync(void){ int i=1000; while(rxIndex!=txIndex && txIndex!=0 && (--i)){ int size=Read(); for(int i=0;i<size;i++) if(((lpInBuffer[i]&0x80)==0) && (lpInBuffer[i]!=0)) rxIndex=lpInBuffer[i]; } if(i<1) return -1; return rxIndex; } int Read(void){ DWORD dwBytesRead=1024; COMSTAT ComStat; DWORD dwErrorFlags; OVERLAPPED m_osRead; memset(&m_osRead,0,sizeof(OVERLAPPED)); m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); ClearCommError(hCom,&dwErrorFlags,&ComStat); dwBytesRead=min(dwBytesRead,(DWORD)ComStat.cbInQue); //clear port buffer
msp430的四种计时器程序
基于msp430g2553定时器详解1已有2682 次阅读2012-8-10 21:11 |/*#include "msp430g2553.h"unsigned char num;void main(){// Stop watchdog timer to prevent time out resetWDTCTL = WDTPW + WDTHOLD;BCSCTL3 |= LFXT1S_2; // Set LFXT1为vol时钟即12kHZCCTL0|= CCIE; //设置捕获/比较控制寄存器,CCIE=0x0010,使能捕获比较中断CCR0 =12000; //设置捕获/比较寄存器,初始值为12000,对于ACLK时钟频率为12khz的频率,相当于1sTA0CTL = TASSEL_1 +TACLR+MC_1; // 设置定时器A控制寄存器,// TASSEL_1选择辅助时钟ACLK,TASSEL_2选择子系统时钟SMCLK,与dco有关;// TACLR=0x0004,清除定时器A计数器//TACTL |= MC0; //设置定时器A控制寄存器,MC0=0x0010,使计数模式为增计数P1SEL&=~BIT6; //P1.6为I/O口P1DIR |=BIT6; //P1.6为输出_EINT(); //使能中断,这是一个C编译器支持的内部过程。
while(1); //无限次while循环}#pragma vector=TIMER0_A0_VECTOR//固定的格式__interrupt void Timer_A (void) //定时器A的CC0中断处理程序必须是没有返回值的{P1OUT ^= BIT6; //将P3.7引脚取反,就是使发光二极管闪烁}*///使用主系统做为时钟源时,可用用num配合使用,达到一秒定时,//也可以通过BCSCTL2 |= SELM_1 + DIVM_3; 选择MCLK并且8分频#include "msp430g2553.h"unsigned char num;void main(){// Stop watchdog timer to prevent time out resetWDTCTL = WDTPW + WDTHOLD;//主系统时钟切换为外部高速晶振if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF){while(1); // If calibration constants erased, trap CPU!!}// Configure Basic ClockBCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ; // Set DCO step + modulation*/CCTL0|= CCIE; //设置捕获/比较控制寄存器,CCIE=0x0010,使能捕获比较中断CCR0 =10000; //设置捕获/比较寄存器,初始值为10000,对于MCLK时钟频率为1Mhz的频率,相当于1sTA0CTL = TASSEL_2 +TACLR+MC_1; // 设置定时器A控制寄存器,// TASSEL_1选择辅助时钟ACLK,TASSEL_2选择子系统时钟SMCLK,与dco有关;// TACLR=0x0004,清除定时器A计数器//TACTL |= MC0(MC_1); //设置定时器A控制寄存器,MC0=0x0010,使计数模式为增计数P1SEL&=~BIT6; //P1.6为I/O口P1DIR |=BIT6; //P1.6为输出_EINT(); //使能中断,这是一个C编译器支持的内部过程。
MSP430G2553寄存器资料讲解
MSP430G2553 学习笔记常用赋值运算符:清除:&=~ , 置位:|= , 测试:&= , 取反:A=看门狗模块:WDT (看门狗)IS1,IS0选择看门狗定时器的定时输出, T 是WDTCNT 的输入时钟源周期0 T x 2(15) 1 T x 2(13) 2 T x 2(9) 3T x 2(6)SSEL选择WDTCNT 的时钟源 0 SMCLK 1ACLKIS0、IS1、SSEL 可确定 WDT 定时时间, WDT 只能定时8种和时钟源相关的时间CNTCL当该位为1时,清除WDTCNT TMSEL工作模式选择看门狗模式1定时器模式NMI选择RST/NMI 引脚功能,在 PUC 后被复位0 RST/NMI引脚为复位端NMIES 选择中断的边沿触发方式0 上升沿触发NMI中断1 下降沿触发NMI中断HOLD 停止看门狗定时器工作,降低功耗0 WDT功能激活1 时钟禁止输入,计数停止WDT (看门狗)配置语句WDTCTL=WDTPW+WDTHOLD ;// 将 WDTPW+WDTHOLD 赋值给 WDTCTL,关闭看门狗定时器控制寄存器( Stop watchdogtimer)IE1 |= WDTIE ;// 使能 WDT 中断WDTCTL = WDT_ADL Y_1000 ;//WDT 1 s / 4 间隔计时器WDTCTL = WDTPW + WDTHOLD + WDTNMI + WDTNMIES ;//WDTCTL 由高8位口令和低8位控制命令组成,要写入操作 WDT的控制命令,出于安全原因必须先正确写入高字节看门狗口令。
口令为5AH,如果口令写错将导致系统复位。
读 WDTCTL时不需要口令。
这个控制寄存器还可以用于设置NMI引脚功能。
WDT 看门狗定时器(Watch Dog Timer),这是16位增计数器,由MSP430所选定的时钟电路产生的固定周期时钟信号对计数器进行加法计数。
基本时钟模块_MSP430G2553
基本时钟模块_MSP430G2553G2xxx系列DCO校准数据(校正寄存器)1MHz:CALBC1_1MHZCALDCO_1MHZ8MHz:CALBC1_8MHZCALDCO_8MHZ12MHz:CALBC1_12MHZCALDCO_12MHZ16MHz:CALBC1_16MHZCALDCO_16MHZ例:设置DCO频率为1MHzif(CALBC1_1MHZ==0xFF || CALDCO_1MHZ==0xFF)while(1);//校准数据是否被擦除,若是则CPU挂起。
BCSCTL1 = CALBC1_1MHZ;DCOCTL = CALDCO_1MHZ;基本时钟模块寄存器寄存器缩写形式类型初始状态DCO控制寄存器DCOCTL 读/写0x60(PUC)基本时钟系统控制器1 BCSCTL1 读/写0x87(POR)基本时钟系统控制器2 BCSCTL2 读/写由PUC复位基本时钟系统控制器3 BCSCTL3 读/写0x05(PUC)中断使能寄存器(特殊功能寄存器)IE1 读/写由PUC复位中断标致寄存器(特殊功能寄存器)IFG1 读/写由PUC复位说明:DCO的频率可以通过软件设定DCOx、MODx、RSELx相应位来调整,DCO频率是通过将f DCO和f DCO+1混频得到。
1、DCOCTL:DCO控制寄存器7 6 5 4 3 2 1 0DCOx MODxrw-0 rw-1 rw-1 rw-0 rw-0 rw-0 rw-0 rw-0 DCOx:DCO频率范围选择位,这些位可以用来在由RESLx设置决定的8个离散的频率范围中选择哪一个。
MODx:调制系数选择位,这些位用来决定在32个DCO时钟周期中f DCO+1占多少个,f DCO 占多少个。
注意:当MODx=0时调制器关闭,DCOx=7时,由于此时没有下一个更高的频率范围f DCO+1可用,因此MODx无效不可用。
2、BCSCTL1:基本时钟系统控制寄存器17 6 5 4 3 2 1 0 XT2OFF XTS(1)(2)DIVAx RSELxrw-(1) rw-(0) rw-(0) rw-(0) rw-0 rw-1 rw-1 rw-1 XT2OFF:第二晶振XT2(可选高频晶振)关闭控制位。
MSP430G2553 系统时钟设置!
MSP430G2553系统学习之系统时钟(ls_core)把MSP430G2553的系统时钟作为系统学习的一篇讲解可见他的重要性,那为什么要把时钟放到第一位呢?学过FPGA的朋友都能体会得到,如果把CPU的所有工作任务作为纵坐标(y轴),那么时钟就是他的横坐标(x轴)了,如下图1 时钟概念所示。
图 1 时钟概念也就是说时钟就是一个微机(小到8位的51单片机大到32位的S3C2440等)在时域上的一个衡量标准。
如果学过FPGA的朋友都很清楚,微机是以对输入的时钟源的脉冲计数的方式来确定时域参数的。
所以,MSP430单片机也一样,拿到他之后首先从时钟入手,一定要养成这种系统学习的好方法,否则再学一百款单片机也是感觉再学新的,做不到举一反三,事半功倍的效果。
如果能够把握这其中的通理,你会有所有的单片机都一样样的感觉,当然学起来也就游刃有余了。
废话不多说了,进入正题。
一、硬件Msp420g2553的系统时钟是由VLOCK、LFXT1CLK和DCOCLK组成,如下图2 系统时钟组成所示:图2 系统时钟组成即2553的系统时钟源有三个分别是片内超低功耗12KHz的内部振荡器;由外部时钟源提供的LFXT1CLK时钟源,也即LanuchPad开发板上未焊接的时钟电路,在这里我们可以焊接一个32.768KHz的低频时钟晶体由2553的XIN引脚输入;片内可数字控制的振荡器DCOCLK,在软件的调节下该时钟的输出范围为0.6MHz到26MHz。
在这三个时钟源的独立工作或是在三个时钟源相互协调配合(是可以通过软件配置的)下为2553系统提供了系统时钟ACLK、MCLK和SMCLK。
ACLK为辅助时钟,由软件选择来自VLOCK、LFXT1CLK之一经过1,2,4,8分频之后得到,为外围模块提供时钟源。
MCLK为主时钟,由软件选择来自VLOCK、LFXT1CLK和DCOCLK之一经过1,2,4,8分频之后得到,为CPU和系统提供时钟。
MSP430G2553_PWM
/************************************************************** 名称:PWM输出* 硬件描述:MSP430G2553 P1.2 P1.6 P2.1 P2.2 P2.4 P2.5* 功能:初始化定时器,输出PWM波* 入口参数:周期Cyc,占空比Occupancy* 出口参数:无* 时间:2014年7月27日* 作者:Jelly-Li* 说明:* Timer_A0_1_init(int Cyc,int Occupancy); P1.2 P1.6* Timer_A1_1_init(int Cyc,int Occupancy); P2.1 P2.2* Timer_A1_2_init(int Cyc,int Occupancy); P2.4 P2.5* 注意:* 使用的那几个IO口* 输出几个信号* 范例:* Timer_A0_1_init(500,100); 20% P1.2 P1.6* Timer_A1_1_init(300,150); 50% P2.1 P2.2* Timer_A1_2_init(8000,200); 2.5% P2.4 P2.5*************************************************************/#include <msp430g2553.h>/************************************************************** 名称:void Timer_A0_1_init(int Cyc,int Occupancy) TA0.1输出PWM* 硬件描述:MSP430G2553 P1.2 P1.6* 功能:输出PWM* 入口参数:周期Cyc,占空比Occupancy* 出口参数:无* 时间:2014年7月27日* 作者:Jelly-Li* 说明:* Timer_A0_1_init(int Cyc,int Occupancy); P1.2 P1.6* 使用P1.2口和P1.6口输出,可以同时也可以只输出一个* 注意:* 要输出几个信号,修改程序* 范例:* Timer_A0_1_init(500,100); 20% P1.2 P1.6*************************************************************/void Timer_A0_1_init(int Cyc,int Occupancy) //TA0.1输出PWM{P1SEL |= BIT2 + BIT6; //P1.2 和P1.6同时输出一样的PWM,可以只用其中一个输出P1DIR |= BIT2 + BIT6;CCR0 = Cyc; //设置周期(频率)CCR1 = Occupancy; //占空比CCR1/CCR0CCTL1 = OUTMOD_7; //输出模式为复位/置位TACTL |= TASSEL_1 + MC_1; //选择时钟,增计数}/************************************************************** 名称:void Timer_A1_1_init(int Cyc,int Occupancy) //TA1.1输出PWM* 硬件描述:MSP430G2553 P2.1 P2.2* 功能:* 入口参数:Cyc* 出口参数:Occupancy* 时间:2014年7月27日* 作者:Jelly-Li* 说明:* Timer_A1_1_init(int Cyc,int Occupancy); P2.1 P2.2* P2.1 P2.2输出TA1.1 OUT1 可以输出两个或任意一个* 注意:* 要输出几个信号,修改程序* 范例:* Timer_A1_1_init(300,150); 50% P2.1 P2.2*************************************************************/void Timer_A1_1_init(int Cyc,int Occupancy) //TA1.1输出PWM{P2SEL |= BIT1 + BIT2; //P2.1 和P2.2同时输出一样的PWMP2DIR |= BIT1 + BIT2;TA1CCR0 = Cyc; //时钟频率TA1CCR1 = Occupancy; //占空比CCR1/CCR0,注意CCR1要写成TA1CCR1 TA1.1由P2.1 P2.2输出TA1CCTL1 = OUTMOD_7; //输出模式为复位/置位,注意CCTL1要写为TA1CCTL1TA1CTL |= TASSEL_1 + MC_1; //时钟,增计数}/************************************************************** 名称:void Timer_A1_2_init(int Cyc,int Occupancy)* 硬件描述:MSP430G2553 P2.4 P2.5* 功能:* 入口参数:Cyc* 出口参数:Occupancy* 时间:2014年7月27日* 作者:Jelly-Li* 说明:* Timer_A1_2_init(int Cyc,int Occupancy); P2.4 P2.5* P2.4 P2.5输出TA1.2 OUT2 可以任意输出一个或者两个,但必须是这两个端口* 注意:* 要输出几个信号,修改程序* 范例:* Timer_A1_2_init(8000,200); 2.5% P2.4 P2.5*************************************************************/void Timer_A1_2_init(int Cyc,int Occupancy) //TA1.2输出PWM{//端口初始化P2SEL |= BIT4 + BIT5; //P2.4 和P2.5同时输出一样的PWMP2DIR |= BIT4 + BIT5;//TA1CCR0 = Cyc; //设置周期,当T1.1不用时,要设置,当T1.1使用时不设置,与T1.1周期一致TA1CCR2 = Occupancy; //占空比CCR2/CCR0,注意CCR2要写成TA1CCR2 TA1.2由P2.4 P2.5输出TA1CCTL2 = OUTMOD_7; //输出模式为复位/置位,注意CCTL2要写为TA1CCTL2//TA1CTL |= TASSEL_1 + MC_1; //时钟,增计数当T1.1不用时,要设置,当T1.1使用时不设置,与T1.1周期一致}。
MSP430G2553制作的自行车测度系统
MSP430G2553制作的自行车测度系统一,功能介绍:本系统主要用于安装在自行车上测量自行车的运行速度和行驶距离。
然后将速度和距离信息显示在显示在LCD显示屏上。
(1) 处理器选用的是TI的MSP430G2553 单片机(2)测速装置选用的是霍尔器件,在车圈上固定一个磁铁,然后430连接霍尔器件,通过输入捕获功能测量车轮的转动输出的脉冲的周期在加上事先计算出车轮的直径就可计算出车子的运行速度和行驶距离(3)显示屏选用的是NOKIA5110 LCD的显示屏,此显示屏的成本比较低,可以满足我的显示要求。
(4)整个系统有两个按键,一个是距离清零按键,按下该按键可以将显示的距离清零,这样方便测量起始地和目的地的距离,另外一个按键是用于打开LCD的背光的,用于夜晚使用。
当夜晚光线比较暗时,按下该按键LCD的背光打开,方便看清楚显示的东西。
在按一下背光熄灭。
(5)整个系统的供电的用到2节7号电池供电,当平常车子没有运行的时候,430处于低功耗状态。
耗电比较少,现在测速系统已将安装在我车子上正常工作3个月了二,系统框图图1是整个系统的框图. LCD显示屏和MSP430G2553的连接采用的SPI接口,霍尔传感器和按键和430之间是IO口连接。
三,系统原理图图2是整个系统的原理图四,程序代码//******************************************************************* ***********#include#include#include#includeunsigned char Count, First_Time;unsigned int REdge1, REdge2, FEdge;unsigned char MST_Data, SLV_Data;#define DC P2OUT = P2OUT|(1<<5)#define SCE P2OUT = P2OUT|(1<<4)#define CLK_H P1OUT=P1OUT|(1<<5)#define CLK_L P1OUT=P1OUT&(~(1<<5))#define Data_H P1OUT=P1OUT|((1<<7))#define Data_L P1OUT=P1OUT&(~(1<<7))#define u8 unsigned char#define u16 unsigned inttypedef struct{u8 x;u8 y;}point;/*------5110屏幕尺寸和功能宏定义------*/#define DATA 1 //数据#define CMD 0 //命令#define LCD_X 84 //液晶屏横坐标宽度#define LCD_ROW LCD_X //液晶屏列宽度void LCD_write_byte(unsigned char dat, unsigned char command); void LCD_init(void);void LCD_init(void);void LCD_set_XY(unsigned char X, unsigned char Y);void LCD_clear(void);void LCD_write_char(unsigned char c);void LCD_write_String(unsigned char X,unsigned char Y,unsigned char *s); void LCD_write_byte(unsigned char dat, unsigned char command){unsigned int i;// LCD_SCE = 0; //5110片选有效,允许输入数据P2OUT &=~BIT4;if (command == 0) //写命令// LCD_DC = 0;P2OUT &=~BIT5;else // LCD_DC = 1;P2OUT |=BIT5;//写数据for (i=0;i<8;i++){CLK_L;//spi_clk=0;if((dat & 0x80)==0x80){Data_H;}else{Data_L;}CLK_H; // spi_clk=1;dat=(dat<<1);}P2OUT |=BIT4;}void LCD_init(void){P2OUT &=~BIT4;P1OUT &= ~BIT4;P1OUT |= BIT4;LCD_write_byte(0x21, 0); // LCD模式设置:芯片活动,水平寻址,使用扩展指令LCD_write_byte(0xc8, 0); // 设置液晶偏置电压LCD_write_byte(0x06, 0); // 温度校正LCD_write_byte(0x13, 0); // 1:48LCD_write_byte(0x20, 0); // 使用基本命令,V=0,水平寻址LCD_clear(); // 清屏LCD_write_byte(0x0c, 0); // 设定显示模式,正常显示P2OUT |=BIT4;}void LCD_set_XY(unsigned char X, unsigned char Y){LCD_write_byte(0x80 | X, 0); // X 行(横坐标)LCD_write_byte(0x40 | Y, 0); // column 列(纵坐标)}/*------------------------------------------//LCD_clear: LCD清屏函数--------------------------------------------*/void LCD_clear(void){unsigned char t;unsigned char k;LCD_set_XY(0,0);for(t=0;t<6;t++){for(k=0;k<84;k++){LCD_write_byte(0x00,1);}}}void LCD_write_char(unsigned char c){unsigned char i;c-=0x20; //ASCII码减去 0x20for (i=0; i<6; i++)LCD_write_byte(ASCII_6_8[c], 1);}void LCD_write_String(unsigned char X,unsigned char Y,unsigned char *s){LCD_set_XY(X,Y);while (*s) //等效*s!='\0'{LCD_write_char(*s);s++;}}static float speed;static unsigned char display[10];static unsigned char display2[10];static unsigned int temp ;static unsigned int distance=0;static float Period;static float Distance_km;int main(void){volatile unsigned int i;unsigned char lcd_buf[6][84];WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerP1DIR |= BIT0; // P1.0/LED OutputP1OUT &= ~BIT0; // LED offif (CALBC1_8MHZ==0xFF) // If calibration constant erased {while(1); // do not load, trap CPU!!}DCOCTL = 0; // Select lowest DCOx and MODx settingsBCSCTL1 = CALBC1_8MHZ; // Set DCO to 8MHzDCOCTL = CALDCO_8MHZ;P1DIR &= ~BIT2;P1SEL |= BIT2;P2DIR &= ~BIT2;P2DIR &= ~BIT1;P2IES |=BIT2+BIT1;P2IE |=BIT2+BIT1;P1DIR |= BIT0;P1DIR|=BIT4|BIT5|BIT7;TA0CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;TA0CTL |= TASSEL_1 + MC_2 + TACLR; // ACLK, Cont Mode; start timer Count = 0x0;First_Time = 0x01;P2OUT = 0x00;P2DIR |=BIT5+BIT4 ;P1DIR |=BIT4 ;_EINT();P1OUT &= ~BIT4; // Now with SPI signals initialized,P1OUT |= BIT4; // reset slaveLCD_init(); //初始化LCD模块LCD_clear(); //清屏幕LCD_write_String(0,1,"Speed:");LCD_write_String(60,1,"km/h");LCD_write_String(0,3,"Dista:");while(1){__bis_SR_register(LPM0_bits+GIE); // Enter LPM0 Period = REdge2 - REdge1; // Calculate Period speed = 1.0362*(32768/Period)*3.6;memset(display,0,sizeof(display));sprintf((char*)display,"%4.1f",speed);LCD_write_String(36,1," ");LCD_write_String(36,1,display);LCD_write_String(60,1,"km/h");LCD_write_String(50,3," ");Distance_km=(float)distance*2.073656/1000.0;if(Distance_km<1){LCD_write_String(34,3," ");Distance_km=Distance_km*1000;sprintf((char*)display2,"%d",(int)Distance_km); LCD_write_String(44,3,display2);}else{LCD_write_String(36,3," ");sprintf((char*)display2,"%6.3f",Distance_km); LCD_write_String(34,3,display2);LCD_write_String(72,3,"km");}}}// TA0_A1 Interrupt vector#pragma vector = TIMER0_A1_VECTOR__interrupt void TIMER0_A1_ISR (void){switch(__even_in_range(TA0IV,0x0A)){case TA0IV_NONE: break; // Vector 0: No interrupt case TA0IV_TACCR1: // Vector 2: TACCR1 CCIFG// distance++;if (TA0CCTL1 & CCI) // Capture Input Pin Status {distance++;if (!Count){REdge1 = TA0CCR1;Count++;}else{REdge2 = TA0CCR1;Count=0x0;__bic_SR_register_on_exit(LPM0_bits +GIE); // Exit LPM0 on return to main }if (First_Time)First_Time = 0x0;}else{if(!First_Time){FEdge = TA0CCR1;}}break;default: break;}}#pragma vector=PORT2_VECTOR__interrupt void PORT2 (void){unsigned int i;for(i=0;i<20000;i++);if(P2IFG&BIT1){distance=0;while(!(P2IN&BIT1));}if(P2IFG&BIT2){P1OUT=P1OUT^BIT0;while(!(P2IN&BIT2));}P2IFG=0x00;__bic_SR_register_on_exit(LPM0_bits +GIE); // Exit LPM0 on return to main }#define LCD_X 84 //液晶屏横坐标宽度#define LCD_ROW LCD_X //液晶屏列宽度。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
秒表,精确到0.1秒,
设置TAR=0xf332溢出中断一次1/32768*3277=0.10000610s,在9分内基本上没有误差
硬件连接:P1.0~P1.7连接至扩展板数码管接口A、B、C、D、E、F、G、,P2.0~P2.3
连接至扩展板数码管接口COM1、COM2、COM3、COM4。
P2.4、P2.5连接至扩展板按键接口S0,S1..
操作按一次S0开始计时,再按一次S0暂停。
按一次S1清零。
#include"msp430.h"
unsigned int a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f},m=0,k=0,n=0,s=0,r=0,t=0,h=0,q=0; unsigned int b[]={0xfe,0xfd,0xfb,0xf7};
void delay(unsigned int i)
{
unsigned int j,k;
for(j=0;j<i;j++)
for(k=0;k<20;k++);
}
int main(void)
{
WDTCTL=WDTPW+WDTHOLD; //关看门狗
P1DIR|=0xff; //P1.0 作输出口
P1OUT=0x00;
P2DIR|=0x0f;
P2REN|=0x30;
P2OUT=0x00;
P2IES|=0x30; //P2.0 口设置下降沿触发
P2IE|=0x30;
P2IFG&=0xcf;
TA0R=0x0000; //设置定时器计数初值
TA0CTL=TASSEL_1+MC_2+TAIE; //设置Timer0
_EINT(); //开启总中断
while(1)
{
P2OUT=b[0];
P1OUT=a[k];
delay(15);
P1OUT=0x00;
P2OUT=b[1];
P1OUT=a[n];
delay(15);
P1OUT=0x00;
P2OUT=b[2];
P1OUT=a[s];
delay(15);
P1OUT=0x00;
P2OUT=b[3];
P1OUT=a[r];
delay(15);
P1OUT=0x00;
}
}
#pragma vector=TIMER0_A1_VECTOR //定时器0 溢出中断服务程序
__interrupt void Timer0_A1(void) //与捕获/比较器1 中断相同中断入口{
TAR=0xf332; //设置定时器计数初值
switch(TA0IV) //判断是否是定时器0 溢出中断
{
case 2: break;
case 4: break;
case 10: if(h==1)
{
m=m+1;
if(m==1)
{ k=k+1;
m=0;
if(k==10)
{ k=0;
n=n+1;
if(n==10)
{ n=0;
s=s+1;
if(s==6)
{s=0;
r=r+1;
if(r==10)
{r=0;
}
}
}
}
}
break; }
else
break;
//改变LED 状态
}
}
#pragma vector=PORT2_VECTOR //外部中断服务程序__interrupt void Port_2(void)
{
if((P2IN&0x10)==0)//等待按键松开
{delay(100);
if((P2IN&0x10)==0)
{ h=h+1;
if(h==2)
{h=0;}
}
}
if((P2IN&0x20)==0)//等待按键松开
{delay(100);
if((P2IN&0x20)==0)
{ k=s=r=n=0;
}
}
P2IFG&=0xcf; //清除P2.0 口中断标志
}。