Stm32 software simulates IIC reading Px4flow optical flow sensor data

Source: Internet
Author: User
Tags ack

This period of time in the national photoelectric Design competition, the use of PX4 Px4flow optical flow sensor, using software to simulate IIC reading data will lead to Px4flow, check the source of data and optical flow, found this optical flow with the STM32 hardware IIC, So the timing requirements of the software simulation IIC may be a little higher, so in the atomic Brother's IIC program has been modified, the code after the test has not crashed, make a note, save it.

1#include"myiic.h"2#include"Delay.h" the  - //Initialize IIC - voidIic_init (void) - {             +gpio_inittypedef gpio_initstructure; -  +Rcc_ahb1periphclockcmd (Rcc_ahb1periph_gpiob, ENABLE);//Enable GPIOB clock A  at   //GPIOB10,B11 Initialization Settings -Gpio_initstructure.gpio_pin = Gpio_pin_10 |Gpio_pin_11; -Gpio_initstructure.gpio_mode = Gpio_mode_out;//Normal Output mode -Gpio_initstructure.gpio_otype = gpio_otype_pp;//push-Pull output -Gpio_initstructure.gpio_speed = Gpio_speed_100mhz;//100MHz -GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up;//Pull Up inGpio_init (Gpiob, &gpio_initstructure);//Initialize -Iic_scl=1; toIic_sda=1; + } - //produce IIC start signal the voidIic_start (void) * { $Sda_out ();//SDA Line outputPanax NotoginsengIic_sda=1;  -      theDelay_us (1);  +      AIic_scl=1; theDelay_us (2);//4 +Iic_sda=0;//start:when CLK is high,data change Form -Delay_us (2);//4 $Iic_scl=0;//Clamp The I²c bus, ready to send or receive data $ }       - //production of IIC stop signal - voidIic_stop (void) the { -Sda_out ();//SDA Line outputWuyiIic_scl=0; the      -Delay_us (1); Wu      -Iic_sda=0;//stop:when CLK is high DATA change form AboutDelay_us (2);//4 $Iic_scl=1;  -      -Delay_us (1); -      AIic_sda=1;//send I²c bus end signal +Delay_us (2);//4 the } - //waiting for the answer signal to arrive $ //return Value: 1, receive reply failed the //0, receive the answer successfully theU8 Iic_wait_ack (void) the { theU8 Ucerrtime=0; -Sda_in ();//SDA set to input inIic_sda=1; theDelay_us (1);//1 theIic_scl=1; AboutDelay_us (1);//1 the      while(READ_SDA)//Maybe we can make the numbers bigger. the     { theucerrtime++; +         if(ucerrtime> -) -         { the iic_stop ();Bayi             return 1; the         } the     } -Iic_scl=0;//Clock Output 0 -     return 0;  the }  the //Generate ACK Response the voidIic_ack (void) the { -Iic_scl=0; the      theDelay_us (1); the     94 sda_out (); theIic_sda=0; theDelay_us (2);//2 theIic_scl=1;98Delay_us (2);//2 AboutIic_scl=0; - }101 //No ACK response is generated102 voidIic_nack (void)103 {104Iic_scl=0; the     106Delay_us (1);107     108 sda_out ();109Iic_sda=1; theDelay_us (2);//2111Iic_scl=1; theDelay_us (2);//2113Iic_scl=0; the }                                           the //IIC sends a byte the //returns the slave has no answer117 //1, there is a response118 //0, no answer119 voidiic_send_byte (U8 txd) - {                        121 U8 T; 122 sda_out (); 123Iic_scl=0;//pull low clock start data transfer124      for(t=0;t<8; t++) the     {              126Iic_sda= (txd&0x80) >>7;127txd<<=1;  -Delay_us (2);//these three delays are necessary for the TEA5767.//2129Iic_scl=1; theDelay_us (2);//2131Iic_scl=0;  theDelay_us (2);//2133     }     134 }         135 //read 1 bytes, ack=1, send ack,ack=0, send Nack136U8 Iic_read_byte (unsignedCharack)137 {138UnsignedCharI,receive=0;139Sda_in ();//SDA set to input $      for(i=0;i<8; i++ )141     {142Iic_scl=0; 143Delay_us (2);//2144Iic_scl=1;145receive<<=1;146         if(READ_SDA)147receive++; 148Delay_us (2);//1149     }                      Max     if(!ack)151Iic_nack ();//Send Nack the     Else153Iic_ack ();//Send Ack154     returnreceive;155}

The empty line interval is the new delay, and the comment is not the same as the changed delay

Stm32 software simulates IIC reading Px4flow optical flow sensor data

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.