📄 main_archive.c
字号:
// GPIOB Periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// I2C1 Periph clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// Configure I2C1 pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// I2C configuration
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = LIS_ADDRESS;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
// I2C Peripheral Enable
I2C_Cmd(I2C1, ENABLE);
// Apply I2C configuration after enabling it
I2C_Init(I2C1, &I2C_InitStructure);
// Send STRAT condition
I2C_GenerateSTART(I2C1, ENABLE);
// Test on EV5 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
// Send EEPROM address for write
I2C_Send7bitAddress(I2C1, LIS_ADDRESS, I2C_Direction_Transmitter);
// Test on EV6 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// Send the EEPROM's internal address to write to
I2C_SendData(I2C1, 0x55);
// Test on EV8 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send the byte to be written /
// I2C_SendData(I2C1, Buff);
I2C_SendData(I2C1, 0x55);
// Test on EV8 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send STOP condition
// I2C_GenerateSTOP(I2C2, ENABLE);
while(1) {
I2C_SendData(I2C1, 0x55);
}
}
void PrbI2C2(void) {
// GPIOB Periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// I2C1 Periph clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
// Configure I2C1 pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// I2C configuration
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = LIS_ADDRESS;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
// I2C Peripheral Enable
I2C_Cmd(I2C2, ENABLE);
// Apply I2C configuration after enabling it
I2C_Init(I2C2, &I2C_InitStructure);
// Send STRAT condition
I2C_GenerateSTART(I2C2, ENABLE);
// Test on EV5 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
// Send EEPROM address for write
I2C_Send7bitAddress(I2C2, LIS_ADDRESS, I2C_Direction_Transmitter);
// Test on EV6 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// Send the EEPROM's internal address to write to
I2C_SendData(I2C2, 0x55);
// Test on EV8 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send the byte to be written /
// I2C_SendData(I2C1, Buff);
I2C_SendData(I2C2, 0x55);
// Test on EV8 and clear it
// while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send STOP condition
// I2C_GenerateSTOP(I2C2, ENABLE);
while(1) {
I2C_SendData(I2C2, 0x55);
}
}
#define WHO_AM_I 0x0F
#define OFFSET_X 0x16
#define OFFSET_Y 0x17
#define OFFSET_Z 0x18
#define GAIN_X 0x19
#define GAIN_Y 0x1A
#define GAIN_Z 0x1B
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define HP_FILTER_RESET 0x23
#define STATUS_REG 0x27
#define OUTX_L 0x28
#define OUTX_H 0x29
#define OUTY_L 0x2A
#define OUTY_H 0x2B
#define OUTZ_L 0x2C
#define OUTZ_H 0x2D
#define FF_WU_CFG 0x30
#define FF_WU_SRC 0x31
#define FF_WU_ACK 0x32
#define FF_WU_THS_L 0x34
#define FF_WU_THS_H 0x35
#define FF_WU_DURATION 0x36
#define DD_CFG 0x38
#define DD_SRC 0x39
#define DD_ACK 0x3A
#define DD_THSI_L 0x3C
#define DD_THSI_H 0x3D
#define DD_THSE_L 0x3E
#define DD_THSE_H 0x3F
char buff_x[10];
char buff_y[10];
char buff_z[10];
/*************************************************************************
* Function Name: main
* Parameters: none
* Return: Int32U
*
* Description: The main subroutine
*
*************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
// Init clock system
Clk_Init();
// PrbI2C1();
LCDInit();
LCDContrast(0x45);
while(1) {
//* // whait until data is ready for reading
//* val=0;
//* while((val&0x01)!=0x01) {
//* ReadReg(&val, 0x27);
//* }
// wait new data
while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)) != Bit_SET);
// X
value = value_h = value_l = 0;
ReadReg(&value_l, OUTX_L);
ReadReg(&value_h, OUTX_H);
value = (value_h<<8)|value_l;
sprintf(buff_x, " X: 0x%04x\0", value);
// Y
value = value_h = value_l = 0;
ReadReg(&value_l, OUTY_L);
ReadReg(&value_h, OUTY_H);
value = (value_h<<8)|value_l;
sprintf(buff_y, " Y: 0x%04x\0", value);
// Z
value = value_h = value_l = 0;
ReadReg(&value_l, OUTZ_L);
ReadReg(&value_h, OUTZ_H);
value = (value_h<<8)|value_l;
sprintf(buff_z, " Z: 0x%04x\0", value);
LCDClear();
LCDStr ( 0, (unsigned char*)buff_x, 0 );
LCDStr ( 1, (unsigned char*)buff_y, 0 );
LCDStr ( 2, (unsigned char*)buff_z, 0 );
LCDUpdate();
for(dly=0; dly<1000000; dly++);
}
/*
while(1) {
Hrd_I2C_WriteByte(0x55);
}
*/
/*
//////////////////////////////////////////////////////////////////////////////
// GPIOB Periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// I2C1 Periph clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// Configure I2C1 pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// I2C configuration
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = LIS_ADDRESS;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
// I2C Peripheral Enable
I2C_Cmd(I2C1, ENABLE);
// Apply I2C configuration after enabling it
I2C_Init(I2C1, &I2C_InitStructure);
// Send I2C1 START condition
I2C_GenerateSTART(I2C1, ENABLE);
// Recuire acknowledge
I2C_AcknowledgeConfig(I2C1, ENABLE);
// Send Address and Cmd for read/write
I2C_SendData(I2C1, LIS_ADDRESS + CMD_WRITE);
// // Wait for acknowledge
// while(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)==SET) {
// I2C_ClearFlag(I2C1, I2C_FLAG_AF);
// }
//
// // Recuire acknowledge
// I2C_AcknowledgeConfig(I2C1, ENABLE);
// // Send Address and Cmd for read/write
// I2C_SendData(I2C1, REG_WHO_AM_I);
// Wait for acknoledge
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)==SET) {
I2C_ClearFlag(I2C1, I2C_FLAG_AF);
}
// Send I2C1 START condition
I2C_GenerateSTART(I2C1, ENABLE);
// Send Address and Cmd for read/write
I2C_SendData(I2C1, LIS_ADDRESS + CMD_READ);
// Wait for acknoledge
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)==SET) {
I2C_ClearFlag(I2C1, I2C_FLAG_AF);
}
value = I2C_ReceiveData(I2C1);
// Recuire acknowledge
I2C_AcknowledgeConfig(I2C1, DISABLE);
// Send I2C1 STOP condition
I2C_GenerateSTOP(I2C1, ENABLE);
//////////////////////////////////////////////////////////////////////////////
*/
/*
// GPIOB Periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// I2C1 Periph clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// Configure I2C1 pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// I2C configuration
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = LIS_ADDRESS;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
// I2C Peripheral Enable
I2C_Cmd(I2C1, ENABLE);
// Apply I2C configuration after enabling it
I2C_Init(I2C1, &I2C_InitStructure);
// Send START condition
I2C_GenerateSTART(I2C1, ENABLE);
// Test on EV5 and clear it
//while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send 3-axis address for write
I2C_Send7bitAddress(I2C1, LIS_ADDRESS+CMD_WRITE, I2C_Direction_Transmitter);
// Test on EV5 and clear it
//while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send the 3-axis's internal address to write to
I2C_SendData(I2C1, REG_WHO_AM_I);
// Test on EV5 and clear it
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send STRAT condition a second time
I2C_GenerateSTART(I2C1, ENABLE);
// Test on EV5 and clear it
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send 3-axis address for read
I2C_Send7bitAddress(I2C1, LIS_ADDRESS+CMD_READ, I2C_Direction_Receiver);
// Test on EV5 and clear it
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Test on EV6 and clear it
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// Test on EV7 and clear it
// if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
// {
// Read a byte from the EEPROM
value = I2C_ReceiveData(I2C1);
// Send STOP Condition
I2C_GenerateSTOP(I2C1, ENABLE);
// }
// Enable Acknowledgement to be ready for another reception
I2C_AcknowledgeConfig(I2C1, ENABLE);
while(1);
*/
/*
// Enable I2C
I2C_Cmd(I2C1, DISABLE);
// Enable I2C1
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// Enable GPIOB clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Configure I2C1 pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -