介紹mikroPascal for AVR 寫的ATmega16讀取RTC和ADC顯示在LCD1602,同時串口輸出,并寫入EEPROM的程序。附帶仿真文件。mikro編譯器分為c,basic,pascal三種,除了語法有所區別外,使用和功能基本一樣。有8051,AVR,PIC,ARM等版本,界面和語法都一樣。學會一種芯片花很少時間就可以轉到另一種芯片。自帶常用庫,幫助文件中例子很多,基本不需要其他文檔就可以開始學習了。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (18.06 KB, 下載次數: 90)
下載附件
2020-4-30 04:05 上傳
單片機源程序如下:
- program RTC_Read;
- var seconds, minutes, hours, day, month, year : byte; // Global date/time variables
- var adc_rd : word;
- // Software I2C connections
- 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;
- // End Software I2C connections
- // LCD pinout definition
- 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;
- // end LCD pinout definitions
- //--------------------- Reads time and date information from RTC (PCF8583)
- procedure Read_Time();
- begin
- Soft_I2C_Start(); // Issue start signal
- Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet
- Soft_I2C_Write(2); // Start from address 2
- Soft_I2C_Start(); // Issue repeated start signal
- Soft_I2C_Write(0xA1); // Address PCF8583 for reading R/W=1
- seconds := Soft_I2C_Read(1); // Read seconds byte
- minutes := Soft_I2C_Read(1); // Read minutes byte
- hours := Soft_I2C_Read(1); // Read hours byte
- day := Soft_I2C_Read(1); // Read year/day byte
- month := Soft_I2C_Read(0); // Read weekday/month byte
- Soft_I2C_Stop(); // Issue stop signal
- end;
- //-------------------- Formats date and time
- 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;
- //-------------------- Output values to LCD
- 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);
- //Lcd_Chr(1,15, (year mod 10) + 48); // Print year vaiable
-
- 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(' ');
- //UART1_Write(13);UART1_Write(10);
-
- 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);
- 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;
- //------------------ Performs project-wide init
- procedure Init_Main();
- begin
- UART1_Init(2400); // Initialize UART module at 9600 bps
- Delay_ms(100); // Wait for UART module to stabilize
- UART1_Write_Text('Start');
- UART1_Write(13);UART1_Write(10);
- Lcd_Init(); // Initialize LCD
- Lcd_Cmd(_LCD_CLEAR); // Clear display
- Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
- //Lcd_Out(1, 1, 'Key :'); // Write message text on LCD
- //Lcd_Out(2, 1, 'Times:');
- //delay_ms(1000);
- //Lcd_Init(); // Initialize LCD
- //Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
- //Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
- LCD_Out(1,1,'Date:'); // Prepare and output static text on LCD
- LCD_Chr(1,8,'-');
- //LCD_Chr(1,11,'-');
- LCD_Out(2,1,'Time:');
- LCD_Chr(2,8,':');
- LCD_Chr(2,11,':');
- LCD_Out(1,12,'V');
-
- Soft_I2C_Init(); // Initialize Soft I2C communication
- end;
- //----------------- Main procedure
- begin
- Init_Main(); // Perform initialization
- while TRUE do // Endless loop
- begin
- Read_Time(); // Read time from RTC(PCF8583)
- Transform_Time(); // Format date and time
- adc_rd := ADC_Read(0); // get ADC value from 2nd channel
- Display_Time(); // Prepare and display on LCD
- if (seconds = 0) then
- EEPROM_Write(minutes,adc_rd/4); // Write some data at address
- delay_ms(1000);
- end;
- end.
復制代碼
51hei.png (4.63 KB, 下載次數: 86)
下載附件
2020-4-30 04:06 上傳
所有資料51hei提供下載:
MikroPascalAVR.zip
(28.8 KB, 下載次數: 50)
2020-4-30 02:42 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|