{"id":73,"date":"2021-03-04T11:45:06","date_gmt":"2021-03-04T03:45:06","guid":{"rendered":"http:\/\/www.iotot.com\/?p=73"},"modified":"2022-05-22T11:24:10","modified_gmt":"2022-05-22T03:24:10","slug":"stm32%e9%a9%b1%e5%8a%a8tmc5160%e6%ad%a5%e8%bf%9b%e7%94%b5%e6%9c%ba%e8%8a%af%e7%89%87","status":"publish","type":"post","link":"https:\/\/blog.iotot.com\/?p=73","title":{"rendered":"STM32\u9a71\u52a8TMC5160\u6b65\u8fdb\u7535\u673a\u82af\u7247"},"content":{"rendered":"\n<h2>1\u3001STM32CubeMX\u5de5\u7a0b\u8bbe\u7f6e<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"713\" height=\"729\" src=\"https:\/\/www.iotot.com\/wp-content\/uploads\/2021\/03\/image-3.png\" alt=\"\" class=\"wp-image-74\" srcset=\"https:\/\/blog.iotot.com\/wp-content\/uploads\/2021\/03\/image-3.png 713w, https:\/\/blog.iotot.com\/wp-content\/uploads\/2021\/03\/image-3-293x300.png 293w, https:\/\/blog.iotot.com\/wp-content\/uploads\/2021\/03\/image-3-624x638.png 624w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/figure>\n\n\n\n<p>\u7ba1\u811a\u5b9a\u4e49<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"718\" height=\"685\" src=\"https:\/\/www.iotot.com\/wp-content\/uploads\/2021\/03\/image-4.png\" alt=\"\" class=\"wp-image-75\" srcset=\"https:\/\/blog.iotot.com\/wp-content\/uploads\/2021\/03\/image-4.png 718w, https:\/\/blog.iotot.com\/wp-content\/uploads\/2021\/03\/image-4-300x286.png 300w, https:\/\/blog.iotot.com\/wp-content\/uploads\/2021\/03\/image-4-624x595.png 624w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/figure>\n\n\n\n<p>\u786c\u4ef6SPI \u53c2\u6570\u8bbe\u7f6e<\/p>\n\n\n\n<h2>2\u3001\u8f6f\u4ef6\u7f16\u5199<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/TMC5160 takes 40 bit data: 8 address and 32 data\nvoid sendData(unsigned long address,long datagram)\n{\n\tunsigned char i;\n\tcmd[0]=address;\n\tcmd[1]=(datagram >> 24) &amp; 0xff;\n\tcmd[2]=(datagram >> 16) &amp; 0xff;\n\tcmd[3]=(datagram >> 8) &amp; 0xff;\n\tcmd[4]=datagram &amp; 0xff;\n\t\n\tHAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET); \t\t\/\/SPI_CS\u7247\u9009\u62c9\u4f4e\t\n\/\/\tSPI_SendByte(address);\n\/\/\tSPI_SendByte((datagram >> 24) &amp; 0xff);\n\/\/\tSPI_SendByte((datagram >> 16) &amp; 0xff);\n\/\/\tSPI_SendByte((datagram >> 8) &amp; 0xff);\n\/\/\tSPI_SendByte(datagram &amp; 0xff);\n\tfor(i=0;i&lt;5;i++)\n\t{\n\t\tif(HAL_SPI_Transmit(&amp;hspi1,&amp;cmd[i],1,100) == HAL_OK)\n\t\t{\n\t\t}\n\t\telse;\n\t}\t\n\tHAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET);  \t\/\/SPI_CS\u7247\u9009\u62c9\u9ad8\n}\n\nunsigned long ReadData(long address)\n{\n\tchar data[4] = {0, 0, 0, 0};\n\tunsigned long datagram = 0;\n\t\n\tHAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET); \t\/\/SPI_CS\u7247\u9009\u62c9\u4f4e\n\t\n\tSPI_SendByte(address);\n\tdata[0] = SPI_ReceiveByte();\/\/SPI_ReceiveByte((datagram >> 24) &amp; 0xff);\n\tdata[1] = SPI_ReceiveByte();\/\/SPI_ReceiveByte((datagram >> 16) &amp; 0xff);\n\tdata[2] = SPI_ReceiveByte();\/\/SPI_ReceiveByte((datagram >> 8) &amp; 0xff);\n\tdata[3] = SPI_ReceiveByte();\/\/SPI_ReceiveByte(datagram &amp; 0xff);\t\n\tHAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET); \t\/\/SPI_CS\u7247\u9009\u62c9\u9ad8\n\t\n\tdatagram = (data[0] &lt;&lt; 24) | (data[1] &lt;&lt; 16) | (data[2] &lt;&lt; 8) | data[3];\t\n\treturn datagram;\n}\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001STM32CubeMX\u5de5\u7a0b\u8bbe\u7f6e \u7ba1\u811a\u5b9a\u4e49 \u786c\u4ef6SPI \u53c2\u6570\u8bbe\u7f6e 2\u3001\u8f6f\u4ef6\u7f16\u5199 \/\/TMC5160 take [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":74,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,24,19],"tags":[21,22,23],"_links":{"self":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":5,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/95"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/media\/74"}],"wp:attachment":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}