The stm32 software simulates IIC to read the PX4FLOW optical flow sensor data.

Source: Internet
Author: User

The stm32 software simulates IIC to read the PX4FLOW optical flow sensor data.

During this time, I made a national photoelectric design competition. I used the px4flow Optical Flow Sensor of px4 and used the software to simulate iic reading data. Occasionally, the px4flow crashed and I checked the source code of the data and optical stream, it is found that the optical flow uses the hardware iic of stm32, so the timing requirement for software simulation iic may be higher, so the iic program of the atom brother is modified, and the code will not crash after the test, take a note and save it.

1 # include "myiic. h "2 # include" delay. h "15 16 // initialize IIC 17 void IIC_Init (void) 18 {19 GPIO_InitTypeDef GPIO_InitStructure; 20 21 RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE ); // enable GPIOB clock 22 23 // GPIOB10, B11 initialization setting 24 bytes = GPIO_Pin_10 | GPIO_Pin_11; 25 bytes = GPIO_Mode_OUT; // normal output mode 26 bytes = GPIO_OType_PP; // push and pull the output 27 GPIO _ InitStructure. GPIO_Speed = GPIO_Speed_100MHz; // 100 MHz 28 MHz = GPIO_PuPd_UP; // pull up 29 GPIO_Init (GPIOB, & GPIO_InitStructure); // initialize 30 IIC_SCL = 1; 31 IIC_SDA = 1; 32} 33 // generation of IIC start signal 34 void IIC_Start (void) 35 {36 SDA_OUT (); // sda line output 37 IIC_SDA = 1; 38 39 delay_us (1 ); 40 41 IIC_SCL = 1; 42 delay_us (2); // 4 43 IIC_SDA = 0; // START: when CLK is high, DATA change form high to low 44 delay _ Us (2); // 4 45 IIC_SCL = 0; // clamp on I2C bus, prepare to send or receive data 46} 47 // generate IIC stop signal 48 void IIC_Stop (void) 49 {50 SDA_OUT (); // sda line output 51 IIC_SCL = 0; 52 53 delay_us (1); 54 55 IIC_SDA = 0; // STOP: when CLK is high DATA change form low to high 56 delay_us (2); // 4 57 IIC_SCL = 1; 58 59 delay_us (1); 60 61 IIC_SDA = 1; // send I2C bus end signal 62 delay_us (2); // 4 63} 64 // wait for the response signal to arrive 65 // return value: 1, receive response failed 66 // 0, response received 67 u8 IIC_Wait_Ack (void) 68 {69 u8 ucErrTime = 0; 70 SDA_IN (); // set SDA to input 71 IIC_SDA = 1; 72 delay_us (1); // 1 73 IIC_SCL = 1; 74 delay_us (1); // 1 75 while (READ_SDA) // The number may increase by 76 {77 ucErrTime ++; 78 if (ucErrTime> 250) 79 {80 IIC_Stop (); 81 return 1; 82} 83} 84 IIC_SCL = 0; // The clock output is 0 85 return 0; 86} 87 // generate ACK response 88 void IIC_Ack (void) 89 {90 IIC_SCL = 0; 91 92 delay_us (1); 93 94 SDA_OUT (); 95 IIC_SDA = 0; 96 delay_us (2); // 2 97 IIC_ SC L = 1; 98 delay_us (2); // 2 99 IIC_SCL = 0; 100} 101 // no ACK response 102 void IIC_NAck (void) 103 {104 IIC_SCL = 0; 105 106 delay_us (1); 107 108 SDA_OUT (); 109 IIC_SDA = 1; 110 delay_us (2); // 2111 IIC_SCL = 1; 112 delay_us (2 ); // 2113 IIC_SCL = 0; 114} 115 // IIC sends a byte 116 // returns whether the slave machine has a response 117 // 1, and a response 118 // 0, no response 119 void IIC_Send_Byte (u8 txd) 120 {121 u8 t; 122 SDA_OUT (); 123 IIC_SCL = 0; // pull down the clock to start data transmission 124 for (t = 0; t <8; t ++) 125 {126 IIC_S DA = (txd & 0x80)> 7; 127 txd <= 1; 128 delay_us (2 ); // The TEA5767 latency is required. // 2129 IIC_SCL = 1; 130 delay_us (2); // 2131 IIC_SCL = 0; 132 delay_us (2 ); // 2133} 134} 135 // read 1 byte. When ack is set to 1, ACK, ack = 0, and nACK 136 u8 IIC_Read_Byte (unsigned char ack) are sent) 137 {138 unsigned char I, receive = 0; 139 SDA_IN (); // set SDA to input 140 for (I = 0; I <8; I ++) 141 {142 iic_check = 0; 143 delay_us (2); // 2144 iic_check = 1; 145 receive <= 1; 146 if (READ_SDA) 147 re Ceive ++; 148 delay_us (2); // 1149} 150 if (! Ack) 151 IIC_NAck (); // send nACK152 else153 IIC_Ack (); // send ACK 154 return receive; 155}

The interval between empty rows is the added latency, and the modified latency is different from the annotation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.