|
本帖最后由 610570143 于 2014-7-28 11:22 编辑
我需要通过i2c读取MPU6050的六轴数据,芯片型号是MSP430F5438。MSP430用的不多。
现在有两种办法,一是直接操作寄存器,另一个是使用MSP430Ware里面的库函数。
库函数:
贴一个例子:Master Receive Single
//!
//! /|\ /|\
// //! MSP430FR5739 10k 10k MSP430F5739
//! slave | | master
//! ----------------- | | -----------------
//! -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
//! | | | | | 32kHz
//! -|XOUT | | | XOUT|-
//! | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
//! | | | P1.0|--> LED
//!
#define SLAVE_ADDRESS 0x48
uint8_t RXData;
void main(void)
{
//Stop WDT
WDT_A_hold(WDT_A_BASE);
//Set DCO frequency to 8MHz
CS_setDCOFreq(CS_DCORSEL_0, CS_DCOFSEL_3);
//Set ACLK = DCO with frequency divider of 8
CS_clockSignalInit(CS_ACLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
//Set SMCLK = DCO with frequency divider of 8
CS_clockSignalInit(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
//Set MCLK = DCO with frequency divider of 8
CS_clockSignalInit(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
// Configure Pins for I2C
//Set P1.6 and P1.7 as Secondary Module Function Input.
/*
* Select Port 1
* Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
*/
GPIO_setAsPeripheralModuleFunctionInputPin(
GPIO_PORT_P1,
GPIO_PIN6 + GPIO_PIN7,
GPIO_SECONDARY_MODULE_FUNCTION
);
EUSCI_B_I2C_masterInit(EUSCI_B0_BASE,
EUSCI_B_I2C_CLOCKSOURCE_SMCLK,
CS_getSMCLK(),
EUSCI_B_I2C_SET_DATA_RATE_400KBPS,
1,
EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD
);
//Specify slave address
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
SLAVE_ADDRESS
);
//Set Master in receive mode
EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
EUSCI_B_I2C_RECEIVE_MODE
);
//Enable I2C Module to start operations
EUSCI_B_I2C_enable(EUSCI_B0_BASE);
EUSCI_B_I2C_clearInterruptFlag(EUSCI_B0_BASE,
EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
);
while (1) {
__delay_cycles(2000);
// I2C start condition
RXData = EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE);
}
}
直接操作寄存器:
例子:Master Receive Single
#include <msp430.h>
unsigned char RXData;
unsigned char flagTest;
int main (void)
{
WDTCTL = WDTPW + WDTHOLD;
// Init SMCLK = MCLk = ACLK = 1MHz
CSCTL0_H = 0xA5;
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting = 8MHz
CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = MCLK = DCO
CSCTL3 = DIVA_3 + DIVS_3 + DIVM_3; // set all dividers to 1MHz
// Configure pins
P1SEL1 |= BIT6 + BIT7;
UCB0CTLW0 |= UCSWRST; //Software reset enabled
UCB0CTLW0 |= UCMODE_3 + UCMST + UCSYNC;//I2C mode, Master mode, sync
UCB0CTLW1 |= UCASTP_2; //automatic stop generated
//after UCB0TBCNT is reached
UCB0BRW = 0x0008; // baudrate = SMCLK / 8
UCB0TBCNT |= 0x0001; //number of bytes to be received
UCB0I2CSA = 0x0048; //slave address
UCB0CTL1 &=~UCSWRST;
UCB0IE |= UCRXIE+UCNACKIE+UCBCNTIE;
P1DIR |= BIT0;
while (1)
{
//__delay_cycles(2000); //程序跑到这里就死了??
flagTest =1;
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(CPUOFF+GIE); // Enter LPM0 w/ interrupt
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_B0_VECTOR
__interrupt void USCIB0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCIB0_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(UCB0IV,0x1E))
{
case 0x00: break; // Vector 0: No interrupts break;
case 0x02: break; // Vector 2: ALIFG break;
case 0x04:
UCB0CTL1 |= UCTXSTT; // I2C start condition
break; // Vector 4: NACKIFG break;
case 0x06: break; // Vector 6: STTIFG break;
case 0x08: break; // Vector 8: STPIFG break;
case 0x0a: break; // Vector 10: RXIFG3 break;
case 0x0c: break; // Vector 14: TXIFG3 break;
case 0x0e: break; // Vector 16: RXIFG2 break;
case 0x10: break; // Vector 18: TXIFG2 break;
case 0x12: break; // Vector 20: RXIFG1 break;
case 0x14: break; // Vector 22: TXIFG1 break;
case 0x16:
RXData = UCB0RXBUF; // Get RX data
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
break; // Vector 24: RXIFG0 break;
case 0x18: break; // Vector 26: TXIFG0 break;
case 0x1a: P1OUT ^= BIT0;
break; // Vector 28: BCNTIFG break;
case 0x1c: break; // Vector 30: clock low timeout break;
case 0x1e: break; // Vector 32: 9th bit break;
default: break;
}
}
上面例程里面是两个MSP430单片机的i2c通信,我现在要做的则是MSP430读取mpu6050传感器。想问一下,直接在例程上面修改的地方多吗?
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|