F430的I2C做从机,该如何判断程序到哪来了,中断是如何响应和退出的?
用F430的I2C做从机,用中断方式做数据传输,需要负责从主机接受多个包含”地址+命令+相应的数据+结束符“的数据并作出相应的处理,怎么提出这个命令来判断是哪个命令,怎么从中断方式来判别我已经接受到了那些数据。特别是分别出来已经接受到了”地址+命令+相应的数据+结束符“的哪一个了,我该怎么控制这个地方,是靠什么时候来读空UCB0RXBUF这个。还是完全靠主机来控制。因为我需要对读出来的命令做相应的程序处理,包括对主机传过来的其他数据做个显示或者存储之类的,还有可能对主机回传数据!哪位大侠帮忙解决下,或者帮忙解释下249做从机时候,采用中断方式时的,程序的执行步骤,怎么进入中断,又是什么时候开始发送和接受数据的,都有什么标志方式和条件没有?
以下是程序:
//******************************************************************************
//MSP430x24x Demo - USCI_B0 I2C Slave TX multiple bytes to MSP430 Master
//
//Description: This demo connects two MSP430's via the I2C bus. The slave
//transmits to the master. This is the slave code. The interrupt driven
//data transmission is demonstrated using the USCI_B0 TX interrupt.
//ACLK = n/a, MCLK = SMCLK = default DCO = ~1.045Mhz
//
// /|\/|\
// MSP430F249 10k10k MSP430F249
// slave | | master
// ----------------- | |-----------------
// -|XINP3.1/UCB0SDA|<-|---+->|P3.1/UCB0SDAXIN|-
// | || | |
// -|XOUT || | XOUT|-
// | P3.2/UCB0SCL|<-+----->|P3.2/UCB0SCL |
// | | | |
//
//B. Nisarga
//Texas Instruments Inc.
//September 2007
//Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.42A
//******************************************************************************
#include "msp430x24x.h"
unsigned char *PTxData; // Pointer to TX data
volatile unsigned char TXByteCtr;
const unsigned char TxData[] = // Table of data to transmit
{
0x11,
0x22,
0x33,
0x44,
0x55
};
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode
UCB0I2COA = 0x48; // Own Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0I2CIE |= UCSTPIE + UCSTTIE; // Enable STT and STP interrupt
IE2 |= UCB0TXIE; // Enable TX interrupt
while (1)
{
PTxData = (unsigned char *)TxData; // Start of TX buffer
TXByteCtr = 0; // Clear TX byte count
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
// Remain in LPM0 until master
// finishes RX
__no_operation(); // Set breakpoint >>here<< and
} // read out the TXByteCtr counter
}
//------------------------------------------------------------------------------
// The USCI_B0 data ISR is used to move data from MSP430 memory to the
// I2C master. PTxData points to the next byte to be transmitted, and TXByteCtr
// keeps track of the number of bytes transmitted.
//------------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
UCB0TXBUF = *PTxData++; // Transmit data at address PTxData
TXByteCtr++; // Increment TX byte counter
}
//------------------------------------------------------------------------------
// The USCI_B0 state ISR is used to wake up the CPU from LPM0 in order to do
// processing in the main program after data has been transmitted. LPM0 is
// only exit in case of a (re-)start or stop condition when actual data
// was transmitted.
//------------------------------------------------------------------------------
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
UCB0STAT &= ~(UCSTPIFG + UCSTTIFG); // Clear interrupt flags
if (TXByteCtr) // Check TX byte counter
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0 if data was
} // transmitted 回复【楼主位】tianyaxtujxlg
-----------------------------------------------------------------------
自己顶一个吧,这个问题也是困惑我很久了,望哪位大侠帮忙解决下,本人不胜感激
页:
[1]