{"id":10111,"date":"2022-09-24T09:20:08","date_gmt":"2022-09-24T01:20:08","guid":{"rendered":"http:\/\/www.szryc.com\/?p=10111"},"modified":"2022-09-24T09:20:08","modified_gmt":"2022-09-24T01:20:08","slug":"%e7%94%a8%e4%b8%80%e4%b8%aagpio%e6%9d%a5%e8%af%bb%e5%8f%96%e6%95%b0%e5%ad%97%e6%b8%a9%e5%ba%a6%e4%bc%a0%e6%84%9f%e5%99%a8ds18b20","status":"publish","type":"post","link":"http:\/\/www.szryc.com\/?p=10111","title":{"rendered":"\u7528\u4e00\u4e2aGPIO\u6765\u8bfb\u53d6\u6570\u5b57\u6e29\u5ea6\u4f20\u611f\u5668DS18B20"},"content":{"rendered":"
\u9996\u5148\u79fb\u690drtthread<\/u>\u00a0nano\uff0c\u6211\u4f7f\u7528\u7684nano\u7248\u672c\u4e0d\u662f\u6700\u65b0<\/u>\u7684\uff0c\u662f3.1.3\uff0c\u5efa\u7acb\u5de5\u7a0b\u67b6\u6784\u4ee5\u53ca\u6dfb\u52a0\u5934\u6587\u4ef6\u8def\u5f84<\/p>\n \u4fee\u6539\u4e00\u4e9b\u914d\u7f6e\uff0cnvic_vector_table_set\u7684\u5b9a\u4e49\u548cfinsh\u6240\u9700\u8981\u7684\u4e32\u53e3\u7684\u914d\u7f6e<\/p>\n <\/p>\n <\/p>\n main\u51fd\u6570\u521b\u5efa\u4e00\u4e2aled\u7ebf\u7a0b<\/p>\n <\/p>\n <\/p>\n \u7528GDlink\u4e0b\u8f7d<\/u>\u4ee3\u7801\uff0c\u7528finsh\u53ef\u67e5\u770bled\u7ebf\u7a0b\u521b\u5efa\u6210\u529f\u3002<\/p>\n \u540c\u6837\u5730\uff0c\u518d\u521b\u5efa\u4e00\u4e2ads18b20\u7684\u8bfb\u53d6\u4efb\u52a1\uff0cds18b20\u7684\u6e29\u5ea6\u8bfb\u53d6\u6d41\u7a0b\uff1a\u590d\u4f4d->\u53d1 SKIP\u00a0ROM<\/u>\u00a0\u547d\u4ee4\uff080XCC\uff09->\u53d1\u5f00\u59cb\u8f6c\u6362\u547d\u4ee4\uff080X44\uff09->\u5ef6\u65f6->\u590d\u4f4d->\u53d1\u9001 SKIP ROM \u547d\u4ee4\uff080XCC\uff09->\u53d1\u8bfb\u5b58\u50a8\u5668\u547d\u4ee4\uff080XBE\uff09->\u8fde\u7eed\u8bfb\u51fa\u4e24\u4e2a\u5b57\u8282\u6570\u636e(\u5373 <\/p>\n \u9996\u5148\u79fb\u690drtthread\u00a0nano\uff0c\u6211\u4f7f\u7528\u7684nano\u7248\u672c\u4e0d\u662f\u6700\u65b0\u7684\uff0c\u662f3.1.3\uff0c\u5efa\u7acb\u5de5\u7a0b\u67b6\u6784\u4ee5\u53ca\u6dfb\u52a0\u5934\u6587\u4ef6\u8def […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"_links":{"self":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/10111"}],"collection":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10111"}],"version-history":[{"count":1,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/10111\/revisions"}],"predecessor-version":[{"id":10119,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/10111\/revisions\/10119"}],"wp:attachment":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}void gd_eval_com_init(uint32_t com)\r\n{\r\n uint32_t COM_ID = 0U;\r\n \r\n if(EVAL_COM == com){\r\n COM_ID = 0U;\r\n }else{\r\n }\r\n\r\n \/* enable COM GPI<\/u>O clock *\/\r\n rcu_periph_clock_enable(EVAL_COM_GPIO_CLK);\r\n\r\n \/* enable USART clock *\/\r\n rcu_periph_clock_enable(COM_CLK[COM_ID]);\r\n\r\n \/* connect port to USARTx_Tx *\/\r\n gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);\r\n\r\n \/* connect port to USARTx_Rx *\/\r\n gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);\r\n\r\n \/* configure USART Tx as alte<\/u>rnate functi<\/u>on push-pull *\/\r\n gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);\r\n gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);\r\n\r\n \/* configure USART Rx as alternate function push-pull *\/\r\n gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);\r\n gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);\r\n\r\n \/* USART configure *\/\r\n usart_deinit(com);\r\n usart_baudrate_set(com, 115200U);\r\n usart_receive_config(com, USART_RECEIVE_ENABLE);\r\n usart_transmit_config(com, USART_TRANSMIT_ENABLE);\r\n\r\n usart_enable(com);\r\n}\r\n \/\/\u5b9e\u73b0\u8be5\u51fd\u6570\uff0c\u624d\u80fd\u4f7f\u7528rt_kprintf\r\nvoid rt_hw_console_output(const char *str) \r\n{\r\n \/* \u8fdb\u5165\u4e34\u754c\u6bb5 *\/\r\n rt_enter_critical();\r\n while(*str!='\ufffd')\r\n {\r\n \/* \u6362\u884c *\/\r\n if (*str == 'n')\/\/RT-Thread<\/u> \u7cfb\u7edf\u4e2d\u5df2\u6709\u7684\u6253\u5370\u5747\u4ee5 n \u7ed3\u5c3e\uff0c\u800c\u5e76\u975e rn\uff0c\u6240\u4ee5\u5728\u5b57\u7b26\u8f93\u51fa\u65f6\uff0c\u9700\u8981\u5728\u8f93\u51fa n \u4e4b\u524d\u8f93\u51fa r\uff0c\u5b8c\u6210\u56de\u8f66\u4e0e\u6362\u884c\uff0c\u5426\u5219\u7cfb\u7edf\u6253\u5370\u51fa\u6765\u7684\u4fe1\u606f\u5c06\u53ea\u6709\u6362\u884c\r\n {\r\n usart_data_transmit(USART0, 'r');\r\n while(usart_flag_get(USART0, USART_FLAG_TC)== RESET);\r\n }\r\n\r\n usart_data_transmit(USART0, *(str++));\r\n while(usart_flag_get(USART0, USART_FLAG_TC)== RESET);\r\n }\r\n\r\n \/* \u9000\u51fa\u4e34\u754c\u6bb5 *\/\r\n rt_exit_critical(); \/\/\u6ce8\u610f\uff1a\u4f7f\u7528\u8fdb\u5165\u4e34\u754c\u6bb5\u8bed\u53e5rt_enter_critical(); \u4e00\u5b9a\u8981\u4f7f\u7528\u9000\u51fa\u4e34\u754c\u6bb5\u8bed\u53e5 rt_exit_critical();\u5426\u5219\u8c03\u5ea6\u5668\u9501\u4f4f\uff0c\u65e0\u6cd5\u8fdb\u884c\u8c03\u5ea6\r\n\r\n}\r\n\/\/\u4f7f\u7528Finsh\u7ec4\u4ef6\u4e09\u6b65\u9aa4\uff1a1.\u5b9e\u73b0\u8be5\u51fd\u6570\u53cart_hw_console_output\u51fd\u6570\uff1b2.rtconfig.h\u4e2d\u5f00\u542fRT_USING_FINSH\u5b8f;3.\u6dfb\u52a0Finsh\u7ec4\u4ef6(cmd.c\u3001msh.c\u3001shell.c)\uff0c\r\nchar rt_hw_console_getchar(void)\r\n{\r\n \/\/\u67e5\u8be2\u65b9\u5f0f\u5b9e\u73b0\uff0c\u8bb0\u5f97\u5c06Usart1\u521d\u59cb\u5316\u4e2d\u7684\u4e2d\u65ad\u63a5\u6536\u914d\u7f6e\u76f8\u5173\u4ee3\u7801\u6ce8\u91ca\u6389\r\n int ch = -1;\r\n \/*\u7b49\u5f85\u4e32\u53e31\u8f93\u5165\u6570\u636e*\/\r\n if(usart_flag_get(USART0, USART_FLAG_RBNE) != RESET)\r\n {\r\n ch = (int)usart_data_receive(USART0);\r\n usart_flag_clear(USART0, USART_FLAG_RBNE);\r\n }\r\n else\r\n {\r\n if(usart_flag_get(USART0, USART_FLAG_ORERR) != RESET)\r\n {\r\n usart_flag_clear(USART0, USART_FLAG_ORERR);\r\n }\r\n rt_thread<\/u>_mdelay(10);\r\n }\r\n return ch;\r\n}<\/pre>\n
#include \r\n#include \r\n#include \"gd32<\/u>f3x0_rcu.h\"\r\n#include \"gd32f3x0_gpio.h\"\r\n#include \"Uart<\/u>.h\"\r\n\r\nstatic rt_thread_t led_thread;\r\n\r\nvoid led_init(void)\r\n{\r\n \/* enable the LED GPIO clock *\/\r\n rcu_periph_clock_enable(RCU_GPIOA);\r\n \/* configure led GPIO port *\/ \r\n gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);\r\n gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);\r\n}\r\n\r\nvoid led_thread_entry(void *param<\/u>eter)\r\n{\r\n led_init();\r\n while(1)\r\n {\r\n \/* turn on led *\/\r\n gpio_bit_write(GPIOA, GPIO_PIN_8, SET);\r\n rt_thread_mdelay(2000);\r\n \/* turn off led *\/\r\n gpio_bit_write(GPIOA, GPIO_PIN_8, RESET);\r\n rt_thread_mdelay(2000);\r\n }\r\n}\r\n\r\nvoid TaskInit(void)\r\n{\r\n led_thread = rt_thread_create(\"ledThread\", \/* \u7ebf\u7a0b\u540d\u5b57 *\/\r\n led_thread_entry, \/* \u7ebf\u7a0b\u5165\u53e3\u51fd\u6570 *\/\r\n RT_NULL, \/* \u7ebf\u7a0b\u5165\u53e3\u51fd\u6570\u53c2\u6570 *\/\r\n 256, \/* \u7ebf\u7a0b\u6808\u5927\u5c0f *\/\r\n 2, \/* \u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7 *\/\r\n 10 \/* \u7ebf\u7a0b\u65f6\u95f4\u7247 *\/\r\n );\r\n if(led_thread != RT_NULL)\r\n {\r\n rt_thread_startup(led_thread);\r\n }\r\n}\r\n\r\nint main()\r\n{\r\n TaskInit();\r\n}<\/pre>\n
\n\u6e29\u5ea6)->\u7ed3\u675f\u3002
\n\u8d34\u51fa\u90e8\u5206\u5173\u952e\u4ee3\u7801<\/p>\n\/*\r\n *\u4e3b\u673a\u7ed9\u4ece\u673a\u53d1\u9001\u590d\u4f4d\u8109\u51b2\r\n *\/\r\nstatic void DS18B20_Rs<\/u>t(void)\r\n{\r\n \/* \u4e3b\u673a\u8bbe\u7f6e\u4e3a\u63a8\u633d\u8f93\u51fa *\/\r\n DS18B20_Mode_Out_PP();\r\n \r\n macDS18B20_DQ_0;\r\n \/* \u4e3b\u673a\u81f3\u5c11\u4ea7\u751f480us\u7684\u4f4e\u7535\u5e73\u590d\u4f4d\u4fe1\u53f7 *\/\r\n rt_hw_us_delay(750);\r\n \r\n \/* \u4e3b\u673a\u5728\u4ea7\u751f\u590d\u4f4d\u4fe1\u53f7\u540e\uff0c\u9700\u5c06\u603b\u7ebf\u62c9\u9ad8 *\/\r\n macDS18B20_DQ_1;\r\n \r\n \/*\u4ece\u673a\u63a5\u6536\u5230\u4e3b\u673a\u7684\u590d\u4f4d\u4fe1\u53f7\u540e\uff0c\u4f1a\u572815~60us\u540e\u7ed9\u4e3b\u673a\u53d1\u4e00\u4e2a\u5b58\u5728\u8109\u51b2*\/\r\n rt_hw_us_delay(15);\r\n}\r\n\r\n\r\n\/*\r\n * \u68c0\u6d4b<\/u>\u4ece\u673a\u7ed9\u4e3b\u673a\u8fd4\u56de\u7684\u5b58\u5728\u8109\u51b2\r\n * 0\uff1a\u6210\u529f\r\n * 1\uff1a\u5931\u8d25\r\n *\/\r\nstatic uint8_t DS18B20_Presence(void)\r\n{\r\n uint8_t pulse_time = 0;\r\n \r\n \/* \u4e3b\u673a\u8bbe\u7f6e\u4e3a\u4e0a\u62c9\u8f93\u5165 *\/\r\n DS18B20_Mode_IPU();\r\n \r\n \/* \u7b49\u5f85\u5b58\u5728\u8109\u51b2\u7684\u5230\u6765\uff0c\u5b58\u5728\u8109\u51b2\u4e3a\u4e00\u4e2a60~240us\u7684\u4f4e\u7535\u5e73\u4fe1\u53f7 \r\n * \u5982\u679c\u5b58\u5728\u8109\u51b2\u6ca1\u6709\u6765\u5219\u505a\u8d85\u65f6\u5904\u7406\uff0c\u4ece\u673a\u63a5\u6536\u5230\u4e3b\u673a\u7684\u590d\u4f4d\u4fe1\u53f7\u540e\uff0c\u4f1a\u572815~60us\u540e\u7ed9\u4e3b\u673a\u53d1\u4e00\u4e2a\u5b58\u5728\u8109\u51b2\r\n *\/\r\n while( macDS18B20_DQ_IN() && pulse_time<100 )\r\n {\r\n pulse_time++;\r\n rt_hw_us_delay(1);\r\n } \r\n \/* \u7ecf\u8fc7100us\u540e\uff0c\u5b58\u5728\u8109\u51b2\u90fd\u8fd8\u6ca1\u6709\u5230\u6765*\/\r\n if( pulse_time >=100 )\r\n return 1;\r\n else\r\n pulse_time = 0;\r\n \r\n \/* \u5b58\u5728\u8109\u51b2\u5230\u6765\uff0c\u4e14\u5b58\u5728\u7684\u65f6\u95f4\u4e0d\u80fd\u8d85\u8fc7240us *\/\r\n while( !macDS18B20_DQ_IN() && pulse_time<240 )\r\n {\r\n pulse_time++;\r\n rt_hw_us_delay(1);\r\n } \r\n if( pulse_time >=240 )\r\n return 1;\r\n else\r\n return 0;\r\n}\r\n\r\n\r\n\/*\r\n * \u4eceDS18B20\u8bfb\u53d6\u4e00\u4e2abit\r\n *\/\r\nstatic uint8_t DS18B20_ReadBit(void)\r\n{\r\n uint8_t dat;\r\n \r\n \/* \u8bfb0\u548c\u8bfb1\u7684\u65f6\u95f4\u81f3\u5c11\u8981\u5927\u4e8e60us *\/ \r\n DS18B20_Mode_Out_PP();\r\n \/* \u8bfb\u65f6\u95f4\u7684\u8d77\u59cb\uff1a\u5fc5\u987b\u7531\u4e3b\u673a\u4ea7\u751f >1us <15us \u7684\u4f4e\u7535\u5e73\u4fe1\u53f7 *\/\r\n macDS18B20_DQ_0;\r\n rt_hw_us_delay(10);\r\n \r\n \/* \u8bbe\u7f6e\u6210\u8f93\u5165\uff0c\u91ca\u653e\u603b\u7ebf\uff0c\u7531\u5916\u90e8\u4e0a\u62c9\u7535\u963b\u5c06\u603b\u7ebf\u62c9\u9ad8 *\/\r\n DS18B20_Mode_IPU();\r\n \/\/rt_hw_us_delay(2);\r\n \r\n if( macDS18B20_DQ_IN() == SET )\r\n dat = 1;\r\n else\r\n dat = 0;\r\n \r\n \/* \u8fd9\u4e2a\u5ef6\u65f6\u53c2\u6570\u8bf7\u53c2\u8003\u65f6\u5e8f\u56fe *\/\r\n rt_hw_us_delay(45);\r\n \r\n return dat;\r\n}\r\n\r\n\r\n\/*\r\n * \u4eceDS18B20\u8bfb\u4e00\u4e2a\u5b57\u8282\uff0c\u4f4e\u4f4d\u5148\u884c\r\n *\/\r\nstatic uint8_t DS18B20_ReadByte(void)\r\n{\r\n uint8_t i, j, dat = 0; \r\n \r\n for(i=0; i<8; i++) \r\n {\r\n j = DS18B20_ReadBit(); \r\n dat = (dat) | (j<>1; \r\n \/* \u51990\u548c\u51991\u7684\u65f6\u95f4\u81f3\u5c11\u8981\u5927\u4e8e60us *\/\r\n if (testb)\r\n { \r\n macDS18B20_DQ_0;\r\n \/* 1us < \u8fd9\u4e2a\u5ef6\u65f6 < 15us *\/\r\n rt_hw_us_delay(8);\r\n \r\n macDS18B20_DQ_1;\r\n rt_hw_us_delay(58);\r\n } \r\n else\r\n { \r\n macDS18B20_DQ_0;\r\n \/* 60us < Tx 0 < 120us *\/\r\n rt_hw_us_delay(70);\r\n \r\n macDS18B20_DQ_1; \r\n \/* 1us < Trec(\u6062\u590d\u65f6\u95f4) < \u65e0\u7a77\u5927*\/\r\n rt_hw_us_delay(2);\r\n }\r\n }\r\n}\r\n\r\n\r\n \/**\r\n * @brief \u8df3\u8fc7\u5339\u914d DS18B20 ROM\r\n * @param \u65e0\r\n * @retval \u65e0\r\n *\/\r\nstatic void DS18B20_SkipRom ( void )\r\n{\r\n DS18B20_Rst(); \r\n \r\n DS18B20_Presence(); \r\n \r\n DS18B20_WriteByte(0XCC); \/* \u8df3\u8fc7 ROM *\/\r\n \r\n}\r\n\r\n\r\n \/**\r\n * @brief \u6267\u884c\u5339\u914d DS18B20 ROM\r\n * @param \u65e0\r\n * @retval \u65e0\r\n *\/\r\nstatic void DS18B20_MatchRom ( void )\r\n{\r\n DS18B20_Rst(); \r\n \r\n DS18B20_Presence(); \r\n \r\n DS18B20_WriteByte(0X55); \/* \u5339\u914d ROM *\/\r\n \r\n}\r\n\r\n\r\n\/*\r\n * \u5b58\u50a8\u7684\u6e29\u5ea6\u662f16 \u4f4d\u7684\u5e26\u7b26\u53f7\u6269\u5c55\u7684\u4e8c\u8fdb\u5236\u8865\u7801\u5f62\u5f0f\r\n * \u5f53\u5de5\u4f5c\u572812\u4f4d\u5206\u8fa8\u7387\u65f6\uff0c\u5176\u4e2d5\u4e2a\u7b26\u53f7\u4f4d\uff0c7\u4e2a\u6574\u6570\u4f4d\uff0c4\u4e2a\u5c0f\u6570\u4f4d\r\n *\r\n * |---------\u6574\u6570----------|-----\u5c0f\u6570 \u5206\u8fa8\u7387 1\/(2^4)=0.0625----|\r\n * \u4f4e\u5b57\u8282 | 2^3 | 2^2 | 2^1 | 2^0 | 2^(-1) | 2^(-2) | 2^(-3) | 2^(-4) |\r\n *\r\n *\r\n * |-----\u7b26\u53f7\u4f4d\uff1a0->\u6b63 1->\u8d1f-------|-----------\u6574\u6570-----------|\r\n * \u9ad8\u5b57\u8282 | s | s | s | s | s | 2^6 | 2^5 | 2^4 |\r\n *\r\n * \r\n * \u6e29\u5ea6 = \u7b26\u53f7\u4f4d + \u6574\u6570 + \u5c0f\u6570*0.0625\r\n *\/\r\n \/**\r\n * @brief \u5728\u8df3\u8fc7\u5339\u914d ROM \u60c5\u51b5\u4e0b\u83b7\u53d6 DS18B20 \u6e29\u5ea6\u503c \r\n * @param \u65e0\r\n * @retval \u6e29\u5ea6\u503c\r\n *\/\r\nfloat DS18B20_GetTemp_SkipRom ( void )\r\n{\r\n uint8_t tpmsb, tplsb;\r\n short s_tem;\r\n float f_tem;\r\n \r\n DS18B20_SkipRom ();\r\n DS18B20_WriteByte(0X44); \/* \u5f00\u59cb\u8f6c\u6362 *\/\r\n \r\n DS18B20_SkipRom ();\r\n DS18B20_WriteByte(0XBE); \/* \u8bfb\u6e29\u5ea6\u503c *\/\r\n \r\n tplsb = DS18B20_ReadByte(); \r\n tpmsb = DS18B20_ReadByte(); \r\n \r\n s_tem = tpmsb<<8;\r\n s_tem = s_tem | tplsb;\r\n \r\n if( s_tem < 0 ) \/* \u8d1f\u6e29\u5ea6 *\/\r\n f_tem = (~s_tem+1) * 0.0625; \r\n else\r\n f_tem = s_tem * 0.0625;\r\n \r\n return f_tem; \r\n}<\/pre>\n