MSP430F249EEprom读写
扫描二维码
随时随地手机看文章
/******************************************************************
**
** File : EEPROM.c | Eeprom write and read |
** Version : 1.0
** Description: Eeprom write and read
** Author : LightWu
** Date : 2013-4-15
**
*******************************************************************/
#include
//注意:两次发送间隔必须要有延时,否则不能再次发送,串口发送格式:
unsigned char PTxData[250]; // Pointer to TX data
unsigned char pHeadT;
unsigned char pTailT;
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
}
//写入一个字节数据
void EepromWrite( unsigned char Addr, unsigned char Data )
{
int i;
pHeadT = 0;
pTailT = 2; //一个地址,一个数据
PTxData[0] = Addr;
PTxData[1] = Data;
for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送
while (UCB0STAT & UCBUSY); // wait until I2C module has
// finished all operations.
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
//进入低功耗状态
while( pHeadT < pTailT ); //等待直到发送完成
pHeadT = 0; //清零
UCB0CTL1 |= UCTXSTP; // I2C stop condition,产生终止信号
while(UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
}
//读N个字节数据
unsigned char EepromRead( unsigned char Addr , unsigned char Num )
{
int i;
pHeadT = 0;
pTailT = 1; //写入1个地址
pHeadR = 0;
pTailR = Num; //读取个数
PTxData[0] = Addr;
for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送
while (UCB0STAT & UCBUSY); // wait until I2C module has
// finished all operations.
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
//进入低功耗状态
//设置为读取接收
for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT; //start Read
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
return 1;
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
IICInit();
UartInit();
while (1)
{
UartSend('M');
EepromWrite(0x13,'A');
EepromWrite(0x11,'B');
EepromWrite(0x12,'C');
EepromRead(0X10,4);
UartSend(RxData[0]);
UartSend(RxData[1]);
UartSend(RxData[2]);
UartSend(RxData[3]);
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 ( pHeadT < pTailT ) // Check TX byte counter
{
UCB0TXBUF = PTxData [pHeadT++ ]; // Load TX buffer
}
else
{
// pHeadT = 0;
// UCB0CTL1 |= UCTXSTP; // I2C stop condition
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
}
}