var seconds, minutes, hours, day, month, year : byte; // Global date/time variables var adc_rd : word; // I2C接口定义 Begin var Soft_I2C_Scl_Output : sbit at PORTC0_bit; Soft_I2C_Sda_Output : sbit at PORTC1_bit; Soft_I2C_Scl_Input : sbit at PINC0_bit; Soft_I2C_Sda_Input : sbit at PINC1_bit; Soft_I2C_Scl_Direction : sbit at DDC0_bit; Soft_I2C_Sda_Direction : sbit at DDC1_bit; // I2C接口定义 End // LCD接口定义 Begin var LCD_RS : sbit at PORTD2_bit; var LCD_EN : sbit at PORTD3_bit; LCD_D4 : sbit at PORTD4_bit; LCD_D5 : sbit at PORTD5_bit; LCD_D6 : sbit at PORTD6_bit; LCD_D7 : sbit at PORTD7_bit; var LCD_RS_Direction : sbit at DDD2_bit; LCD_EN_Direction : sbit at DDD3_bit; LCD_D4_Direction : sbit at DDD4_bit; LCD_D5_Direction : sbit at DDD5_bit; LCD_D6_Direction : sbit at DDD6_bit; LCD_D7_Direction : sbit at DDD7_bit; // LCD接口定义 End // 从PCF8583取得时间和日期 procedure Read_Time(); begin Soft_I2C_Start(); // I2C启动信号 Soft_I2C_Write(0xA0); // PCF8583的物理地址:0xA0 Soft_I2C_Write(2); // 启动PCF8583 Soft_I2C_Start(); // I2C启动信号 Soft_I2C_Write(0xA1); // PCF8583的读操作地址:0xA1 seconds := Soft_I2C_Read(1); // 读取秒 minutes := Soft_I2C_Read(1); // 读取分 hours := Soft_I2C_Read(1); // 读取小时 day := Soft_I2C_Read(1); // 读取日期 month := Soft_I2C_Read(0); // 读取月份 Soft_I2C_Stop(); // I2C停止信号 end; //格式化日期、时间 procedure Transform_Time() ; begin seconds := ((seconds and 0xF0) shr 4)*10 + (seconds and 0x0F); // Transform seconds minutes := ((minutes and 0xF0) shr 4)*10 + (minutes and 0x0F); // Transform months hours := ((hours and 0xF0) shr 4)*10 + (hours and 0x0F); // Transform hours year := (day and 0xC0) shr 6; // Transform year day := ((day and 0x30) shr 4)*10 + (day and 0x0F); // Transform day month := ((month and 0x10) shr 4)*10 + (month and 0x0F); // Transform month end; //输出数据到LCD1602上显示 procedure Display_Time(); begin // 显示日期 Lcd_Chr(1, 9, (day / 10) + 48); // Print tens digit of day variable Lcd_Chr(1, 10, (day mod 10) + 48); // Print oness digit of day variable Lcd_Chr(1, 6, (month / 10) + 48); Lcd_Chr(1,7, (month mod 10) + 48); // 显示时间 UART1_Write_Text('Time: '); Lcd_Chr(2, 6, (hours / 10) + 48); Lcd_Chr(2, 7, (hours mod 10) + 48); UART1_Write(hours/10+48); UART1_Write(hours mod 10 + 48); UART1_Write(':'); Lcd_Chr(2, 9, (minutes / 10) + 48); Lcd_Chr(2,10, (minutes mod 10) + 48); UART1_Write(minutes/10+48); UART1_Write(minutes mod 10 + 48); UART1_Write(':'); Lcd_Chr(2,12, (seconds / 10) + 48); Lcd_Chr(2,13, (seconds mod 10) + 48); UART1_Write(seconds/10+48); UART1_Write(seconds mod 10 + 48); UART1_Write(' '); // 显示ADC结果 Lcd_Chr(1,13,(adc_rd/1000) + 48); Lcd_Chr(1,14,((adc_rd mod 1000)/100) + 48); Lcd_Chr(1,15,(adc_rd mod 100)/10 + 48); Lcd_Chr(1,16,(adc_rd mod 10) + 48); // 串口输出adc数据 UART1_Write_Text('Volt: '); UART1_Write((adc_rd/1000) + 48); UART1_Write((adc_rd mod 1000)/100 + 48); UART1_Write((adc_rd mod 100)/10 + 48); UART1_Write((adc_rd mod 10) + 48); UART1_Write(13);UART1_Write(10); end; // 系统初始化 procedure Init_Main(); begin UART1_Init(9600); // 初始化UART:n,8,1,9600 Delay_ms(100); // 等待UART初始化完成 UART1_Write_Text('Start'); // 输出Start UART1_Write(13);UART1_Write(10); // 换行 Lcd_Init(); // 初始化LCD Lcd_Cmd(_LCD_CLEAR); // 清除显示 Lcd_Cmd(_LCD_CURSOR_OFF); // 关闭光标 LCD_Out(1,1,'Date:'); // 位置(1,1)显示Date: LCD_Chr(1,8,'-'); // 位置(1,8)显示- LCD_Out(2,1,'Time:'); // 位置(2,1)显示Time: LCD_Chr(2,8,':'); // 位置(2,8)显示: LCD_Chr(2,11,':'); // 位置(2,11)显示: LCD_Out(1,12,'V'); // 位置(1,12)显示V Soft_I2C_Init(); // Initialize Soft I2C communication end; //----------------- Main procedure begin Init_Main(); // 系统初始化 while TRUE do // 主循环 begin Read_Time(); // 从PCF8583取得时间和日期 Transform_Time(); // 格式化日期、时间 adc_rd := ADC_Read(0); // 获取ADC数据,这个数据将作为EEPROM的地址来使用 Display_Time(); // 显示数据 if (seconds = 0) then // 秒=0时,数据写入eeprom,eeprom的地址由ADC的数值定 EEPROM_Write(minutes,adc_rd/4); // Write some data at address delay_ms(1000); end; end. |