MSP430串口使用总结

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

WDTCTL = WDTPW + WDTHOLD;
//关闭看门狗
} //************************************************************************* // MSP430 串口初始化 //************************************************************************* void UART_Init() { U1CTL|=SWRST + CHAR; U1TCTL|=SSEL1; U1BR1=baud_h; U1BR0=baud_l; U1MCTL=0x00; ME2|=UTXE1; ME2|=URXE1; U1CTL&=~SWRST; IE2|=URXIE1; P3SEL|= BIT6 + BIT7; P3DIR|= BIT6; //复位 SWRST,8 位数据模式 //SMCLK 为 串 口 时 钟 //BRCLK=8MHZ,Baud=BRCLK/N //N=UBR+(UxMCTL)/8 //微调寄存器为 0,波特率 9600bps //UART1 发送使能 //UART1 接收使能 //接收中断使能位 //设置 IO 口为第二功能模式,启用 UART 功能 //设置 TXD1 口方向为输出
ห้องสมุดไป่ตู้
********************************************************************/ #include <msp430x14x.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 /*当 BRCLK=CPU_F 时用下面的公式可以计算,否则要根据设置加入分频系数*/ #define baud 9600 // 设 置 波 特 率 的 大 小 #define baud_setting (uint)((ulong)CPU_F/((ulong)baud)) // 波 特 率 计 算 公 式 #define baud_h (uchar)(baud_setting>>8) // 提 取 高 位 #define baud_l (uchar)(baud_setting) //低位 /*RS485 控制管脚*/ #define RS485_CTR1 P5OUT |= BIT2; // 控 制 线 置 高 , RS485 发 送 状 态 #define RS485_CTR0 P5OUT &= ~BIT2; // 控 制 线 置 低 , RS485 接 收 状 态 //*********************************************************************** // MSP430IO 口初始化 //*********************************************************************** void Port_Init() { P5DIR |= BIT2; P5OUT |= BIT2; //P20 口为 RS485 控制口,输出模式 //置高 P20
} //*********************************************************************** // 系统时钟初始化 //*********************************************************************** void Clock_Init() { uchar i; BCSCTL1&=~XT2OFF; BCSCTL2|=SELM1+SELS; do{ IFG1&=~OFIFG; for(i=0;i<100;i++) _NOP(); } while((IFG1&OFIFG)!=0); IFG1&=~OFIFG; //如果标志位 1,则继续循环等待 //打开 XT2 振荡器 //MCLK 为 8MHZ,SMCLK 为 8MHZ //清楚振荡器错误标志
4、4 4、5 4、6 empty.
5、1 实例 1 main.c /******************************************************************** //电脑端必备 RS232 转 RS485 模块,否则无法进行直接的连接 //RS485 之间通过双绞线连接,如果距离近,可以不使用 120 欧电阻匹配 //电脑端通过串口调试助手操作,先将数据由电脑发往开发板 //开发板板会将收到的数据再发送到串口调试助手,接收采用中断模块,发送用查询 //波特率更改请通过 config.h 文件,直接更改 baud 参数即可 //时间:2011.09.19
MSP430 串口使用总结 _EINT();是打开全局中断,跟 51 中 EA=1;效果相似 _DINT();是关闭全局中断,跟 51 中 EA=0;效果相似 1、 MSP430 波特率发生器 MSP430 的波特率发生器比较特别 波特率发生器是用波特率选择寄存器和调整控制寄存器来产生串行数据位定时。
CHAR:UxCTL Bit 4 Character length,7-bit or 8-bit character length 0:7-bit data 1:8-bit data SWRST:UxCTL Bit 0 Software reset enable 0:Disabled,USART reset released for operation 1:Eable,USART logic held in reset state } 4、3 UxTCTL(UTCTLx),USART Transmit Control Register { SSELx:Bits 5-4 Source select,These bits select the BRCLK source clock 00:UCLKI 01:ACLK 10:SMCLK 11:SMCLK } UxBR0,USART Baud Rate Control Register 0,低 8 位 UxBR1,USART Baud Rate Control Register 1,高 8 位 UxMCTL,USART Modulation Control Register UxMCTL Bits 7-0:Modulation bits, these bits select the modulation for BRCLK IFG1,Interrupt Flag Register 1 UTXIFG0: IFG1 Bit 7,USART0 transmit interrupt flag, UTXIFGO is set when UOTXBUF is 0:No interrupt pending//不挂起中断 1:Interrupt pending//等待中断处理 URXIFG0:IFG1 Bit 6 ,USART0 receive interrupt flag,URXIFG0 is set when U0RXBUF has received. 0:No interrupt pending 1:Interrupt pending 4、7 IE1,Interrupt Enable Register 1 UTXIE0 Bit 7,USART0 transmit interrupt enable.This bit enables the UTXIFG0 interrupt 0:Interrupt not enable 1:Interrupt enable URXIE0 Bit 6 USART0 receive interrupt enable.This bit enables the URXIFG0 interrupt 0:Interrupt not enable 1:Interrupt enable
,异步通信模式(UART) 2、 具有同步串口模式(SPI) 作为异步通信时,P3.4、P3.5,P3.6、P3.7 的第二功能分别是 UTXD0、URXD0,UTXD1、 URXD1 3、 使用方法 3、1 程序架构 配置寄存器设置工作模式 { 设置 IO 口为第二功能作为串口收发引脚; 使能串口收发功能;// ME1:Module Enable Register 1 选择每帧数据位为 7 或者 8;// UxCTL(UCTLx):USART Control Register 选择波特率发生器时钟源;// UxTCTL(UTCTLx),USART Transmit Control Register 配置波特性(查表得出值再配置 UxBR0、UxBR1、UxMCTL) ; 软件清除串口复位位(SWRST) ; 若采用中断方式则使能接收、发送中断; } 编写接收、发送程序,可采用查询方式或中断方式。同 51 单片机不同的是:UTXIFG、 URXIFG 在发送下一个数据和读取数据时被自动清零,无需软件清除。 3、2 细节描述 配置波特率时用户手册上有速查表,如下: 设置波特率时: 要选择合适的时钟源。 对于较低的波特率 (9600/s) , 可选择 ACLK; 大于 9600 要选用 SMCLK,因为波特率发生器分频系数要求大于 3。 UxBRO(低)UxBR1(高)值的计算:选择的时钟源/波特率,在取整。为了精确,MSP430 设置了最小分频功能,通过 UxMCTL 来完成。 4、 相关寄存器 4、1 ME1:Module Enable Register 1 { UTXE0:ME1 Bit 7 USART0 transmit enable,this bit enables the transmitter for USART0 0:Module not enable 1:Modele enable URXEO:ME1 Bit 6 USARTO receive enable,this bit enables the receiver for USART0 0:Module not enabled 1:Module enable } 4、2 UxCTL(UCTLx):USART Control Register {
} //************************************************************************* // 串口 1 发送数据函数 //************************************************************************* void Send_Byte(uchar data) { RS485_CTR1; while(!(IFG2&UTXIFG1)); U1TXBUF=data; //RS485_CTR0; //发送寄存器空的时候发送数据
} //*********************************************************************** // MSP430 内部看门狗初始化 //*********************************************************************** void WDT_Init() {
相关文档
最新文档