xxxxxpp 发表于 2024-11-27 21:17

使用串口调试助手不能回显数据,但是能控制灯的状态

<p>使用的是MSP430F2618的芯片和MSP-TS430PM64的开发板,写了一个使串口能够控制LED灯并且对输入的数字进行回显,使用串口调试助手输入0和1能正常控制灯,就是不能接收到数字,求助求助大神啊来帮帮,搞了好几天都没解决,已经要崩溃了。</p>

<p>下面是代码:</p>

<p>#include &lt;msp430.h&gt;</p>

<p>void main(void)<br />
{<br />
&nbsp; &nbsp; WDTCTL = WDTPW + WDTHOLD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 停止看门狗定时器</p>

<p>&nbsp; &nbsp; // 初始化 DCO 为 1 MHz<br />
&nbsp; &nbsp; BCSCTL1 = CALBC1_1MHZ;<br />
&nbsp; &nbsp; DCOCTL = CALDCO_1MHZ;</p>

<p>&nbsp; &nbsp; // 配置 LED<br />
&nbsp; &nbsp; P1DIR |= BIT0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 设置 P1.0 为输出模式<br />
&nbsp; &nbsp; P1OUT &amp;= ~BIT0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 初始化 LED 为关闭状态</p>

<p>&nbsp; &nbsp; // 配置串口<br />
&nbsp; &nbsp; P3SEL |= 0x30; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 设置 P3.4 和 P3.5 为 USCI_A0 的 TX 和 RX 引脚<br />
&nbsp; &nbsp; UCA0CTL1 |= UCSSEL_2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 使用 SMCLK 作为时钟源<br />
&nbsp; &nbsp; UCA0BR0 = 104; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 设置波特率为 9600,SMCLK = 1 MHz<br />
&nbsp; &nbsp; UCA0BR1 = 0;<br />
&nbsp; &nbsp; UCA0MCTL = UCBRS0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 设置调制<br />
&nbsp; &nbsp; UCA0CTL1 &amp;= ~UCSWRST; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 初始化 USCI 状态机<br />
&nbsp; &nbsp; IFG2 &amp;= ~UCA0RXIFG; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 清除 RX 中断标志位<br />
&nbsp; &nbsp; IE2 |= UCA0RXIE; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 启用 RX 中断</p>

<p>&nbsp; &nbsp; __delay_cycles(1000000); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 等待系统稳定(1 秒)<br />
&nbsp; &nbsp; __bis_SR_register(CPUOFF + GIE); &nbsp; &nbsp;// 进入低功耗模式,启用中断</p>

<p>&nbsp; &nbsp; while (1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 无限循环,保持程序运行<br />
}</p>

<p>// USCI_A0 RX 中断服务例程<br />
#pragma vector=USCIAB0RX_VECTOR<br />
__interrupt void USCI0RX_ISR(void)<br />
{<br />
&nbsp; &nbsp; unsigned char receivedChar = UCA0RXBUF; &nbsp;// 获取接收到的字符</p>

<p>&nbsp; &nbsp; if (receivedChar == &#39;1&#39;) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; P1OUT |= BIT0; &nbsp;// 点亮 LED<br />
&nbsp; &nbsp; } else if (receivedChar == &#39;0&#39;) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; P1OUT &amp;= ~BIT0; // 关闭 LED<br />
&nbsp; &nbsp; }</p>

<p>&nbsp; &nbsp; while (!(IFG2 &amp; UCA0TXIFG)); &nbsp; &nbsp; &nbsp; &nbsp;// 等待 TX 缓冲区准备好<br />
&nbsp; &nbsp; UCA0TXBUF = receivedChar; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 回显接收到的字符<br />
}<br />
&nbsp;</p>

xxxxxpp 发表于 2024-11-27 21:18

<div style="text-align: center;"></div>

<p>&nbsp;</p>

秦天qintian0303 发表于 2024-11-28 08:26

<p>正常往外边发送能成功吗?接收应该没问题了&nbsp;</p>

wangerxian 发表于 2024-11-28 09:16

<p>你写一个单片机定时串口发送数据,看看电脑串口助手能不能收到。</p>

xxxxxpp 发表于 2024-11-28 21:11

秦天qintian0303 发表于 2024-11-28 08:26
正常往外边发送能成功吗?接收应该没问题了&nbsp;

<p>谢谢回答,已经解决了,可能是引脚坏了,换了一个内部寄存器的另外两个引脚做收发是没有问题的</p>

xxxxxpp 发表于 2024-11-28 21:11

wangerxian 发表于 2024-11-28 09:16
你写一个单片机定时串口发送数据,看看电脑串口助手能不能收到。

<p>谢谢回答,已经解决了,可能是引脚坏了,换了一个内部寄存器的另外两个引脚做收发是没有问题的</p>

wangerxian 发表于 2024-11-29 10:38

<div class='shownolgin' data-isdigest='no'>xxxxxpp 发表于 2024-11-28 21:11
谢谢回答,已经解决了,可能是引脚坏了,换了一个内部寄存器的另外两个引脚做收发是没有问题的

<p>确实有可能,冬天产生静电比较多,有可能电坏了。</p>
</div><script>showreplylogin();</script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 使用串口调试助手不能回显数据,但是能控制灯的状态