#include "iocc2430.h"
#include "string.h"
#define LED3 P0_1
#define LED4 P1_4
#define LED5 P1_1
#define LED6 P1_0
void UartTX_Send_String(char *p);
void time2string(unsigned int value,char *result);
int counter;
int counter2;
char resultstr[6];
void Timer(void)
{
counter=10;
counter2=0;
UartTX_Send_String("\r\n進入秒表,按任意鍵開始計時\r\n");
P0DIR = P0DIR | 0x02;// 0000 0010設置p0_1引腳為輸出
P1DIR = P1DIR | 0x13;// 0001 0011設置p1_0、p1_1、p1_4、引腳為輸出
LED3=0;
LED4=0;
LED5=0;
LED6=0;//初始熄滅指示燈LED6
T1CTL = 0x0a; //0000 1010單片機時鐘頻率16MHz,32分頻后是500 000Hz;自動重裝模式(0->T1CC0中的計數值);
T1CC0L = 0x50;//計數次數50000 = 0xc350
T1CC0H = 0xc3;//計數次數50000 = 0xc350
//定時器中斷頻率是200 000/ 50 000 = 10Hz
while(URX0IF == 0);
URX0IF = 0; //清標識
T1IE = 1; //T1中斷使能
EA = 1; //系統中斷使能
UartTX_Send_String("\r\n計時中……\r\n");
while(URX0IF == 0);
URX0IF = 0; //清標識
EA = 0; //關閉系統中斷
T1IE = 0; //關閉T1中斷
time2string(counter2, resultstr);
UartTX_Send_String("\r\n本次計數值:");
UartTX_Send_String(resultstr);
UartTX_Send_String(" 秒\r\n");
UartTX_Send_String("\r\n退出秒表\r\n");
LED3=0;
LED4=0;
LED5=0;
LED6=0;
}
/*定時器每過0.1秒溢出一次*/
#pragma vector=T1_VECTOR
__interrupt void T1_ISR(void)
{
counter--;
if(counter%10==0)
{
counter2++;
if(counter2%1==0)
{
LED3=!LED3; //LED3亮滅切換
}
if(counter2%2==0)
{
LED4=!LED4; //LED4亮滅切換
}
if(counter2%4==0)
{
LED5=!LED5; //LED5亮滅切換
}
if(counter2%8==0)
{
LED6=!LED6; //LED6亮滅切換
}
}
}
void time2string(unsigned int value,char *result)
{
unsigned int i,j;
char temp[6];
i=0;
while(value!=0)
{
temp[i]='0'+(value%10);
value = value/10;
i++;
}
j=0;
while(i!=0)
{
result[j++]=temp[--i];
}
result[j]='\0';
}
|