Home > other >  51 single chip microcomputer, through the light intensity changes automatically adjust led10 brightn
51 single chip microcomputer, through the light intensity changes automatically adjust led10 brightn

Time:11-18

#include
#include

# define uint unsigned int
# define uchar unsigned char
# define PCF8591ADDR 0 x90//PCF8591 hardware address
1//# define I2cRead direction of I2C read a
# define I2cWrite 0//I2C write direction a
# define CH0 0//AD channel 0
1//# define CH1 AD channel 1
# define CH2//AD channel 2
# define CH3 3//AD channel 3
# define DAout 0 x40//DA output command

Sbit DU=P2 ^ 6;//digital section selected
Sbit WE=P2 ^ 7;//digital section selected
Sbit SCL=P2 ^ 1;//the I2C bus clock
Sbit SDA=P2 ^ 0;//the I2C bus data
Uchar num.//digital tube display the value of the
Bit AckFlag;//reply flag bit

//total Yin digital section selected table 0-9
Uchar code SMGduan []={x5b x3f 0, 0 x06, 0, 0, x4f x66 0, 0 x6d, 0 x7d, 0 x07, 0 x7f, 0 x6f,};
//digital tube a selected code
Uchar code SMGwei []={0 xfe, 0 XFD, 0 XFB};

/*====================================
Function: delay (uint z)
Parameters: z delay millisecond set, the range of 0-65535
The return value: no
Description: 12 t/M Fosc11.0592 millisecond delay
====================================*/
Void delay (uint z)
{
Uint x, y;
For (x=z; X & gt; 0; X -)
For (y=114; Y & gt; 0; y--);
}

/*====================================
Function: the display (uchar I)
Parameters: I show that numerical values range 0-255
The return value: no
Description: three Yin dynamic display of digital tube
====================================*/
Void the display (uchar I)
{
The static uchar wei;
P0=0 XFF;//remove break code
WE=1;//open a latch
P0=SMGwei [wei];
WE=0;//latch a selected data
The switch (wei)
{
Case 0: DU=1; P0=SMGduan [100] I/; DU=0; break;
Case 1: DU=1; P0=SMGduan 100/10 % [I]; DU=0; break;
Case 2: DU=1; P0=SMGduan [10] I %; DU=0; break;
}
Wei++;
If (wei==3)
Wei=0;
}
//initialization timer 0
Void timer0Init ()
{
EA=1;//open the total interruption
ET0=1;//open the timer 0 interrupt
TR0=1;//start the timer 0
TMOD |=0 x01;//timer working mode 1 dec bit timing mode
Xed TH0=0;
TL0=0 XFF;//time 5 ms
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IIC communication code
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*====================================
Function: delay5us ()
Parameters: no
The return value: no
Description: 5 us delay function
====================================*/
Void delay5us ()
{
_nop_();
}

/*====================================
Function: I2cStart ()
Parameters: no
The return value: no
Description: the I2C bus start signal
====================================*/
Void I2cStart ()
{
//clock bus for data bus during the high level higher and lower initial models
SCL=1;
SDA=1;
Delay5us ();//state keep 5 us
SDA=0;
Delay5us ();//state keep 5 us
}

/*====================================
Function: I2cStop ()
Parameters: no
The return value: no
Description: the I2C bus stop sign
====================================*/
Void I2cStop ()
{
//the clock during the bus for the high level, data bus from lower to produce high end signal
SCL=0;
SDA=0;
SCL=1;
Delay5us ();//state keep 5 us
SDA=1;
Delay5us ();//state keep 5 us
}

/*====================================
Function: ReadACK ()
Parameters: no
Return value: 1 non response, zero reply
Description: the I2C bus read response signal from machine
====================================*/
Bit ReadACK ()
{
SCL=0;//lower the clock bus, allowed from the machine control SDA
SCL=1;//up, read the SDA
Delay5us ();
If (SDA)//NOACK
{
SCL=0;
The return (1);//return 1
}
The else//ACK
{
SCL=0;
return(0);//return 0
}
}

/*====================================
Function: SendACK (bit I)
Host sends the response parameters: 1, 0 to send reply
The return value: no
Description: the host sends a response signal
====================================*/
Void SendACK (bit I)
{
SCL=0;//lower clock bus, allow the host control SDA
If (I)//send a reply
SDA=1;
The else//send reply
SDA=0;
SCL=1;//up bus, let from machine-readable SDA
Delay5us ();//keep 5 us
SCL=0;//lower the clock bus, allowing the SDA release
SDA=1;//release data bus
}

/*====================================
Function: I2cSendByte uchar (DAT)
Parameters: DAT needs to send data
The return value: no
Description: the I2C send a byte data
====================================*/
Void I2cSendByte uchar (DAT)
{
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;//write 1
The else
SDA=0;//write 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, allowing the SDA release
SDA=1;//release data bus
}

/*====================================
Function: Pcf8591DA (uchar Ctrl, DAT)
Parameters: Ctrl control 8591 bytes, DAT to write data
The return value: no
Description: the PCF8591 turn digital analog output
====================================*/
Void Pcf8591DA (uchar Ctrl, DAT)
{
I2cStart();//I2C start signal
I2cSendByte (PCF8591ADDR + I2cWrite);//sending device address and direction of read and write a
If (ReadACK ())//read from machine reply
AckFlag=1;//NOACK
The else
AckFlag=0;//ACK
I2cSendByte (Ctrl);//send the control byte
If (ReadACK ())//read from machine reply
AckFlag=1;//NOACK
The else
AckFlag=0;//ACK
I2cSendByte (DAT);//send a byte of data
If (ReadACK ())//read from machine reply
AckFlag=1;//NOACK
The else
AckFlag=0;//ACK
I2cStop();//I2C stop signal
}


/*====================================
Function: I2cReadByte ()
Parameters: no
Return value: return to read a byte of data
Description: the I2C bus read a byte of data
====================================*/
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related