立即注册 登录
返回首页

uid:271566的个人空间

日志

关于CC2430一个简易的秒表功能实现

已有 318 次阅读2018-1-4 19:38

#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';
}

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

返回顶部