用串口屏開發軟件自帶的串口屏模擬器,顯示8051輸出的時鐘和溫度曲線。開發軟件可在廣州大彩官網下載,無需注冊,免費使用,附帶詳細資料。可以和proteus通過虛擬串口聯調,無需任何硬件。si結尾文件是串口屏文件,可以用模擬器打開。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (24.12 KB, 下載次數: 56)
下載附件
2020-5-15 04:45 上傳
單片機源程序如下:
- //Declarations------------------------------------------------------------------
- //const code char truck_bmp[1024];
- //--------------------------------------------------------------end-declarations
- extern void AT24C02_Write1Byte(char AT24C02_Address,char AT24C02_1Byte);
- extern char AT24C02_Read1Byte(char AT24C02_Address);
- extern void Display_24C02(void);
- //extern unsigned char ReadADC8591(unsigned char Channel);
- bit button;
- char seconds, minutes, hours, date, month; // Global date/time variables
- unsigned int year;
- unsigned int RTCModuleAddress, YearOffset; // RTC chip description variables
- //unsigned int i;
- // Software I2C connections
- sbit Soft_I2C_Scl at P2_6_bit;
- sbit Soft_I2C_Sda at P2_7_bit;
- // OneWire pinout
- sbit OW_Bit at P3_7_bit;
- // end OneWire definition
- // Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
- // 18S20: 9 (default setting; can be 9,10,11,or 12)
- // 18B20: 12
- const unsigned short TEMP_RESOLUTION = 12;
- char *text = "000.0000";
- unsigned char temp1; unsigned char temp2;unsigned int temp;
- // Glcd module connections
- char GLCD_DataPort at P0;
- sbit GLCD_CS1 at P2_0_bit;
- sbit GLCD_CS2 at P2_1_bit;
- sbit GLCD_RS at P2_2_bit;
- sbit GLCD_RW at P2_3_bit;
- sbit GLCD_EN at P2_4_bit;
- sbit GLCD_RST at P2_5_bit;
- // End Glcd module connections
- void Display_Temperature(unsigned int temp2write) {
- const unsigned short RES_SHIFT =TEMP_RESOLUTION - 8 ;
- char temp_whole;
- unsigned int temp_fraction;
- // check if temperature is negative
- if (temp2write > 0x0900) {
- text[0] = '-';
- temp2write = ~temp2write+1;
- }
- else
- {text[0] = ' ';}
- // extract temp_whole
- temp_whole = temp2write >> RES_SHIFT ;
- // convert temp_whole to characters
- if (temp_whole/100)
- {text[0] = '1';}
- //else
- //{text[0] = '0';}
- text[1] = (temp_whole/10)%10 + 48; // Extract tens digit
- text[2] = temp_whole%10 + 48; // Extract ones digit
- text[3] = '.';
- // extract temp_fraction and convert it to unsigned int
- temp_fraction = temp2write << (4-RES_SHIFT);
- temp_fraction &= 0x000F;
- temp_fraction *= 625;
- // convert temp_fraction to characters
- text[4] = temp_fraction/1000 + 48; // Extract thousands digit
- text[5] = (temp_fraction/100)%10 + 48; // Extract hundreds digit
- text[6] = (temp_fraction/10)%10 + 48; // Extract tens digit
- text[7] = temp_fraction%10 + 48; // Extract ones digit
- Glcd_Write_Text(text, 50, 1, 0); // Write string
- UART1_Write(0xEE);UART1_Write(0xB1); UART1_Write(0x10);UART1_Write(0x00);UART1_Write(0x00);
- UART1_Write(0x00);UART1_Write(0x03);UART1_Write(text[0]); UART1_Write(text[1]);UART1_Write(text[2]);
- UART1_Write(text[3]); UART1_Write(text[4]);UART1_Write(text[5]);UART1_Write(text[6]); UART1_Write(text[7]);
- UART1_Write(0xFF);UART1_Write(0xFC);UART1_Write(0xFF);UART1_Write(0xFF);
- /*
- // send temperature to UART
- UART1_Write_Text("Temp: ");
- UART1_Write_Text(text);
- UART1_Write('"'); // degree sign
- UART1_Write('C'); // celsius
- UART1_Write(13); // CR
- UART1_Write(10); // LF
- //Lcd_Out(1, 6, text);
- */
- if (seconds==0)
- {
- AT24C02_Write1Byte(minutes,temp_whole);
- }
- }
- //------------------ Performs project-wide init
- void Init_Main() {
- Soft_I2C_Init(); // Initialize Soft I2C communication
- // DS1307
- RTCModuleAddress = 0xD0;
- YearOffset = 2000;
- Glcd_Init(); // Initialize GLCD
- Glcd_Fill(0xff); // Clear GLCD
- //Glcd_Circle(64, 32, 20, 1);
- //Glcd_Rectangle(5, 5, 40, 40, 1);
- Glcd_Set_Font(Character8x7, 8, 7, 32);// Change font
- text = "Temp: ";
- Glcd_Write_Text(text, 5, 1, 0); // Write string
- }
- //--------------------- Reads time and date information from DS1307 RTC
- void Read_Time_DS1307() {
- char byte_read;
- char i;
- Soft_I2C_Start(); // Issue start signal
- Soft_I2C_Write(RTCModuleAddress); // RTC module address + write (R#/W = 0)
- Soft_I2C_Write(0); // Start from seconds byte
- Soft_I2C_Start(); // Issue repeated start signal
- Soft_I2C_Write(RTCModuleAddress+1); // RTC module address + read (R#/W = 1)
- byte_read = Soft_I2C_Read(1); // Read seconds byte
- seconds = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform seconds
- byte_read = Soft_I2C_Read(1); // Read minutes byte
- minutes = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform minutes
- byte_read = Soft_I2C_Read(1); // Read hours byte
- if (byte_read.B6) { // 12h format
- hours = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform hours
- if (byte_read.B5) // PM flag
- hours = hours + 12;
- }
- else
- hours = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform hours
- byte_read = Soft_I2C_Read(1); // Read weekday byte
- byte_read = Soft_I2C_Read(1); // Read date byte
- date = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform date
- byte_read = Soft_I2C_Read(1); // Read month byte
- month = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform month
- byte_read = Soft_I2C_Read(0); // Read year byte
- year = YearOffset + ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F); // Transform year
- //for (i =1; i<=10 ;i++)
- //{byte_read=Soft_I2C_Read(1);}
- Soft_I2C_Stop(); // Issue stop signal
- }
- //--------------------- Reads time and date information from RTC
- void Read_Time() {
-
- Read_Time_DS1307();
- }
- //-------------------- Output values to LCD
- void Display_Time() {
- //char year1[8];char month1[4];char date1[4];char hours1[4];char minutes1[4];char seconds1[4];
- Glcd_Write_Text("DATE:", 5, 3, 0); // Write string
- //Glcd_Set_Font(Font5x7, 8, 7, 32);// Change font
- 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);
- 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);
- Glcd_Write_Char(date/10+48,112,3,0);Glcd_Write_Char(date%10+48,120,3,0);
- Glcd_Write_Text("TIME:", 5, 4, 0); // Write string
- Glcd_Write_Char(hours/10+48,52,4,0);Glcd_Write_Char(hours%10+48,60,4,0);Glcd_Write_Char(':',68,4,0);
- Glcd_Write_Char(minutes/10+48,76,4,0);Glcd_Write_Char(minutes%10+48,84,4,0);Glcd_Write_Char(':',92,4,0);
- Glcd_Write_Char(seconds/10+48,100,4,0);Glcd_Write_Char(seconds%10+48,108,4,0);
- //IntToStr(year, year1); ByteToStr(month, month1);ByteToStr(date, date1);
- //ByteToStr(hours, hours1); ByteToStr(minutes, minutes1);ByteToStr(seconds, seconds1);
-
- /*UART1_Write_Text("DATE:");
- UART1_Write(year/1000+48); UART1_Write((year%1000)/100+48);UART1_Write((year%100)/10+48);UART1_Write(year%10+48);//print year
- UART1_Write('-');
- UART1_Write(month/10+48);UART1_Write(month%10+48); //Print month
- UART1_Write('-');
- UART1_Write(date/10+48);UART1_Write(date%10+48); //Print date
- UART1_Write(' '); // CR
- UART1_Write(' '); // LF
- */
- //UART1_Write_Text("Time:");
- if (seconds%20==0)
- {
- UART1_Write(0xEE);UART1_Write(0xB1); UART1_Write(0x10);UART1_Write(0x00);UART1_Write(0x00);
- UART1_Write(0x00);UART1_Write(0x01);UART1_Write(0x00);UART1_Write(0x00);UART1_Write(0x00);
- UART1_Write(minutes*3+seconds/20);UART1_Write(0xFF);UART1_Write(0xFC);UART1_Write(0xFF);UART1_Write(0xFF);
- //UART1_Write(13); // CR
- //UART1_Write(10); // LF
- if (hours>=12) hours=hours-12;
- UART1_Write(0xEE);UART1_Write(0xB1); UART1_Write(0x10);UART1_Write(0x00);UART1_Write(0x00);
- UART1_Write(0x00);UART1_Write(0x05);UART1_Write(0x00);UART1_Write(0x00);UART1_Write((hours*60+minutes)/256);
- UART1_Write((hours*60+minutes)%256);UART1_Write(0xFF);UART1_Write(0xFC);UART1_Write(0xFF);UART1_Write(0xFF);
- //UART1_Write(13); // CR
- //UART1_Write(10); // LF
-
- //UART1_Write(hours/10+48);UART1_Write(hours%10+48); //Print hour
- //UART1_Write(':');
- //UART1_Write(minutes/10+48);UART1_Write(minutes%10+48); //Print minute
- //UART1_Write(':');
- //UART1_Write(seconds/10+48);UART1_Write(seconds%10+48); //Print second
- //UART1_Write(13); // CR
- //UART1_Write(10); // LF
- }
- }
- void main() {
- Init_Main(); // Perform initialization
- IE = 0x81; // Setting the Interrupts:
- UART1_Init(4800); // Initialize UART module at 4800 bps
- Ow_Reset(); // Onewire reset signal
- Ow_Write(0xCC); // Issue command SKIP_ROM
- Ow_Write(0x44); // Issue command CONVERT_T
- Delay_us(120);
- Ow_Reset();
- Ow_Write(0xCC); // Issue command SKIP_ROM
- Ow_Write(0xBE); // Issue command READ_SCRATCHPAD
- Delay_ms(1000);
- //Delay_ms(100); // Wait for UART module to stabilize
-
- //UART1_Write_Text("Start");
- //UART1_Write(13); // CR
- //UART1_Write(10); // LF
- //--- main loop
- do {
- //--- perform temperature reading
- Ow_Reset(); // Onewire reset signal
- Ow_Write(0xCC); // Issue command SKIP_ROM
- Ow_Write(0x44); // Issue command CONVERT_T
- Delay_us(120);
- Ow_Reset();
- Ow_Write(0xCC); // Issue command SKIP_ROM
- Ow_Write(0xBE); // Issue command READ_SCRATCHPAD
- Read_Time(); // Read time from RTC
- Display_Time(); // Prepare and display on LCD
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
51hei.png (8.83 KB, 下載次數: 76)
下載附件
2020-5-15 04:44 上傳
所有資料51hei提供下載:
8051 12864 ds18B20 串口屏.zip
(6.76 MB, 下載次數: 36)
2020-5-15 03:44 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|