1459 views|0 replies

3836

Posts

19

Resources
The OP
 

msp430g2553 hardware IIC [Copy link]

#include "msp430g2553.h"
#include "uart.h"

unsigned char RX_Data;
void I2C_Init(unsigned char SA);//I2C initialization, SA is the slave device address
void I2C_ReadData(unsigned char address);//I2C reads data from a certain address
unsigned char I2C_WriteData(unsigned char address,unsigned char data);//I2C writes data to a certain address
void delay(void);

void I2C_Init(unsigned char SA)
{
P1SEL |= (BIT6 + BIT7);
P1SEL2|= (BIT6 + BIT7);
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 30; // fSCL = SMCLK/80 = ~100kHz
UCB0BR1 = 0;
UCB0CTL0 &= ~UCSLA10; // 7位地址模式
UCB0I2CSA = SA; // Slave Address is 2ch
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
delay();
}

unsigned char I2C_WriteData(unsigned char address,unsigned char data)
{
while( UCB0CTL1& UCTXSTP );
UCB0CTL1 |= UCTR; // write mode
UCB0CTL1 |= UCTXSTT; // send start bit

UCB0TXBUF = address; // Send byte address
// Wait for UCTXIFG=1 and UCTXSTT=0 to change at the same time and wait for a flag bit
while((IFG2 & UCB0TXIFG)==0)
{
if( UCB0STAT& UCNACKIFG ) // If there is no response UCNACKIFG=1
{
return 1;
}
}

UCB0TXBUF = data; // Send byte content
while((IFG2 & UCB0TXIFG)==0); // Wait for UCTXIFG=1

UCB0CTL1 |= UCTXSTP;
while(UCB0CTL1& UCTXSTP); // Wait for sending to complete

return 0;
}

unsigned char I2C_WriteNData( unsigned char address, unsigned char *pWbuf, unsigned char len)
{
unsigned char i;
while( UCB0CTL1& UCTXSTP );
UCB0CTL1 |= UCTR; // write mode
UCB0CTL1 |= UCTXSTT; // send start bit

UCB0TXBUF = address; // Send byte address
// Wait for UCTXIFG=1 and UCTXSTT=0 to change at the same time and wait for a flag bit
while((IFG2 & UCB0TXIFG)==0)
{
if( UCB0STAT& UCNACKIFG ) // If there is no response UCNACKIFG=1
{
return 1;
}
}

for( i= 0; i < len; i++)
{
UCB0TXBUF = *pWbuf++; // Send register content
while(UCB0CTL1& UCTXSTP); // Wait for UCTXIFG=1
}

UCB0CTL1 |= UCTXSTP;
while(UCB0CTL1& UCTXSTP); // Wait for sending to complete

return 0;
}
void I2C_ReadData(unsigned char address)
{
UCB0CTL1 |= UCTR ;
UCB0CTL1 |= UCTXSTT; // I2C TX, start condition
UCB0TXBUF = address;////eeprom low addr
while((IFG2 & UCB0TXIFG)==0);
UCB0CTL1 &= ~UCTR; // I2C RX,
UCB0CTL1 |= UCTXSTT;
while(UCB0CTL1&UCTXSTT);
UCB0CTL1 |= UCTXSTP;
while((IFG2&UCB0RXIFG)==0);
RX_Data = UCB0RXBUF;
while(UCB0CTL1 & UCTXSTP);
}


unsigned char I2C_ReadNData(unsigned char address, unsigned char *pRead, unsigned char len )
{
unsigned char i;
while( UCB0CTL1& UCTXSTP );
UCB0CTL1 |= UCTR; // Write mode
UCB0CTL1 |= UCTXSTT; // Send start bit and write control byte
UCB0TXBUF = address; // Send byte address
// Wait for UCTXIFG=1 and UCTXSTT=0 to change at the same time Wait for a flag bit
while((IFG2 & UCB0TXIFG)==0);
UCB0CTL1 &= ~UCTR; // Read mode
UCB0CTL1 |= UCTXSTT; // Send start bit and read control byte
while(UCB0CTL1& UCTXSTT); // Wait for UCTXSTT=0
// If no response UCNACKIFG = 1
for( i= 0; i< len -1 ; i++)
{
while((IFG2&UCB0RXIFG)==0); // Read byte content, excluding the last byte content
*pRead++= UCB0RXBUF;
}
UCB0CTL1 |= UCTXSTP; // Send stop bit before receiving the last byte
while((IFG2&UCB0RXIFG)==0); // Read the last byte content
*pRead = UCB0RXBUF;
while( UCB0CTL1& UCTXSTP );
return 0;
}
void delay(void)
{
unsigned int i,n;
for(i=0;i<100;i++)
for(n=0;n<0xff;n++);
}

int main( void )
{
unsigned char ReadBuf[10];
unsigned char temp = 1;
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1=CALBC1_1MHZ;
DCOCTL=CALDCO_1MHZ;
Uart_Init();
P1DIR |= BIT0;
P1OUT |= BIT0;
I2C_Init(0x51);//1010 000X>>1

//I2C_WriteData(0x02,5);
_EINT();
//I2C_Init(0x51);//1010 000X>>1
//I2C_ReadData(tUart.RecBuf[0]-0x30);
while(1)
{
if(tUart.RecFlag)
{
tUart.RecFlag = 0;
tUart.Len = 0;

//I2C_Init(0x51);//1010 000X>>1
//I2C_ReadData(tUart.RecBuf[0]-0x30);
I2C_WriteData(0x03,temp++);
I2C_ReadNData(0x02, ReadBuf, 8 );
UartSendStr(ReadBuf);
//UartSendByte(RX_Data);
}
}
}

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list