//******************************************************************************
// MSP430F5438 Demo - USCI_A0, 9600 UART, SMCLK, LPM0, Echo with over-sampling
//
// Description: Echo a received character, RX ISR used. Normal mode is LPM0.
// USCI_A0 RX interrupt triggers TX Echo.
// If UCOS16=1, UCBRx=Fbrclk/(16*Baudrate)
// Baud rate divider with UCBRx = 1MHz/(16*9600) = ~6.8
// ACLK = REFO = ~32768Hz, MCLK = SMCLK = default DCO = 32 x ACLK = 1048576Hz
// See User Guide for baud rate divider table
//
// MSP430F5438
// -----------------
// /|\| |
// | | |
// --|RST |
// | |
// | P3.4/UCA0TXD|------------>
// | | 9600 - 8N1
// | P3.5/UCA0RXD|<------------
//
// M Smertneck / W. Goh
// Texas Instruments Inc.
// September 2008
// Built with CCE Version: 3.2.2 and IAR Embedded Workbench Version: 4.11B
//******************************************************************************
#include "msp430x54x.h"
unsigned char x;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 6; // 1MHz 9600 (see User's Guide)
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
// over sampling
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
__no_operation(); // For debugger
}
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV,4))
{
case 0:break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
break;
case 4: // Vector 4 - TXIFG
break;
default: break;
}
}
按照里面所说,应该是PC机发送什么就是应该返回什么,但是程序烧完,也是出不来,出来也是乱码, 作为新手头痛啊!
#include
#define uint unsigned int
#define uchar unsigned char
int k,i;
uchar temp,sendchar,receivechar;
/*
unsigned char UCA3Read()
{
while (!(UCA3IFG&UCRXIFG));
return UCA3RXBUF;
}
void UCA3Write( unsigned char sendchar)
{
while (!(UCA3IFG&UCTXIFG));
UCA3TXBUF = sendchar;
}
*/
void main()
{
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
P5DIR |= BIT4;
P5SEL|= (BIT2+BIT3); // 端口功能选择外部晶振XT2=24MHz
UCSCTL6 &= ~XT2OFF ; // XT2振荡器使能
UCSCTL6 |= XT2DRIVE0 + XT2DRIVE1 ; // XT2 最大驱动模式 24~32MHz
UCSCTL3 |= SELREF_2; //FLLREFCLK =REFOCLK .且FLLREFCLK不分频
UCSCTL4 |= (SELA__REFOCLK+SELM__XT2CLK+SELS__XT2CLK); // ACLK=REFO,SMCLK=XT2,MCLK=XT2
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG); // 清除 XT2,XT1,DCO 错误标志
SFRIFG1 &= ~OFIFG ; //清除晶振失效中断标志
}while(SFRIFG1&OFIFG);// 检测振荡器错误标志
UCA3CTL1|=UCSWRST; // 使能关闭状态机。USCI在复位后逻辑电平保持不变
P10SEL|= 0X30; // 选择引脚功能0x30
P10DIR|= 0X10 ; // 选择引脚功能
UCA3CTL1|= UCSSEL__SMCLK ; // UART_CLK = SMCLK
UCA3BR0= 0x0d ; //粗调波特率控制寄存器0(UCA3BRx的低八位)9600
UCA3BR1=0x00 ; //粗调波特率控制寄存器0(UCA3BRx的高八位)
UCA3MCTL|=(UCBRS_0 + UCBRF_3+UCOS16); // UCBRSx=0, UCBRFx=3 UCOS16=1过采样模式使能
UCA3CTL1&= ~UCSWRST ; // 启动状态机
while(1)
{
receivechar = UCA3RXBUF;
UCA3TXBUF = 0x55;
for( i = 8000; i > 0; i--);
}
}
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV,4))
{
case 0:break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
while (!(UCA3IFG&UCRXIFG));
UCA3TXBUF = receivechar;
break;
case 4:
while (!(UCA3IFG&UCTXIFG));
UCA3TXBUF = sendchar;
break;
default: break;
}
}
修改了一下,懵懵懂懂的,参照TI及一些厂家的程序,这个又可以用, 而且关于那TXD与RXD的定义,定义在P10却是接在P5.4,5可以正常运行
有修改了一个,但是无法进行收发,在12864液晶上显示,从PC机接收到的也是不准确的...
/////////////////////////////////////////////////////////////
// MSP430F5438
// -----------------
// /|\| |
// | | |
// --|RST |
// | |
// | P3.4/UCA0TXD|------------>
// | | 9600 - 8N1
// | P3.5/UCA0RXD|<------------
/////////////////////////////////////////////////////////////////
#include
#include "LCD12864.h"
#include "XT2.h"
#include "key_4_4.h"
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
unsigned char Key_Val; //存放键值
uchar dat;
void max232() //max232串口设置
{
P3SEL = 0x30; //P3.4/P3.5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSWRST; //要在UCSWRST=1的情况下设置串口
UCA0CTL1 |= UCSSEL__SMCLK; //选择SMCLK=1.5MHz
UCA0BR0 = 0x9c; // 1.5MHz/9600=156.25=0x009c
UCA0BR1 = 0x00;
UCA0MCTL |= UCBRS_2 + UCBRF_0; //0.25*8≈2,波特率调整
UCA0CTL1 &= ~UCSWRST; //初始化UART状态机
// UCA0IE |= UCRXIE+UCTXIE; //使能USART0 TXD/RXD
}
void key_P1() //P1.0,P1.1独立键盘
{
if((P1IN&0x0f)==0x02)
{
delay_ms(5);
if((P1IN&0x0f)==0x02)
{
P1OUT=BIT4;
UCA0IE |= UCTXIE; //(UCTxIE 置1 和UCRxIE 置 0)开发送中断,关闭收中断
}
while((P1IN&0x0f)==0x02);
}
if((P1IN&0x0f)==0x01)
{
delay_ms(5);
if((P1IN&0x0f)==0x01)
{
P1OUT=BIT5;
UCA0IE|= UCRXIE; //(UCTxIE 置0 和UCRxIE 置 1)关闭发送中断,开接收中断
}
while((P1IN&0x0f)==0x01);
}
}
void main(void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
initsys();//TX2时钟初始化
max232(); //max232串口设置
P9DIR|=0xff;
P9OUT =0xff;
P8DIR|=0xff;
P8OUT =0xff;
P2DIR=0xf0;//按键接口设置P2.0-P2.3 为输入方向
P2OUT|=0xf0;
P1DIR=0xfC;//P1.0,P1.1为独立键盘
LCD_Init(); //LCD12864初始化
diaply(); //数据显示
_EINT();//打开全局中断
while(1)
{
key_P1(); //P1.0,P1.1独立键盘
key_display();//按键值显示
}
}
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV,4))
{
case 0:break; // Vector 0 - no interrupt
// Vector 2 - RXIFG
case 2:
dat = UCA0RXBUF; //接收数据
LCD_WriteComm(0x8D);//写命令
LCD_WriteByte(0x30+dat/100);
LCD_WriteByte(0x30+dat/10%10);
LCD_WriteByte(0x30+dat%10);
UCA0IE=0x02; //开发送中断,关闭收中断
break;
// Vector 4 - TXIFG
case 4: delay_us(500);
UCA0TXBUF = Key_Val;
UCA0IE=0x01; //关闭发送中断,开接收中断
break;
default: break;
}
}
将 UCA0IE |= UCRXIE+UCTXIE; //使能 USART0 TXD/RXD 打开,屏蔽key_P1() 就能输入键盘值,也可以接受PC机给的数据,但是发布出去,接收的数值也不对,求指点
现在已经能够,发送接收10以下的数字,但是要以十六进制在串口小助手上面,发送与接收,有没有人懂为什么啊
引用: 原帖由 纹身灬猎人 于 2013-1-24 17:02 发表
现在已经能够,发送接收10以下的数字,但是要以十六进制在串口小助手上面,发送与接收,有没有人懂为什么啊
数据格式的问题
那就要写转换数据的代码吗?还是在定义变量哪里改格式?
回复 7楼 lr2131 的帖子
了解明白,这个得自己动脑