OK6410 development board bare metal DS18B20 driver

Publisher:独行于世Latest update time:2017-02-26 Source: eefocusKeywords:OK6410 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I believe that those who have played with 51 should have played with DS18B20. Although it is used on ARM11, the operation is still the same. One more thing is that you need to switch the IO direction before reading and writing IO. In fact, 51 needs to be switched to a high level before reading, which is generally done by the compiler.

  1. /****************************************************** *************************************************** ********** 

  2.  * File name: ds18b20.c 

  3.  * Function: S3C6410 DS18B20 driver 

  4.  * Author: cp1300@139.com 

  5.  * Created: September 17, 2012 22:45 

  6.  * Last modified: September 17, 2012 

  7.  * Details: Delay function support is required 

  8.  * Make sure the delay accuracy is as high as possible 

  9.  * Do not interrupt the reading process for a long time, because 1wire has strict timing requirements. 

  10. *************************************************** *************************************************** *********/  

  11. #include "system.h"  

  12. #include "timer.h"  

  13. #include "delay.h"  

  14. #include "ds18b20.h"  

  15.   

  16.   

  17. //DS18B20 uses GPIOE0  

  18. #define Set18b20IOout() (rGPECON |= 1) //Set DS18B20 IO to output,  

  19. #define Set18b20IOin() (rGPECON &= (~0xf)) //Set DS18B20 IO to floating input,  

  20. #define Read18b20IO() ((rGPEDAT & BIT0) ? 1 : 0) //Read DS18B20 IO  

  21. #define Write18b20IO(x) (x ? (rGPEDAT |= BIT0) : (rGPEDAT &= ~BIT0)) //Write DS18B20 IO  

  22.   

  23. /****************************************************** *************************************************** ************************ 

  24. *Function: u8 DS18B20_Reset(void) 

  25. *Function: Reset DS18B20 

  26. *Parameters: None 

  27. *Return: 0: success; 1: failure 

  28. *Depends on: underlying macro definitions 

  29. *Author : cp1300@139.com 

  30. *Time: 20120917 

  31. *Last modified: 20120917 

  32. * Description: None 

  33. *************************************************** *************************************************** *********************/  

  34. u8 DS18B20_Reset(void)  

  35. {  

  36.     u8 i = 0;  

  37.       

  38.     Set18b20IOout(); //Host port push-pull output mode  

  39.     Write18b20IO(1);  

  40.     Delay_US(1);  

  41.     Write18b20IO(0); //Pull the bus low for 480us~240us  

  42.     Delay_US(500); //>480US delay  

  43.     Write18b20IO(1);  

  44.     Delay_US(2); //Reset completed  

  45.     Set18b20IOin(); //Host port floating input mode  

  46.     while(Read18b20IO()) //Wait for low level response signal  

  47.     {  

  48.         i++;  

  49.         Delay_US(1);  

  50.         if(i > 100)  

  51.             return 1; //Waiting timeout, initialization failed, return 1;  

  52.     }  

  53.     Delay_US(250); //Skip reply signal  

  54.     return 0x00; //DS18B20 detected and initialized successfully  

  55. }  

  56.   

  57.   

  58. /****************************************************** *************************************************** ************************ 

  59. *Function: u8 DS18B20_ReadData(void) 

  60. *Function: Read DS18B20 data 

  61. *Parameters: None 

  62. *Return: data 

  63. *Depends on: underlying macro definitions 

  64. *Author : cp1300@139.com 

  65. *Time: 20120917 

  66. *Last modified: 20120917 

  67. * Description: None 

  68. *************************************************** *************************************************** *********************/  

  69. u8 DS18B20_ReadData(void)  

  70. {  

  71.     u8 i,data = 0;  

  72.       

  73.     for(i = 0;i < 8;i ++)  

  74.     {  

  75.         Set18b20IOout(); //Host port push-pull output mode  

  76.         Write18b20IO(0); //Pull the bus low for 10-15us  

  77.         data >>= 1;  

  78.         Delay_US(12);  

  79.         Write18b20IO(1); //Release the bus  

  80.         Set18b20IOin(); //Host port floating input mode  

  81.         Delay_US(1);  

  82.         if(Read18b20IO()) //Read data, about 40-45us delay after reading  

  83.             data |= 0x80;  

  84.         Delay_US(42);  

  85.     }  

  86.     return data;  

  87. }  

  88.   

  89.   

  90. /****************************************************** *************************************************** ************************ 

  91. *Function: void DS18B20_WriteData(u8 data) 

  92. *Function: Write data to DS18B20 

  93. *Parameter: data 

  94. *Return: None 

  95. *Depends on: underlying macro definitions 

  96. *Author : cp1300@139.com 

  97. *Time: 20120917 

  98. *Last modified: 20120917 

  99. * Description: None 

  100. *************************************************** *************************************************** *********************/  

  101. void DS18B20_WriteData(u8 data)  

  102. {  

  103.     u8 i;  

  104.       

  105.     Set18b20IOout(); //Host port push-pull output mode  

  106.     for(i = 0;i < 8;i ++)  

  107.     {  

  108.         Write18b20IO(0); //Pull the bus low for 10-15us  

  109.         Delay_US(12);  

  110.         Write18b20IO(data & 0x01); //Write data bit, keep 20-45us  

  111.         Delay_US(30);  

  112.         Write18b20IO(1); //Release the bus  

  113.         data >>= 1;  

  114.         Delay_US(2);  

  115.     }  

  116. }  

  117.   

  118.   

  119.   

  120. /****************************************************** *************************************************** ************************ 

  121. *Function: s16 DS18B20_ReadTemper(void) 

  122. *Function: Read DS18B20 temperature 

  123. *Parameters: None 

  124. *Return: Temperature value 

  125. *Depends on: underlying macro definitions 

  126. *Author : cp1300@139.com 

  127. *Time: 20120917 

  128. *Last modified: 20120917 

  129. * Note: The temperature value is expanded 100 times, and the temperature value is a signed number. 

  130. *************************************************** *************************************************** *********************/  

  131. s16 DS18B20_ReadTemper(void)  

  132. {  

  133.     u8th, tl;  

  134.     s16 data;  

  135.       

  136.     if(DS18B20_Reset())   

  137.     {  

  138.         return 0xffff; //return error  

  139.     }  

  140.     DS18B20_WriteData(0xcc); //Skip reading serial number  

  141.     DS18B20_WriteData(0x44); //Start temperature conversion  

  142.     DS18B20_Reset();  

  143.     DS18B20_WriteData(0xcc); //Skip reading serial number  

  144.     DS18B20_WriteData(0xbe); //Read temperature  

  145.     tl = DS18B20_ReadData(); //Read the lower eight bits  

  146.     th = DS18B20_ReadData(); //Read the upper eight bits  

  147.     data = th;  

  148.     data <<= 8;  

  149.     data |= tl;  

  150.     data *= 6.25; //Expand the temperature value 100 times, accurate to 2 decimal places  

  151.       

  152.     return data;  

  153. }  


  1. #ifndef DS18B20_H_  

  2. #define DS18B20_H_  

  3.   

  4.   

  5. s16 DS18B20_ReadTemper(void); //Read DS18B20 temperature  

  6.   

  7. #endif /*DS18B20_H_*/  



DELAY.C


  1. /****************************************************** *************************************************** ********** 

  2.  * File name: delay.c 

  3.  * Function: Delay function, call timer 1 for delay 

  4.  * Author: cp1300@139.com 

  5.  * Created: September 17, 2012 22:32 

  6.  * Last modified: September 17, 2012 

  7.  * Details: Delay error 1-2us, using timer 1 

  8.  * Cannot be used in interrupt service routines 

  9. *************************************************** *************************************************** *********/  

  10. #include "system.h"  

  11. #include "timer.h"  

  12. #include "delay.h"  

  13.   

  14.   

  15. /****************************************************** *************************************************** ************************ 

  16. *Function: void Delay_US(u32 Delay) 

  17. *Function: US delay 

  18. *Parameter: Delay: delay time, unit US 

  19. *Return: None 

  20. *Depends on: underlying macro definitions 

  21. *Author : cp1300@139.com 

  22. *Time: 20120917 

  23. *Last modified: 20120917 

  24. * Note: If timer 0 is called for delay, then timer 0 cannot be used, and the delay error is 1US 

  25. *************************************************** *************************************************** *********************/  

  26. void Delay_US(u32 Delay)  

  27. {  

  28.     rTCFG0 |= 65; //Timer 0 pre-divided by 65+1, PCLK=66 provides clock, 66 division generates 1MHz timer clock  

  29.     rTCON &= (~0xff); //Clear settings  

  30.     rTCON |= BIT3; //Timer 0 automatic update enable  

  31.     rTCNTB0 = Delay; //Reload value  

  32.     rTINT_CSTAT |= BIT5; //Clear interrupt flag  

  33.     rTINT_CSTAT &= ~BIT0; //Disable timer 0 interrupt  

  34.     //The following operation starts timer 0  

  35.     rTCON |= BIT1; //manual update  

  36.     rTCON &= ~BIT1; //End manual update  

  37.     rTCON |= BIT0; //Start timer 0      

  38.     while(!(rTINT_CSTAT & BIT5)); //Wait for the delay to arrive  

  39.     rTINT_CSTAT |= BIT5; //Clear interrupt flag  

  40. }  

  41.   

  42.   

  43.   

  44. /****************************************************** *************************************************** ************************ 

  45. *Function: void Delay_MS(u32 Delay) 

  46. *Function: MS delay 

  47. *Parameter: Delay: delay time, unit US 

  48. *Return: None 

  49. *Depends on: underlying macro definitions 

  50. *Author : cp1300@139.com 

  51. *Time : 20120918 

  52. *Last modified: 20120918 

  53. * Note: If timer 0 is called for delay, then timer 0 cannot be used, and the delay error is <1ms 

  54. *************************************************** *************************************************** *********************/  

  55. void Delay_MS(u32 Delay)  

  56. {  

  57.     while(Delay --)  

  58.     {  

  59.         Delay_US(1000);  

  60.     }  

  61. }  


Later, this program will be ported to LINUX as a DS18B20 driver.


Keywords:OK6410 Reference address:OK6410 development board bare metal DS18B20 driver

Previous article:S3C6410 Bare Metal Hardware JPEG Decoding
Next article:S3C6410 bare metal resistive screen driver

Recommended ReadingLatest update time:2024-11-16 15:19

OK6410 block device driver uses memory to simulate disk
ramblock.c source code: // Refer to drivers/block/xd.c,z2ram.c #include "linux/major.h" #include "linux/vmalloc.h" #include "linux/init.h" #include "linux/module.h" #include "linux/blkdev.h" #include "linux/bitops.h" #include "linux/hdreg.h"   #define RAMBLOCK_SIZE 0x100000 static struct gendisk *ramblock_disk; static
[Microcontroller]
Installation of the cross compiler for the ok6410 development board
1. In the previous article, we said that we have created a forlinx folder in the root directory of ubuntu12 in the virtual machine. Now copy arm-linux-gcc.4.3.2.tgz to forlinx. I use the xftp that I created last time, which is quite cool. 2. cd ../../       ls       cd forlinx         mkdir /usr/local/arm    
[Microcontroller]
Installation of the cross compiler for the ok6410 development board
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号