时间的掌控

原理图

源代码

/* Main.c file generated by New Project wizard
 *
 * Created:   周日 2月 16 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>
void Delay(unsigned i)
{
   while(i!=0)
      i--;
}

int main (void)
 { 
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL |=(1<<6);
   LPC_GPIO2->DIR=0xFF;
   //LPC_GPIO3->DIR=0x01;
   LPC_GPIO3->DIR=0x03;
   LPC_GPIO3->DATA=0x00;
    while (1)
   {
      /*
      LPC_GPIO2->DATA=0x40; Delay(80000);
      LPC_GPIO2->DATA=0xF9; Delay(80000);
      LPC_GPIO2->DATA=0x24; Delay(80000);
      LPC_GPIO2->DATA=0x30; Delay(80000);
      LPC_GPIO2->DATA=0x19; Delay(80000);
      
      LPC_GPIO2->DATA=0x12; Delay(80000);
      LPC_GPIO2->DATA=0x02; Delay(80000);
      LPC_GPIO2->DATA=0x78; Delay(80000);
      LPC_GPIO2->DATA=0x00; Delay(80000);
      LPC_GPIO2->DATA=0x10; Delay(80000);
      */
      LPC_GPIO3->DATA=0x01;
      LPC_GPIO2->DATA=0x40;Delay(80000);
      LPC_GPIO3->DATA=0x02;
      LPC_GPIO2->DATA=0xF9;Delay(80000);
    }
   return 0;
 }   

会出现0 1交替闪动

但是当把Delay之中的值减少时,如减为8000,那么会出现数字错位现象,本不会出现1的地方出现了数字1;来不出现0的地方出现了数字0,这是由于时间太短,晶体管没有反应过来导致的

这怎么办呢?

这就需要使用定时器了

/* Main.c file generated by New Project wizard
 *
 * Created:   周日 2月 16 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>



void T16BO_init(void)
{
   LPC_SYSCON->SYSAHBCLKCTRL |=(1<<7);
   LPC_TMR16BO->IR=0x01; //MRO中断复位,请中断
   LPC_TMR16BO->MCR=0x04;//MRO中断产生时停止TC和PC,并停止定时器工作
}

void T16BO_delay_ms(uint16_t ms)
{
   LPC_TMR16BO->TCR=0x02; //复位定时器(bit1,写1复位)
   LPC_TMR16BO->PR=SystemCoreClock/1000-1; //1ms TC +1
   LPC_TMR16BO->MRO=ms; //MRO为16位寄存器,值不要超过65535
   LPC_TMR16BO->TCR=0x01;//启动定时器,TCR[0]=1
   while(LPC_TMR16BO->TCR);
}
int main (void)
 { 
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL |=(1<<6);
   LPC_GPIO2->DIR=0xFF;
   //LPC_GPIO3->DIR=0x01;
   LPC_GPIO3->DIR=0x03;
   //LPC_GPIO3->DATA=0x00;
   T16BO_init();
    while (1)
   {
      /*
      LPC_GPIO2->DATA=0x40; Delay(80000);
      LPC_GPIO2->DATA=0xF9; Delay(80000);
      LPC_GPIO2->DATA=0x24; Delay(80000);
      LPC_GPIO2->DATA=0x30; Delay(80000);
      LPC_GPIO2->DATA=0x19; Delay(80000);
      
      LPC_GPIO2->DATA=0x12; Delay(80000);
      LPC_GPIO2->DATA=0x02; Delay(80000);
      LPC_GPIO2->DATA=0x78; Delay(80000);
      LPC_GPIO2->DATA=0x00; Delay(80000);
      LPC_GPIO2->DATA=0x10; Delay(80000);
      */
      
      LPC_GPIO3->DATA=0x01;
      LPC_GPIO2->DATA=0x40;//Delay(8000);
      T16BO_delay_ms(10);
    
      LPC_GPIO3->DATA=0x02;
      LPC_GPIO2->DATA=0xF9;//Delay(8000);
      T16BO_delay_ms(10);
    }
   return 0;
 }   

但是编译保错

../main.c: In function 'T16BO_init':
../main.c:15:4: error: 'LPC_TMR16BO' undeclared (first use in this function)
../main.c:15:4: note: each undeclared identifier is reported only once for each function it appears in
../main.c: In function 'T16BO_delay_ms':
../main.c:21:4: error: 'LPC_TMR16BO' undeclared (first use in this function)
make: *** [main.o] Error 1

 此问题先放着,待问老师后再作思量

每个硬件对“时间”的理解,是来自时钟周期的。调节时钟周期,即使是同样的硬件、软件,对时间的度量也会发生改变

原文地址:https://www.cnblogs.com/caishunzhe/p/12315858.html