1329|0

3836

帖子

19

TA的资源

纯净的硅(中级)

楼主
 

msp430f5529 uart pwm adc [复制链接]

//msp430f5529的串口使用程序使用示例
//--------------------------------------------- uart 头文件 -------------------------------------------------------------//
#ifndef UART_H_
#define UART_H_
#include <msp430f5529.h>
#include "config.h"
//默认为115200  
void USCIA0_Init(void);
void USCIA0_SendChar(u8 c);
u8 USCIA0_ReceiveChar(void);
void USCIA0_SendString(u8 *str);
void USCIA1_Init(void);
void USCIA1_SendChar(u8 c);
u8 USCIA1_ReceiveChar(void);
void USCIA1_SendString(u8 *str);
//void Uart1_Send_AF(void);
#endif /* #ifndef USCI_A0_h */
/*
//使用例程
void main(void)
{
    volatile unsigned int i;
    u8 data;
  
    dog_Disable();
    UCS_Init();
    //key_Init();
    USCIA0_Init();   
    while(1)
    {
        USCIA0_SendChar('A');
        USCIA0_SendString(" luoxn28");
        data = USCIA0_ReceiveChar();
if(data != 0)
{
        USCIA0_SendChar(data);
        USCIA0_SendChar('\r');USCIA0_SendChar('\n');
}      
        delay(10);      
    }
}
*/
//--------------------------------------------- uart 源文件 -------------------------------------------------------------//
#include "msp430_UART.h"

#if USCIA0_EN >0
/*********************************************************
*名称:USCIA0_Init
*功能:串口初始化
*入口参数:无
*出口参数:无
*说明:设置为P3.3和P3.4为串口通信端口 3.3-UCA0TXD 3.4-UCA0RXD
**********************************************************/
void USCIA0_Init(void)
{
  P3SEL |= BIT3+BIT4;                       // P3.3,4 = USCI_A0 TXD/RXD
  UCA0CTL1 |= UCSWRST;                      // **Put state machine in reset**
  UCA0CTL1 |= UCSSEL__ACLK;                     // ACLK
  UCA0BR0 = 34;           // 4MHz 115200
  UCA0BR1 = 0;            // 4MHz 115200
  UCA0MCTL |= UCBRS_6 + UCBRF_0;            // Modulation UCBRSx=1, UCBRFx=0
  UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  //IE2 |= UCA0RXIE + UCA0TXIE;  // Enable USCI_A0 TX/RX interrupt
  //IE2 |= UCA0RXIE;             // Enable USCI_A0 RX interrupt
  //__bis_SR_register(GIE);      // Enter LPM3 w/ interrupts enabled
}
//串口0发送字符函数
void USCIA0_SendChar(u8 c)
{
    UCA0TXBUF=c;   
    while(!(UCA0IFG & UCTXIFG));   // USCI_A0 TX buffer ready?
    UCA0IFG &= ~UCTXIFG;
}
//串口0接收字符函数
u8 USCIA0_ReceiveChar(void)
{
    u8 data = 0;

//阻塞式返回值
    while(!(UCA0IFG & UCRXIFG));   // USCI_A0 TX buffer ready?
    UCA0IFG &= ~UCRXIFG;
    data = UCA0RXBUF;
    return data;
/* 
//非阻塞式返回值
    if(UCA0IFG & UCRXIFG)
  {
UCA0IFG &= ~UCRXIFG;
data = UCA0RXBUF;
}
return data;
*/    
}
//串口0发送字符串函数
void USCIA0_SendString(u8 *str)
{
    while(*str != '\0')
    {
        USCIA0_SendChar(*str);
        str++;
    }
}
#endif //#if USCIA0_EN >0
#if USCIA1_EN > 0
/*********************************************************
*名称:USCIA0_Init
*功能:串口初始化
*入口参数:无
*出口参数:无
*说明:设置为P4.4和P4.5为串口通信端口 4.4-UCA1TXD 4.5-UCA1RXD
**********************************************************/
//串口1初始化函数
void USCIA1_Init(void)
{
  P4SEL |= BIT4+BIT5;                       // P3.3,4 = USCI_A0 TXD/RXD
  UCA1CTL1 |= UCSWRST;                      // **Put state machine in reset**
  UCA1CTL1 |= UCSSEL__ACLK;                     // ACLK
  UCA1BR0 = 34;           // 4MHz 115200
  UCA1BR1 = 0;            // 4MHz 115200
  UCA1MCTL |= UCBRS_6 + UCBRF_0;            // Modulation UCBRSx=1, UCBRFx=0
  UCA1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  //IE2 |= UCA0RXIE + UCA0TXIE;  // Enable USCI_A0 TX/RX interrupt
  //IE2 |= UCA0RXIE;             // Enable USCI_A0 RX interrupt
  //__bis_SR_register(GIE);      // Enter LPM3 w/ interrupts enabled
}
//串口1发送字符函数
void USCIA1_SendChar(u8 c)
{
    UCA1TXBUF=c;   
    while(!(UCA1IFG & UCTXIFG));   // USCI_A0 TX buffer ready?
    UCA1IFG &= ~UCTXIFG;
}
//串口1接收字符函数
u8 USCIA1_ReceiveChar(void)
{
    u8 data = 0;

//阻塞式返回值
    while(!(UCA1IFG & UCRXIFG));   // USCI_A0 TX buffer ready?
    UCA1IFG &= ~UCRXIFG;
    data = UCA1RXBUF;
    return data;
/* 
//非阻塞式返回值
    if(UCA1IFG & UCRXIFG)
  {
UCA1IFG &= ~UCRXIFG;
data = UCA1RXBUF;
}
return data;
*/    
}
//串口1发送字符串函数
void USCIA1_SendString(u8 *str)
{
    while(*str != '\0')
    {
        USCIA1_SendChar(*str);
        str++;
    }
}
#endif //#if USCIA1_EN > 0

/*
#include "UART.h"

void UART0_Init(void)
{
    P3SEL = 0x30;                             // 配置P3.4和P3.5脚为串口的输入输出口
    UCA0CTL1 |= UCSWRST;                      // 复位串口
    UCA0CTL1 |= UCSSEL_1;                     // 选择串口时钟为ACLK(32768HZ)
    UCA0BR0 = 0x03;                           // 设置串口波特率为9600
    UCA0BR1 = 0x00;                           
    UCA0MCTL = UCBRS_3+UCBRF_0;               
    UCA0CTL1 &= ~UCSWRST;                     // 打开串口
    UCA0IE |= UCRXIE;                         // 打开串口接收中断
}
*/

 
点赞 关注

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表