找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3042|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

求助!1602顯示不了速度,有沒有大佬幫忙看看。

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
這是主程序,按鍵程序和1602顯示程序應(yīng)該都沒有錯(cuò)誤。有proteus仿真,1602速度顯示一直是0000
#include<reg51.h>
#include "key.h"
#include "1602.c"
uint i,z,cou,zhuan,msec;     //定義參數(shù)
unsigned char count=0;                  
/*-------------------外部中斷1計(jì)數(shù)程序-------------------*/
void counter(void) interrupt 2
{   

        cou++;  
        if(cou==30)      //30次循環(huán)為發(fā)動(dòng)機(jī)轉(zhuǎn)一圈  
        {   
                cou=0;     //初始化計(jì)數(shù)   
             z++;      //轉(zhuǎn)圈計(jì)數(shù)加1
          }   


}
/*-------------------------------------------------------*/
/*-----------------內(nèi)部中斷計(jì)時(shí)計(jì)數(shù)程序-----------------*/
void Timer_0(void) interrupt 3
{  
   TH1=0x3c;      //50ms定時(shí)  
   TL1=0xb0;  
   msec++;  
   if(msec==20)      //50*20=1S  
   {   
        msec=0;
        zhuan=60*z;        //每分鐘轉(zhuǎn)速        
        z=0;

   }
}
/*-------------------------------------------------------*/

void Timer_Init()
{
  TMOD=0X11;      //T0定時(shí)方式1
        TH1=0X3c;
  TL1=0Xb0;
        IT1= 1;
  EX1= 1;
  TH0=0Xfc;
  TL0=0X66;       //計(jì)數(shù)初值設(shè)置為1ms
  ET0=1;          //打開定時(shí)器0的中斷
  TR0=1;          //打開定時(shí)器0
  EA=1;           //開總中斷
}
void main()
{
  in1=0;
  in2=1;
  ena=1;
  Timer_Init();
        InitLcd1602();
  while(1)
  {
    key();
                LcdShowStr(0, 0, "Speed=    r/min");
                LcdShowDat(6, 0,4, zhuan);
  }
}
void Timer() interrupt 1      //特別注意此處,0--外部中斷0,1--定時(shí)器中斷0,2--外部中斷1,3--定時(shí)器中斷1,4--串行口中斷1
{
  TR0=0;
  TH0=0Xfc;
  TL0=0X66;       //重新賦計(jì)數(shù)初值為1ms
  if(count<=PWM_Count)
  {
    ena=1;
  }
  else
  {
    ena=0;
  }
  count++;
  if(count>=100)
  {
    count=0;
  }
  TR0=1;
}




1602顯示程序
#define uchar unsigned char
#define uint unsigned int
#define LCD1602_DB  P0//數(shù)據(jù)端,可改為任意Px口
void LcdWriteCmd(uchar cmd);
/*   1602控制端,根據(jù)硬件自改IO口  */
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E  = P2^2;



/* 延時(shí)函數(shù) x ms */
void delay(uint x)
{
                unsigned int i,j;
                for(i=x;i>0;i--)
                for(j=110;j>0;j--);
}


/*  Lcd1602初始化 */
void InitLcd1602()
{
        LcdWriteCmd(0x38);
        LcdWriteCmd(0x0C);
        LcdWriteCmd(0x06);
        LcdWriteCmd(0x01);//清屏指令
}

/*  LCD等待函數(shù) */
void LcdWaitReady()
{
    uchar sta;

    LCD1602_DB = 0xFF;
    LCD1602_RS = 0;
    LCD1602_RW = 1;
    do {
        LCD1602_E = 1;
        sta = LCD1602_DB;
        LCD1602_E = 0;
    } while (sta & 0x80);
}

/*  LCD寫命令函數(shù) */
void LcdWriteCmd(uchar cmd)
{
    LcdWaitReady();
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}

/*  LCD寫數(shù)據(jù)函數(shù) */
void LcdWriteDat(uint dat)
{
    LcdWaitReady();
    LCD1602_RS = 1;
    LCD1602_RW = 0;
    LCD1602_DB = dat;
    LCD1602_E  = 1;
    LCD1602_E  = 0;
}

/*  LCD數(shù)據(jù)位置函數(shù) */
void LcdSetCursor(uchar x, uchar y)
{
    uchar addr;
    if (y == 0)  
    addr = 0x00 + x;
    else
    addr = 0x40 + x;  
    LcdWriteCmd(addr | 0x80);  
}

/*

LCDx寫字符函數(shù)
變量:x, y :(x,y)為第一個(gè)字符坐標(biāo)
例:第2排第3個(gè)字符之后顯示lcd
                LcdShowStr(1,2,"lcd");
*/
void LcdShowStr(uchar x, uchar y, uchar *str)
{
    LcdSetCursor(x, y);   
    while (*str != '\0')
    {
        LcdWriteDat(*str++);
    }
}

/*
LCD寫數(shù)字函數(shù)
變量:x, y :(x,y)為第一個(gè)字符坐標(biāo)
      z:顯示數(shù)字位數(shù)
例:第2排第3個(gè)字符之后顯示1666只顯示2位
                LcdShowStr(1,2,1666,2);
*/



void LcdShowDat(uchar x, uchar y,uchar z, uint dat)
{
        LcdSetCursor(x, y);
if(z==4)
{  LcdWriteDat(0x30+dat/1000);
        LcdWriteDat(0x30+dat/100%10);
        LcdWriteDat(0x30+dat/10%10);
        LcdWriteDat(0x30+dat%10);}       
if(z==3)
{  
        LcdWriteDat(0x30+dat/100);
        LcdWriteDat(0x30+dat/10%10);
        LcdWriteDat(0x30+dat%10);}
if(z==2){
        LcdWriteDat(0x30+dat/10%10);
        LcdWriteDat(0x30+dat%10);}
}


按鍵程序
#ifndef key_h
#define key_h
sbit key_1=P1^0;      //順時(shí)針轉(zhuǎn)動(dòng)
sbit key_2=P1^1;      //逆時(shí)針轉(zhuǎn)動(dòng)
sbit key_3=P1^2;      // 加速轉(zhuǎn)動(dòng)
sbit key_4=P1^3;      //減速轉(zhuǎn)動(dòng)
sbit key_5=P1^4;      //停止轉(zhuǎn)動(dòng)
unsigned char PWM_Count=20;   //占空比控制字
unsigned char n=5;   //速度增減量5
sbit in1=P2^7;
sbit in2=P2^6;
sbit ena=P2^5;
void delays()
{
  unsigned char i;
  for(i=80;i>0;i--);
}
void key()
{
  P1=0XFF;
  if(key_1==0)
  {
    delays();
    if(key_1==0)
    {
      in1=0;
      in2=1;
    }                           //順時(shí)針轉(zhuǎn)動(dòng)
    while(!key_1);
  }

    if(key_2==0)
  {
    delays();
    if(key_2==0)
    {
      in1=1;
      in2=0;
    }
    while(!key_2);
  }                            //逆時(shí)針轉(zhuǎn)動(dòng)

    if(key_3==0)
  {
    delays();
    if(key_3==0)
    {
      PWM_Count=PWM_Count+n;
      if(PWM_Count>=100)
        PWM_Count=100;
    }
    while(!key_3);
  }                           //加速轉(zhuǎn)動(dòng) ,如果大于等于100就全速轉(zhuǎn)動(dòng)

    if(key_4==0)
  {
    delays();
    if(key_4==0)
    {
     if(PWM_Count>10)
     {
       PWM_Count=PWM_Count-n;
     }
      else
      PWM_Count=5;           //減速轉(zhuǎn)動(dòng),最低轉(zhuǎn)速為5
    }
    while(!key_4);
  }

    if(key_5==0)
  {
    delays();
    if(key_5==0)
    {
      in1=0;
      in2=0;
    }
    while(!key_5);
  }                              //電機(jī)停止轉(zhuǎn)動(dòng)
}


QQ截圖20190428095407.png (39.46 KB, 下載次數(shù): 79)

速度顯示一直是四個(gè)零

速度顯示一直是四個(gè)零
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表