找回密码
 立即注册

QQ登录

只需一步,快速开始

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

ATmega16读取RTC和ADC显示在LCD1602,同时串口输出,并写入EEPROM Proteus仿真程序

[复制链接]
跳转到指定楼层
楼主
介绍mikroPascal for AVR 写的ATmega16读取RTC和ADC显示在LCD1602,同时串口输出,并写入EEPROM的程序。附带仿真文件。mikro编译器分为c,basic,pascal三种,除了语法有所区别外,使用和功能基本一样。有8051,AVR,PIC,ARM等版本,界面和语法都一样。学会一种芯片花很少时间就可以转到另一种芯片。自带常用库,帮助文件中例子很多,基本不需要其他文档就可以开始学习了。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. program RTC_Read;

  2. var seconds, minutes, hours, day, month, year : byte;    // Global date/time variables
  3. var adc_rd : word;

  4. // Software I2C connections
  5.    var Soft_I2C_Scl_Output    : sbit at PORTC0_bit;
  6.        Soft_I2C_Sda_Output    : sbit at PORTC1_bit;
  7.        Soft_I2C_Scl_Input     : sbit at PINC0_bit;
  8.        Soft_I2C_Sda_Input     : sbit at PINC1_bit;
  9.        Soft_I2C_Scl_Direction : sbit at DDC0_bit;
  10.        Soft_I2C_Sda_Direction : sbit at DDC1_bit;
  11. // End Software I2C connections

  12. // LCD pinout definition
  13. var LCD_RS : sbit at PORTD2_bit;
  14. var LCD_EN : sbit at PORTD3_bit;
  15.     LCD_D4 : sbit at PORTD4_bit;
  16.     LCD_D5 : sbit at PORTD5_bit;
  17.     LCD_D6 : sbit at PORTD6_bit;
  18.     LCD_D7 : sbit at PORTD7_bit;

  19. var LCD_RS_Direction : sbit at DDD2_bit;
  20.     LCD_EN_Direction : sbit at DDD3_bit;
  21.     LCD_D4_Direction : sbit at DDD4_bit;
  22.     LCD_D5_Direction : sbit at DDD5_bit;
  23.     LCD_D6_Direction : sbit at DDD6_bit;
  24.     LCD_D7_Direction : sbit at DDD7_bit;
  25. // end LCD pinout definitions



  26. //--------------------- Reads time and date information from RTC (PCF8583)
  27. procedure Read_Time();
  28.   begin
  29.     Soft_I2C_Start();               // Issue start signal
  30.     Soft_I2C_Write(0xA0);           // Address PCF8583, see PCF8583 datasheet
  31.     Soft_I2C_Write(2);              // Start from address 2
  32.     Soft_I2C_Start();               // Issue repeated start signal
  33.     Soft_I2C_Write(0xA1);           // Address PCF8583 for reading R/W=1
  34.     seconds := Soft_I2C_Read(1);    // Read seconds byte
  35.     minutes := Soft_I2C_Read(1);    // Read minutes byte
  36.     hours := Soft_I2C_Read(1);      // Read hours byte
  37.     day := Soft_I2C_Read(1);        // Read year/day byte
  38.     month := Soft_I2C_Read(0);      // Read weekday/month byte

  39.     Soft_I2C_Stop();                // Issue stop signal
  40.   end;

  41. //-------------------- Formats date and time
  42. procedure Transform_Time() ;
  43.   begin
  44.     seconds  :=  ((seconds and 0xF0) shr 4)*10 + (seconds and 0x0F);  // Transform seconds
  45.     minutes  :=  ((minutes and 0xF0) shr 4)*10 + (minutes and 0x0F);  // Transform months
  46.     hours    :=  ((hours and 0xF0)  shr 4)*10  + (hours and 0x0F);    // Transform hours
  47.     year     :=   (day and 0xC0) shr 6;                               // Transform year
  48.     day      :=  ((day and 0x30) shr 4)*10    + (day and 0x0F);       // Transform day
  49.     month    :=  ((month and 0x10)  shr 4)*10 + (month and 0x0F);     // Transform month
  50.   end;

  51. //-------------------- Output values to LCD
  52. procedure Display_Time();
  53.   begin
  54.      Lcd_Chr(1, 9, (day / 10)   + 48);    // Print tens digit of day variable
  55.      Lcd_Chr(1, 10, (day mod 10)   + 48);  // Print oness digit of day variable
  56.      Lcd_Chr(1, 6, (month / 10) + 48);
  57.      Lcd_Chr(1,7, (month mod 10) + 48);
  58.      //Lcd_Chr(1,15, (year mod 10)  + 48);    // Print year vaiable
  59.      
  60.      UART1_Write_Text('Time: ');
  61.      Lcd_Chr(2, 6, (hours / 10)   + 48);
  62.      Lcd_Chr(2, 7, (hours mod 10)   + 48);
  63.      UART1_Write(hours/10+48); UART1_Write(hours mod 10 + 48); UART1_Write(':');
  64.      Lcd_Chr(2, 9, (minutes / 10) + 48);
  65.      Lcd_Chr(2,10, (minutes mod 10) + 48);
  66.      UART1_Write(minutes/10+48); UART1_Write(minutes mod 10 + 48); UART1_Write(':');
  67.      Lcd_Chr(2,12, (seconds / 10) + 48);
  68.      Lcd_Chr(2,13, (seconds mod 10) + 48);
  69.      UART1_Write(seconds/10+48); UART1_Write(seconds mod 10 + 48); UART1_Write(' ');
  70.      //UART1_Write(13);UART1_Write(10);
  71.      
  72.      Lcd_Chr(1,13,(adc_rd/1000) + 48);
  73.      Lcd_Chr(1,14,((adc_rd mod 1000)/100) + 48);
  74.      Lcd_Chr(1,15,(adc_rd mod 100)/10 + 48);
  75.      Lcd_Chr(1,16,(adc_rd mod 10) + 48);
  76.      UART1_Write_Text('Volt: ');
  77.      UART1_Write((adc_rd/1000) + 48);
  78.      UART1_Write((adc_rd mod 1000)/100 + 48);
  79.      UART1_Write((adc_rd mod 100)/10 + 48);
  80.      UART1_Write((adc_rd mod 10) + 48);
  81.      UART1_Write(13);UART1_Write(10);
  82.   end;

  83. //------------------ Performs project-wide init
  84. procedure Init_Main();
  85.   begin
  86.     UART1_Init(2400);                         // Initialize UART module at 9600 bps
  87.     Delay_ms(100);                            // Wait for UART module to stabilize
  88.     UART1_Write_Text('Start');
  89.     UART1_Write(13);UART1_Write(10);

  90.     Lcd_Init();                              // Initialize LCD
  91.     Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  92.     Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  93.     //Lcd_Out(1, 1, 'Key  :');                 // Write message text on LCD
  94.     //Lcd_Out(2, 1, 'Times:');
  95.     //delay_ms(1000);

  96.     //Lcd_Init();                // Initialize LCD
  97.     //Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
  98.     //Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off

  99.     LCD_Out(1,1,'Date:');      // Prepare and output static text on LCD
  100.     LCD_Chr(1,8,'-');
  101.     //LCD_Chr(1,11,'-');
  102.     LCD_Out(2,1,'Time:');
  103.     LCD_Chr(2,8,':');
  104.     LCD_Chr(2,11,':');
  105.     LCD_Out(1,12,'V');
  106.    
  107.     Soft_I2C_Init();           // Initialize Soft I2C communication
  108.   end;

  109. //----------------- Main procedure
  110.   begin

  111.    Init_Main();                // Perform initialization

  112.     while TRUE do              // Endless loop
  113.       begin
  114.         Read_Time();           // Read time from RTC(PCF8583)
  115.         Transform_Time();      // Format date and time
  116.         adc_rd := ADC_Read(0);  // get ADC value from 2nd channel
  117.         Display_Time();        // Prepare and display on LCD
  118.         if (seconds = 0) then
  119.             EEPROM_Write(minutes,adc_rd/4);   // Write some data at address

  120.         delay_ms(1000);
  121.       end;
  122.   end.
复制代码

所有资料51hei提供下载:
MikroPascalAVR.zip (28.8 KB, 下载次数: 50)


评分

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

查看全部评分

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

使用道具 举报

沙发
ID:623452 发表于 2020-6-26 21:13 | 只看该作者
这个源码的工程文件要如何打开啊?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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