a beginner using pic controller

Upload: dana-utomo

Post on 31-Oct-2015

30 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/16/2019 A Beginner Using PIC Controller

    1/8

    A Beginners data logger project using PIC12F683 microcontroller

    It is a very simple data logger project based on PIC12F683 microcontroller. The microcontroller

    reads temperature values from a temperature sensor on a regular interval basis and stores

    them into its internal EEPROM memory. The recorded temperatures can be later transferred to

    a PC through serial interface. I originally published this project on electronics-lab.com last

    summer. I thought this could be a very good learning project for beginners, and so I am posting

    it here for Embedded Labs readers too.

    Finished temperature logger powered from a 9V battery

    Theory

    The sensor used in this project is DS18B20. It is a digital temperature sensor manufactured by

    Dallas Semiconductor (now MAXIM) that can measure temperature ranging from -55C to

    +125C with an accuracy of 0.5C over the range of -10C to +85C. The sensor provides the

    measured temperature (C) output in user-configurable 9, 10, 11, or 12-bit data corresponding

    to the desired resolution of 0.5, 0.25, 0.125, and 0.0625 C. The sensor communicates with a

    host microcontroller over a 1-wire bus. Readers are suggested to refer thedatasheet on

    Maxims website for details on this sensor. Please keep in mind that there are three versions of

    this sensors, namely DS1820, DS18S20, and DS18B20, and they have some architectural

    differences. All of them have the same pin configuration and therefore the circuit diagram would

    be same for all three types. However, some modification in the software may be required while

    the microcontroller reads the temperature data from them.

    http://www.electronics-lab.com/projects/test/012/index.htmlhttp://www.maxim-ic.com/datasheet/index.mvp/id/2812/t/alhttp://www.maxim-ic.com/datasheet/index.mvp/id/2812/t/alhttp://www.maxim-ic.com/datasheet/index.mvp/id/2812/t/alhttp://embedded-lab.com/blog/wp-content/uploads/2011/04/TitleImage.jpghttp://www.maxim-ic.com/datasheet/index.mvp/id/2812/t/alhttp://www.electronics-lab.com/projects/test/012/index.html
  • 7/16/2019 A Beginner Using PIC Controller

    2/8

    PIC12F683 has 256 bytes of internal EEPROM. Each temperature reading is stored as a byte,

    which means only the eight most significant bits of DS18B20 output is recorded. Therefore, the

    temperature resolution is decreased down to 1 C. This temperature logger can store up to 254

    temperature values (254 bytes) in its internal EEPROM. The remaining two EEPROM locations

    are used to store the sampling time and number of samples information. Three tact switches are

    used to provide user inputs for controlling the operation of the data logger.

    Circuit Diagram

    The PIC microcontroller uses its internal clock source operated at 4.0 MHz. The DS18B20

    sensor is interfaced to GP0 pin (7) of the microcontroller. An LED connected to the GP2 pin

    serves as the only display in the circuit to indicate various actions of the data logger. For

    example, it blinks every time a sample is recorded into EEPROM. The circuit is powered with +5

    V derived from a 9V battery using an LM78L05 regulator IC. The LM78L05 circuit is a very

    common circuit and therefore, it is not shown here.

    Circuit diagram of data logger

    The three tact switches provide the following functions.

    Start: Starts data logging

    Stop: Stops the logging procedure

    Send/Reset: Transfers data to PC through serial port. However, if it is held pressed for 2

    sec or more, the EEPROM locations are cleared and ready for new recordings.

    Selection of sampling time

    http://embedded-lab.com/blog/wp-content/uploads/2011/04/DataLoggerPIC12F683.png
  • 7/16/2019 A Beginner Using PIC Controller

    3/8

    This data logger offers three options for sampling interval: 1 sec, 1min, and 10 min. The

    selection is made through the same three tact switches. Here is how it works. Suppose if 10 min

    sampling time is needed, then first turn OFF the power, hold the Send/Reset button pressed,

    turn the power ON, and wait till the LED glows. Once the LED glows, release the button, and the

    sampling interval is set to 10 min. The new sampling time will be updated to EEPROM location

    0 so that in case of power failure, the previous sampling time will be restored. Similarly, the useof Start or Stop button instead of the Send/Reset one sets the sampling time to 1 sec, or 1

    min respectively. With 10 min sampling interval, this data logger can record temperature

    samples over 42 hours.

    Serial Interface to PC

    Transferring data to PC through serial port requires a voltage translation circuit to convert the

    TTL logic levels from PIC12F683 to appropriate RS232 voltage levels. A regular PNP transistor

    with few other passive components can do this job. The RS232 standard uses a -3 to -12 V for

    Logic 1 and +3 to +12 V for Logic 0. The required negative voltage is stolen from the TX pin of

    the RS232 port on PCs side which is unused as there wont be any data transfer from the PC toPIC12F683. Under idle condition the TX pin on PCs side is held high (-12 V). The two figures

    below describe the operation of converting TTL levels for 1 and 0 to corresponding RS232

    levels. The positive terminal of the 10 uF capacitor is grounded because its negative terminal

    has to deal with a more negative voltage.

    Translating TTL Logic 1 level to RS232 Logic 1 level

    http://embedded-lab.com/blog/wp-content/uploads/2011/04/TTL1toRS2321.jpg
  • 7/16/2019 A Beginner Using PIC Controller

    4/8

    Translating TTL Logic 0 level to RS232 Logic 0 level

    Finished project board

    Software

    http://embedded-lab.com/blog/wp-content/uploads/2011/04/FinishedProject.jpghttp://embedded-lab.com/blog/wp-content/uploads/2011/04/TTL0toRS2320.jpg
  • 7/16/2019 A Beginner Using PIC Controller

    5/8

    The firmware for PIC12F683 is developed in C using mikroC Pro for PIC compiler from

    mikroElektronika. PIC12F683 doesnt have built-in hardware UART module for serial

    communication but the mikroC compiler has built-in library routines to implement software

    UART through any digital I/O pin of PIC microcontroller. Due to wide use of DS18B20 sensor for

    temperature measurements, mikroC also provides 1-wire library routines for controlling the

    operation of this sensor. The built-in functions for communicating with a 1-wire sensor are,

    Ow_Reset is used for reseting sensor;

    Ow_Read is used for receiving data from sensor; and

    Ow_Write is used for sending commands to sensor.

    MikroC also provides EEPROM library for read and write operations to internal EEPROM

    locations. The overall programming is made much easier with the help of these built-in library

    functions of mikroC. The source code with HEX files can be downloaded from the links below.

    Download mikroC project files (for DS18B20 sensor)

    Download mikroC project files (for DS1820 sensor)

    The configuration bits setup for PIC12F683 can be done through Edit Project window in mikroC.

    The figure below shows the required settings for this project.

    Programming sequence

    When the circuit is powered on, the LED blinks for 3 times that shows the data logger is turned

    on and being initiated. The program checks for any key pressed for setting sampling interval. If

    yes, it identifies the key and store the appropriate sampling interval information to EEPROM

    location 0. The LED is turned on to indicate that the sampling interval has been set and its time

    to release the pressed key. The program is now inside the main loop. The three switches (Start,

    Stop and Send/Reset) operate in Interrupt-on-change mode, which means, any time the button

    http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/http://embedded-lab.com/uploads/HexFiles/PIC12F/DataLogger_DS18B20.ziphttp://embedded-lab.com/uploads/HexFiles/PIC12F/DataLogger_DS1820.ziphttp://embedded-lab.com/blog/wp-content/uploads/2011/04/ProjectSetting.jpghttp://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/http://embedded-lab.com/uploads/HexFiles/PIC12F/DataLogger_DS18B20.ziphttp://embedded-lab.com/uploads/HexFiles/PIC12F/DataLogger_DS1820.zip
  • 7/16/2019 A Beginner Using PIC Controller

    6/8

    is pressed, an interrupt is generated. Pressing the Start button begins data recording. Each time

    a temperature sample is recorded in to the EEPROM, the LED blinks to indicate that the logging

    process is going on. Pressing the Stop button will interrupt this process, while Send will initiate

    the serial transfer of recorded samples through GP1 pin (6). If the Send button is pressed for

    more than 2 sec, the entire EEPROM locations will be cleared.

    On PCs side, the hyperterminal program is used to receive logged temperature data from

    PIC12F683. The rate of data transfer is set to 9600 baud on both end. The figure below shows

    the hyperterminal settings for this project.

    Hyperterminal settings on PC

    http://embedded-lab.com/blog/wp-content/uploads/2011/04/HyperTerminalSetting.jpg
  • 7/16/2019 A Beginner Using PIC Controller

    7/8

    Connecting to PC's serial port

    Data received by the hyperterminal program

    Conclusion and future work

    http://embedded-lab.com/blog/wp-content/uploads/2011/04/HyperTerminal.jpghttp://embedded-lab.com/blog/wp-content/uploads/2011/04/RS232Connection.jpg
  • 7/16/2019 A Beginner Using PIC Controller

    8/8

    A simple data logger project was demonstrated using a PIC12F683 micrcontroller and DS18B20

    temperature sensor. A number of improvements can be done with this project, such as adding

    an external EEPROM for expanding its recording capacity. A separate application program can

    be developed on PCs side to receive the serial data from the logger and generate temperature

    vs time plots. A real time clock chip can also be added in the project to keep record of the actual

    time stamp. A bigger PIC with more I/O pins would be more appropriate for implementing theseadditional features.