標(biāo)題: 關(guān)于CC2430 用定時器中斷來實(shí)現(xiàn)秒表 [打印本頁]

作者: Ren_energy    時間: 2018-1-4 19:42
標(biāo)題: 關(guān)于CC2430 用定時器中斷來實(shí)現(xiàn)秒表
#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進(jìn)入秒表,按任意鍵開始計(jì)時\r\n");
  P0DIR =  P0DIR | 0x02;// 0000 0010設(shè)置p0_1引腳為輸出
  P1DIR =  P1DIR | 0x13;// 0001 0011設(shè)置p1_0、p1_1、p1_4、引腳為輸出

  LED3=0;  
  LED4=0;
  LED5=0;
  LED6=0;//初始熄滅指示燈LED6

  T1CTL = 0x0a;        //0000 1010單片機(jī)時鐘頻率16MHz,32分頻后是500 000Hz;自動重裝模式(0->T1CC0中的計(jì)數(shù)值);
  T1CC0L = 0x50;//計(jì)數(shù)次數(shù)50000 = 0xc350
  T1CC0H = 0xc3;//計(jì)數(shù)次數(shù)50000 = 0xc350
  //定時器中斷頻率是200 000/ 50 000 = 10Hz

  while(URX0IF == 0);
  URX0IF = 0; //清標(biāo)識
  T1IE = 1;   //T1中斷使能
  EA   = 1;   //系統(tǒng)中斷使能
  UartTX_Send_String("\r\n計(jì)時中……\r\n");

  while(URX0IF == 0);

  URX0IF = 0; //清標(biāo)識
  EA   = 0;   //關(guān)閉系統(tǒng)中斷
  T1IE = 0;   //關(guān)閉T1中斷

  time2string(counter2, resultstr);
  UartTX_Send_String("\r\n本次計(jì)數(shù)值:");
  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';
}






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1