| | |
| | | } |
| | | |
| | | |
| | | //void delay_us(uint32_t nTimer) |
| | | //{ |
| | | // uint32_t i=0; |
| | | // for(i=0;i<nTimer;i++){ |
| | | // __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | // __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | // __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | // __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | // __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | // __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | // __NOP();__NOP();__NOP();__NOP(); |
| | | // } |
| | | //} |
| | | static void delay_us(uint32_t nTimer) |
| | | { |
| | | uint32_t i=0; |
| | | for(i=0;i<nTimer;i++){ |
| | | __NOP(); |
| | | __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); |
| | | |
| | | |
| | | } |
| | | } |
| | | AxesRaw_t lis2dhdata; |
| | | |
| | | //OLED初始化IIC |
| | |
| | | return acc_g; |
| | | } |
| | | |
| | | |
| | | void IIC2_SDA_OUT() |
| | | { |
| | | gpio_pin_set_dir(SDA_PIN , GPIO_DIR_OUT, 1); |
| | | } |
| | | void IIC2_SDA_IN() |
| | | { |
| | | gpio_pin_set_dir(SDA_PIN,GPIO_DIR_IN,1); //SDA设置为输入等待接收端的应答信号 |
| | | } |
| | | //产生IIC起始信号 |
| | | void IIC2_Start(void) |
| | | { |
| | | // IIC2_SDA_OUT(); //sda线输出 |
| | | SDA_1; |
| | | IIC2_SDA_OUT(); //sda线输出 |
| | | SDA_1; |
| | | // delay_us(10); |
| | | SCL_1; |
| | | // delay_us(10); |
| | | SCL_1; |
| | | // delay_us(10); |
| | | SDA_0;//START:when CLK is high,DATA change form high to low |
| | | delay_us(10); |
| | | SCL_0;//钳住I2C总线,准备发送或接收数据 |
| | | } |
| | | SDA_0;//START:when CLK is high,DATA change form high to low |
| | | delay_us(10); |
| | | SCL_0;//钳住I2C总线,准备发送或接收数据 |
| | | } |
| | | //产生IIC停止信号 |
| | | void IIC2_Stop(void) |
| | | { |
| | | // IIC2_SDA_OUT();//sda线输出 |
| | | SCL_0; |
| | | IIC2_SDA_OUT();//sda线输出 |
| | | SCL_0; |
| | | // delay_us(10); |
| | | SDA_0;//STOP:when CLK is high DATA change form low to high |
| | | delay_us(10); |
| | | SCL_1; |
| | | SDA_0;//STOP:when CLK is high DATA change form low to high |
| | | delay_us(10); |
| | | SCL_1; |
| | | // delay_us(10); |
| | | SDA_1;//发送I2C总线结束信号 |
| | | delay_us(10); |
| | | SDA_1;//发送I2C总线结束信号 |
| | | delay_us(10); |
| | | |
| | | } |
| | | |
| | | //等待应答信号到来 |
| | | //返回值:1,接收应答失败 |
| | | // 0,接收应答成功 |
| | | |
| | | uint8_t IIC2_Wait_Ack(void) |
| | | { |
| | | uint8_t ucErrTime=0; |
| | | // IIC2_SDA_IN(); //SDA设置为输入 |
| | | gpio_pin_set_dir(SDA_PIN , GPIO_DIR_IN, 1); |
| | | SDA_1; |
| | | delay_us(6); |
| | | SCL_1; |
| | | delay_us(6); |
| | | uint8_t ucErrTime=0; |
| | | IIC2_SDA_IN(); |
| | | SDA_1; |
| | | delay_us(15); |
| | | SCL_1; |
| | | delay_us(15); |
| | | |
| | | while(IIC2_READ_SDA) |
| | | { |
| | | ucErrTime++; |
| | | if(ucErrTime>250) |
| | | { |
| | | IIC2_Stop(); |
| | | return 1; |
| | | } |
| | | } |
| | | gpio_pin_set_dir(SDA_PIN , GPIO_DIR_OUT, 1); |
| | | SCL_0;//时钟输出0 |
| | | return 0; |
| | | } |
| | | while(IIC2_READ_SDA) |
| | | { |
| | | ucErrTime++; |
| | | if(ucErrTime>250) |
| | | { |
| | | IIC2_Stop(); |
| | | return 1; |
| | | } |
| | | } |
| | | SCL_0;//时钟输出0 |
| | | return 0; |
| | | } |
| | | //产生ACK应答 |
| | | void IIC2_Ack(void) |
| | | { |
| | | SCL_0; |
| | | // IIC2_SDA_OUT(); |
| | | SDA_0; |
| | | delay_us(10); |
| | | SCL_1; |
| | | delay_us(10); |
| | | SCL_0; |
| | | SCL_0; |
| | | IIC2_SDA_OUT(); |
| | | SDA_0; |
| | | delay_us(10); |
| | | SCL_1; |
| | | delay_us(10); |
| | | SCL_0; |
| | | } |
| | | //不产生ACK应答 |
| | | //不产生ACK应答 |
| | | void IIC2_NAck(void) |
| | | { |
| | | SCL_0; |
| | | // IIC2_SDA_OUT(); |
| | | SDA_1; |
| | | delay_us(10); |
| | | SCL_1; |
| | | delay_us(10); |
| | | SCL_0; |
| | | SCL_0; |
| | | IIC2_SDA_OUT(); |
| | | SDA_1; |
| | | delay_us(10); |
| | | SCL_1; |
| | | delay_us(10); |
| | | SCL_0; |
| | | } |
| | | |
| | | |
| | | //IIC发送一个字节,WRITE |
| | | //返回从机有无应答 |
| | | //1,有应答 |
| | | //0,无应答 |
| | | //0,无应答 |
| | | void IIC2_Send_Byte(uint8_t txd) |
| | | { |
| | | uint8_t t; |
| | | // IIC2_SDA_OUT(); |
| | | SCL_0;//拉低时钟开始数据传输 |
| | | for(t=0; t<8; t++) |
| | | { |
| | | if((txd&0x80)>>7) |
| | | { |
| | | SDA_1; |
| | | } |
| | | else |
| | | { |
| | | SDA_0; |
| | | } |
| | | txd <<= 1; |
| | | delay_us(10); |
| | | SCL_1; |
| | | delay_us(10); |
| | | SCL_0; |
| | | delay_us(10); |
| | | } |
| | | } |
| | | { |
| | | uint8_t t; |
| | | IIC2_SDA_OUT(); |
| | | SCL_0;//拉低时钟开始数据传输 |
| | | for(t=0;t<8;t++) |
| | | { |
| | | if((txd&0x80)>>7) |
| | | { |
| | | SDA_1; |
| | | } |
| | | else |
| | | { |
| | | SDA_0; |
| | | } |
| | | txd <<= 1; |
| | | delay_us(10); |
| | | SCL_1; |
| | | delay_us(10); |
| | | SCL_0; |
| | | delay_us(10); |
| | | } |
| | | } |
| | | |
| | | //读1个字节,ack=1时,发送ACK,ack=0,发送nACK |
| | | //读1个字节,ack=1时,发送ACK,ack=0,发送nACK |
| | | uint8_t IIC2_Read_Byte(unsigned char ack) |
| | | { |
| | | unsigned char i,receive=0; |
| | | // IIC2_SDA_IN();//SDA设置为输入 |
| | | for(i=0; i<8; i++ ) |
| | | { |
| | | SCL_0; |
| | | delay_us(10); |
| | | SCL_1; |
| | | receive<<=1; |
| | | if(IIC2_READ_SDA)receive++; |
| | | delay_us(5); |
| | | } |
| | | unsigned char i,receive=0; |
| | | IIC2_SDA_IN();//SDA设置为输入 |
| | | for(i=0;i<8;i++ ) |
| | | { |
| | | SCL_0; |
| | | delay_us(10); |
| | | SCL_1; |
| | | receive<<=1; |
| | | if(IIC2_READ_SDA)receive++; |
| | | delay_us(5); |
| | | } |
| | | if (!ack) |
| | | IIC2_NAck();//发送nACK |
| | | else |
| | | IIC2_Ack(); //发送ACK |
| | | IIC2_Ack(); //发送ACK |
| | | return receive; |
| | | } |
| | | |