Home > other >  # # 51 single chip microcomputer for help
# # 51 single chip microcomputer for help

Time:12-02

Just learning to 51 single chip microcomputer, is probably the I2C communication program to realize the function, PCF8591 modulus/d/a conversion, read the photosensitive analog channel 0,
But after I wrote, digital tube display is always 255, according to the truth when I through the environment brightness change MCU on photosensitive resistance's value, the value of digital tube display also should change, but it's the same
Is it a wrong
Compilation shows no error warning
The code below

#include
#include


# define uchar unsigned char
# define uint unsigned int
# define PCF8591ADDR 0 x90//macro definition said device address
# define IIcRead 1//host from machine-readable data
# define IIcWrite 0//host by writing data from machine

Uchar code smgduan []={x5b x3f 0, 0 x06, 0, 0 x4f, 0 x66, 0 x6d, 0 x7d, 0 x07, 0 x7f, 0 x6f};
Uchar smgwei []={0 xfe, 0 XFD, 0 XFB};
Uchar num=0;//digital tube display the value of the
Bit AckFlag;//reply flag bit

Sbit DU=P2 ^ 6;
Sbit WE=P2 ^ 7;
Sbit SDA=P2 ^ 0;
Sbit SCL=P2 ^ 1;

Void delay (uint z)//the time delay of the custom function
{
Uint x, y;
For (x=z; X> 0; X -)
For (y=114; Y> 0; Y -);

}

Void the display (uint I)
{
The static uchar wei;//add the static variables defined is local static variable function after you will not lose its value
//I hope it's value will not be removed after the exit, it still is the scope of the function
P0=0 XFF;//remove break code
WE=1;
P0=smgwei [wei];//digital tube an array of
WE=0;//latch a selected data low level off

The switch (wei)
{
Case 0: DU=1; P0=smgduan [100] I/; DU=0; break;//one hundred
Case 1: DU=1; P0=smgduan 100/10 % [I]; DU=0; break;//10
Case 2: DU=1; P0=smgduan [10] I %; DU=0; break;//bits
}

Wei++;
If (wei==3)//wei equals 3, shows the three digital tube, and then move from zero, the first digital tube display
{
Wei=0;
}

}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IIC communication
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void delay5us ()
{
_nop_ ();//the nop instruction is almost 5 us
}

Void I2Cstart ()//start signal
{
SCL=1;
SDA=1;
Delay5us ();
SDA=0;
Delay5us ();

}

Void I2Cstop ()//termination signal
{
SCL=0;
SDA=0;
SCL=1;
Delay5us ();
SDA=1;
Delay5us ();

}

Bit readACK ()//IIC host read from machine reply
{
SCL=0;
SCL=1;
Delay5us ();
If (SDA)
{
SCL=0;
The return (1);
}
The else
{
SCL=0;
Return (0);
}
}

Void sendACK bit (I)//host sends a response signal
{
SCL=0;
If (I)
SDA=1;
The else
SDA=0;
SCL=1;
Delay5us ();
SCL=0;//lower the clock bus
SDA=1;//release data bus
}

Void IIcSendByte (uchar DAT)//I2C send DAT a byte data needs to send data
{
uchar i;
for(i=0; i<8; I++)//write 8 times, each time to write one
{
SCL=0;//lower the clock bus, allowing the SDA change
If (DAT & amp; 0 x80)//write data highest
SDA=1;
The else
SDA=0;
SCL=1;//up the clock, let from machine-readable SDA
DAT & lt; <=1;//for sending a left shift under one
}
SCL=0;//lower the clock bus
SDA=1;//release data bus


}


Uchar IIcReadByte ()//I2C bus read a byte of data
{
Uchar I, DAT.
for(i=0; i<8; I++)
{
DAT & lt; <=1;//data left one, is ready to receive a
SCL=0;//lower the clock bus, allowed to vary from the machine control SDA
SCL=1;//up the clock bus, read the data on the SDA
If (SDA)
DAT |=0 x01;

}
Return (DAT);

}
Uchar PCF8591Read (uchar Ctrl)
{
Uchar DAT.
I2Cstart ();
IIcSendByte (PCF8591ADDR + IIcWrite);//IIC sends a byte
If (readACK ())//read from machine reply
AckFlag=1;//not reply
The else
AckFlag=0;//reply
IIcSendByte (Ctrl);//IIC sent a first address
ReadACK ();
I2Cstart ();
IIcSendByte (PCF8591ADDR + IIcRead);
If (readACK ())//read from machine reply
AckFlag=1;//not reply
The else
AckFlag=0;//reply
DAT=IIcReadByte ();//read a byte
SendACK (1);
I2Cstop ();
Return (DAT);

}

//timer 0 initialization function
Void timer0Init ()
{
EA=1;//open the total interruption
ET0=1;//open the timer 0 interrupt
TR0=1;//start the timer 0
TMOD |=0 x01;//select 16-bit timer timer 0 first mode, TL0 TH0 full use
Xed TH0=0;
TL0=0 XFF;
}

Void main ()//by a hardware reset, this time don't need to reset the program
{
Timer0Init ();//initialization timer 0
While (1)
{
EA=0;
Num=PCF8591Read (0);
EA=1;
delay(5);

}


}

Void timer0 () interrupt 1
{
Xed TH0=0;//replay initial
TL0=0 XFF;//time 5 millisecond
The display (num);//will display function calls in the interrupt function inside the

}
  • Related