当前位置: 首页> 汽车> 车展 > 51单片机第16步_点灯

51单片机第16步_点灯

时间:2025/7/8 22:31:44来源:https://blog.csdn.net/weixin_42550185/article/details/140077158 浏览次数: 0次

本章重点学习软件延时和普通IO口输出。

1、delay.c如下:

#include <intrins.h>  //包含头文件intrins.h,要放在stdio.h的头文件之前;

//使能函数如下:

//1 _nop_(); 相当于汇编的NOP指令;

//2 bit  _testbit_( bit bit_value ); 对bit_value进行测试,若bit_value=1,返回1,否则返回0;

//3 _cror_( unsigned char x, unsigned char n ); 将字节型变量x的值,向右循环移动n位,然后将其值返回;

//相当于汇编的RR A命令;

//4 _iror_( unsigned int x,  unsigned char n ); 将双字节型变量x的值,向右循环移动n位,然后将其值返回;

//5 _lror_( unsigned long x, unsigned char n ); 将4字节型变量x的值,向右循环移动n位,然后将其值返回;

//6 _crol_( unsigned char x, unsigned char n ); 将字节型变量x的值,向左循环移动n位,然后将其值返回;

//7 _irol_( unsigned int x,  unsigned char n ); 将双字节型变量x的值,向左循环移动n位,然后将其值返回;

//8 _lrol_( unsigned long x, unsigned char n ); 将4字节型变量x的值,向左循环移动n位,然后将其值返回;

//以上的循环左移和循环右移,同C语言的左移和右移是不同的,使用时要小心

///函数声明//

void delay_x_10us(unsigned char x);  //延时x个10us@12MHz,0<x<256;

void delay_ms(unsigned int x); //延时x毫秒@12MHz,0<x<1024;

void delay_s( unsigned char x ); //延时x秒@12MHz,0<x<256;

//函数功能:延时x个10us@12MHz,0<x<256;

void delay_x_10us(unsigned char x)

{ while(--x)

    { _nop_();_nop_();_nop_();_nop_();}

  _nop_();

}

//函数功能:延时x毫秒@12MHz,0<x<1024,当x>=1024,delay_ms(x)延时存在误差;

void delay_ms( unsigned int x )

{ unsigned char i;

  i=x>>8;

  while(--x)

    { delay_x_10us(99);

}

  if(i==0)     {delay_x_10us(97);_nop_();_nop_();_nop_();_nop_();_nop_();}

  else if(i==1){delay_x_10us(97);_nop_();_nop_();_nop_();_nop_();}

  else if(i==2){delay_x_10us(97);_nop_();_nop_();_nop_();}

  else         {delay_x_10us(97);_nop_();_nop_();}

}

//函数功能:延时x秒@12MHz,0<x<256

void delay_s( unsigned char x )

{ while(--x)

    { delay_ms(999);

  delay_x_10us(99);

  _nop_();_nop_();_nop_();_nop_();

}

  delay_ms(999);

  delay_x_10us(98);

  _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();

}

2、main.c如下:

#include <REG51.h>  //包含头文件REG51.h,使能51内部寄存器;

#include <delay.h>

sbit  LED = P1^0;

void main(void)

{ delay_1_cycle(); //延时1us@12MHz;

  delay_2_cycle(); //延时2us@12MHz;

  delay_3_cycle(); //延时3us@12MHz;

  delay_4_cycle(); //延时4us@12MHz;

  delay_5_cycle(); //延时5us@12MHz;

  delay_6_cycle(); //延时6us@12MHz;

  delay_7_cycle(); //延时7us@12MHz;

  delay_8_cycle(); //延时8us@12MHz;

  delay_9_cycle(); //延时9us@12MHz;

  delay_x_10us(1);  //延时10us@12MHz;

  delay_ms(1);      //延时1毫秒@12MHz;

  delay_s(1);       //延时1秒@12MHz;

  

  for(;;)

    { LED=0;

  delay_ms(500);  //延时500ms@12MHz;

  LED=1;

  delay_ms(500);  //延时500ms@12MHz;

}

}

关键字:51单片机第16步_点灯

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: