当前位置: 代码迷 >> 综合 >> MSP430F5529 程序
  详细解决方案

MSP430F5529 程序

热度:82   发布时间:2024-01-29 12:13:23.0
//编程实现:编写串口接收程序,如果出现奇偶校验错误,点亮P1.3端口的LED,如果出现接收溢出错误,点亮P1.4端口的LED。
//默认8N1数据格式
#include <msp430.h>int main(void)
{WDTCTL = WDTPW | WDTHOLD;     //关闭看门狗P3SEL = BIT3 + BIT4;                     //将P3.3,4引脚选择为特殊引脚,开启串口到引脚的映射UCA0CTL1 |= UCSWRST;                   //在对串口模块进行设置时需要将其先关掉UCA0CTL1 |= UCSSEL_1;                //选择串口模块的工作时钟为ACLK=32768HZUCA0BR0 = 0x0D;         //设置波特率为2400 UCA0BR1_UCA0BR0这两个寄存器合起来要等于32768/2400UCA0BR1 = 0x00;         //设置波特率为2400 UCA0BR1_UCA0BR0这两个寄存器合起来要等于32768/2400UCA0MCTL |= UCBRS_6 + UCBRF_0; //微调制,因为32768/2400不是整数,所以会有误码,通过微调制减少误码率,微调制数值和32768/2400的小数部分相关联UCA0STAT |= UCLISTEN; //打开状态监听UCA0CTL1 &= ~UCSWRST;                 //设置完后 打开串口模块P1DIR |= (BIT3 + BIT4);                 //设置P1.3 P1.4为输出模式P1OUT &= ~(BIT3 + BIT4);                 //让P1.3 P1.4 输出低电平 表示关灯状态while (1){if (UCA0STAT | UCPE)                 //条件满足 则是奇偶校验错误{P1OUT |= BIT3;                 //P1.3输出高电平 点亮LED}else{P1OUT &= ~ BIT3;                 //P1.3输出低电平 熄灭LED}if (UCA0STAT | UCOE)                 //条件满足 则是溢出错误{P1OUT |= BIT4;                 //P1.4输出高电平 点亮LED}else{P1OUT &= ~BIT4;                 //P1.4输出低电平 熄灭LED}}}
// 编程实现:在MSP430F5529单片机系统上,P1.0、P1.1、P1.2和P1.3端口分别接了红色、绿色、蓝色、白色4只LED,均为高电平点亮。P1.4、P1.5、P1.6端口各接有一只按键(S1、S2、S3),按下为低电平。要求同时实现以下逻辑:
//(1)S1与S2中任意一个按键处于按下状态,红灯亮;
//(2)S2与S3同时处于按下状态时,绿灯亮;
//(3)S1与S3状态不同时,蓝灯亮;
//(4)S1按下后,白灯一直亮,直到S2按下后才灭。#include <msp430.h>int main(void)
{char key1_ifg, key2_ifg, key3_ifg; //按下标志WDTCTL = WDTPW | WDTHOLD;     //关闭看门狗P1DIR |= (BIT0 + BIT1 + BIT2 + BIT3);               //设置四个灯为输出模式 红色、绿色、蓝色、白色P1OUT &= ~(BIT0 + BIT1 + BIT2 + BIT3);  //输出低电平 初始状态全部小灯不亮P1DIR &= ~(BIT4 + BIT5 + BIT6);    //设置三个按键为输入模式 按键按下是低电平P1REN |= (BIT4 + BIT5 + BIT6); //上下拉使能P1OUT |= (BIT4 + BIT5 + BIT6); //开启内部上拉while (1){if ((P1IN & BIT4) == 0) //S1按键按下状态{key1_ifg = 1; //按下标志}else{key1_ifg = 0;}if ((P1IN & BIT5) == 0) //S2按键按下状态{key2_ifg = 1; //按下标志}else{key2_ifg = 0;}if ((P1IN & BIT6) == 0) //S3按键按下状态{key3_ifg = 1; //按下标志}else{key3_ifg = 0;}if (key1_ifg == 1 || key2_ifg == 1) //S1与S2中任意一个按键处于按下状态,红灯亮;{P1OUT |= BIT0; //红灯亮}else{P1OUT &= ~BIT0; //红灯不亮}if (key2_ifg == 1 && key3_ifg == 1) //S2与S3同时处于按下状态时,绿灯亮;{P1OUT |= BIT1; //绿灯亮}else{P1OUT &= ~BIT1; //绿灯不亮}if ((key1_ifg == 1 && key3_ifg == 0)|| (key1_ifg == 0 && key3_ifg == 1)) //S1与S3状态不同时,蓝灯亮;{P1OUT |= BIT2; //蓝灯亮}else{P1OUT &= ~BIT2; //蓝灯不亮}if (key1_ifg == 1) //S1按下后,白灯一直亮 检测到一次后白灯会一直亮{P1OUT |= BIT3; //白灯亮 刚开始白灯没亮}if (key2_ifg == 1) //S1按下后,白灯一直亮 检测到一次后白灯会一直亮{P1OUT &= ~BIT3; //熄灭白灯}}}
//编程实现:在MSP430F5529单片机系统上,P1.0、P1.1和P1.2端口分别接了红色、绿色、蓝色3只LED,均为高电平点亮。用定时器A实现以下事件:
//(1)红色LED每秒闪烁1次(0.5s亮,0.5s灭);
//(2)绿色LED每秒闪烁2次(0.25s亮,0.25s灭);
//(3)蓝色LED每秒闪烁1次(0.25s亮,0.75s灭)。
//提示:选择ACLK 32768Hz时钟,增计数模式#include <msp430.h>int main(void)
{WDTCTL = WDTPW + WDTHOLD;                 // Stop WDTP1DIR |= (BIT0 + BIT1 + BIT2);P1OUT &= ~(BIT0 + BIT1 + BIT2);TA0CCTL0 = CCIE;                          // CCR0 interrupt enabledTA0CCR0 = 327;                          //10MS一次中断TA0CTL = TASSEL_1 + MC_1 + TACLR;         // SMCLK, upmode, clear TAR__bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, enable interrupts}int led1_ifg, led2_ifg, led3_ifg; //时间
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
{//(1)红色LED每秒闪烁1次(0.5s亮,0.5s灭);led1_ifg = (led1_ifg + 1) % 100;//100*10ms=1sif (led1_ifg < 50){P1OUT |= BIT0;}else{P1OUT &= BIT0;}//(2)绿色LED每秒闪烁2次(0.25s亮,0.25s灭);led2_ifg = (led2_ifg + 1) % 50;if (led2_ifg < 25){P1OUT |= BIT1;}else{P1OUT &= BIT1;}//(3)蓝色LED每秒闪烁1次(0.25s亮,0.75s灭)。led3_ifg = led3_ifg + 1) % 100;if led3_ifg < 25){P1OUT |= BIT2;}else{P1OUT &= BIT2;}
}
  相关解决方案