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

📄 lcd1602._c

📁 用AVR及1602LCD在PROTEUS环境下仿真
💻 _C
字号:
//ICC-AVR application builder : 2007-4-21 20:12:02
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>
#include "lcd1602.h"

/*unsigned char TempBuffer[10];
void IntToStr(unsigned int t, unsigned char *str, unsigned char n) 
{
	unsigned char a[5]; 
	char i, j;                                 	
	a[0]=(t/10000)%10;         //取得整数值到数组         	
	a[1]=(t/1000)%10;                                     	
	a[2]=(t/100)%10;                                      	
	a[3]=(t/10)%10;                                       	
	a[4]=(t/1)%10;                                        	
                                                      
	for(i=0; i<5; i++)         //转成ASCII码              	
		a[i]=a[i]+'0';                                    	
	for(i=0; a[i]=='0' && i<=3; i++);                     	
	for(j=5-n; j<i; j++)       //填充空格                 	
		{ *str='a';  str++; }                             	
	for(; i<5; i++)                                       	
		{ *str=a[i]; str++; }  //加入有效的数字           	
	*str='\0'; 
} */

unsigned char second[2],minute[2],hour[2];
unsigned char Year[4],Month[2],Day[2];
unsigned char sec=0,min=5,hou=18;
unsigned int year=2007;
unsigned char month=4,day=23;
unsigned char flash_flag=0,time_set=0;

 
void IntToStr(unsigned char t, unsigned char *str) 
{
	unsigned char a[2]; 
	char i;                                 	
	                                     	
	a[0]=(t/10)%10;   //取得整数值到数组                                      	
	a[1]=(t/1)%10;                                       	
	for(i=0; i<2; i++)         //转成ASCII码              	
		a[i]=a[i]+'0'; 
    for(i=0;i<2;i++)
	{
	 *str=a[i];
	 str++;
	} 
	*str='\0';
}
//**************************************************************
void IntToStr1(unsigned int t, unsigned char *str) 
{
	unsigned char a[4]; 
	char i, j;                                 	
	a[0]=(t/1000)%10;         //取得整数值到数组         	
	a[1]=(t/100)%10;                                     	
	a[2]=(t/10)%10;                                      	
	a[3]=(t/1)%10;                              	                                      	
	for(i=0; i<4; i++)         //转成ASCII码              	
		a[i]=a[i]+'0';
	for(i=0;i<4;i++)
	{
	 *str=a[i];
	 str++;
	} 
	*str='\0';
}
		  
//主函数
void main(void)
{
 /*unsigned char sec=0,min=5,hou=18;
 unsigned int year=2007;
 unsigned char month=4,day=23;*/
 
 init_devices();
 lcd_init();
 GotoXY(0,0);
 Print("Date:");
 GotoXY(0,1);
 Print("Time:           ");
 //********************************************************
  GotoXY(13,0);//显示':'
  Print(":");
  GotoXY(10,0);
  Print(":");
 //循环函数********************************************************
 while(1)
 {
 //********************************************************
  WDR();  
  IntToStr(sec,&second[0]);//把两位的秒位转换成两个一位的数字才能显示;
  GotoXY(12,1);
  if(time_set!=1)
  {   
   Print(&second[0]);
  }
  else if(time_set==1)//当时间设定选定秒位时,秒位开始闪烁;
  {
   if((flash_flag)%2==0)
   {
    Print(&second[0]);
   }
   else
   {
    Print("  ");
   }
  } 
  //********************************************************  
  if((flash_flag%2)==0)
  {
   GotoXY(11,1);//显示':'
   Print(":");
   GotoXY(8,1);
   Print(":");
  }
  else
  {
   GotoXY(11,1);//显示' ';实现':'闪烁效果
   Print(" ");
   GotoXY(8,1);
   Print(" ");
  } 
  //********************************************************  
  IntToStr(min,&minute[0]);//转换分位,显示
  GotoXY(9,1);
  if(time_set!=2)//按下时间设定键,第2下为分设置
  {   
   Print(&minute[0]);
  }
  else if(time_set==2)
  {
   if((flash_flag)%2==0)
   {
    Print(&minute[0]);
   }
   else
   {
    Print("  ");
   }
  }  
  //********************************************************  
  IntToStr(hou,&hour[0]);//转换小时位,显示
  GotoXY(6,1);
  if(time_set!=3)//按下时间设定键,第3下为小时设置
  {   
   Print(&hour[0]);
  }
  else if(time_set==3)
  {
   if((flash_flag)%2==0)
   {
    Print(&hour[0]);
   }
   else
   {
    Print("  ");
   }
  }  
  //********************************************************
  IntToStr(day,&Day[0]);//转换日位,显示
  GotoXY(14,0);
  if(time_set!=4)//按下时间设定键,第4下为日设置
  {   
   Print(&Day[0]);
  }
  else if(time_set==4)
  {
   if((flash_flag)%2==0)
   {
    Print(&Day[0]);
   }
   else
   {
    Print("  ");
   }
  }  
  
  //********************************************************
  IntToStr(month,&Month[0]);//转换月位,显示
  GotoXY(11,0);
  if(time_set!=5)//按下时间设定键,第5下为月设置
  {   
   Print(&Month[0]);
  }
  else if(time_set==5)
  {
   if((flash_flag)%2==0)
   {
    Print(&Month[0]);
   }
   else
   {
    Print("  ");
   }
  }  
  //********************************************************
  IntToStr1(year,&Year[0]);//转换年位,显示
  GotoXY(6,0);
  if(time_set!=6)//按下时间设定键,第6下为年设置
  {   
   Print(&Year[0]);
  }
  else if(time_set==6)
  {
   if((flash_flag)%2==0)
   {
    Print(&Year[0]);
   }
   else
   {
    Print("    ");
   }
  }  
  WDR();
 }
}

//******************************************************************
//每次延时500MS,经过两次延时后,秒位加1;
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
  WDR();
  flash_flag++;
  if((flash_flag%2)==0)//判断是否是一秒
  {
   
   sec++;   
   if(sec==60)  //超过60秒,分加1;秒清0
   {
    min++;
    sec=0;   
   }
   if(min==60)            //超过60分,小时位进1,
   {
    hou++;
    min=0;   
   }
   if(hou==24)  	  		 //小时位超过24,则清0         
   {
    hou=0;
	day++;
   }
   if(((year%4==0)&&(year%100!=0))||(year%400==0))  	 //判断是否为闰年 1为闰年        
   {
    if((month==2)&&(day==30))//闰年2月份是29天
	{
	 month++;
	 day=0;	 	 
	}
	else if((month<=7)&&(month%2==0)&&(day==31))//其他月份小于等于7月份的偶数月份是30天
	{
	 month++;
	 day=0;	  
	}
	else if((month<=7)&&(month%2!=0)&&(day==32))//奇数是31天;
	{
	 month++;
	 day=0;
	}
	else if((month>7)&&(month%2==0)&&(day==32))//大于7月份的偶数月份是31天
	{
	 month++;
	 day=0;	  
	}
	else if((month>7)&&(month%2!=0)&&(day==31))//奇数是30天;
	{
	 month++;
	 day=0;
	}	
   }
   else if((month==2)&&(day==29))//不是闰年二月份是28天;
   {
    month++;
    day=0;
   }
   else if((month<=7)&&(month%2==0)&&(day==31))//其他月份小于等于7月份的偶数月份是30天
   {
    month++;
    day=0;	  
   }
   else if((month<=7)&&(month%2!=0)&&(day==32))//奇数是31天;
   {
    month++;
    day=0;
   }
   else if((month>7)&&(month%2==0)&&(day==32))//大于7月份的偶数月份是31天
   {
    month++;
    day=0;	  
   }
   else if((month>7)&&(month%2!=0)&&(day==31))//奇数是30天;
   {
    month++;
    day=0;
   }
   if(month==13)
   {
    year++;
    month=0;
   }
   
  }
  
 
 TCNT1H = 0xC2; //reload counter high value
 TCNT1L = 0xF7; //reload counter low value
 WDR();
}
//******************************************************************
//每按一次时间设定键,其值加1,在正常情况下为0,每加1代表要修改的位置变化一次;
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 //external interupt on INT0 
 WDR();
 GICR=0X40;     //清除INT0中断,延时20MS判断是否为干扰;
 delay_ms(20); 
 if((PIND&0x04)==0x00)
 {
  time_set++;         //不是干扰,加1
  if(time_set==7)//超过6次就返回正常状态;
  {
   time_set=0;
  }
 }
 while((PIND&0x04)==0x00)//判断按键是否释放;
 {
  WDR();      //一定要在判断按键释放循环中喂狗;
 }
 GICR=0XC0;//恢复中断INT0
}
//******************************************************************
//当time_set在某位时,单击此按键加一;
#pragma interrupt_handler int1_isr:3
void int1_isr(void)
{
 //external interupt on INT1
 WDR();
 GICR=0X80;
 delay_ms(20); 
 if((PIND&0x08)==0x00)
 {
  switch(time_set)//判断是哪个位要改变数值;
  {
   case 1: 
   {
    sec++;
    if(sec==60)
	{
	 sec=0;
	}
	break;//一定要用 break跳出;
   }
   case 2:
   {
    min++;
    if(min==60)            //超过60分,清0
    {
     min=0;       
    }
	break;
   }
   case 3:
   {
    hou++;
	if(hou==24)
	{
	 hou=0;
	}
	break;
   }
   case 4:
   {
    day++;
	if(((year%4==0)&&(year%100!=0))||(year%400==0))  	 //判断是否为闰年 1为闰年        
    {
     if((month==2)&&(day==30))//闰年2月份是29天
	 {
	 //month++;
	  day=0;	 	 
	 }
	 else if((month<=7)&&(month%2==0)&&(day==31))//其他月份小于等于7月份的偶数月份是30天
	 {
	 //month++;
	  day=0;	  
	 }
	 else if((month<=7)&&(month%2!=0)&&(day==32))//奇数是31天;
	 {
	 //month++;
	  day=0;
	 }
	 else if((month>7)&&(month%2==0)&&(day==32))//大于7月份的偶数月份是31天
	 {
	 //month++;
	  day=0;	  
	 }
	 else if((month>7)&&(month%2!=0)&&(day==31))//奇数是30天;
	 {
	 //month++;
	  day=0;
	 }	
    }
    else if((month==2)&&(day==29))//不是闰年二月份是28天;
    {
    //month++;
     day=0;
    }
    else if((month<=7)&&(month%2==0)&&(day==31))//其他月份小于等于7月份的偶数月份是30天
    {
   // month++;
     day=0;	  
    }
    else if((month<=7)&&(month%2!=0)&&(day==32))//奇数是31天;
    {
    //month++;
     day=0;
    }
    else if((month>7)&&(month%2==0)&&(day==32))//大于7月份的偶数月份是31天
    {
   // month++;
     day=0;	  
    }
    else if((month>7)&&(month%2!=0)&&(day==31))//奇数是30天;
    {
    //month++;
     day=0;
    }
	break;
   }
   case 5:
   {
    month++;
    if(month==13)
	{
	 month=0;
	}
	break;
   }
   case 6: year++;break;
  }
  
 }
 while((PIND&0x08)==0x00)
 {
  WDR();
 }
 GICR=0XC0;
 
}

⌨️ 快捷键说明

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