1686|0

1704

帖子

0

TA的资源

五彩晶圆(初级)

楼主
 

MSP430F249—SPI主从机通信 [复制链接]

1。主机发送读取

/******************************************************************
**                                                       
**  File : SPI.c     | Master Send |                                    
**  Version : 1.0     
** Description : SPI Master Send and Receive                                                                       
**  Author : LightWu                              
**  Date : 2013-4-16                                                       
**                                                   
*******************************************************************/


#include "MSP430x24x.h"


unsigned char Data1;


void Delay(void)
{
  int m;
  for(m=5000;m>0;m--);
}


void SpiInit(void)
{
    P3SEL |= 0x0E;                            // P3.3,2,1 USCI_B0 option select,注意管脚配置
    P3DIR |= 0x01;                            // P3.0 output direction
    UCB0CTL0 |= UCCKPH + UCMSB + UCMST + UCSYNC;       // 3-pin, 8-bit SPI mstr, MSB 1st
    UCB0CTL1 |= UCSSEL_2;                     // SMCLK
    UCB0BR0 = 0x02;
    UCB0BR1 = 0;
    UCB0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
    
    IE2 |= UCB0RXIE;            // 打开接收中断
}


void main(void)
{
        // Stop watchdog timer to prevent time out reset
        WDTCTL = WDTPW + WDTHOLD;       //关狗


        SpiInit();
        
     
        while(1)
        {
                //Data1 = TLC549Read();
            
                //注意需要加延时
                Delay();
                
                P3OUT &= ~0x01;                         // Enable TLC549, /CS reset
            
                UCB0TXBUF = 0x55;                     // Transmit first character


                _BIS_SR(LPM0_bits + GIE);                 // CPU off, enable interrupts
            
                
        }
        


}


#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIA0RX_ISR (void)
{
        Data1 = UCB0RXBUF;                       // data = 00|DATA
        
        P3OUT |= 0x01;                          // Disable TLC549, /CS set
        
        __bic_SR_register_on_exit(LPM0_bits);        // Exit LPM0
}   

2.从机接收发送

/******************************************************************
**                                                       
**  File : SPI.c     | Slave Receive and Send |                                    
**  Version : 1.0     
** Description : SPI Slave Receive and Send                                                                      
**  Author : LightWu                              
**  Date : 2013-4-16                                                       
**                                                   
*******************************************************************/
#include <msp430.h>


int main(void)
{
  volatile unsigned int i;


  WDTCTL = WDTPW+WDTHOLD;                   // Stop watchdog timer
                                            // it is not yet in SPI mode
  P3SEL |= 0x0E;                            // P3.3,2,1 option select
  UCB0CTL1 = UCSWRST;                       // **Put state machine in reset**
  UCB0CTL0 |= UCCKPH + UCSYNC+UCMSB;          //3-pin, 8-bit SPI master
  UCB0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  IE2 |= UCB0RXIE;                          // Enable USCI_B0 RX interrupt


  _BIS_SR(LPM3_bits + GIE);                 // Enter LPM4, enable interrupts
}


// Echo character
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIB0RX_ISR (void)
{
  while (!(IFG2 & UCB0TXIFG));              // USCI_B0 TX buffer ready?
  UCB0TXBUF = UCB0RXBUF;
}

 
点赞 关注

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

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

 
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
快速回复 返回顶部 返回列表