⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chuan.c

📁 PIC16F877A RTC1307 LCD 16x2 keypad 4x4 and a windows application connect with PIC on RS232
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <16F877A.h>
#fuses NOLVP, NOWDT, XT
#use delay(clock=4000000)

#use i2c(master,Slow,SDA=PIN_C4,SCL=PIN_C3)
#use rs232(baud=2400, xmit=PIN_C6, rcv=PIN_C7,parity=N,bits=8)

#include <LCD_VED.c>
#include <KBD_VED.c>
#include <TIME_VED.h>

#define   ADDRTC   0xD0
#define ACK 0
#define NACK 1
#include <stdlib.h>
#define _0 0
#define _1 1
#define _21 2
#define _22 3
#define _end_eeprom 0xFD //vi FC,FD,FE,FF
#define _monday 1
#define _tuesday 2
#define _wednesday 3
#define _thursday 4
#define _friday 5
#define _saturday 6
#define _sunday 7


byte min_temp;
//str_day_time temp;
#org 0x1000
void display_bcd(byte param);
#org 0x1800
void day_of_week(struct_date_time &temp);
#org 0x0800
void delete_time_eeprom(byte addr_del);
#org 0x1800
void cap22_xuat_menu_hen_gio();//menu hen gio
#org 0x1800
void cap21_xuat_menu_chinh_gio();//menu chinh gio

//#org 0x1800
#org 0x1000
void write_time_eeprom(byte addr_wr, struct_time_eeprom m_time, int1 m_weekly, byte c_input)
{
   byte temp;

   temp = m_time.h_hour;
   temp = temp<<4;
   temp = temp + m_time.l_hour;
   write_eeprom(addr_wr, temp);

   temp = m_time.h_minute;
   temp = temp<<4;
   temp = temp + m_time.l_minute;
   write_eeprom(addr_wr + 1, temp);
   
   if ( m_weekly == 1 )
   {
      c_input = c_input | 0x10;
   }
   //nguoc lai thi thoi
   write_eeprom(addr_wr + 2, c_input);
}

#org 0x0800
void init_time(struct_date_time param)
{
   I2C_start(); // The following Enables the Oscillator /
   I2C_write(0xD0); // address the part to write /
   I2C_write(0x00); // position the address pointer to 0 /
   I2C_write(0); // write 0 to the seconds register, clear the CH bit /
   I2C_write(param.minutes);
   I2C_write(param.hours);
   I2C_write(param.day); // Thoi gian ban dau la: 3/11/2005 - 12h/30ph/00sec
   I2C_write(param.date);
   I2C_write(param.month);
   I2C_write(param.year);
   I2C_write(0x10);// enable sqwe, 1Hz output /
   I2C_stop();
}

#org 0x1800
void lcd_blink_on(byte x,byte y)
{
   lcd_gotoxy(x,y);
   lcd_send_byte(0,0x0D);
}

#org 0x800
void lcd_blink_off(byte x,byte y)
{
   lcd_gotoxy(x,y);
   lcd_send_byte(0,0x0C);
}

#org 0x0800
void read_day_lcd(byte &day,byte &mon,byte &year)
{
      int8 count=4;
      char c;
      byte temp;
         lcd_gotoxy(count,1);
         //read day
         c = lcd_getc(count,1); count++;
         temp= c- 0x30;//lay so hang chuc
         day = temp<<4;
         c = lcd_getc(count,1); count++;
         temp = c- 0x30;
         day = temp|day;
         count++;
         //read mon
         c = lcd_getc(count,1); count++;
         temp= c - 0x30;//lay so hang chuc
         mon = temp<<4;
         c = lcd_getc(count,1); count++;
         temp = c- 0x30;
         mon = temp|mon;
         count++;
         //read year
         count = count +2;
         c = lcd_getc(count,1); count++;
         temp= c- 0x30;//lay so hang chuc
         year = temp<<4;
         c = lcd_getc(count,1); count++;
         temp = c- 0x30;
         year = temp|year;
}

#org 0x1800
struct_date_time day_input(byte &cancel)
{
   struct_date_time temp;
   char c;
   int1 check = 1;
   int8 count = 4;
   byte d,m,y;
   lcd_blink_on(count,1);
   while (check)
   {
      c = kbd_getc();
      if (c != 0)//a key is pressed {
          if((c!='*')&&(c!='#'))
          {
            count++;
            lcd_gotoxy(count-1,1);
            lcd_putc(c);
            lcd_blink_on(count,1);
            if((count==6)||(count==9))
            {
               count++;
               lcd_blink_on(count,1);
            }
            if(count ==10)
            {  count = count+2;
               lcd_blink_on(count,1);
            }
            if(count==14){
                count = 4;
                lcd_blink_on(count,1);
            }
          }
          else if(c=='*')
          {
            if(count==4){
                check = 0;
                cancel = 1;
            }
            else{
               count = 4;
               lcd_blink_on(count,1);
            }
         }
         else
         {
            check = 0;
            cancel = 0;
         }
      }//end while
      lcd_blink_off(count,1);
      read_day_lcd(d,m,y);
      temp.date = d;
      temp.month = m;
      temp.year = y;
   return temp;
}

#org 0x1800
void read_hours_lcd(byte &hours,byte &min, int8 count){
      char c;
      byte temp;
         c = lcd_getc(count,1);
         count++;
         temp= c- 0x30;//lay so hang chuc
         hours = temp<<4;
         c = lcd_getc(count,1);
         temp = c - 0x30;
         hours = temp|hours;
         count = count + 2;//dich qua dau :
         //read minutes
         c = lcd_getc(count,1);
         count++;
         temp= c- 0x30;//lay so hang chuc
         min = temp<<4;
         c = lcd_getc(count,1);
         count++;
         temp = c- 0x30;
         min = temp|min;
}
#org 0x1800
int1 check_hours(struct_date_time param)
{
   byte h,m;
   h = param.hours;
   m = param.minutes;
   if(h > 0x23)
      return 0;
   if(m > 0x59)
      return 0;
   return 1;
}
#org 0x1800
void update_time(struct_date_time param) {
   I2C_start(); // The following Enables the Oscillator /
   I2C_write(0xD0); // address the part to write /
   I2C_write(0x01); // position the address pointer to 0 /
   I2C_write(param.minutes);
   I2C_write(param.hours);
}
#org 0x1000
struct_date_time hours_input(byte &cancel){
     struct_date_time temp;
    char c, pre_c;
    int8 count=6;
    byte h,m;
    byte m_vitri_nhap = 1;
    lcd_blink_on(count,1);
     while (true){
      c = kbd_getc();
      if (c != 0)//a key is pressed
      {
         if((c!='*')&&(c!='#'))
         {
            if(m_vitri_nhap == 1)
            {
               if(0x29<c && c<0x33)
               {
                  pre_c = c;
                  lcd_putc(c);
                  m_vitri_nhap = 2;
               }
            }else if(m_vitri_nhap == 2)
            {
               if(pre_c == '2')
               {
                  if(0x29<c && c <0x34)
                  {
                     lcd_putc(c);
                     m_vitri_nhap = 3;
                     lcd_gotoxy(9,1);
                  }
               }else
               {
                  lcd_putc(c);
                  m_vitri_nhap = 3;
                  lcd_gotoxy(9,1);
            }

            }else if(m_vitri_nhap == 3)
            {
               if((0x29 < c)&&(c < 0x36))
               {
                  lcd_putc(c);
                  m_vitri_nhap = 4;
               }
            }else if(m_vitri_nhap == 4)
            {
               lcd_putc(c);
               m_vitri_nhap = 1;
               lcd_gotoxy(6,1);
            }
         }else if(c=='*')
         {
            if(count==6)
            {
               cancel =1;
               break;
             }
            else
            {
               count = 6;
               lcd_blink_on(count,1);
            }
         }else//neu  la #
         {
                cancel = 0;
                break;
          }
      }//end if
   }//end while
   lcd_blink_off(count,1);
   read_hours_lcd(h,m,6);
   temp.hours = h;
   temp.minutes = m;

   return temp;
}

#org 0x1000
void write_day(struct_date_time param) {
   day_of_week(param);
   I2C_start(); // The following Enables the Oscillator /
   I2C_write(0xD0); // address the part to write /
   
   I2C_write(0x03); // position the address pointer to 4 /

   I2C_write(param.day);
   I2C_write(param.date);
   I2C_write(param.month);
   I2C_write(param.year);
   I2C_stop();
}
#org 0x0800
void display_bcd(byte param){
   byte hight,low;
   hight = param&0xF0;
   hight = hight>>4;
   low =   param&0x0F;
   lcd_putc(hight+0x30);
   lcd_putc(low+0x30);
}
#org 0x1800
struct_date_time read_time(){
   struct_date_time temp;
   byte Sec, Min, Hrs, Day, Date, Mon, Yr;
   i2c_start();
   i2c_write(0xD0);   //Gui dia chi cua slave
   i2c_write(0x00);  //thiet lap lai con tro - set register pointer
   I2C_start();
   I2C_write(ADDRTC|1); // gui lenh doc du lieu
   sec = i2c_read(1)&0x7F; /* starts w/last address stored in register pointer */
   min = i2c_read(1);
   hrs = i2c_read(1) & 0x3F;
   day = i2c_read(1);
   date = i2c_read(1);
   mon = i2c_read(1) & 0x7F;
   yr = i2c_read(0);
   i2c_stop();
   temp.seconds=sec;
   temp.minutes =min;
   temp.hours=hrs;
   temp.day=day;
   temp.date=date;
   temp.month=mon;
   temp.year=yr;
   return temp;
}
#org 0x1800
int1 check_date(struct_date_time param){
   int8 div;
   byte date,Mon,yr;
   int Years;
   date = param.date;
   Mon = param.month;
   Yr =(param.year >>4)*10 +((param.year<<4)>>4) ;
   Years=2000+yr;
   if( (Mon==0x00)||(date==0x00) )return 0;
   if(Mon>0x12)return 0;

   if( (Mon==0x01)||(Mon==0x03)||(Mon==0x05)||(Mon==0x07)||(Mon==0x08)||(Mon==0x10)||(Mon==0x12) ){
      if(date>0x31)return 0;
   }
   if (Mon==0x02){
      if(date>0x29) return 0;
      else if(date==0x29){
         div = Years%100;
         if( (div==0)&&(Years%400 ==0) ) return 1;
         if( (div!=0)&&(Years%4 ==0) ) return 1;
         return 0;
      }
     }
    if((Mon==0x04)||(Mon==0x06)||(Mon==0x09)||(Mon==0x11)) {
     if(date > 0x30) return 0;
    }

   return 1;

}
#org 0x0800
int1 update_date(byte &cancel){
   ///DS1307?????A乬???乬???//
         int1 result;
         struct_date_time temp;
         temp=read_time();
    /// LCD??????乬????\????///
         lcd_putc("\f");
         lcd_gotoxy(1,0);
         lcd_putc("*Esc       #Save");

         lcd_gotoxy(4,1);
         display_bcd(temp.date);
         lcd_putc("/");
         display_bcd(temp.month);
         lcd_putc("/20");
         display_bcd(temp.year);
   ////////////////////////////
        temp = day_input(cancel);
        result = check_date(temp);
        if((result==0)||(cancel)) return 0;
   ///DS1307???乪??////
         write_day(temp);
         return 1;

}
#org 0x0800
int1 edit_time(byte &cancel){
   ///DS1307?????A乬???乬???//
         int1 result;
         struct_date_time temp;
         temp = read_time();
    /// LCD??????乬????\????///
         lcd_putc("\f");

         lcd_gotoxy(1,0);
         lcd_putc("*Esc       #Save");


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -