Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / apps / communication / 1-wire_mcu / mcu /

main.c

   1  /*******************************************************************************
   2     main: Zobrazeni teploty cidla v MCU na displeji.
   3     Copyright (C) 2009 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Ladislav Sorcik <ladislav.sorcik@gmail.com>
   6                Karel Slany <slany AT fit.vutbr.cz>
   7  
   8     LICENSE TERMS
   9  
  10     Redistribution and use in source and binary forms, with or without
  11     modification, are permitted provided that the following conditions
  12     are met:
  13     1. Redistributions of source code must retain the above copyright
  14        notice, this list of conditions and the following disclaimer.
  15     2. Redistributions in binary form must reproduce the above copyright
  16        notice, this list of conditions and the following disclaimer in
  17        the documentation and/or other materials provided with the
  18        distribution.
  19     3. All advertising materials mentioning features or use of this software
  20        or firmware must display the following acknowledgement:
  21  
  22          This product includes software developed by the University of
  23          Technology, Faculty of Information Technology, Brno and its
  24          contributors.
  25  
  26     4. Neither the name of the Company nor the names of its contributors
  27        may be used to endorse or promote products derived from this
  28        software without specific prior written permission.
  29  
  30     This software or firmware is provided ``as is'', and any express or implied
  31     warranties, including, but not limited to, the implied warranties of
  32     merchantability and fitness for a particular purpose are disclaimed.
  33     In no event shall the company or contributors be liable for any
  34     direct, indirect, incidental, special, exemplary, or consequential
  35     damages (including, but not limited to, procurement of substitute
  36     goods or services; loss of use, data, or profits; or business
  37     interruption) however caused and on any theory of liability, whether
  38     in contract, strict liability, or tort (including negligence or
  39     otherwise) arising in any way out of the use of this software, even
  40     if advised of the possibility of such damage.
  41  
  42     $Id$
  43  
  44  
  45  *******************************************************************************/
  46  
  47  #include <fitkitlib.h>
  48  #include <stdio.h>
  49  #include <string.h>
  50  #include <lcd/display.h>
  51  
  52  #include "one_wire.h"
  53  
  54  
  55  /*******************************************************************************
  56   * Prints user help (help command)
  57  *******************************************************************************/
  58  void print_user_help(void)
  59  {
  60      term_send_str_crlf("DS18B20 temeperature measurement.");
  61  }
  62  
  63  
  64  /*******************************************************************************
  65   * User command decoding and execution
  66  *******************************************************************************/
  67  unsigned char decode_user_cmd(char *cmd_ucase, char *cmd)
  68  {
  69     return (CMD_UNKNOWN);
  70  }
  71  
  72  
  73  /*******************************************************************************
  74   * Peripheral/component initialization
  75  *******************************************************************************/
  76  void fpga_initialized()
  77  {
  78     LCD_init();
  79     LCD_write_string("Thermometer ...");
  80  }
  81  
  82  
  83  /*******************************************************************************
  84   * Appends a decadic number with at least digits characters, leading with fill
  85  *******************************************************************************/
  86  void LCD_append_long(unsigned long number, unsigned char digits, char fill)
  87  {
  88    char buf[10];
  89    unsigned char length;
  90    long2str(number, buf, 10);
  91    length = strlen(buf);
  92    if (length < digits) {
  93      digits -= length;
  94      for (length = 0; length < digits; length++) {
  95        LCD_append_char(fill);
  96      }
  97    }
  98    LCD_append_string(buf);
  99  }
 100  
 101  
 102  /*******************************************************************************
 103   * Reads temperature from device which is the only device on the bus
 104  *******************************************************************************/
 105  unsigned int ds_get_temperature_single_dev(void)
 106  {
 107    unsigned int temperature = 0;
 108  
 109    ow_reset();
 110    ow_write_byte(0xCC);      // skip ROM - no need to send device id
 111                              // because there is only one
 112    ow_write_byte(0x44);      // start temperature AD conversion
 113    while (!ow_read_bit()) {} // wait for conversion end
 114    ow_reset();
 115    ow_write_byte(0xCC);      // skip ROM - no need to send device id
 116    ow_write_byte(0xBE);      // read
 117    //((char*) & temperature)[1] = ow_read_byte();
 118    //((char*) & temperature)[0] = ow_read_byte();
 119    temperature = ow_read_byte();
 120    temperature |= ((unsigned int) ow_read_byte()) << 8;
 121    ow_read_byte();
 122    ow_read_byte();
 123    ow_read_byte();
 124    ow_read_byte();
 125    ow_read_byte();
 126    ow_read_byte();
 127    ow_read_byte();
 128    return temperature;
 129  }
 130  
 131  
 132  /*******************************************************************************
 133   * Tells all the devices on the bus to start temperature measurement
 134  *******************************************************************************/
 135  void ds_measure_all(void)
 136  {
 137    ow_reset();
 138    ow_write_byte(0xCC);      // skip ROM - no need to send device id
 139                              // because all are going to measure
 140    ow_write_byte(0x44);      // start temperature AD conversion
 141    while (!ow_read_bit()) {} // wait for conversion end
 142  }
 143  
 144  
 145  /*******************************************************************************
 146   * Reads temperature from a selected device
 147  *******************************************************************************/
 148  unsigned int ds_get_temperature_selected(unsigned char idx)
 149  {
 150    unsigned int temperature = 0;
 151  
 152    ow_reset();
 153    ow_send_match_rom(idx);   // select by index
 154    ow_write_byte(0xBE);      // read
 155    temperature = ow_read_byte();
 156    temperature |= ((unsigned int) ow_read_byte()) << 8;
 157    ow_read_byte();
 158    ow_read_byte();
 159    ow_read_byte();
 160    ow_read_byte();
 161    ow_read_byte();
 162    ow_read_byte();
 163    ow_read_byte();
 164    return temperature;
 165  }
 166  
 167  
 168  /*******************************************************************************
 169   * Main function
 170  *******************************************************************************/
 171  int main(void)
 172  {
 173    short cntr = 0;
 174    signed int temperature, whole, fractional;
 175    unsigned long i;
 176  
 177    initialize_hardware();
 178  
 179    set_led_d6(0);
 180  
 181    ow_find_devices();
 182  
 183    LCD_write_string("Found ");
 184    LCD_append_long(num_ROMs, 2, '0');
 185    LCD_append_string(" devs");
 186    delay_ms(1000);
 187  
 188    while (1)
 189    {
 190      ds_measure_all();
 191      for (i = 0; i < num_ROMs; i++) {
 192        temperature = ds_get_temperature_selected(i);
 193        LCD_write_string("dev");
 194        LCD_append_long(i, 1, '0');
 195        LCD_append_string(":  ");
 196        whole = temperature >> 4;
 197        fractional = 0;
 198        if (temperature >= 0)
 199        {
 200          //if (temperature & 0x0001) fractional +=  625;
 201          //if (temperature & 0x0002) fractional += 1250;
 202          //if (temperature & 0x0004) fractional += 2500;
 203          if (temperature & 0x0008) fractional += 5000;
 204          LCD_append_string(" ");
 205          LCD_append_long(whole, 3, ' ');
 206          LCD_append_char('.');
 207          LCD_append_long(fractional, 4, '0');
 208        }
 209        else
 210        {
 211          if (temperature & 0x000f)
 212          {
 213            whole += 1;
 214            fractional = 10000;
 215            //if (temperature & 0x0001) fractional -=  625;
 216            //if (temperature & 0x0002) fractional -= 1250;
 217            //if (temperature & 0x0004) fractional -= 2500;
 218            if (temperature & 0x0008) fractional -= 5000;
 219          }
 220          whole *= -1;
 221          LCD_append_string(" ");
 222          LCD_append_long(whole, 3, ' ');
 223          LCD_append_char('.');
 224          LCD_append_long(fractional, 4, '0');
 225        }
 226  
 227        flip_led_d6();
 228        delay_ms(1000);
 229      }
 230  
 231    }
 232  
 233  }
 234  
Zobrazeno: 679791x Naposledy: 28.6.2022 20:47:36