标签:
1 #include <msp430x14x.h> 2 3 void int_clk(); 4 void delay(int i); 5 void main() 6 { 7 WDTCTL = WDTPW + WDTHOLD; 8 int_clk(); 9 P6DIR = 0XFF; 10 P6OUT = 0Xff; 11 while(1) 12 { 13 P6OUT = 0XFF; 14 delay(20000); 15 P6OUT = 0x00; 16 delay(20000); 17 } 18 } 19 20 void int_clk() 21 { 22 BCSCTL1 &= ~XT2OFF; 23 BCSCTL2 |= SELM_1 + SELS; 24 do 25 { 26 IFG1 &= ~OFIFG; 27 for(int i=0;i<100;i++) 28 _NOP(); 29 }while((IFG1 & OFIFG)!=0); 30 IFG1 &= ~OFIFG; 31 } 32 33 void delay(int i) 34 { 35 while(i--); 36 }
提示:IAR的配置要正确
升级版本:
1 /*********************************************** 2 程序功能:实现流水灯以三种流动方式和四种流动速度 3 的不同组合而进行点亮"流动" 4 ------------------------------------------------*/ 5 #include <msp430x14x.h> 6 int flag=0,i=0,j=0,dir=0,speed=0; 7 void int_clk(); 8 void delay(int i); 9 void main() 10 { 11 WDTCTL = WDTPW + WDTHOLD; 12 int_clk(); 13 P6DIR = 0XFF; 14 P6OUT = 0XFF; 15 16 TACCTL0 = CCIE; //使能CCR0中断 17 TACCR0 = 50000; 18 TACTL = TASSEL_2 + MC_1 + ID_3; //定时器A选择SMCLK,增计数,8分频 19 20 _EINT(); 21 LPM0; 22 } 23 /******************************************* 24 函数名称:Timer_A 25 功 能:定时器A的中断服务函数,在这里通过标志 26 控制流水灯的流动方向和流动速度 27 参 数:无 28 返回值 :无 29 ********************************************/ 30 #pragma vector=TIMERA0_VECTOR 31 __interrupt void Timer_A(void) 32 { 33 if(flag == 0) 34 { 35 P6OUT = ~(0x80>>(i++)); //灯的点亮顺序D8 -> D1 36 } 37 else if(flag == 1) 38 { 39 P6OUT = ~(0x01<<(i++)); //灯的点亮顺序D1 -> D8 40 } 41 else 42 { 43 if(dir) //灯的点亮顺序 D8 -> D1,D1 -> D8,循环绕圈 44 P6OUT = ~(0x80>>(i++)); 45 else 46 P6OUT = ~(0x01<<(i++)); 47 } 48 49 if(i == 8) 50 { 51 i = 0; 52 dir = ~dir; 53 } 54 delay(200); 55 j++; 56 if(j == 40) 57 { 58 i = 0; 59 j = 0; 60 flag++; 61 if(flag == 4) flag = 0; 62 switch(speed) 63 { 64 case 0: 65 // TACTL &=~ (ID0 + ID1); 66 TACTL |= ID_3; 67 break; 68 case 1: 69 // TACTL &=~ (ID0 + ID1); 70 TACTL |= ID_2; 71 break; 72 case 2: 73 // TACTL &=~ (ID0 + ID1); 74 TACTL |= ID_1; 75 break; 76 case 3: 77 // TACTL &=~ (ID0 + ID1); 78 TACTL |= ID_0; 79 break; 80 default: 81 break; 82 } 83 if(flag != 3) speed++; 84 if(speed == 4) speed = 0; 85 } 86 } 87 void int_clk() 88 { 89 BCSCTL1 &= ~XT2OFF; 90 BCSCTL2 |= SELM_1 + SELS; //DCO在800KHZ左右 91 do 92 { 93 IFG1 &= ~OFIFG; 94 for(int i=0;i<100;i++) 95 _NOP(); 96 }while((IFG1 & OFIFG)!=0); 97 IFG1 &= ~OFIFG; 98 } 99 100 void delay(int i) 101 { 102 while(i--); 103 }
标签:
原文地址:http://www.cnblogs.com/boyiliushui/p/4624440.html