找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2463|回复: 0
打印 上一主题 下一主题
收起左侧

8051单片机控制大彩串口屏模拟器显示时钟和温度曲线代替上位机

[复制链接]
跳转到指定楼层
楼主
用串口屏开发软件自带的串口屏模拟器,显示8051输出的时钟和温度曲线。开发软件可在广州大彩官网下载,无需注册,免费使用,附带详细资料。可以和proteus通过虚拟串口联调,无需任何硬件。si结尾文件是串口屏文件,可以用模拟器打开。

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


单片机源程序如下:
  1. //Declarations------------------------------------------------------------------
  2. //const code char truck_bmp[1024];
  3. //--------------------------------------------------------------end-declarations
  4. extern void AT24C02_Write1Byte(char AT24C02_Address,char AT24C02_1Byte);
  5. extern char AT24C02_Read1Byte(char AT24C02_Address);
  6. extern void Display_24C02(void);

  7. //extern unsigned char ReadADC8591(unsigned char Channel);

  8. bit button;
  9. char seconds, minutes, hours, date, month;  // Global date/time variables
  10. unsigned int year;
  11. unsigned int RTCModuleAddress, YearOffset; // RTC chip description variables
  12. //unsigned int i;

  13. // Software I2C connections
  14. sbit Soft_I2C_Scl at P2_6_bit;
  15. sbit Soft_I2C_Sda at P2_7_bit;

  16. // OneWire pinout
  17. sbit OW_Bit at P3_7_bit;
  18. // end OneWire definition



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

  23. char *text = "000.0000";
  24. unsigned char temp1; unsigned char temp2;unsigned int temp;


  25. // Glcd module connections
  26. char GLCD_DataPort at P0;

  27. sbit GLCD_CS1 at P2_0_bit;
  28. sbit GLCD_CS2 at P2_1_bit;
  29. sbit GLCD_RS  at P2_2_bit;
  30. sbit GLCD_RW  at P2_3_bit;
  31. sbit GLCD_EN  at P2_4_bit;
  32. sbit GLCD_RST at P2_5_bit;
  33. // End Glcd module connections

  34. void Display_Temperature(unsigned int temp2write) {
  35.   const unsigned short RES_SHIFT =TEMP_RESOLUTION - 8 ;
  36.   char temp_whole;
  37.   unsigned int temp_fraction;


  38.   // check if temperature is negative
  39.   if (temp2write > 0x0900) {
  40.     text[0] = '-';
  41.     temp2write = ~temp2write+1;
  42.   }
  43.   else
  44.     {text[0] = ' ';}

  45.   // extract temp_whole
  46.   temp_whole = temp2write >> RES_SHIFT ;

  47.   // convert temp_whole to characters
  48.   if (temp_whole/100)
  49.      {text[0] = '1';}
  50.   //else
  51.      //{text[0] = '0';}

  52.   text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  53.   text[2] =  temp_whole%10     + 48;             // Extract ones digit
  54.   text[3] = '.';
  55.   // extract temp_fraction and convert it to unsigned int
  56.   temp_fraction  = temp2write << (4-RES_SHIFT);
  57.   temp_fraction &= 0x000F;
  58.   temp_fraction *= 625;

  59.   // convert temp_fraction to characters
  60.   text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  61.   text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  62.   text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  63.   text[7] =  temp_fraction%10  + 48;         // Extract ones digit


  64.   Glcd_Write_Text(text, 50, 1, 0);   // Write string

  65.   UART1_Write(0xEE);UART1_Write(0xB1); UART1_Write(0x10);UART1_Write(0x00);UART1_Write(0x00);
  66.   UART1_Write(0x00);UART1_Write(0x03);UART1_Write(text[0]); UART1_Write(text[1]);UART1_Write(text[2]);
  67.   UART1_Write(text[3]); UART1_Write(text[4]);UART1_Write(text[5]);UART1_Write(text[6]); UART1_Write(text[7]);
  68.   UART1_Write(0xFF);UART1_Write(0xFC);UART1_Write(0xFF);UART1_Write(0xFF);
  69.   /*
  70.   // send temperature to UART
  71.   UART1_Write_Text("Temp: ");
  72.   UART1_Write_Text(text);
  73.   UART1_Write('"');                              // degree sign
  74.   UART1_Write('C');                              // celsius
  75.   UART1_Write(13);                               // CR
  76.   UART1_Write(10);                               // LF
  77.   //Lcd_Out(1, 6, text);
  78.   */
  79.   if (seconds==0)
  80.        {
  81.         AT24C02_Write1Byte(minutes,temp_whole);
  82.         }

  83. }

  84. //------------------ Performs project-wide init
  85. void Init_Main() {
  86.     Soft_I2C_Init();           // Initialize Soft I2C communication
  87.     // DS1307
  88.     RTCModuleAddress   = 0xD0;
  89.     YearOffset         = 2000;

  90.   Glcd_Init();                              // Initialize GLCD
  91.   Glcd_Fill(0xff);                          // Clear GLCD
  92.   //Glcd_Circle(64, 32, 20, 1);
  93.   //Glcd_Rectangle(5, 5, 40, 40, 1);

  94.   Glcd_Set_Font(Character8x7, 8, 7, 32);// Change font
  95.   text = "Temp:   ";
  96.   Glcd_Write_Text(text, 5, 1, 0);   // Write string
  97. }




  98. //--------------------- Reads time and date information from DS1307 RTC
  99. void Read_Time_DS1307() {
  100.   char byte_read;
  101.   char i;
  102.   Soft_I2C_Start();                   // Issue start signal
  103.   Soft_I2C_Write(RTCModuleAddress);   // RTC module address + write (R#/W = 0)
  104.   Soft_I2C_Write(0);                  // Start from seconds byte
  105.   Soft_I2C_Start();                   // Issue repeated start signal
  106.   Soft_I2C_Write(RTCModuleAddress+1); // RTC module address + read  (R#/W = 1)

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

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

  111.   byte_read = Soft_I2C_Read(1);                                // Read hours byte
  112.   if (byte_read.B6) {                                          // 12h format
  113.     hours = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform hours
  114.     if (byte_read.B5)                                          // PM flag
  115.       hours = hours + 12;
  116.   }
  117.   else
  118.     hours = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform hours

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

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

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

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

  126.   //for (i =1; i<=10 ;i++)
  127.   //{byte_read=Soft_I2C_Read(1);}
  128.   Soft_I2C_Stop();                    // Issue stop signal

  129. }

  130. //--------------------- Reads time and date information from RTC
  131. void Read_Time() {
  132.   
  133.     Read_Time_DS1307();
  134. }



  135. //-------------------- Output values to LCD
  136. void Display_Time() {
  137.   //char year1[8];char month1[4];char date1[4];char hours1[4];char minutes1[4];char seconds1[4];
  138.    Glcd_Write_Text("DATE:", 5, 3, 0);   // Write string
  139.    //Glcd_Set_Font(Font5x7, 8, 7, 32);// Change font
  140.    Glcd_Write_Char(year/1000+48,52,3,0);Glcd_Write_Char((year%1000)/100+48,60,3,0);Glcd_Write_Char((year%100)/10+48,68,3,0);Glcd_Write_Char(year%10+48,76,3,0);
  141.    Glcd_Write_Char(' ',84,3,0);Glcd_Write_Char(month/10+48,90,3,0); Glcd_Write_Char(month%10+48,98,3,0);Glcd_Write_Char(' ',106,3,0);
  142.    Glcd_Write_Char(date/10+48,112,3,0);Glcd_Write_Char(date%10+48,120,3,0);
  143.    Glcd_Write_Text("TIME:", 5, 4, 0);   // Write string
  144.    Glcd_Write_Char(hours/10+48,52,4,0);Glcd_Write_Char(hours%10+48,60,4,0);Glcd_Write_Char(':',68,4,0);
  145.    Glcd_Write_Char(minutes/10+48,76,4,0);Glcd_Write_Char(minutes%10+48,84,4,0);Glcd_Write_Char(':',92,4,0);
  146.    Glcd_Write_Char(seconds/10+48,100,4,0);Glcd_Write_Char(seconds%10+48,108,4,0);

  147.   //IntToStr(year, year1); ByteToStr(month, month1);ByteToStr(date, date1);
  148.   //ByteToStr(hours, hours1); ByteToStr(minutes, minutes1);ByteToStr(seconds, seconds1);
  149.   
  150.   /*UART1_Write_Text("DATE:");
  151.   UART1_Write(year/1000+48); UART1_Write((year%1000)/100+48);UART1_Write((year%100)/10+48);UART1_Write(year%10+48);//print year
  152.   UART1_Write('-');
  153.   UART1_Write(month/10+48);UART1_Write(month%10+48);              //Print month
  154.   UART1_Write('-');
  155.   UART1_Write(date/10+48);UART1_Write(date%10+48);       //Print date
  156.   UART1_Write(' ');                               // CR
  157.   UART1_Write(' ');                               // LF
  158.   */
  159.   //UART1_Write_Text("Time:");
  160.   if (seconds%20==0)
  161.   {
  162.   UART1_Write(0xEE);UART1_Write(0xB1); UART1_Write(0x10);UART1_Write(0x00);UART1_Write(0x00);
  163.   UART1_Write(0x00);UART1_Write(0x01);UART1_Write(0x00);UART1_Write(0x00);UART1_Write(0x00);
  164.   UART1_Write(minutes*3+seconds/20);UART1_Write(0xFF);UART1_Write(0xFC);UART1_Write(0xFF);UART1_Write(0xFF);
  165.   //UART1_Write(13);                               // CR
  166.   //UART1_Write(10);                               // LF
  167.   if (hours>=12) hours=hours-12;
  168.   UART1_Write(0xEE);UART1_Write(0xB1); UART1_Write(0x10);UART1_Write(0x00);UART1_Write(0x00);
  169.   UART1_Write(0x00);UART1_Write(0x05);UART1_Write(0x00);UART1_Write(0x00);UART1_Write((hours*60+minutes)/256);
  170.   UART1_Write((hours*60+minutes)%256);UART1_Write(0xFF);UART1_Write(0xFC);UART1_Write(0xFF);UART1_Write(0xFF);
  171.   //UART1_Write(13);                               // CR
  172.   //UART1_Write(10);                               // LF
  173.   
  174.   //UART1_Write(hours/10+48);UART1_Write(hours%10+48);              //Print hour
  175.   //UART1_Write(':');
  176.   //UART1_Write(minutes/10+48);UART1_Write(minutes%10+48);       //Print minute
  177.   //UART1_Write(':');
  178.   //UART1_Write(seconds/10+48);UART1_Write(seconds%10+48);       //Print second
  179.   //UART1_Write(13);                               // CR
  180.   //UART1_Write(10);                               // LF
  181.   }


  182. }

  183. void main() {
  184.     Init_Main();               // Perform initialization
  185.     IE = 0x81;                                // Setting the Interrupts:
  186.     UART1_Init(4800);                              // Initialize UART module at 4800 bps

  187.      Ow_Reset();                                  // Onewire reset signal
  188.      Ow_Write(0xCC);                              // Issue command SKIP_ROM
  189.      Ow_Write(0x44);                              // Issue command CONVERT_T
  190.      Delay_us(120);

  191.      Ow_Reset();
  192.      Ow_Write(0xCC);                              // Issue command SKIP_ROM
  193.      Ow_Write(0xBE);                              // Issue command READ_SCRATCHPAD


  194.      Delay_ms(1000);


  195.   //Delay_ms(100);                                 // Wait for UART module to stabilize
  196.   
  197.   //UART1_Write_Text("Start");
  198.   //UART1_Write(13);                               // CR
  199.   //UART1_Write(10);                               // LF

  200.   //--- main loop
  201.   do {
  202.     //--- perform temperature reading
  203.     Ow_Reset();                                  // Onewire reset signal
  204.     Ow_Write(0xCC);                              // Issue command SKIP_ROM
  205.     Ow_Write(0x44);                              // Issue command CONVERT_T
  206.     Delay_us(120);

  207.     Ow_Reset();
  208.     Ow_Write(0xCC);                              // Issue command SKIP_ROM
  209.     Ow_Write(0xBE);                              // Issue command READ_SCRATCHPAD

  210.     Read_Time();             // Read time from RTC
  211.     Display_Time();          // Prepare and display on LCD

  212. ……………………

  213. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
8051 12864 ds18B20 串口屏.zip (6.76 MB, 下载次数: 36)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表