Professional Documents
Culture Documents
Project
Project
ACKNOWLEDGEMENT CERTIFICATE EMBEDDED SYSTEMS 8051 MICROCONTROLLER INTRODUCTION TO LIGHT SENSING ROBOT COMPONENTS USED P89V51RD2 (8052 DERIVATIVE) IC MAX 232 REGULATOR 7805 LIGHT EMITTING DIODE (LED) RESISTORS CAPACITORS DC MOTORS MOTOR DRIVING IC L293D PROGRAM OF MOTOR WITH IC LIGHT DEPENDENT RESISTORS (LDR) IC PCF8591P(ANALOG TO DIGITAL CONVERTER) INTER INTEGRATED CIRCUIT(I2C) PROTOCOL PROGRAM OF ADC USING I2C PROTOCOL COMPLETE MAIN PROGRAM WORKNG OF LIGHT SENSOR ROBOT PROJECT APPLICATIONS FUTURE MODIFICATIONS
EMBEDDED SYSTEM
An embedded system is a computer system designed to perform one or a few dedicated functions,often with real-time computing constraints. It is embedded as part of a complete device often including hardware and mechanical parts. In contrast, a general-purpose computer, such as a personal computer, is designed to be flexible and to meet a wide range of an end-user's needs. Embedded systems control many of the common devices in use today. Embedded systems are controlled by a main processing core that is typically either a microcontroller or a digital signal processor (DSP) Since the embedded system is dedicated to specific tasks, design engineers can optimize it, reducing the size and cost of the product, or increasing the reliability and performance. Some embedded systems are mass-produced, benefiting from economies of scale. Physically, embedded systems range from portable devices such as digital watches and MP3 players, to large stationary installations like traffic lights, factory controllers, or the systems controlling nuclear power plants. Complexity varies from low, with a single microcontroller chip, to very high with multiple units, peripherals and networks mounted inside a large chassis or enclosure. In general, "embedded system" is not an exactly defined term, as many systems have some element of programmability. For example, Handheld computers share some elements with embedded systems such as the operating systems and microprocessors which power them but are not truly embedded systems, because they allow different applications to be loaded and peripherals to be connected.
CHARACTERISTICS
1. Embedded systems are designed to do some specific task, rather than be a generalpurpose computer for multiple tasks. Some also have real-time performance constraints that must be met, for reasons such as safety and usability; others may have low or no performance requirements, allowing the system hardware to be simplified to reduce costs. 2. Embedded systems are not always standalone devices. Many embedded systems consist of small, computerized parts within a larger device that serves a more general purpose. For example, the Gibson Robot Guitar features an embedded system for tuning the strings, but the overall purpose of the Robot Guitar is, of course, to play music. [5] Similarly, an embedded system in an automobile provides a specific function as a subsystem of the car itself.
3. The program instructions written for embedded systems are referred to as firmware, and are stored in read-only memory or Flash memory chips. They run with limited computer hardware resources: little memory, small or non-existent keyboard and/or screen.
USER INTERFACE
Embedded systems range from no user interface at all dedicated only to one task to complex graphical user interfaces that resemble modern computer desktop operating systems. A more sophisticated devices use graphical screen with touch sensing or screen-edge buttons provide flexibility while minimizing space used: the meaning of the buttons can change with the screen, and selection involves the natural behavior of pointing at what's desired. Handheld systems often have a screen with a "joystick button" for a pointing device. Some systems provide user interface remotely with the help of serial (e.g. RS-232, USB) or network (e.g. Ethernet) connection. In spite of installed client software and cables are needed this approach usually gives a lot of advantages: extends the capabilities of embedded system, avoids the cost of a display, simplifies BSP, allows to build reach user interface on PC. One of the well established model in this direction is the combination of embedded web server running on embedded device and user interface in web browser on PC
efficiency and reduce pollution. Other automotive safety systems such as anti-lock braking system (ABS), Electronic Stability Control (ESC/ESP), traction control (TCS) and automatic four-wheel drive. In addition to commonly described embedded systems based on small computers, a new class of miniature wireless devices called motes are quickly gaining popularity as the field of wireless sensor networking rises. Wireless sensor networking, WSN, makes use of miniaturization made possible by advanced IC design to couple full wireless subsystems to sophisticated sensor, enabling people and companies to measure a myriad of things in the physical world and act on this information through IT monitoring and control systems. These motes are completely self contained, and will typically run off a battery source for many years before the batteries need to be changed or charged.
MICROCONTROLLER 8051
The Intel 8051 is a Harvard architecture, single chip microcontroller (C) which was developed by Intel in 1980 for use in embedded systems. Intel's original versions were popular in the 1980s and early 1990s, but has today largely been superseded by a vast range of faster and/or functionally enhanced 8051-compatible devices manufactured by more than 20 independent manufacturers including Atmel, Infineon Technologies, Maxim Integrated Products, NXP, Nuvoton, ST Microelectronics, Silicon Laboratories, Texas Instruments and Cypress Semiconductor. Intel's official designation for the 8051 family of Cs is MCS 51.
It provides many functions (CPU, RAM, ROM, I/O, interrupt logic, timer, etc.) in a single package 8-bit ALU, Accumulator and Registers; hence it is an 8-bit microcontroller 8-bit data bus - It can access 8 bits of data in one operation 16-bit address bus - It can access 216 memory locations - 64 kiB (65536 locations) each of RAM and ROM On-chip RAM - 128 bytes (data memory) On-chip ROM - 4 kiB (program memory) Four byte bi-directional input/output port UART (serial port) Two 16-bit Counter/timers Two-level interrupt priority Power saving mode
A particularly useful feature of the 8051 core is the inclusion of a boolean processing engine which allowsbit-level boolean logic operations to be carried out directly and efficiently on internal registers and RAM. This feature helped to cement the 8051's popularity in industrial control applications. Another valued feature is that it has four separate register sets, which can be used to greatly reduce interrupt latency compared to the more common method of storing interrupt context on a stack. The 8051 UARTs make it simple to use the chip as a serial communications interface. External pins can be configured to connect to internal shift registers in a variety of ways, and the internal
timers can also be used, allowing serial communications in a number of modes, both synchronous and asynchronous. Some modes allow communications with no external components. A mode compatible with an RS-485 multi-point communications environment is achievable, but the 8051's real strength is fitting in with existing ad-hoc protocols (e.g., when controlling serial-controlled devices). Once a UART, and a timer if necessary, have been configured, the programmer needs only to write a simple interrupt routine to refill the sendshift register whenever the last bit is shifted out by the UART and/or empty the full receive shift register (copy the data somewhere else). The main program then performs serial reads and writes simply by reading and writing 8-bit data to stacks. 8051 based microcontrollers typically include one or two UARTs, two or three timers, 128 or 256 bytes of internal data RAM (16 bytes of which are bit-addressable), up to 128 bytes of I/O, 512 bytes to 64 kB of internal program memory, and sometimes a quantity of extended data RAM (ERAM) located in the external data space. The original 8051 core ran at 12 clock cycles per machine cycle, with most instructions executing in one or two machine cycles. With a 12 MHz clock frequency, the 8051 could thus execute 1 million one-cycle instructions per second or 500,000 two-cycle instructions per second. Enhanced 8051 cores are now commonly used which run at six, four, two, or even one clock per machine cycle, and have clock frequencies of up to 100 MHz, and are thus capable of an even greater number of instructions per second. All SILabs, some Dallas and a few Atmel devices have single cycle cores. Even higher speed single cycle 8051 cores, in the range 130 MHz to 150 MHz, are now available in internet downloadable form for use inprogrammable logic devices such as FPGAs, and at many hundreds of MHz in ASICs, for example the netlist from www.e8051.com. Top clock speed 8051 cores reach over 200 MHz in today's FPGA technologies - as an example www.dcd.pl. Common features included in modern 8051 based microcontrollers include built-in reset timers with brown-out detection, on-chip oscillators, self-programmable Flash ROM program memory, bootloader code in ROM, EEPROM non-volatile data storage, IC, SPI, and USB host interfaces, CAN or LIN bus, PWM generators, analog comparators, A/D and D/A converters, RTCs, extra counters and timers, in-circuit debugging facilities, more interrupt sources, and extra power saving modes.
PROGRAMMING
There are various high level language compilers for 8051. Several C compilers are available for the 8051, most of which feature extensions that allow the programmer to specify where each variable should be stored in its six types of memory, and provide access to 8051 specific hardware features such as the multiple register banks and bit manipulation instructions. There are many commercial C compilers. SDCC is a popular open source C compiler. Other high level languages such as Forth, BASIC, Pascal/Object Pascal, PL/M and Modula 2 are available for the 8051, but they are less widely used than C and assembly.
In addition to these, various softwares are also used. These are keil uvision2 required for the programming of the device in embedded C language. Another is flash magic used for downloading the hex file of the written program to the device with help of data cable and com port. The specifications, description and respective programs of some of the major components used are described hereafter.
MAX232
Description
The MAX232 is an integrated circuit that converts signals from an RS-232 serial port to signals suitable for use in TTL compatible digital logic circuits. The MAX232 is a dual driver/receiver and typically converts the RX, TX, CTS and RTS signals. It is a dual driver/receiver that includes a capacitive voltage generator to supply TIA/EIA-232-F voltage levels from a single 5-V supply. Each receiver converts TIA/EIA-232-F inputs to 5-V TTL/CMOS levels. These receivers have a typical threshold of 1.3 V, a typical hysteresis of 0.5 V, and can accept 30-V inputs. Each driver converts TTL/CMOS input levels into TIA/EIA-232-F levels. The driver, receiver, and voltage-generator functions are available as cells in the Texas Instruments LinASIC library.
FEATURES
Meets or Exceeds TIA/EIA-232-F and ITU Operates From a Single 5-V Power Supply With 1.0-_F Charge-Pump Capacitors Operates Up To 120 kbit/s Two Drivers and Two Receivers 30-V Input Levels Low Supply Current . . . 8 mA Typical ESD Protection Exceeds JESD 22 2000-V Human-Body Model (A114-A) Upgrade With Improved ESD (15-kV HBM) and 0.1-_F Charge-Pump Capacitors is
REGULATOR 7805
Description
The MC78XX/LM78XX/MC78XXA series of three terminal positive regulators are available in the TO-220/D-PAK package and with several fixed output voltages, making them useful in a wide range of applications. Each type employs internal current limiting, thermal shut down and safe operating area protection, making it essentially indestructible. If adequate heat sinking is provided, they can deliver over 1A output current. Although designed primarily as fixed voltage regulators, these devices can be used with external components to obtain adjustable voltages and currents.
Features Output Current up to 1A Output Voltages of 5, 6, 8, 9, 10, 12, 15, 18, 24V Thermal Overload Protection Short Circuit Protection Output Transistor Safe Operating Area Protection
Circuit symbol:
TESTING AN LED Never connect an LED directly to a battery or power supply! It will be destroyed almost instantly because too much current will pass through and burn it out. LEDs must have a resistor in series to limit the current to a safe value, for quick testing purposes a 1k resistor is suitable for most LEDs if your supply voltage is 12V or less. Remember to connect the LED the correct way round. . COLOURS OF LEDS LEDs are available in red, orange, amber, yellow, green, blue and white. Blue and white LEDs are much more expensive than the other colours. The colour of an LED is determined by the semiconductor material, not by the colouring of the 'package' (the plastic body). LEDs of all colours are available in uncoloured packages which may be diffused (milky) or clear (often described as 'water clear'). The coloured packages are also available as diffused (the standard type) or transparent.
RESISTORS
A resistor is a two-terminal electronic component that produces a voltage across its terminals that is proportional to the electric current through it in accordance with Ohm's law: V = IR Resistors are elements of electrical networks and electronic circuits and are ubiquitous in most electronic equipment. Practical resistors can be made of various compounds and films, as well as resistance wire (wire made of a high-resistivity alloy, such as nickel/chrome). The primary characteristics of a resistor are the resistance, the tolerance, maximum working voltage and the power rating. Other characteristics include temperature coefficient, noise, and inductance. Less well-known is critical resistance, the value below which power dissipation limits the maximum permitted current flow, and above which the limit is applied voltage. Critical resistance depends upon the materials constituting the resistor as well as its physical dimensions; it's determined by design. Resistors can be integrated into hybrid and printed circuits, as well as integrated circuits. Size, and position of leads (or terminals) are relevant to equipment designers; resistors must be physically large enough not to overheat when dissipating their power.
CAPACITORS
A capacitor or condenser is a passive electronic component consisting of a pair of conductors separated by a dielectric. When a voltage potential difference exists between the conductors, an electric field is present in the dielectric. This field stores energy and produces a mechanical force between the plates. The effect is greatest between wide, flat, parallel, narrowly separated conductors. An ideal capacitor is characterized by a single constant value, capacitance, which is measured in farads. This is the ratio of the electric charge on each conductor to the potential difference between them. In practice, the dielectric between the plates passes a small amount of leakage current. The conductors and leads introduce an equivalent series resistance and the dielectric has an electric field strength limit resulting in a breakdown voltage. In a way, a capacitor is a little like a battery. Although they work in completely different ways, capacitors and batteries both store electrical energy. We know that a battery has two terminals. Inside the battery, chemical reactions produce electrons on one terminal and absorb electrons on the other terminal. A capacitor is much simpler than a battery, as it can't produce new electrons -it only stores them.
DC MOTOR
A DC motor is designed to run on DC electric power. A simple 2-pole DC electric motor (here red represents a magnet or winding with a "North" polarization, while green represents a magnet or winding with a "South" polarization).
Every DC motor has six basic parts -- axle, rotor (a.k.a., armature), stator, commutator, field magnet(s), and brushes. In most common DC motors (and all that BEAMers will see), the external magnetic field is produced by high-strength permanent magnets1. The stator is the stationary part of the motor -- this includes the motor casing, as well as two or more permanent magnet pole pieces. The rotor (together with the axle and attached commutator) rotate with respect to the stator. The rotor consists of windings (generally on a core), the windings being electrically connected to the commutator. The above diagram shows a common motor layout -with the rotor inside the stator (field) magnets. By far the most common DC motor types are the brushed and brushless types, which use internal and external commutation respectively to create an oscillating AC current from the DC source so they are not purely DC machines in a strict sense.
BRUSHED DC MOTORS
The classic DC motor design generates an oscillating current in a wound rotor, or armature, with a split ring commutator, and either a wound or permanent magnet stator. A rotor consists of one or more coils of wire wound around a core on a shaft; an electrical power source is connected to the rotor coil through the commutator and its brushes, causing current to flow in it, producing electromagnetism. The commutator causes the current in the coils to be switched as the rotor turns, keeping the magnetic poles of the rotor from ever fully aligning with the magnetic poles of
the stator field, so that the rotor never stops (like a compass needle does) but rather keeps rotating indefinitely (as long as power is applied and is sufficient for the motor to overcome the shaft torque load and internal losses due to friction, etc.) Many of the limitations of the classic commutator DC motor are due to the need for brushes to press against the commutator. This creates friction. At higher speeds, brushes have increasing difficulty in maintaining contact. Brushes may bounce off the irregularities in the commutator surface, creating sparks. (Sparks are also created inevitably by the brushes making and breaking circuits through the rotor coils as the brushes cross the insulating gaps between commutator sections. Depending on the commutator design, this may include the brushes shorting together adjacent sectionsand hence coil endsmomentarily while crossing the gaps. Furthermore, the inductance of the rotor coils causes the voltage across each to rise when its circuit is opened, increasing the sparking of the brushes.) This sparking limits the maximum speed of the machine, as too-rapid sparking will overheat, erode, or even melt the commutator. The current density per unit area of the brushes, in combination with their resistivity, limits the output of the motor. The making and breaking of electric contact also causeselectrical noise, and the sparks additionally cause RFI. Brushes eventually wear out and require replacement, and the commutator itself is subject to wear and maintenance (on larger motors) or replacement (on small motors). The commutator assembly on a large machine is a costly element, requiring precision assembly of many parts. On small motors, the commutator is usually permanently integrated into the rotor, so replacing it usually requires replacing the whole rotor. Large brushes are desired for a larger brush contact area to maximize motor output, but small brushes are desired for low mass to maximize the speed at which the motor can run without the brushes excessively bouncing and sparking (comparable to the problem of "valve float" in internal combustion engines). (Small brushes are also desirable for lower cost.) Stiffer brush springs can also be used to make brushes of a given mass work at a higher speed, but at the cost of greater friction losses (lower efficiency) and accelerated brush and commutator wear. Therefore, DC motor brush design entails a trade-off between output power, speed, and efficiency/wear.
BRUSHLESS DC MOTORS
Some of the problems of the brushed DC motor are eliminated in the brushless design. In this motor, the mechanical "rotating switch" or commutator/brushgear assembly is replaced by an external electronic switch synchronised to the rotor's position. Brushless motors are typically 8590% efficient or more (higher efficiency for a brushless electric motor of up to 96.5% were
reported by researchers at the Tokai University in Japan in 2009[16]), whereas DC motors with brushgear are typically 75-80% efficient. Midway between ordinary DC motors and stepper motors lies the realm of the brushless DC motor. Built in a fashion very similar to stepper motors, these often use a permanent magnet external rotor, three phases of driving coils, one or more Hall effect sensors to sense the position of the rotor, and the associated drive electronics. The coils are activated, one phase after the other, by the drive electronics as cued by the signals from either Hall effect sensors or from the back EMF (electromotive force) of the undriven coils. In effect, they act as three-phase synchronous motors containing their own variable-frequency drive electronics. A specialized class of brushless DC motor controllers utilize EMF feedback through the main phase connections instead of Hall effect sensors to determine position and velocity. These motors are used extensively in electric radio-controlled vehicles. When configured with the magnets on the outside, these are referred to by modelists as outrunner motors. Brushless DC motors are commonly used where precise speed control is necessary, as in computer disk drives or in video cassette recorders, the spindles within CD, CD-ROM (etc.) drives, and mechanisms within office products such as fans, laser printers and photocopiers.
The L293D is a quadruple push-pull 4 channel driver capable of delivering 600 mA (1.2 A peak surge) per channel. The L293D is ideal for controlling the forward/reverse/brake motions of small DC motors controlled by a microcontroller such as a PIC or BASIC Stamp. The L293D is a high voltage, high current four channel driver designed to accept standard TTL logic levels and drive inductive loads (such as relays solenoids, DC and stepping motors) and switching power transistors. The L293D is suitable for use in switching applications at frequencies up to 5 KHz.
FEATURES INCLUDE :
600 mA Output Current Capability Per Driver Pulsed Current 1.2 A / Driver Wide Supply Voltage Range: 4.5 V to 36 V Separate Input-Logic Supply NE Package Designed for Heat Sinking Thermal Shutdown & Internal ESD Protection High-Noise-Immunity Inputs
Pin diagram :
This is a very basic circuit.It describes the pin connections of IC L293D i.e. a push pull four channel driver with diodes. L293D is a monolithic integrated high voltage, high current four channel driver designed to accept standard DTL or TTL logic levels and drive inductive loads and switching power transistors.It has 600mA output current capability per channel and 1.2A peak output current per channel (non repetitive).
Sample Programs:/* motor speed control using hyperterminal*/ #include <REG51F.H> sbit en12=P1^0; sbit ip1=P1^1; sbit ip2=P1^2; unsigned char ch; void rx(); void tx(); void timer1(); void main() { en12=1; timer1(); while(1) { rx(); tx(); if(ch=='A') { en12=1; ip1=1; ip2=0; } if(ch=='B') { en12=0; ip1=0; ip2=0;
} } }
void rx() { while(!RI); ch=SBUF; RI=0; } void tx() { while(!TI); SBUF=ch; TI=0; } void timer1() { TMOD=0x20; TH1=0xfd; TR1=1; SCON=0x52; }
/*varying motor speed*/ #include <REG51F.H> unsigned char ton; unsigned char toff;
sbit en12=P1^0; sbit ip1=P1^1; sbit ip2=P1^2; void main() { ton=0x10; toff=0x54; while(1) { { if(ton==0) if (toff==0) { ton=0x10; toff=0x54; } else { en12=0; toff--; } } else { en12=1; ton--; ip1=1; ip2=0; } } }
FEATURES
Case Style:Radial Dark Resistance:20Mohm Rating Voltage:200V dc Resistor Element Type:Light Dependent Body Diameter:5.1mm External Length / Height:2.3mm External Width:4.3mm Lead Diameter:0.48mm Lead Length:30mm Max Operating Temperature:30C Max Resistance @ Lux A:100kohm Max Resistance @ Lux B:2000kohm Min Resistance @ Lux A:20kohm Min Resistance @ Lux B:500kohm Peak Spectral Response Wavelength:550nm Power Dissipation:60mW Resistance @ Lux A:100kohm Voltage Rating AC:200V Voltage Rating DC:200V
The PCF8591 is a single-chip, single-supply low power 8-bit CMOS data acquisition device with four analog inputs, one analog output and a serial I2C-bus interface. Three address pins A0, A1 and A2 are used for programming the hardware address, allowing the use of up to eight devices connected to the I2C-bus without additional hardware. Address, control and data to and from the device are transferred serially via the two-line bidirectional I2C-bus. The functions of the device include analog input multiplexing, on-chip track and hold function, 8-bit analog-to-digital conversion and an 8-bit digital-to-analog conversion. The maximum conversion rate is given by the maximum speed of the I2C-bus.
PIN DIAGRAM:-
ADDRESSING
Each PCF8591 device in an I2C-bus system is activated by sending a valid address to the device. The address consists of a fixed part and a programmable part. The programmable part must be set according to the address pins A0, A1 and A2. The address always has to be sent as the first byte after the start condition in the I2C-bus protocol. The last bit of the address byte is the read/write-bit which sets the direction of the following data transfer.
The second byte sent to a PCF8591 device will be stored in its control register and is required to control the device function. The upper nibble of the control register is used for enabling the analog output, and for programming the analog inputs as single-ended or differential inputs. The lower nibble selects one of the analog input channels defined by the upper nibble (see Fig.4). If the auto-increment flag is set the channel number is incremented automatically after each A/D conversion. If the auto-increment mode is desired in applications where the internal oscillator is used, the analog output enable flag in the control byte (bit 6) should be set. This allows the internal oscillator to run continuously, thereby preventing conversion errors resulting from oscillator start-up delay. The analog output enable flag may be reset at other times to reduce quiescent power consumption. The selection of a non-existing input channel results in the highest available channel number being allocated. Therefore, if the auto-increment flag is set, the next selected channel will be always channel 0. The most significant bits of both nibbles are reserved for future functions and have to be set to 0. After a Power-on reset condition all bits of the control register are reset to 0. The D/A converter and the oscillator are disabled for power saving. The analog output is switched to a high-impedance state.
I2C PINS
SCL = Serial Clock This signal will always be driven by the 8051in our example (8051 is always the master). SDA = Serial Data This signal is driven by either the micro or the I2C peripheral chip. Both SCL and SDA are open-drain when output on the micro and on all I2C peripheral chips.
TRANSFERRING OF BITS
One data bit is transferred during each clock pulse. The data on the SDA line must remain stable during the HIGH period of the clock pulse as changes in the data line at this time will be interpreted as a control signal.
A/D CONVERSION
The A/D converter makes use of the successive approximation conversion technique. The onchip D/A converter and a high-gain comparator are used temporarily during an A/D conversion cycle.An A/D conversion cycle is always started after sending a valid read mode address to a PCF8591 device. The A/D conversion cycle is triggered at the trailing edge of the acknowledge clock pulse and is executed while transmitting the result of the previous conversion. Once a conversion cycle is triggered an input voltage sample of the selected channel is stored on the chip and is converted to the corresponding 8-bit binary code. The conversion result is stored in the ADC data register and awaits transmission. If the auto-increment flag is set the next channel is selected. The first byte transmitted in a read cycle contains the conversion result code of the previous read cycle. After a Power-on reset condition the first byte read is a hexadecimal 80. The maximum A/D conversion rate is given by the actual speed of the I2C-bus.
I2C-BUS PROTOCOL
After a start condition a valid hardware address has to be sent to a PCF8591 device. The read/write bit defines the direction of the following single or multiple byte data transfer. For the format and the timing of the start condition (S), the stop condition (P) and the acknowledge bit (A) refer to the I2C-bus characteristics. In the write mode a data transfer is terminated by sending either a stop condition or the start condition of the next data transfer.
CONDITIONS
ACKNOWLEDGE (ACK)-: NO ACKNOWLEDGE (NAK)-:
START CONDITION-:
STOP CONDITION-:
sbit en1 = P1^0; sbit dc1 = P1^1; sbit dc2 = P1^2; sbit dc3 = P1^3; sbit dc4 = P1^4; sbit en2 = P1^5; unsigned char value,value1,value2; sbit sda = P0^0; sbit scl = P0^1; bit flag = 0;
void main() { while(1) { read_byte_adc(); m_d(); } } void m_d() { if (value > value1) { en2=1; en1=1; dc1=dc3=1; dc2=dc4=0; } else { if (value1 > value) { en2=1; en1=1; dc1=dc3=0; dc2=dc4=1; } } }
void read_byte_adc() { start(); shout(0x90); if (flag) P0=0xf3; else { shout(0x04); if (flag) P0=0xf1; else { start(); shout(0x91); if (flag) P0=0xf2; else { shin(); ack(); value=shin(); ack(); value1=shin(); // ack(); // value2=shin(); nak(); } stop();
} } stop(); } void start() { sda=1; _nop_(); scl=1; _nop_(); _nop_(); sda=0; _nop_(); _nop_(); scl=0; } void stop() { sda=0; scl=1; _nop_(); _nop_(); sda=1; _nop_(); _nop_(); scl=0; }
void shout(unsigned char dat) { unsigned char k,j=0x80; for(k=0;k<8;k++) { sda = (dat & j); j = j>>1; scl = 1; _nop_(); _nop_(); _nop_(); _nop_(); scl = 0; } sda = 1; scl = 1; _nop_(); _nop_(); flag = sda; _nop_(); _nop_(); scl = 0; } int shin() { unsigned char i, q=0x00,c=0x80; sda=1; for(i=0;i<8;i++)
scl=1; _nop_(); _nop_(); _nop_(); _nop_(); flag = sda; if(flag) q = (q|c); c = c>>1; _nop_(); _nop_(); _nop_(); _nop_(); scl=0;
} return q; } void ack() { sda=0; scl=1; _nop_(); _nop_(); _nop_(); _nop_(); scl=0; }
PROJECT APPLICATIONS
TINY LIGHT SENSOR WITH LOGIC OUTPUT DRAWS LESS THAN 10A
A light-sensing circuit that consumes very little power can serve as an automatic Backlight sensor in portable instruments. This function is easily implemented with a logic gate or Schmitttrigger inverter, but those approaches draw a considerable amount of supply current.This light sensor provides a low-to-high output transition at a light level determined by the value of R1.