搜索
bottom↓
回复: 2

求高手解决问题

[复制链接]

出0入0汤圆

发表于 2012-10-20 14:26:58 | 显示全部楼层 |阅读模式

#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
uchar  Count;
//bit Cut_int_=1;

int Pulsecount=0;
int temp,weigh=0;
sbit DIR=P3^1;
sbit SCL=P3^6;
sbit SDA=P3^7;

unsigned char const dofly[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};// 显示段码值01234567
unsigned char  display[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char const seg[]={0x01,0x02,0x04,0x08};//分别对应相应的数码管点亮
/*********************************************************
延时函数
*********************************************************/
void Delay(uint num)//延时函数
{
  while( --num );
}
/********************************************************
显示函数
*********************************************************/
  void play(void)
  {
   int i=0;
   for(i=0;i<4;i++)
          { P1=dofly[display[i]];//取显示数据
           P2=seg[i];  //取段码
          
           Delay(50); //扫描间隙延时
                }
                i=0;
  }
void iic_start(void)
{
  SDA = 1;                                                                       
  _nop_();
  SCL = 1;
  somenop;
  SDA = 0;
  somenop;
  SCL = 0;
}

void iic_stop(void)
{
  SDA = 0;
  _nop_();
  SCL = 1;
  somenop;
  SDA = 1;
}

void iic_ack(bit ackbit)
{
  if(ackbit)
        SDA = 0;
  else
        SDA = 1;
  somenop;
  SCL = 1;
  somenop;
  SCL = 0;
  SDA = 1;
  somenop;
}

bit iic_waitack(void)
{
  SDA = 1;
  somenop;
  SCL = 1;
  somenop;
  if(SDA)   
  {   
        SCL = 0;
        iic_stop();
        return 0;
  }
  else  
  {
        SCL = 0;
        return 1;
  }
}

void iic_sendbyte(unsigned char byt)
{
  unsigned char i;
  for(i=0;i<8;i++)
  {   
          if(byt&0x80)
                SDA=1;
        else
                SDA=0;
        somenop;
        SCL=1;
        byt<<=1;
        somenop;
        SCL=0;
  }
}
                               
unsigned char iic_recbyte(void)
{
  unsigned char dat;
  unsigned char i;                         
  for(i=0;i<8;i++)
  {   
        SCL=1;
        somenop;
        dat<<=1;
        if(SDA)
           dat|=0x01;
        SCL=0;
        somenop;
  }
  return dat;
}

void wrbyte_24c02(unsigned char add,unsigned int dat)
{
  // Device Address 1100 000 R/W
  iic_start();
  iic_sendbyte(0xa0);
  iic_waitack();
  iic_sendbyte(add);
  iic_waitack();
  iic_sendbyte(dat);
  iic_waitack();
  iic_stop();
  Delay(10);
}

unsigned char rdbyte_24c02(unsigned char add)
{
  // Device Address 1100 000 R/W
  unsigned char da;
  iic_start();
  iic_sendbyte(0xa0);
  iic_waitack();
  iic_sendbyte(add);
  iic_waitack();
  iic_start();
  iic_sendbyte(0xa1);
  iic_waitack();
  da = iic_recbyte();
  iic_ack(0);
  iic_stop();
  return da;
}       

/********************************************************
主函数
*********************************************************/
int main()
{
   P0=0xff;
   P2=0xff;
   P1=0xff;
   P3=0;
   Count=0x00;
   TMOD=0x01;
   TH0=0x4c; TL0=0x00;      //50ms定时
   IT0=1; IT1=1;         //下降沿触发   
   IP=0x05;
   EA=1; EX0=1;EX1=1;ET0=1; TR0=1;          //  

   while(1)  
   play();
   return 0;
}
void  counter(void) interrupt 0 using 1
{
          //Pulsecount=rdbyte_24c02(0x01);
        if(DIR)  
                {Pulsecount--;}
          else
                {
        /*        if(Pulsecount==0)
                Pulsecount=0;
                else */
                Pulsecount++;
                }
                wrbyte_24c02(0x01,Pulsecount);
                Delay(1000);       
}        

void  counter1(void) interrupt 2 using 3
{        EX0=0;
        //Pulsecount=rdbyte_24c02(0x01);
           if(DIR)  
                {
        /*        if(Pulsecount==0)
                Pulsecount=0;
                else */
                Pulsecount++;
                }
          else
                {Pulsecount--;}
                wrbyte_24c02(0x01,Pulsecount);
               
        EX0=1;       
}

/*********************************************************
  Time0中断函数
**********************************************************/
void Time0(void) interrupt 1 using 2
{
   TH0=0x4c;               //50ms定时
   TL0=0x00;
   Count++;
   if(Count==2)
   {
    Count=0;
        Pulsecount=rdbyte_24c02(0x01);

    weigh=Pulsecount*0.25/2;       
       
        display[0] = weigh/1000;
        display[1] = weigh%1000%100/10;
        display[2] = weigh%1000/100;
        display[3] = weigh%1000%100%10;

   }
}  
/*********************************************************/
程序实现功能:拉绳编码器测距离,并带掉电保存功能
出现的问题是,weight一直不会变一直为“0”

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2012-10-20 20:25:37 | 显示全部楼层
I2C。。。

出0入0汤圆

发表于 2012-10-20 22:30:07 | 显示全部楼层
unsigned char rdbyte_24c02(unsigned char add)
{
  // Device Address 1100 000 R/W
  unsigned char da;
  iic_start();
  iic_sendbyte(0xa0);
  iic_waitack();
  iic_sendbyte(add);
  iic_waitack();
//  iic_stop(); ?
  iic_start();
  iic_sendbyte(0xa1);
  iic_waitack();
  da = iic_recbyte();
  iic_ack(0);
  iic_stop();
  return da;
}   
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 20:24

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表