久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: MikroC8051寫的,讀取ds18B20溫度,DS1307時鐘,在1602顯示,同時串口輸出。 [打印本頁]

作者: hotpet    時間: 2020-5-1 05:44
標題: MikroC8051寫的,讀取ds18B20溫度,DS1307時鐘,在1602顯示,同時串口輸出。
8051仿真讀取18B20溫度,DS1307時鐘,顯示在1602液晶。同時每分鐘一次將溫度和電阻模擬的ADC值記入24C02 EEPROM。通過串口顯示全部數值,并每分鐘一次輸出24C02全部存儲數值。程序用MikroC8051編寫,使用了自帶的onewire和soft i2c庫。仿真串口輸出已經映射到虛擬串口1,可以用YSPD虛擬串口軟件加串口助手查看。

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機源程序如下:
  1. /*
  2. * Project name:
  3.      OneWire (Interfacing the DS1820 temperature sensor - all versions)
  4. * Copyright:
  5.      (c) Mikroelektronika, 2013.
  6. * Revision History:
  7.      20101129:
  8.        - initial release (SZ)
  9. * Description:
  10.      This code demonstrates one-wire communication with temperature sensor
  11.      DS18x20 connected to P3.7 pin.
  12.      MCU reads temperature from the sensor and sends it on the UART.
  13.      The display format of the temperature is 'xxx.xxxx癈'. To obtain correct
  14.      results, the 18x20's temperature resolution has to be adjusted (constant
  15.      TEMP_RESOLUTION).
  16. * Test configuration:
  17.      MCU:             AT89S8252
  18.                      
  19.                      

  20.      Oscillator:      External oscillator 11.0592MHz
  21.      Ext. Modules:    -
  22.      SW:              mikroC PRO for 8051
  23. * NOTES:
  24.      -  also, pull-up P3.7
  25.      - UART: SW11.1 and SW11.3 - ON
  26. */
  27. extern void AT24C02_Write1Byte(char AT24C02_Address,char AT24C02_1Byte);
  28. extern char AT24C02_Read1Byte(char AT24C02_Address);
  29. extern void Display_24C02(void);
  30. extern unsigned char ReadADC8591(unsigned char Channel);
  31. unsigned char AT24C02Address = 0;
  32. unsigned char LightVolt; unsigned char Volt[4];
  33. char seconds, minutes, hours, date, month;  // Global date/time variables
  34. unsigned int year;
  35. unsigned int RTCModuleAddress, YearOffset; // RTC chip description variables
  36. //unsigned int i;

  37. //#define PCF8583         // Uncomment this line if you use PCF8583 RTC chip (mE RTC extra board)
  38. #define DS1307          // Uncomment this line if you use DS1307 RTC chip (mE RTC2 extra board)

  39. // Software I2C connections
  40. sbit Soft_I2C_Scl at P2_0_bit;
  41. sbit Soft_I2C_Sda at P2_1_bit;
  42. // End Software I2C connections


  43. // Lcd module connections
  44. sbit LCD_RS at P2_2_bit;
  45. sbit LCD_EN at P2_3_bit;

  46. sbit LCD_D4 at P2_4_bit;
  47. sbit LCD_D5 at P2_5_bit;
  48. sbit LCD_D6 at P2_6_bit;
  49. sbit LCD_D7 at P2_7_bit;
  50. // End Lcd module connections





  51. // OneWire pinout
  52. sbit OW_Bit at P3.B7;
  53. // end OneWire definition


  54. //  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
  55. //  18S20: 9  (default setting; can be 9,10,11,or 12)
  56. //  18B20: 12
  57. const unsigned short TEMP_RESOLUTION = 12;

  58. char *text = "000.0000";
  59. unsigned char temp1; unsigned char temp2;unsigned int temp;

  60. const char character[] = {7,5,7,0,0,0,0,0};

  61. void CustomChar(char pos_row, char pos_char) {
  62.   char i;
  63.     Lcd_Cmd(64);
  64.     for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
  65.     Lcd_Cmd(_LCD_RETURN_HOME);
  66.     Lcd_Chr(pos_row, pos_char, 0);
  67. }

  68. void Display_Temperature(unsigned int temp2write) {
  69.   const unsigned short RES_SHIFT =TEMP_RESOLUTION - 8 ;
  70.   char temp_whole;
  71.   unsigned int temp_fraction;

  72.   // check if temperature is negative
  73.   if (temp2write > 0x0900) {
  74.     text[0] = '-';
  75.     temp2write = ~temp2write+1;
  76.   }
  77.   else
  78.     {text[0] = ' ';}
  79.    
  80.   // extract temp_whole
  81.   temp_whole = temp2write >> RES_SHIFT ;

  82.   // convert temp_whole to characters
  83.   if (temp_whole/100)
  84.      {text[0] = '1';}
  85.   //else
  86.      //{text[0] = '0';}

  87.   text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  88.   text[2] =  temp_whole%10     + 48;             // Extract ones digit

  89.   // extract temp_fraction and convert it to unsigned int
  90.   temp_fraction  = temp2write << (4-RES_SHIFT);
  91.   temp_fraction &= 0x000F;
  92.   temp_fraction *= 625;

  93.   // convert temp_fraction to characters
  94.   text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  95.   text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  96.   text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  97.   text[7] =  temp_fraction%10      + 48;         // Extract ones digit

  98.   // send temperature to UART
  99.   UART1_Write_Text("Temp: ");
  100.   UART1_Write_Text(text);
  101.   UART1_Write('"');                              // degree sign
  102.   UART1_Write('C');                              // celsius
  103.   UART1_Write(13);                               // CR
  104.   UART1_Write(10);                               // LF
  105.   Lcd_Out(1, 6, text);
  106.   
  107.   if (seconds==0)
  108.        {
  109.         AT24C02_Write1Byte(minutes,temp_whole);
  110.         }
  111.   
  112. }



  113. //------------------ Performs project-wide init
  114. void Init_Main() {

  115.   #ifdef PCF8583
  116.     RTCModuleAddress   = 0xA0;
  117.     YearOffset         = 2008;
  118.   #endif

  119.   #ifdef DS1307
  120.     RTCModuleAddress   = 0xD0;
  121.     YearOffset         = 2000;
  122.   #endif


  123.   Soft_I2C_Init();           // Initialize Soft I2C communication
  124.   Lcd_Init();                // Initialize LCD
  125.   Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
  126.   Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off

  127.   //LCD_Out(1,1,"Date:");      // Prepare and output static text on LCD
  128.   //Lcd_Chr(1,8,':');
  129.   //Lcd_Chr(1,11,':');
  130.   LCD_Out(2,1,"Time:");
  131.   Lcd_Chr(2,8,':');
  132.   Lcd_Chr(2,11,':');
  133. }

  134. //--------------------- Reads time and date information from PCF8583 RTC
  135. void Read_Time_PCF8583() {
  136.   char byte_read;

  137.   Soft_I2C_Start();                   // Issue start signal
  138.   Soft_I2C_Write(RTCModuleAddress);   // RTC module address + write (R#/W = 0)
  139.   Soft_I2C_Write(2);                  // Start from seconds byte
  140.   Soft_I2C_Start();                   // Issue repeated start signal
  141.   Soft_I2C_Write(RTCModuleAddress+1); // RTC module address + read  (R#/W = 1)

  142.   byte_read = Soft_I2C_Read(1);                                // Read seconds byte
  143.   seconds = ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F); // Transform seconds

  144.   byte_read = Soft_I2C_Read(1);                                // Read minutes byte
  145.   minutes = ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F); // Transform minutes

  146.   byte_read = Soft_I2C_Read(1);                                // Read hours byte
  147.   hours = ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F);   // Transform hours
  148.   if ( (byte_read.B7) && (byte_read.B6) )                      // 12h format && PM flag
  149.     hours = hours + 12;

  150.   byte_read = Soft_I2C_Read(1);                                // Read year/date byte
  151.   year = YearOffset + ((byte_read & 0xC0) >> 6);               // Transform year
  152.   date = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F);    // Transform date

  153.   byte_read = Soft_I2C_Read(0);                                // Read weekday/month byte
  154.   month = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F);   // Transform month

  155.   Soft_I2C_Stop();                    // Issue stop signal
  156. }

  157. //--------------------- Reads time and date information from DS1307 RTC
  158. void Read_Time_DS1307() {
  159.   char byte_read;
  160.   char i;
  161.   Soft_I2C_Start();                   // Issue start signal
  162.   Soft_I2C_Write(RTCModuleAddress);   // RTC module address + write (R#/W = 0)
  163.   Soft_I2C_Write(0);                  // Start from seconds byte
  164.   Soft_I2C_Start();                   // Issue repeated start signal
  165.   Soft_I2C_Write(RTCModuleAddress+1); // RTC module address + read  (R#/W = 1)

  166.   byte_read = Soft_I2C_Read(1);                                // Read seconds byte
  167.   seconds = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform seconds

  168.   byte_read = Soft_I2C_Read(1);                                // Read minutes byte
  169.   minutes = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform minutes

  170.   byte_read = Soft_I2C_Read(1);                                // Read hours byte
  171.   if (byte_read.B6) {                                          // 12h format
  172.     hours = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform hours
  173.     if (byte_read.B5)                                          // PM flag
  174.       hours = hours + 12;
  175.   }
  176.   else
  177.     hours = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform hours

  178.   byte_read = Soft_I2C_Read(1);                                // Read weekday byte

  179.   byte_read = Soft_I2C_Read(1);                                // Read date byte
  180.   date = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F);    // Transform date

  181.   byte_read = Soft_I2C_Read(1);                                // Read month byte
  182.   month = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F);   // Transform month

  183.   byte_read = Soft_I2C_Read(0);                                // Read year byte
  184.   year = YearOffset + ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F); // Transform year

  185.   //for (i =1; i<=10 ;i++)
  186.   //{byte_read=Soft_I2C_Read(1);}
  187.   Soft_I2C_Stop();                    // Issue stop signal
  188. }

  189. //--------------------- Reads time and date information from RTC
  190. void Read_Time() {
  191.   #ifdef PCF8583
  192.     Read_Time_PCF8583();
  193.   #endif

  194.   #ifdef DS1307
  195.     Read_Time_DS1307();
  196.   #endif
  197. }

  198. //-------------------- Output values to LCD
  199. void Display_Time() {
  200.   //char year1[8];char month1[4];char date1[4];char hours1[4];char minutes1[4];char seconds1[4];
  201.   
  202.   Lcd_Chr(2, 6, (hours / 10)   + 48);
  203.   Lcd_Chr(2, 7, (hours % 10)   + 48);
  204.   Lcd_Chr(2, 9, (minutes / 10) + 48);
  205.   Lcd_Chr(2,10, (minutes % 10) + 48);
  206.   Lcd_Chr(2,12, (seconds / 10) + 48);
  207.   Lcd_Chr(2,13, (seconds % 10) + 48);
  208.   
  209.   //IntToStr(year, year1); ByteToStr(month, month1);ByteToStr(date, date1);
  210.   //ByteToStr(hours, hours1); ByteToStr(minutes, minutes1);ByteToStr(seconds, seconds1);
  211.   UART1_Write_Text("DATE:");
  212.   UART1_Write(year/1000+48); UART1_Write((year%1000)/100+48);UART1_Write((year%100)/10+48);UART1_Write(year%10+48);//print year
  213.   UART1_Write('-');
  214.   UART1_Write(month/10+48);UART1_Write(month%10+48);              //Print month
  215.   UART1_Write('-');
  216.   UART1_Write(date/10+48);UART1_Write(date%10+48);       //Print date
  217.   UART1_Write(' ');                               // CR
  218.   UART1_Write(' ');                               // LF
  219.   
  220.   UART1_Write_Text("Time:");
  221.   UART1_Write(hours/10+48);UART1_Write(hours%10+48);              //Print hour
  222.   UART1_Write(':');
  223.   UART1_Write(minutes/10+48);UART1_Write(minutes%10+48);       //Print minute
  224.   UART1_Write(':');
  225.   UART1_Write(seconds/10+48);UART1_Write(seconds%10+48);       //Print second
  226.   UART1_Write(13);                               // CR
  227.   UART1_Write(10);                               // LF

  228.   /*LCD_Out(1,1,"Date:");      // Prepare and output static text on LCD
  229.   Lcd_Chr(1,8,':');
  230.   Lcd_Chr(1,11,':');
  231.   Lcd_Chr(1, 6, (date / 10)   + 48);    // Print tens digit of date variable
  232.   Lcd_Chr(1, 7, (date % 10)   + 48);    // Print oness digit of date variable
  233.   Lcd_Chr(1, 9, (month / 10)  + 48);
  234.   Lcd_Chr(1,10, (month % 10)  + 48);
  235.   Lcd_Chr(1,12, ((year / 1000) % 10) + 48);    // Print year
  236.   Lcd_Chr(1,13, ((year / 100)  % 10) + 48);
  237.   Lcd_Chr(1,14, ((year / 10)   % 10) + 48);
  238.   Lcd_Chr(1,15, (year % 10)          + 48);
  239.   */

  240. }






  241. void main() {

  242.     Init_Main();               // Perform initialization
  243.     UART1_Init(4800);                              // Initialize UART module at 4800 bps
  244.      //Lcd_Init();                              // Initialize Lcd
  245.      //Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  246.      //Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  247.      Lcd_Out(1, 1, "Temp:");                 // Write message text on Lcd
  248.      //Lcd_Out(2, 1, "Time:");
  249.      CustomChar(1, 14);
  250.      Lcd_Chr(1, 15, 'C');
  251.   //Delay_ms(5);Skip first read due to not accurate
  252.      Ow_Reset();                                  // Onewire reset signal
  253.      Ow_Write(0xCC);                              // Issue command SKIP_ROM
  254.      Ow_Write(0x44);                              // Issue command CONVERT_T
  255.      Delay_us(120);

  256.      Ow_Reset();
  257.      Ow_Write(0xCC);                              // Issue command SKIP_ROM
  258.      Ow_Write(0xBE);                              // Issue command READ_SCRATCHPAD
  259.      
  260.      
  261.      Delay_ms(1000);


  262.   //Delay_ms(100);                                 // Wait for UART module to stabilize
  263.   UART1_Write_Text("Start");
  264.   UART1_Write(13);                               // CR
  265.   UART1_Write(10);                               // LF
  266.   
  267.   //--- main loop
  268.   do {
  269.     //--- perform temperature reading
  270.     Ow_Reset();                                  // Onewire reset signal
  271.     Ow_Write(0xCC);                              // Issue command SKIP_ROM
  272.     Ow_Write(0x44);                              // Issue command CONVERT_T
  273.     Delay_us(120);

  274.     Ow_Reset();
  275.     Ow_Write(0xCC);                              // Issue command SKIP_ROM
  276.     Ow_Write(0xBE);                              // Issue command READ_SCRATCHPAD
  277.    
  278.     Read_Time();             // Read time from RTC
  279.     Display_Time();          // Prepare and display on LCD

  280.     Delay_ms(400);
  281.     temp1 = Ow_Read();
  282.     temp2 = Ow_Read();

  283.     temp = (temp2 << 8) + temp1;

  284.     //--- Format and display result on Lcd
  285.     Display_Temperature(temp);
  286.     LightVolt = ReadADC8591(0);
  287.     UART1_Write_Text("The volt on LDR is ");
  288.     ByteToStr(LightVolt, volt);UART1_Write_Text(volt);//UART1_Write_Text("/255)*5V");
  289.     UART1_Write(13);                               // CR
  290.     UART1_Write(10);                               // LF
  291.     if (seconds==30)
  292.        {AT24C02_Write1Byte(minutes+128,LightVolt); }
  293.    
  294.     if(seconds==0)
  295.        {Display_24C02();}

  296.     Delay_ms(450);
  297.     UART1_Write(13);                               // CR
  298.     UART1_Write(10);                               // LF
  299.   } while (1);
  300. }
復制代碼

所有資料51hei提供下載:
8051 18B20 I2C.zip (38.46 KB, 下載次數: 39)


作者: wwh2382    時間: 2020-7-25 14:36
不錯,可以仿真,謝謝







歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 午夜精品一区二区三区在线 | 国产美女在线观看 | 福利久久| 国产清纯白嫩初高生视频在线观看 | 国产三级一区二区 | 久久久久亚洲精品 | 国产一伦一伦一伦 | 人人操日日干 | 一区二区在线 | 亚洲九色 | 天天操夜夜艹 | 欧美日韩久久久久 | 精品国产99 | 成人激情视频在线观看 | 午夜天堂精品久久久久 | 日本精品久久 | av黄色免费在线观看 | 国产精品欧美大片 | 久久91精品久久久久久9鸭 | 国产99精品 | 国产精品高潮呻吟久久 | 国产精品三级久久久久久电影 | 精品久久久久国产 | 亚洲欧美成人影院 | 九九综合 | 亚洲天堂免费在线 | 亚洲精品一区二区在线观看 | 特黄级国产片 | 亚洲精品乱码久久久久久黑人 | 日日噜噜噜夜夜爽爽狠狠视频, | 羞羞视频免费观看入口 | 黄a免费网络 | 精品一区二区电影 | 欧美激情精品久久久久久免费 | 国产精品一区二区在线播放 | 久久久国产一区 | 精品亚洲一区二区三区 | 成人教育av | 久久久久国产精品 | 欧美影院 | 伊人伊人伊人 |