微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于ARM7 TDMI-S CPU的LPC2103的多功能电子钟

基于ARM7 TDMI-S CPU的LPC2103的多功能电子钟

时间:11-18 来源:互联网 点击:

UINT8 dis_bit=0; //显示切换标志位
UINT8 settime=0; //进入时间设置标志位
UINT8 alarm=0;//闹钟设置有效标志位
UINT8 al_shi1=0; // 以下是闹钟的序号和用数组储存的闹钟时、分位
UINT8 al_num=0;
UINT8 al_shi[4];
UINT8 al_fen[4];
UINT8 al_yunxu=0; //闹钟才开始比较,确保设置期间不比较
UINT8 LS=0; //闹钟时间到,铃声有效位,0为无效状态
UINT8 al_on3off[4]={0,0,0,0}; //用数组记录闹钟是否开启
UINT8 al_flag=0;//闹钟组数1.2.3,0表示没有闹钟被挂起
UINT8 al_fen1=0;
static UINT8 g_Dot = 0;
static UINT16 display = 0;//数码管显示缓存

UINT16 nian=2008;//与时间相关的变量
UINT8 yue=8;
UINT8ri=8;
UINT8week=5;
UINT8shi=20;
UINT8 fen=0;
UINT8miao;
//--------------------------------------------------------------------------------

void UART1_Init (void)
{
UINT16 Fdiv;

PINSEL0 = 0x00050000;
U1LCR = 0x83; // DLAB = 1,可设置波特率
Fdiv = (Fpclk / 16)/ UART_BPS; // 设置波特率
U1DLM = Fdiv / 256;
U1DLL = Fdiv % 256;
U1LCR = 0x03;
}

void GPIO_init(UINT8 GPIO_num) //初始为GPIO功能
{
if(GPIO_num < 16)
PINSEL0&= ~(0x03 < (GPIO_num * 2));
else
PINSEL1&= ~(0x03 < ((GPIO_num-16) * 2));
}

void GPIO_inout(UINT8 GPIO_num,UINT8 in_out) //设置GPIO为输入0或者输出1
{
if(in_out)
IODIR |= (0x01 < GPIO_num);//output 1
else
IODIR &= ~(0x01 < GPIO_num);//input 0
}

UINT8 GPIO_READ(UINT8 GPIO_num) //读出GPIO的状态
{
if(IOPIN & (1 < GPIO_num))
return 1;
else
return 0;
}

void GPIO_SET(UINT8 GPIO_num,UINT8 High_Low) //GPIO置1或者置0
{
if(High_Low)
IOSET |= (0x01 < GPIO_num);
else
IOCLR |= (0x01 < GPIO_num);
}
void LEDchange(UINT8 GPIO_num)//对二极管进行取反
{
if ((IO0PIN & (0x01 < GPIO_num)) == 0) {
IO0SET |= (0x01 < GPIO_num); // 点亮发光二极管
}
else IO0CLR |= (0x01 < GPIO_num);
}

UINT8 nian_deal()
{ if((nian%4==0&&nian%100!=0)||nian%400==0)
return 1;
else return 0;

}

UINT8 yue_deal()
{
switch(yue)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 2:
{
if(nian_deal())
return 29;
else return 28;
}
case 4:
case 6:
case 9:
case 11: return 31;
default: return 1;
}
}

void keysure()//按键的确认 ,并执行相关功能
{
if(GPIO_READ(14)==0)//对每一位进行切换
{
if(settime!=0)
{
switch(dis_bit)
{case 0:
if(settime==1)
{shi++;
if(shi>23)
shi=0;
}
else {fen++;
if(fen>59)
fen=0;

}
break;
case 1:
week++;
if(week>7)
week=1;
break;
case 2:
nian++;
break;
case 3:
if(settime==1)
{yue++;
if(yue>12)
yue=1;
}
else
{ri++;
if(ri>yue_deal())
ri=1;
}
break;
}

}
if(alarm!=0)
{if(alarm==1)
{
al_shi1++;
if(al_shi1>23)
al_shi1=0;
al_shi[al_num]=al_shi1;
}
else
{al_fen1++;
if(al_fen1>59)
al_fen1=0;
al_fen[al_num]=al_fen1;
}

}

}
if(GPIO_READ(15)==0)
{
if(settime!=0)
{
switch(dis_bit)
{case 0:
if(settime==1)
{shi--;
if(shi>23)
shi=23;
}
else {fen--;
if(fen>59)
fen=59;
}
break;
case 1:
week--;
if(week==0)
week=7;
break;
case 2:
nian--;
break;
case 3:
if(settime==1)
{yue--;
if(yue==0)
yue=12;
}
else
{ri--;
if(ri==0)
ri=yue_deal();
}
break;
}

}
if(alarm!=0)
{if(alarm==1)
{
al_shi1--;
if(al_shi1>23)
al_shi1=23;
al_shi[al_num]=al_shi1;
}
else
{al_fen1--;
if(al_fen1>59)
al_fen1=59;
al_fen[al_num]=al_fen1;
}

}
}
if(GPIO_READ(16)==0)
{

if(dis_bit==3)
dis_bit=0;
elsedis_bit++;

}
if(GPIO_READ(18)==0)
{if(settime>1)
{
settime=0;
T1TCR = 0x01;
GPIO_SET(23,1);
}
else {settime++;
T1TCR = 0x00;
GPIO_SET(23,0);}
}
if(GPIO_READ(2)==0)
{al_yunxu=0;
alarm=1;
al_num++;
if(al_num>3)
al_num=1;
//al_num++;
}
if(GPIO_READ(3)==0)
{
alarm++; //对闹钟的时分为进行设置切换
if(alarm>2)
alarm=1;
}
if(GPIO_READ(4)==0)
{
if(alarm!=0)
{alarm=0;
al_shi1=0;
al_yunxu=1;
al_flag=0;
for(al_num=0;al_num<4;al_num++)
{ if(al_on3off[al_num]==1)
al_flag++;
}
if(al_flag!=0)
GPIO_SET(24,0);
if(al_flag==0)
GPIO_SET(24,1);
}
else
{

LS=0;

T0TCR=0x00; //启动对响铃进行关闭
GPIO_SET(25,1);
GPIO_SET(11,1);
GPIO_SET(12,1);

}
}

if(GPIO_READ(27)==0)
{
if(alarm!=0)
{al_on3off[al_num]=1;

}
if(settime!=0)
nian=nian+10;

//表示有闹钟挂起,要加上一些标示位
}
if(GPIO_READ(6)==0)
{ if(alarm!=0)
{
al_on3off[al_num]=0;
}
if(settime!=0)
nian=nian-10;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top