现在的位置: 首页 > 综合 > 正文

MSP430F249IIC

2013年10月08日 ⁄ 综合 ⁄ 共 3931字 ⁄ 字号 评论关闭

/******************************************************************
**                                                       
**  File
: IIC.c     | IIC Communication |                                    
**  Version
: 1.0     
**  Description : 430 WRITE
AND READ 24C02  VIA IIC BUS                                                                     

**  Author
: LightWu                              
**  Date
: 2013-4-11                                                       
**                                                   
*******************************************************************/

#include <msp430.h>

//注意:两次发送间隔必须要有延时,否则不能再次发送,串口发送格式:

unsigned char PTxData[250];                     // Pointer to TX data
unsigned char pHead;
unsigned char pTail;

unsigned char pHeadR;
unsigned char pTailR;

unsigned char TXByteCtr;
const unsigned char TxData[] =              // Table of data to transmit
{
  0x01,
  0x02,
  0x03,
  0x04,
  0x05
};
unsigned char RxData[] ;

void UartInit(void)
{
  if (CALBC1_1MHZ==0xFF) // If calibration constant erased
  {
    while(1);                               // do not load, trap CPU!!

  } 
  DCOCTL = 0;                               // Select lowest DCOx and MODx settings
  BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
  DCOCTL = CALDCO_1MHZ;
  P3SEL |= 0x30;                             // P3.4,5 = USCI_A0 TXD/RXD
  UCA0CTL1 |= UCSSEL_2;                     // SMCLK
  UCA0BR0 = 104;                            // 1MHz 9600; (104)decimal = 0x068h
  UCA0BR1 = 0;                              // 1MHz 9600
  UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
  UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
}
void UartSend( unsigned char Data )
{
      UCA0TXBUF = Data;                    // TX -> RXed character
      while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?,发送缓冲区空
 
}
void IICInit(void)
{
  UCB0CTL1 |= UCSWRST;                      // Enable SW reset
  UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // I2C Master, synchronous mode
  UCB0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset
  UCB0BR0 = 12;                             // fSCL = SMCLK/12 = ~100kHz
  UCB0BR1 = 0;
  UCB0I2CSA = 0xA0>>1;                         // Slave Address is ,注意地址需要右移一位,24C02地址为0XA0,故要写入0X50
                                            // 7位地址模式,器件会发送一位读写位,正好8位。
  UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation
  IE2 |= UCB0TXIE;                          // Enable TX interrupt
  IE2 |= UCB0RXIE; 

}
void IICSend( void )
{       
    int i;
    
    for(i=3000;i>0;i--);                   //两次发送间隔必须要有延时,否则不能再次发送

    while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

    UCB0CTL1 |= UCTR + UCTXSTT;             // I2C TX, start condition
   
    __bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts

}
void IICRecv( void )
{       
    int i;
    
    for(i=3000;i>0;i--);                   //两次发送间隔必须要有延时,否则不能再次发送
    

   while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent 
 
    UCB0CTL1 &= ~UCTR;
    UCB0CTL1 |= UCTXSTT; 
   
    __bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts

}

int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  P3SEL |= 0x06;                            // Assign I2C pins to USCI_B0
  
  IICInit();
  
  UartInit();

  while (1)
  {
      UartSend('A');
      
      PTxData[0] = 1;
      PTxData[1] = 'M';
      PTxData[2] = 3;
      PTxData[3] = 4;
      PTxData[4] = 5;
      pTail = 2;
      
      IICSend();
        
      UartSend('B');
      
      //////////////////////
      
      pHeadR = 0; 
      pTailR = 3;
      IICRecv();
      
      UartSend(RxData[0]);
      UartSend(RxData[1]);
      UartSend(RxData[2]);
      
      while(1);

  }
}

//------------------------------------------------------------------------------
// The USCIAB0TX_ISR is structured such that it can be used to transmit any
// number of bytes by pre-loading TXByteCtr with the byte count. Also, TXData
// points to the next byte to transmit.
//------------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
  if ( pHead < pTail )                            // Check TX byte counter
  {

      UCB0TXBUF = PTxData [pHead++ ];                 // Load TX buffer
        
  }
  else
  {
     pHead = 0; 
      
   // UCB0CTL1 |= UCTXSTP;                    // I2C stop condition,if write finish
    IFG2 &= ~UCB0TXIFG;                     // Clear USCI_B0 TX int flag
    __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
  
  }
}

#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
    
  if ( pHeadR < pTailR )                            // Check TX byte counter
  {

      RxData[ pHeadR++ ] = UCB0RXBUF;                   // Move RX data to address PRxData
  
  }
  else
  {  
      pHeadR = 0; 
      
      UCB0CTL1 |= UCTXSTP;                    // I2C stop condition
      IFG2 &= ~UCB0RXIFG;                     // Clear USCI_B0 RX int flag
      __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
      
  }

}

抱歉!评论已关闭.