标题: 基于51单片机的直流电机测速代码 [打印本页]

作者: 单片机小白一个    时间: 2019-5-20 11:13
标题: 基于51单片机的直流电机测速代码
需要用到测速模块(槽型光耦和霍尔传感器模块都可以)
用到了外部中断0和定时器1,用两个定时器,一个定时,一个计数,应该也可以,没试过

部分代码如下:

/*测速部分变量定义*/
uchar a,b,val,zhuan,cishu,num,r=5/*cm*/,c;//r为码盘半径(cm)
uint lucheng,endprice;//   元/米
uchar tablev[]="V:   m/s";
uchar tablejuli[]="juli:";
uchar circle,juli;


void Dist_INT (void)           //  外部中断0初始化
{  
    EX0 = 1;         //允许外部中断0中断
    IT0 = 1;         //1:下降沿触发  0低电平触发
    EA = 1;            //中断总开关
}
void Dist() interrupt 0//中断函数
{
    cishu++;       //被遮挡次数
    if(cishu>=4)  //码盘4格
    {
        cishu=0;   
        circle++;  //圈数+1
    }   
}
void T0_time() interrupt 1
{
  TH0=0x3c;      //50ms定时  
  TL0=0xb0;
  num++;
  if(num==20) //如果到了20次,说明1秒时间
  {  
   num=0;
   zhuan=circle;
   circle=0;
//   lcd_wcom(0x92+0x40);
//   lcd_wdat(zhuan/100+0x30);
//   lcd_wdat(zhuan%100/10+0x30);
//   lcd_wdat(zhuan%100%10+0x30);
   lucheng=(uint)(2*3.14*r*zhuan/100+lucheng);//lucheng 单位为米
   }
}  

最重要的部分已经给出,剩下的显示和定时器1的配置自己写吧,这部分程序是从我的一个大程序中摘出来的,所以代码没有写全


作者: sunsaga1989    时间: 2019-5-22 20:30
感谢楼主的分享                           
作者: kanyingchang    时间: 2022-9-2 18:18
目前还是小白,先来点赞。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1