432偏计算机编程,谈谈MSP432的编程方法

本帖最后由 平湖秋月 于 2015-5-18 21:16 编辑

基于寄存器的编程示例

//******************************************************************************

//  MSP432P401 Demo - eUSCI_B0 I2C Master TX bytes to Multiple Slaves

//

//  Description: This demo connects two MSP432's via the I2C bus.

//  The master transmits to 4 different I2C slave addresses 0x0A,0x0B,0x0C&0x0D.

//  Each slave address has a specific related data in the array TXData[].

//  At the end of four I2C transactions the slave address rolls over and begins

//  again at 0x0A.

//

//  Use with msp432p401_euscia0_i2c_multislave.c

//

//                                /|\  /|\

//               MSP432P401      10k  10k     MSP432P401

//                   slave         |    |        master

//             -----------------   |    |   -----------------

//            |     P1.6/UCB0SDA||P1.6/UCB0SDA     |

//            |                 |  |       |                 |

//            |                 |  |       |                 |

//            |     P1.7/UCB0SCL||P1.7/UCB0SCL     |

//            |                 |          |                 |

//

//   Wei Zhao

//   Texas Instruments Inc.

//   June 2014

//   Built with Code Composer Studio V6.0

//******************************************************************************

#include "msp.h"

#include uint8_t TXData[]= {0xA1,0xB1,0xC1,0xD1};        // Pointer to TX data

uint8_t SlaveAddress[]= {0x0A,0x0B,0x0C,0x0D};

uint8_t TXByteCtr;

uint8_t SlaveFlag = 0;

int main(void)

{

volatile uint32_t i;

WDTCTL = WDTPW | WDTHOLD;                         // Stop watchdog timer

// Configure Pins for I2C

P1SEL0 |= BIT6 | BIT7;                            // I2C pins

__enable_interrupt();

NVIC_ISER0 = 1 << ((INT_EUSCIB0 - 16) & 31); // Enable eUSCIB0 interrupt in NVIC module

// Configure USCI_B0 for I2C mode

UCB0CTLW0 |= UCSWRST;                             // put eUSCI_B in reset state

UCB0CTLW0 |= UCMODE_3 | UCMST;                    // I2C master mode, SMCLK

UCB0BRW = 0x0018;                                 // baudrate = SMCLK /24

UCB0CTLW0 &=~ UCSWRST;                            // clear reset register

UCB0IE |= UCTXIE0 | UCNACKIE;                     // transmit and NACK interrupt enable

SlaveFlag =0;

while(1)

{

SCB_SCR |= SCB_SCR_SLEEPONEXIT;                   // Don't wake up on exit from ISR

for (i = 1000; i > 0; i--);                       // Delay between transmissions

UCB0I2CSA = SlaveAddress[SlaveFlag];              // configure slave address

TXByteCtr = 1;                                    // Load TX byte counter

while (UCB0CTLW0 & UCTXSTP);                      // Ensure stop condition got sent

UCB0CTLW0 |= UCTR | UCTXSTT;                      // I2C TX, start condition

__sleep();

__no_operation();

// Change Slave address

SlaveFlag++;

if (SlaveFlag>3)                                  // Roll over slave address

{

SlaveFlag =0;

}

}

}

// I2C interrupt service routine

void eUSCIB0IsrHandler(void)

{

if (UCB0IFG & UCNACKIFG)

{

UCB0IFG &= ~ UCNACKIFG;

UCB0CTL1 |= UCTXSTT;                  // I2C start condition

}

if (UCB0IFG & UCTXIFG0)

{

UCB0IFG &= ~ UCTXIFG0;

if (TXByteCtr)                                // Check TX byte counter

{

UCB0TXBUF = TXData[SlaveFlag];            // Load TX buffer

TXByteCtr--;                              // Decrement TX byte counter

}

else

{

UCB0CTLW0 |= UCTXSTP;                     // I2C stop condition

UCB0IFG &= ~UCTXIFG;                      // Clear USCI_B0 TX int flag

SCB_SCR &= ~SCB_SCR_SLEEPONEXIT;          // Wake up on exit from ISR

}

}

}

MSP432 低功耗高性能并存10.1 Digital I/O Introduction The digital I/O features include: • Independently programmable individual I/Os • Any combination of input or output • Individually configurable interrupts for ports (available for certain ports only) • Independent input and output data registers • Individually configurable pullup or pulldown resistors • Wake-up capability from ultra-low power modes (available for certain ports only) • Individually configurable high drive I/Os (available for certain I/Os only) Devices within the family may have up to eleven digital I/O ports implemented (P1 to P10 and PJ). Most ports contain eight I/O lines; however, some ports may contain less (see the device-specific data sheet for ports available). Each I/O line is individually configurable for input or output direction, and each can be individually read or written. Each I/O line is individually configurable for pullup or pulldown resistors. Certain ports have interrupt and wake-up capability from ultra-low power modes (see device specific data sheet for ports with interrupt and wake-up capability). Each interrupt can be individually enabled and configured to provide an interrupt on a rising or falling edge of an input signal. All interrupts are fed into an encoded Interrupt Vector register, allowing the application to determine which sub-pin of a port has generated the event. Individual ports can be accessed as byte-wide ports or can be combined into half-word-wide ports. Port pairs P1 and P2, P3 and P4, P5 and P6, P7 and P8, and so on, are associated with the names PA, PB, PC, PD, and so on, respectively. All port registers are handled in this manner with this naming convention. The main exception are the interrupt vector registers, for example, interrupts for ports P1 and P2 must be handled through P1IV and P2IV, PAIV does not exist. When writing to port PA with half-word operations, all 16 bits are written to the port. When writing to the lower byte of port PA using byte operations, the upper byte remains unchanged. Similarly, writing to the upper byte of port PA using byte instructions leaves the lower byte unchanged. When writing to a port that contains less than the maximum number of bits possible, the unused bits are don't care. Ports PB, PC, PD, PE, and PF behave similarly.
当然可以!以下是一个简单的MSP432P401R单片机与MPU6050陀螺仪和加速度计通信的示例程序: ```c #include <msp.h> #include <stdio.h> #define MPU6050_ADDR 0x68 void I2C_init() { // 初始化I2C模块 P1->SEL0 |= BIT6 | BIT7; // 配置P1.6和P1.7为I2C功能 P1->SEL1 &= ~(BIT6 | BIT7); UCB0CTLW0 |= UCSWRST; // 将I2C控制器置为复位状态 UCB0CTLW0 |= UCMST | UCMODE_3 | UCSYNC | UCMM; // 配置为主模式、I2C模式、同步通信、多主机模式 UCB0BRW = 10; // 设置时钟分频,用于生成I2C时钟 UCB0CTLW0 &= ~UCSWRST; // 将I2C控制器解除复位 } void I2C_writeRegister(uint8_t regAddr, uint8_t data) { while (UCB0STATW & UCBBUSY) {} // 等待总线空闲 UCB0I2CSA = MPU6050_ADDR; // 设置从机地址 UCB0CTLW0 |= UCTR + UCTXSTT; // 发送开始位和写命令 while (!(UCB0IFG & UCTXIFG0)) {} // 等待发送缓冲区准备好 UCB0TXBUF = regAddr; // 发送寄存器地址 while (!(UCB0IFG & UCTXIFG0)) {} // 等待发送缓冲区准备好 UCB0TXBUF = data; // 发送数据 while (UCB0CTLW0 & UCTXSTP) {} // 等待停止位发送完成 } void I2C_readRegister(uint8_t regAddr, uint8_t* data) { while (UCB0STATW & UCBBUSY) {} // 等待总线空闲 UCB0I2CSA = MPU6050_ADDR; // 设置从机地址 UCB0CTLW0 |= UCTR + UCTXSTT; // 发送开始位和写命令 while (!(UCB0IFG & UCTXIFG0)) {} // 等待发送缓冲区准备好 UCB0TXBUF = regAddr; // 发送寄存器地址 while (UCB0CTLW0 & UCTXSTT) {} // 等待重复启动位发送完成 UCB0CTLW0 &= ~UCTR; // 切换到读模式 UCB0CTLW0 |= UCTXSTT; // 发送重复启动位和读命令 while (!(UCB0IFG & UCRXIFG0)) {} // 等待接收数据 *data = UCB0RXBUF; // 读取数据 UCB0CTLW0 |= UCTXSTP; // 发送停止位 } int main(void) { WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // 停用看门狗定时器 I2C_init(); // 初始化I2C模块 // 配置MPU6050 I2C_writeRegister(0x6B, 0x00); // 电源管理寄存器,解除复位状态 I2C_writeRegister(0x1B, 0x08); // 加速度计配置寄存器,设置量程为±4g I2C_writeRegister(0x1C, 0x08); // 陀螺仪配置寄存器,设置量程为±500°/s uint8_t accelX_H, accelX_L, accelY_H, accelY_L, accelZ_H, accelZ_L; uint8_t gyroX_H, gyroX_L, gyroY_H, gyroY_L, gyroZ_H, gyroZ_L; while (1) { I2C_readRegister(0x3B, &accelX_H); // 读取加速度计X轴高字节数据 I2C_readRegister(0x3C, &accelX_L); // 读取加速度计X轴低字节数据 I2C_readRegister(0x3D, &accelY_H); // 读取加速度计Y轴高字节数据 I2C_readRegister(0x3E, &accelY_L); // 读取加速度计Y轴低字节数据 I2C_readRegister(0x3F, &accelZ_H); // 读取加速度计Z轴高字节数据 I2C_readRegister(0x40, &accelZ_L); // 读取加速度计Z轴低字节数据 I2C_readRegister(0x43, &gyroX_H); // 读取陀螺仪X轴高字节数据 I2C_readRegister(0x44, &gyroX_L); // 读取陀螺仪X轴低字节数据 I2C_readRegister(0x45, &gyroY_H); // 读取陀螺仪Y轴高字节数据 I2C_readRegister(0x46, &gyroY_L); // 读取陀螺仪Y轴低字节数据 I2C_readRegister(0x47, &gyroZ_H); // 读取陀螺仪Z轴高字节数据 I2C_readRegister(0x48, &gyroZ_L); // 读取陀螺仪Z轴低字节数据 int16_t accelX = (accelX_H << 8) | accelX_L; // 将高字节和低字节组合成16位数据 int16_t accelY = (accelY_H << 8) | accelY_L; int16_t accelZ = (accelZ_H << 8) | accelZ_L; int16_t gyroX = (gyroX_H << 8) | gyroX_L; int16_t gyroY = (gyroY_H << 8) | gyroY_L; int16_t gyroZ = (gyroZ_H << 8) | gyroZ_L; printf("加速度计数据:\n"); printf("X轴: %d\n", accelX); printf("Y轴: %d\n", accelY); printf("Z轴: %d\n", accelZ); printf("陀螺仪数据:\n"); printf("X轴: %d\n", gyroX); printf("Y轴: %d\n", gyroY); printf("Z轴: %d\n", gyroZ); __delay_cycles(1000000); // 延时1秒 } } ``` 这个示例程序使用MSP432P401R单片机通过I2C通信协议与MPU6050陀螺仪和加速度计进行通信。程序初始化I2C模块并配置MPU6050的寄存器,然后循环读取加速度计和陀螺仪的数据并打印出来。 请注意,这只是一个简单的示例程序,你可能需要根据自己的具体需求进行修改和扩展。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值