我写了一段程序,想通过I2C与GY-521陀螺仪通信,结果受到的全是相同的值,这是为什么??
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/adc.h"
#include "driverlib/pwm.h"
#include "driverlib/i2c.h"
#define SLAVE_ADDR 0x68//从机地址为GY-521的地址0X68
#define uint32 unsigned int
//串口初始化
void UARTINIT()
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//使能 UART0
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//设置传输的速率
UARTStdioConfig(0, 115200, 16000000);
}
int main(void)
{
uint32 a;
SysCtlClockSet(SYSCTL_SYSDIV_5 |SYSCTL_USE_PLL|SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);
UARTINIT();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
//PB2,PB3
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);
I2CMasterInitExpClk(I2C0_BASE,SysCtlClockGet(),false);
I2CMasterSlaveAddrSet(I2C0_BASE,SLAVE_ADDR,true);
//I2CMasterBusBusy()
while(1)
{
while(I2CMasterErr(I2C0_BASE));
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_RECEIVE);
a = I2CMasterDataGet(I2C0_BASE);
//
while(I2CMasterBusy(I2C0_BASE));
UARTprintf("%02x",a);
}
}
link Zhao:
uint32 a为局部变量,编译器有可能会在程序中对其优化,a = I2CMasterDataGet(I2C0_BASE);这句被优化掉了;
试试volatile uint32 a;
link Zhao:
uint32 a为局部变量,编译器有可能会在程序中对其优化,a = I2CMasterDataGet(I2C0_BASE);这句被优化掉了;
试试volatile uint32 a;