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

📄 -ͦ

📁 手把手教你学AVR单片机C程序设计实验程序
💻
字号:
#include <iom16.h>
#include"RW_EEPROM.h"
#include<intrinsics.h>
#include "lcd1602_8bit.c"
#define GET_BIT(x,y) (x&(1<<y))
#define CPL_BIT(x,y) (x^=(1<<y))
#define SET_BIT(x,y) (x|=(1<<y))
#define CLR_BIT(x,y) (x&=~(1<<y))
#define S1 4
#define S2 5
#define S3 6
#define S4 7
#define uchar unsigned char	//变量类型的宏定义
#define uint  unsigned int	
uchar __flash title1[]={" This is a test "};
uchar __flash title2[]={"  About Timer   "};
uchar __flash title3[]={"    /  /        "};
uchar __flash title4[]={"  :  :      :   "};
uchar __flash title5[]={" Press S3      "};
uchar __flash title6[]={" Write set_time "};
uchar outflag,status;
struct time			
{
uint year;
uchar month,day;	
uchar hour,minute,second;			
};					
struct time run_time,set_time;	
/************************************************/
void port_init(void)		//端口初始化子函数
{							//端口初始化子函数开始
 PORTA = 0xFF;				// PA端口初始化输出11111111
 DDRA  = 0xFF;				//将PA端口设为输出
 PORTB = 0xFF;				// PB端口初始化输出11111111
 DDRB  = 0xFF;				//将PB端口设为输出
 PORTC = 0xFF; 				// PC端口初始化输出11111111
 DDRC  = 0xFF;				//将PC端口设为输出
 PORTD = 0xFF;				// PD端口初始化输出11111111
 DDRD  = 0x00;				//将PD端口设为输入
}							//端口初始化子函数结束
//***************************
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xE1; //setup
 TCNT1L = 0x7C;
 TCCR1A = 0x00;
 TCCR1B = 0x05; //start Timer
 TIMSK = 0x04; //timer interrupt sources
}
/*********************************************/
void init_devices(void)		//芯片的初始化子函数
{							
 port_init();				//调用端口初始化子函数
 timer1_init();				//调用定时器0初始化子函数
 SREG=0x80;					//使能总中断
}			
/************************************/
uchar conv(uint year,uchar month)	//101	
{uchar len;						//102
  switch(month)			//103
  {						//104
  case 1:len=31;break;		//105
  case 3:len=31;break;		//106
  case 5:len=31;break;		//107
  case 7:len=31;break;		//108
  case 8:len=31;break;		//109
  case 10:len=31;break;		//110
  case 12:len=31;break;		//111
  case 4:len=30;break;		//112
  case 6:len=30;break;		//113
  case 9:len=30;break;		//114
  case 11:len=30;break;		//115
  case 2:if(year%4==0&&year%100!=0||year%400==0)len=29;	//116
	else len=28;break;//117
  default:return 0;		//118
  }						//119
return len;				//120
}								//121

/************************************/
void scan_S4(void)
{
  if(GET_BIT(PIND,S4)==0)
  {status++;Delay_nms(250);}
}  
/***********************/
void main(void)			//定义主函数
{	 					
init_devices();			//调用芯片初始化子函数
Delay_nms(400);
InitLcd();
ePutstr(0,0,title1);
ePutstr(0,1,title2);
Delay_nms(2000);
SREG=0x00;
set_time.hour=READ_EEPROM(50);Delay_nms(10);
set_time.minute=READ_EEPROM(60);Delay_nms(10);
if(set_time.hour>24)set_time.hour=0;
if(set_time.minute>60)set_time.minute=0;
ePutstr(0,0,title3);
ePutstr(0,1,title4);
  while(1)				//无限循环
  {						
   switch(status)	
   {
   case 0:scan_S4();DisplayOneChar(15,0,status+0x30);
          SREG=0x80;
          if(GET_BIT(PIND,S3)==0)
          {CPL_BIT(outflag,0);}
          if(GET_BIT(outflag,0)==0)
          {
           DisplayOneChar(11,0,'O');Delay_nms(10);
           DisplayOneChar(12,0,'F');Delay_nms(10);
           DisplayOneChar(13,0,'F');Delay_nms(10);
          }
          else 
          {
           DisplayOneChar(11,0,' ');Delay_nms(10);
           DisplayOneChar(12,0,'O');Delay_nms(10);
           DisplayOneChar(13,0,'N');Delay_nms(10);
           if((run_time.hour==set_time.hour)&&(run_time.minute==set_time.minute))
            CLR_BIT(PORTB,7);
          }
          DisplayOneChar(0,0,run_time.year/1000+0x30);Delay_nms(10);
          DisplayOneChar(1,0,(run_time.year/100)%10+0x30);Delay_nms(10);
          DisplayOneChar(2,0,(run_time.year%100)/10+0x30);Delay_nms(10);
          DisplayOneChar(3,0,run_time.year%10+0x30);Delay_nms(10);
          DisplayOneChar(5,0,run_time.month/10+0x30);Delay_nms(10);
          DisplayOneChar(6,0,run_time.month%10+0x30);Delay_nms(10);
          DisplayOneChar(8,0,run_time.day/10+0x30);Delay_nms(10);
          DisplayOneChar(9,0,run_time.day%10+0x30);Delay_nms(10);
          DisplayOneChar(0,1,run_time.hour/10+0x30);Delay_nms(10);
          DisplayOneChar(1,1,run_time.hour%10+0x30);Delay_nms(10);
          DisplayOneChar(3,1,run_time.minute/10+0x30);Delay_nms(10);
          DisplayOneChar(4,1,run_time.minute%10+0x30);Delay_nms(10);
          DisplayOneChar(6,1,run_time.second/10+0x30);Delay_nms(10);
          DisplayOneChar(7,1,run_time.second%10+0x30);Delay_nms(10);
          DisplayOneChar(10,1,set_time.hour/10+0x30);Delay_nms(10);
          DisplayOneChar(11,1,set_time.hour%10+0x30);Delay_nms(10);
          DisplayOneChar(13,1,set_time.minute/10+0x30);Delay_nms(10);
          DisplayOneChar(14,1,set_time.minute%10+0x30);Delay_nms(10);
          break;
   case 1:scan_S4();SREG=0x00;
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {run_time.year=(run_time.year)+100;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {run_time.year=(run_time.year)-100;
          Delay_nms(250);}
          DisplayOneChar(0,0,run_time.year/1000+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(1,0,(run_time.year/100)%10+0x30);Delay_nms(10);
          break;
   case 2:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {run_time.year++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {run_time.year--;
          Delay_nms(250);}
          DisplayOneChar(2,0,(run_time.year%100)/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(3,0,run_time.year%10+0x30);Delay_nms(10);
          break;
   case 3:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {if(run_time.month<12)run_time.month++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {if(run_time.month>1)run_time.month--;
          Delay_nms(250);}
          DisplayOneChar(5,0,run_time.month/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(6,0,run_time.month%10+0x30);Delay_nms(10);
          break;
   case 4:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {if(run_time.day<31)run_time.day++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {if(run_time.day>1)run_time.day--;
          Delay_nms(250);}
          DisplayOneChar(8,0,run_time.day/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(9,0,run_time.day%10+0x30);Delay_nms(10);
          break;        
   case 5:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {if(run_time.hour<23)run_time.hour++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {if(run_time.hour>1)run_time.hour--;
          Delay_nms(250);}
          DisplayOneChar(0,1,run_time.hour/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(1,1,run_time.hour%10+0x30);Delay_nms(10);
          break;       
   case 6:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {if(run_time.minute<59)run_time.minute++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {if(run_time.minute>0)run_time.minute--;
          Delay_nms(250);}
          DisplayOneChar(3,1,run_time.minute/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(4,1,run_time.minute%10+0x30);Delay_nms(10);
          break; 
   case 7:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {if(set_time.hour<23)set_time.hour++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {if(set_time.hour>0)set_time.hour--;
          Delay_nms(250);}
          DisplayOneChar(10,1,set_time.hour/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(11,1,set_time.hour%10+0x30);Delay_nms(10);
          break; 
   case 8:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          LcdWriteCommand(0x0f,1); 
          if(GET_BIT(PIND,S1)==0)
          {if(set_time.minute<59)set_time.minute++;
          Delay_nms(250);}
          if(GET_BIT(PIND,S2)==0)
          {if(set_time.minute>0)set_time.minute--;
          Delay_nms(250);}
          DisplayOneChar(13,1,set_time.minute/10+0x30);Delay_nms(10);
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(14,1,set_time.minute%10+0x30);Delay_nms(10);
          break;
   case 9:scan_S4();
          LcdWriteCommand(0x0c,1); 
          DisplayOneChar(15,0,status+0x30);Delay_nms(10);
          ePutstr(0,0,title5);
          ePutstr(0,1,title6);
          if(GET_BIT(PIND,S3)==0)
          {
           WRITE_EEPROM(50,set_time.hour);Delay_nms(10);
           WRITE_EEPROM(60,set_time.minute);Delay_nms(10);
           DisplayOneChar(11,0,'O');Delay_nms(10);
           DisplayOneChar(12,0,'K');Delay_nms(10);
           Delay_nms(100);
          } 
          break;
   case 10:ePutstr(0,0,title3);
           ePutstr(0,1,title4);
           if(status>9)status=0;
           break;
   default:break;
   }
  }
}
/********************/
#pragma vector=TIMER1_OVF_vect
__interrupt void timer1_ovf_isr(void)
{
  uchar tempday;
 //TIMER1 has overflowed
 TCNT1H = 0xE1; //reload counter high value
 TCNT1L = 0x7C; //reload counter low value
 tempday=conv(run_time.year,run_time.month);	
 if(++run_time.second>59){run_time.minute++;run_time.second=0;}
 if(run_time.minute>59){run_time.hour++;run_time.minute=0;}
 if(run_time.hour>23){run_time.day++;run_time.hour=0;}
 if(run_time.day>tempday){run_time.month++;run_time.day=1;}
 if(run_time.month>12){run_time.year++;run_time.month=1;}
}

⌨️ 快捷键说明

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