找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2281|回復(fù): 2
收起左側(cè)

為什么仿真出來阻值一直是0000.00k,單片機(jī)程序哪里有問題啊?

[復(fù)制鏈接]
ID:828701 發(fā)表于 2021-6-10 19:53 | 顯示全部樓層 |閱讀模式
仿真圖.png
/************************************
*********STC89C51**********
**********12MHz晶振********/

#include "reg51.h"
#include<stdio.h>
#include<intrins.h>
#define uint8 unsigned char
#define uint16 unsigned int
#define uchar unsigned char
#define uint unsigned int


uint16 cnt2ms;        //10ms計(jì)數(shù)器
uint8 cnt1s;        //1秒計(jì)數(shù)器

//電阻--------------------------------
#define        Z1 13700053.00
#define        Z2 3685279.00


float RZ=0;
unsigned long   RX=0;
unsigned long   RX2=0;
unsigned long   RX3=0;
unsigned long   RX4=0;
unsigned long   RX5=0;
unsigned long   RX6=0;
unsigned long   RX7=0;
unsigned long   RX8=0;
unsigned long   RX9=0;



//電感--------------------------------
float LZ=0;
#define        C1 0.0261

//電容--------------------------------
float CZ=0;
float CX=0;
#define        R1 22274

uint8 KL=0;        //
unsigned int Pulsecnt=0;        //脈沖個(gè)數(shù)
unsigned int Pulsenum;        //脈沖個(gè)數(shù)
uint8 start=0;        //
uint16 PulseTime;        //10ms計(jì)數(shù)器
uint16 Pulse;        //10ms計(jì)數(shù)器
bit flag=0;
uint8 CH=0;        //
unsigned long x;
void Init_Extint(void);
void TimerInitProc();

void Init_System(void);
void Display(uint8 chose_dat, uint8 dat);
void ShowDisp(uint8 tPulsenum, uint8 tPulsecnt, uint8 tcnt1s);
void DelayMs(uint8 Ms);
uint8 Pulse_FLAG=0;
uint8 Pulse_5=0;
/********************************按鍵*****************************************/
sbit    K1=P1^3;
sbit    K2=P1^4;
sbit    K3=P1^5;

sbit    A0=P2^1;
sbit    A1=P2^0;

sbit    RED=P3^6;
sbit    YEW=P3^4;
sbit    GRE=P3^3;
/********************************LCD液晶**************************************/
#define LCD_Data P0           //LCD的數(shù)據(jù)口
sbit    LCD_BF=LCD_Data^7;    //LCD忙信號(hào)位
sbit    LCD_RS=P1^0;
sbit    LCD_RW=P1^1;
sbit    LCD_EN=P1^2;



#define LCD_GO_HOME               0x02         //AC=0,光標(biāo)、畫面回HOME位

//輸入方式設(shè)置
#define LCD_AC_AUTO_INCREMENT     0x06         //數(shù)據(jù)讀、寫操作后,AC自動(dòng)增一
#define LCD_AC_AUTO_DECREASE      0x04         //數(shù)據(jù)讀、寫操作后,AC自動(dòng)減一
#define LCD_MOVE_ENABLE           0x05         //數(shù)據(jù)讀、寫操作,畫面平移
#define LCD_MOVE_DISENABLE        0x04         //數(shù)據(jù)讀、寫操作,畫面不動(dòng)

//設(shè)置顯示、光標(biāo)及閃爍開、關(guān)
#define LCD_DISPLAY_ON            0x0C         //顯示開
#define LCD_DISPLAY_OFF           0x08         //顯示關(guān)
#define LCD_CURSOR_ON             0x0A         //光標(biāo)顯示
#define LCD_CURSOR_OFF            0x08         //光標(biāo)不顯示
#define LCD_CURSOR_BLINK_ON       0x09         //光標(biāo)閃爍
#define LCD_CURSOR_BLINK_OFF      0x08         //光標(biāo)不閃爍

//工作方式設(shè)置
#define LCD_DISPLAY_DOUBLE_LINE   0x38         //兩行顯示
#define LCD_DISPLAY_SINGLE_LINE   0x30         //單行顯示
/*定義子程序*/
void LCD_ClrAll(void);              //清屏
void Judge_LCD_busy(void);           //檢測(cè)是否忙碌
void LCD_Write(uchar WriteData);     //寫控制字
void LCD_write_data(uchar LCD_data); //寫數(shù)據(jù)顯示
void LCD_cursor(uchar x);    //光標(biāo)起始地址
void LCD_printc(unsigned char lcd_data) ;  //輸出一個(gè)字符
void LCD_prints(unsigned char *lcd_string);//輸出字符串



/*LCD1602忙碌判斷子程序*/
void Judge_LCD_busy(void)   //判斷LCD1602是否忙狀態(tài)
{
while(1)
  {
   LCD_EN=0;
   LCD_RS=0;
   LCD_RW=1;
   LCD_Data=0xff;
   LCD_EN=1;   //EN 是 1—0 使能
   if(!LCD_BF)break; //LCD_BF=1表示忙碌,需要等待。
  }
   LCD_EN=0;
}
/******LCD清屏***************/
void LCD_ClrAll(void)
{

Judge_LCD_busy();   //判斷是否忙碌
LCD_RS=0;
LCD_RW=0;
LCD_Data=0x01;
LCD_EN=1;
LCD_EN=0;

}
/*******LCD寫數(shù)據(jù)定義各種模式*********/
void LCD_Write(uchar WriteData)   //寫指令到LCD
{
Judge_LCD_busy();
LCD_RS=0;
LCD_RW=0;

P0=WriteData; //把WriteData的數(shù)據(jù)送到數(shù)據(jù)口
LCD_EN=1;
LCD_EN=0;
}
/********LCD顯示數(shù)據(jù)***********/
void LCD_write_data(uchar LCD_data)      //輸出一個(gè)字節(jié)數(shù)據(jù)到LCD
{
Judge_LCD_busy();
LCD_RS=1;
LCD_RW=0;

P0=LCD_data;
LCD_EN=1;
LCD_EN=0;
}
/****光標(biāo)位置的確定***/
void LCD_cursor(uchar x)            //LCD光標(biāo)定位到x處
{
LCD_Write(0x80+x);                       //第一行地址是0x80

}
/*輸出一個(gè)字符*/
void LCD_printc(unsigned char lcd_data)
{
LCD_write_data(lcd_data);
}

/*輸出字符串*/
void LCD_prints(unsigned char *lcd_string)
{
unsigned char i=0;
while(lcd_string[ i]!=0x00)
  {
   LCD_write_data(lcd_string[ i]);
   i++;
  }
}

/*初始化程序*/
void LCD_initial(void)                        //初始化LCD
{
LCD_Write(LCD_AC_AUTO_INCREMENT|LCD_MOVE_DISENABLE);
LCD_Write(LCD_DISPLAY_ON|LCD_CURSOR_OFF);
LCD_Write(LCD_DISPLAY_DOUBLE_LINE);
LCD_ClrAll();

}

/****延時(shí)******/
void delay_1ms(uint x)
{
uint j;
uchar i;
for(j=0;j<x;j++)
{
for(i=0;i<120;i++);
}
}
void Key_SM(void){

        if(K3==0){
             delay_1ms(5);
            if(K3==0){
                YEW=1;RED=0;
                GRE=1;                  
                LCD_cursor(0x00);
                LCD_prints("   MEASURE Lx   ");  
                //----電感檔-------
                A0=0;
                A1=1;
                CH=2;//電感檔

                while(K3==0){}
                     
                 }
            }

        if(K2==0){
             delay_1ms(5);
            if(K2==0){
                GRE=1;YEW=0;
                RED=1;                  
                LCD_cursor(0x00);            
                LCD_prints("   MEASURE Rx   ");  
               //----電阻檔-------
                A0=1;
                A1=0;
                CH=1;//電阻檔     
                 
                while(K2==0){}
                     }

                }
       if(K1==0){
             delay_1ms(5);
            if(K1==0){
               YEW=1;GRE=0;RED=1;
               LCD_cursor(0x00);
               LCD_prints("   MEASURE Cx   ");  
               //----電容檔-------
                A0=0;
                A1=0;
                CH=3;//電容擋                     
               while(K1==0){}
                       }

                }


}

void  Value_to_ASCII(unsigned long value,uchar add)
{
    unsigned char temp[] = "0000.000K ";
    temp[0] = value/1000000 + 0x30;//數(shù)值改成字符,液晶顯示需要ASCII碼
    value = value%1000000;
    temp[1] = value/100000 + 0x30;
    value = value%100000;
    temp[2] = value/10000 + 0x30;
    value = value%10000;
    temp[3] = value/1000 + 0x30;
    value = value%1000;
    temp[5] = value/100 + 0x30;
    value = value%100;
    temp[6] = value/10 + 0x30;
    value = value%10;
    temp[7] = value + 0x30;  
   
    LCD_cursor(add);   //光標(biāo)起始地址 ,第一行地址是0x00~0x0F,第二行地址是0x40~0x4f
    LCD_prints(temp);
}

void  Value_to_ASCIIL(unsigned long value,uchar add)
{
    unsigned char temp[] = "000000uH ";
    temp[0] = value/100000 + 0x30;
    value = value%100000;
    temp[1] = value/10000 + 0x30;
    value = value%10000;
    temp[2] = value/1000 + 0x30;
    value = value%1000;
    temp[3] = value/100 + 0x30;
    value = value%100;
    temp[4] = value/10 + 0x30;
    value = value%10;
    temp[5] = value + 0x30;  
   
    LCD_cursor(add);   //光標(biāo)起始地址 ,第一行地址是0x00~0x0F,第二行地址是0x40~0x4f
    LCD_prints(temp);
}
void  Value_to_ASCIIC(unsigned long value,uchar add)
{
    unsigned char temp[] = "00000.0pF ";
    temp[0] = value/100000 + 0x30;
    value = value%100000;
    temp[1] = value/10000 + 0x30;
    value = value%10000;
    temp[2] = value/1000 + 0x30;
    value = value%1000;
    temp[3] = value/100 + 0x30;
    value = value%100;
    temp[4] = value/10 + 0x30;
    value = value%10;
    temp[6] = value + 0x30;  
   
    LCD_cursor(add);   //光標(biāo)起始地址 ,第一行地址是0x00~0x0F,第二行地址是0x40~0x4f
    LCD_prints(temp);
}
//--------------------主程序--------------------------------
void main()
{
   
      TMOD=0x51;                 //設(shè)置定時(shí)器0,方式1:16位定時(shí)器
      TH1=0;
      TL1=0;
      TH0=(65536-2000)/256; //設(shè)定定時(shí)周期
      TL0=(65536-2000)%256;
      TR0=1;
      TR1=1;
      ET1=1;
      ET0=1;
      EA=1;
//----電阻檔-------
      A0=1;
      A1=0;
    LCD_initial();
          delay_1ms(50);
    LCD_initial();
          delay_1ms(50);
    LCD_cursor(0x00);
               
    LCD_prints("   MEASURE Rx   ");
    GRE=1;
        YEW=0;
          RED=1;
    LCD_cursor(0x45);
    CH=1;
   
        while(1)
        {

     Key_SM();
    if(flag==1){

        flag=0;

        x=Pulsenum*65536+TH1*256+TL1;//計(jì)算1s內(nèi)的脈沖個(gè)數(shù),頻率=脈沖個(gè)數(shù)
if(CH==1){

        RZ=(Z1/x);//測(cè)量100到1000
        RX=RZ-1000;
        RX=RX>>1;

if(RX>=1005){
        RX=RX-1005;
    }else{RX=0;}

    if(x==0){RX=0;RX2=0;RX3=0;RX4=0;RX5=0;RX6=0;RX7=0;RX8=0;RX9=0;}
  if(RX>1000000){RX=1000000; }
            
              RX2=(RX+RX3+RX4+RX5+RX6+RX7+RX8+RX9)>>3;//求平均提高精度

RX9=RX8;RX8=RX7;RX7=RX6;RX6=RX5;RX5=RX4;RX4=RX3;RX3=RX;
              //保存上一次的阻值
            
         Value_to_ASCII(RX2,0x44);//更新阻值顯示



}
if(CH==2){

      
        LZ=(x*39*C1)/1000000;
        LZ=LZ*x;
        LZ=1000000/LZ;
    if(x==1)LZ=0;
  if(LZ>999999)LZ=999999; //限制最高值
         Value_to_ASCIIL(LZ,0x44);//更新電感值顯示
}
if(CH==3){

       CZ=6600000/x;
    if(x==1||x>30500)CZ=0;
  if(CZ>999999)CZ=999999;  //限制最高值
         Value_to_ASCIIC(CZ,0x44);//更新電容值顯示
}
         cnt2ms=0;
         Pulsenum=0;
         TH1=0;
         TL1=0;
         TR1=1;
        
    }
        
        }
}
/*******************************************************************************
* 函 數(shù) 名: Exti0_interrupt
* 函數(shù)功能: /T0引腳下降沿進(jìn)入中斷
* 入口參數(shù): 無
* 返    回: 無
*******************************************************************************/
void T1_interrupt(void) interrupt 3 using 0
{
   
  Pulsenum++;        //保存測(cè)得脈搏值  
        
}
/********************************************************************
函數(shù)功能:定時(shí)器/計(jì)數(shù)器1中斷處理
入口參數(shù):null
返    回:null
備    注:null
********************************************************************/
void Timer0IntProc() interrupt 1 using 0 //2ms
{
  TH0=(65536-2000)/256;
  TL0=(65536-2000)%256;
    cnt2ms++;

        if(cnt2ms==497)        //1s計(jì)時(shí)到
        {
        
        TR1=0;
        
        
                cnt2ms = 0;
            
        flag=1;
        }

}








#ifndef __INTRINS_H__
#define __INTRINS_H__

extern void          _nop_     (void);
extern bit           _testbit_ (bit);
extern unsigned char _cror_    (unsigned char, unsigned char);
extern unsigned int  _iror_    (unsigned int,  unsigned char);
extern unsigned long _lror_    (unsigned long, unsigned char);
extern unsigned char _crol_    (unsigned char, unsigned char);
extern unsigned int  _irol_    (unsigned int,  unsigned char);
extern unsigned long _lrol_    (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
extern void          _push_    (unsigned char _sfr);
extern void          _pop_     (unsigned char _sfr);
#endif








#ifndef __STDIO_H__
#define __STDIO_H__
#ifndef EOF
#define EOF -1
#endif
#ifndef NULL
#define NULL ((void *) 0)
#endif
#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif
#pragma SAVE
#pragma REGPARMS
extern char _getkey (void);
extern char getchar (void);
extern char ungetchar (char);
extern char putchar (char);
extern int printf   (const char *, ...);
extern int sprintf  (char *, const char *, ...);
extern int vprintf  (const char *, char *);
extern int vsprintf (char *, const char *, char *);
extern char *gets (char *, int n);
extern int scanf (const char *, ...);
extern int sscanf (char *, const char *, ...);
extern int puts (const char *);
#pragma RESTORE
#endif




#ifndef __INTRINS_H__
#define __INTRINS_H__
extern void          _nop_     (void);
extern bit           _testbit_ (bit);
extern unsigned char _cror_    (unsigned char, unsigned char);
extern unsigned int  _iror_    (unsigned int,  unsigned char);
extern unsigned long _lror_    (unsigned long, unsigned char);
extern unsigned char _crol_    (unsigned char, unsigned char);
extern unsigned int  _irol_    (unsigned int,  unsigned char);
extern unsigned long _lrol_    (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
extern void          _push_    (unsigned char _sfr);
extern void          _pop_     (unsigned char _sfr);
#endif
回復(fù)

使用道具 舉報(bào)

ID:824490 發(fā)表于 2021-6-11 14:49 | 顯示全部樓層
單片機(jī)程序有沒有問題不知道,沒有人會(huì)仔細(xì)地去看這么長(zhǎng)篇大論的代碼!
先說說你這個(gè)項(xiàng)目的需求?再簡(jiǎn)述一下電路的組成,分析一下工作原理,這樣大家才能更好地幫到你。
如同上醫(yī)院,一進(jìn)門就喊:肚子疼!然后就要找醫(yī)生拿藥吃?
回復(fù)

使用道具 舉報(bào)

ID:123289 發(fā)表于 2021-6-11 13:21 | 顯示全部樓層
先說說你的測(cè)量原理是什么?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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