📄 msp430-tusb3410_demo_cce.c
字号:
return true; // EEPROM not ready/not detected
else
return false; // EEPROM detected
}
//------------------------------------------------------------------------------
// void EEPROM_ByteWrite(unsigned int Address, unsigned char Data)
//
// This function writes one byte to the specified address of an externally
// connected EEPROM using the I2C module. After the write, it waits until
// the EEPROM returns an ACK which indicates that the write was completed.
//
// IN: Address // EEPROM address to write to
// Data // 8-bit data
// OUT: -
//------------------------------------------------------------------------------
void EEPROM_ByteWrite(unsigned int Address, unsigned char Data)
{
while (I2CDCTL & I2CBUSY); // Wait for I2C module
I2CBuffer[2] = Address >> 8; // 16-bit address MSB
I2CBuffer[1] = Address; // 16-bit address LSB
I2CBuffer[0] = Data;
I2CTxPtr = 2; // Set I2CBuffer[] pointer
U0CTL |= MST; // I2C master mode
I2CTCTL |= I2CTRX; // Transmit mode (R/W bit = 0)
I2CIFG &= ~TXRDYIFG;
I2CIE = TXRDYIE; // Transmit ready interrupt
I2CNDAT = 3; // 1 control + 3 data bytes
I2CTCTL |= I2CSTT + I2CSTP; // Gen. start and stop condition
// Acknowledge Polling. The EEPROM will not acknowledge if a write cycle is
// in progress. It can be used to determine when a write cycle is completed.
while (EEPROM_CheckNACK());
}
//------------------------------------------------------------------------------
// unsigned char EEPROM_CurrentAddressRead(void)
//
// This function reads one byte of data from the current address of the
// EEPROM internal address pointer.
//
// IN: -
// OUT: return() 8-bit data from EEPROM
//------------------------------------------------------------------------------
unsigned char EEPROM_CurrentAddressRead(void)
{
while (I2CDCTL & I2CBUSY); // Wait for I2C module
U0CTL |= MST; // I2C master mode
I2CTCTL &= ~I2CTRX; // Receive mode (R/W bit = 1)
I2CIFG &= ~RXRDYIFG;
I2CIE = RXRDYIE; // Enable RX ready interrupt
I2CNDAT = 1; // 1 data byte
I2CIFG &= ~ARDYIFG; // Clear interrupt flag
I2CTCTL |= I2CSTT + I2CSTP; // Gen. start and stop condition
while (!(I2CIFG & ARDYIFG)); // Wait until RX is finished
return I2CBuffer[0];
}
//------------------------------------------------------------------------------
// unsigned char EEPROM_RandomRead(unsigned int Address)
//
// This function reads one byte of data from the specified address of the
// EEPROM internal address pointer.
//
// IN: Address EEPROM address to read from
// OUT: return() 8-bit data from EEPROM
//------------------------------------------------------------------------------
unsigned char EEPROM_RandomRead(unsigned int Address)
{
while (I2CDCTL & I2CBUSY); // Wait for I2C module
I2CBuffer[1] = Address >> 8; // 16-bit address MSB
I2CBuffer[0] = Address; // 16-bit address LSB
I2CTxPtr = 1; // Set I2CBuffer[] pointer
U0CTL |= MST; // I2C master mode
I2CTCTL |= I2CTRX; // Transmit mode (R/W bit = 0)
I2CIFG &= ~TXRDYIFG;
I2CIE = TXRDYIE; // Transmit ready interrupt
I2CNDAT = 2; // 1 control + 2 data bytes
I2CIFG &= ~ARDYIFG; // Clear interrupt flag
I2CTCTL |= I2CSTT; // Gen. start condition
while (!(I2CIFG & ARDYIFG)); // Wait until address was send
I2CTCTL &= ~I2CTRX; // Receive mode (R/W bit = 1)
I2CIFG &= ~RXRDYIFG;
I2CIE = RXRDYIE; // Enable RX ready interrupt
I2CNDAT = 1; // 1 data byte
I2CIFG &= ~ARDYIFG; // Clear interrupt flag
I2CTCTL |= I2CSTT + I2CSTP; // Gen. start and stop condition
while (!(I2CIFG & ARDYIFG)); // Wait until RX is finished
return I2CBuffer[0];
}
//------------------------------------------------------------------------------
// void EEPROM_Write(unsigned int Address, unsigned char *Buffer,
// unsigned int Count)
//
// This function writes a block of data to a specified address in the
// external EEPROM.
//
// IN: Address EEPROM start address
// Buffer Start address of data to write to EEPROM
// Count Size of data block in bytes
// OUT: -
//------------------------------------------------------------------------------
void EEPROM_Write(unsigned int Address, unsigned char *Buffer,
unsigned int Count)
{
unsigned int i;
for(i = 0; i < Count; i++)
EEPROM_ByteWrite(Address++, Buffer[i]);
}
//------------------------------------------------------------------------------
// void EEPROM_Read(unsigned int Address, unsigned char *Buffer,
// unsigned int Count)
//
// This function reads a block of data from the external EEPROM and stores
// it to MSP430 memory.
//
// IN: Address EEPROM start address
// Buffer Start address of the buffer to store EEPROM data
// Count Size of data block in bytes
// OUT: -
//------------------------------------------------------------------------------
void EEPROM_Read(unsigned int Address, unsigned char *Buffer,
unsigned int Count)
{
unsigned int i = 0;
Buffer[i] = EEPROM_RandomRead(Address);
for(i = 1; i < Count; i++)
Buffer[i] = EEPROM_CurrentAddressRead();
}
//------------------------------------------------------------------------------
// unsigned int EEPROM_Verify(unsigned int Address, unsigned char *Buffer,
// unsigned int Count)
//
// This function compares a block of data in the external EEPROM and the MSP
// memory. The return value indicates whether the data is identical or not.
//
// IN: Address EEPROM start address
// Buffer Start address of the buffer to store EEPROM data
// Count Size of data block in bytes
// OUT: return() false on data mismatch, true on match
//------------------------------------------------------------------------------
unsigned int EEPROM_Verify(unsigned int Address, unsigned char *Buffer,
unsigned int Count)
{
unsigned int i = 0;
if (Buffer[i] != EEPROM_RandomRead(Address))
return false; // EEPROM contents mismatch
for (i = 1; i < Count; i++)
if (Buffer[i] != EEPROM_CurrentAddressRead())
return false; // EEPROM contents mismatch
return true; // EEPROM matches buffer
}
//------------------------------------------------------------------------------
// void I2CISR(void)
//
// The I2C interrupt service function is used for data receiption and
// transmission. Global variables are used for data exchange.
//
// IN: I2CBuffer[]
// I2CTxPtr
// OUT: I2CBuffer[]
// I2CTxPtr
//------------------------------------------------------------------------------
__interrupt void I2CISR(void)
{
switch (I2CIV)
{
case I2CIV_NONE :
case I2CIV_AL :
case I2CIV_NACK :
case I2CIV_OA :
case I2CIV_ARDY :
break;
case I2CIV_RXRDY :
I2CBuffer[0] = I2CDRB; // Store RX data in buffer
break;
case I2CIV_TXRDY :
I2CDRB = I2CBuffer[I2CTxPtr];
if (I2CTxPtr-- == 0) // TX finished?
I2CIE &= ~TXRDYIE; // Disable TX ready interrupt
break;
case I2CIV_GC :
case I2CIV_STT :
default :
_never_executed(); // Use calculated branching
}
}
USART0TX_ISR(I2CISR)
//------------------------------------------------------------------------------
// void TIMERB0ISR(void)
//
// This function uses Timer_B7 to process SW1 push-button events.
// The timer is used in capture mode to generate an interrupt on the initial
// button press. After that, compare mode is activated, and the status of the
// button is polled every 50,000 ACLK cycles for debounce purposes.
//
// IN: -
// OUT: ButtonState Current status of SW1/SW2/SW3/SW4
// ButtonSet Flag register that indicates button press
// ButtonReleased Flag register that indicates button release
//------------------------------------------------------------------------------
__interrupt void TIMERB0ISR(void)
{
if (TBCCTL0 & CAP) // In capture mode?
{
ButtonState |= 0x01; // Indicate SW1 button press
ButtonSet |= 0x01;
ButtonReleased &= ~0x01;
TBCCTL0 &= ~CAP; // Disable capture mode
TBCCR0 += 50000; // Time to next compare event
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
else if (!(TBCCTL0 & CCI))
{
ButtonState &= ~0x01; // Indicate SW1 button release
ButtonSet &= ~0x01;
ButtonReleased |= 0x01;
TBCCTL0 |= CAP; // Enable capture mode
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
}
TIMERB0_ISR(TIMERB0ISR)
//------------------------------------------------------------------------------
// void TIMERB1ISR(void)
//
// This function uses Timer_B7 to process SW2, SW3, and SW4 push-button events.
// The timer is used in capture mode to generate an interrupt on the initial
// button press. After that, compare mode is activated, and the status of the
// button is polled every 50,000 ACLK cycles for debounce purposes.
//
// IN: -
// OUT: ButtonState Current status of SW1/SW2/SW3/SW4
// ButtonSet Flag register that indicates button press
// ButtonReleased Flag register that indicates button release
//------------------------------------------------------------------------------
__interrupt void TIMERB1ISR(void)
{
switch (TBIV)
{
case 0x00 :
case 0x02 :
break;
case 0x04 :
if (TBCCTL2 & CAP) // In capture mode?
{
ButtonState |= 0x02; // Indicate SW2 button press
ButtonSet |= 0x02;
ButtonReleased &= ~0x02;
TBCCTL2 &= ~CAP; // Disable capture mode
TBCCR2 += 50000; // Time to next compare event
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
else if (!(TBCCTL2 & CCI))
{
ButtonState &= ~0x02; // Indicate SW2 button release
ButtonSet &= ~0x02;
ButtonReleased |= 0x02;
TBCCTL2 |= CAP; // Enable capture mode
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
break;
case 0x06 :
break;
case 0x08 :
if (TBCCTL4 & CAP) // In capture mode?
{
ButtonState |= 0x04; // Indicate SW3 button press
ButtonSet |= 0x04;
ButtonReleased &= ~0x04;
TBCCTL4 &= ~CAP; // Disable capture mode
TBCCR4 += 50000; // Time to next compare event
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
else if (!(TBCCTL4 & CCI))
{
ButtonState &= ~0x04; // Indicate SW3 button release
ButtonSet &= ~0x04;
ButtonReleased |= 0x04;
TBCCTL4 |= CAP; // Enable capture mode
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
break;
case 0x0a :
break;
case 0x0c :
if (TBCCTL6 & CAP) // In capture mode?
{
ButtonState |= 0x08; // Indicate SW4 button press
ButtonSet |= 0x08;
ButtonReleased &= ~0x08;
TBCCTL6 &= ~CAP; // Disable capture mode
TBCCR6 += 50000; // Time to next compare event
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
else if (!(TBCCTL6 & CCI))
{
ButtonState &= ~0x08; // Indicate SW4 button release
ButtonSet &= ~0x08;
ButtonReleased |= 0x08;
TBCCTL6 |= CAP; // Enable capture mode
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
break;
case 0x0e :
default :
_never_executed(); // Use calculated branching
}
}
TIMERB1_ISR(TIMERB1ISR)
//------------------------------------------------------------------------------
// void USART0RXISR(void)
//
// This function reads out a received character from the UART. After that,
// the MSP430 returns active to process any pending events.
//
// IN: -
// OUT: RxData Received character
// DataReceived Flag to indicate that a char was RX'd
//------------------------------------------------------------------------------
__interrupt void USART0RXISR(void)
{
RxData = U0RXBUF;
DataReceived = true;
__bic_SR_register_on_exit(LPM0_bits); // Wake-up CPU
}
USART0RX_ISR(USART0RXISR)
//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -