#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”
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
// 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;
}
一周热门 更多>