OpenCores
URL https://opencores.org/ocsvn/sockit_owm/sockit_owm/trunk

Subversion Repositories sockit_owm

[/] [sockit_owm/] [trunk/] [demo/] [Terasic_DE1/] [software/] [onewire/] [onewire.c] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 iztok
/*
2
 * "Hello World" example.
3
 *
4
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
5
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
6
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
7
 * device in your system's hardware.
8
 * The memory footprint of this hosted application is ~69 kbytes by default
9
 * using the standard reference design.
10
 *
11
 * For a reduced footprint version of this template, and an explanation of how
12
 * to reduce the memory footprint for a given application, see the
13
 * "small_hello_world" template.
14
 *
15
 */
16
 
17
#include <stdio.h>
18
#include "system.h"
19
 
20
#include "ownet.h"
21
#include "findtype.h"
22
#include "temp10.h"
23
#include "temp28.h"
24
#include "temp42.h"
25
 
26
// defines
27
#define MAXDEVICES         20
28
#define ONEWIRE_P           0
29
 
30
// local functions
31
void DisplaySerialNum(uchar sn[8]);
32
 
33
int main()
34
{
35
  uchar FamilySN[MAXDEVICES][8];
36
  float current_temp;
37
  int i = 0;
38
  int j = 0;
39
  int NumDevices = 0;
40
  SMALLINT didRead = 0;
41
 
42
  //use port number for 1-wire
43
  uchar portnum = ONEWIRE_P;
44
 
45
  //----------------------------------------
46
  // Introduction header
47
  printf("\r\nTemperature device demo:\r\n");
48
 
49
  // attempt to acquire the 1-Wire Net
50
  if (!owAcquire(portnum,NULL))
51
  {
52
     printf("Acquire failed\r\n");
53
#ifdef SOCKIT_OWM_ERR_ENABLE
54
     while(owHasErrors())
55
        printf("  - Error %d\r\n", owGetErrorNum());
56
     return 1;
57
#endif
58
  }
59
 
60
  do
61
  {
62
     j = 0;
63
     // Find the device(s)
64
     NumDevices  = 0;
65
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x10, MAXDEVICES-NumDevices);
66
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x28, MAXDEVICES-NumDevices);
67
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x42, MAXDEVICES-NumDevices);
68
     if (NumDevices)
69
     {
70
        printf("\r\n");
71
        // read the temperature and print serial number and temperature
72
        for (i = NumDevices; i; i--)
73
        {
74
           printf("(%d) ", j++);
75
           DisplaySerialNum(FamilySN[i-1]);
76
           if (FamilySN[i-1][0] == 0x10)
77
              didRead = ReadTemperature10(portnum, FamilySN[i-1],&current_temp);
78
           if (FamilySN[i-1][0] == 0x28)
79
              didRead = ReadTemperature28(portnum, FamilySN[i-1],&current_temp);
80
           if (FamilySN[i-1][0] == 0x42)
81
              didRead = ReadTemperature42(portnum, FamilySN[i-1],&current_temp);
82
 
83
           if (didRead)
84
           {
85
              printf(" %5.1f Celsius\r\n", current_temp);
86
           }
87
           else
88
           {
89
              printf("  Convert failed.  Device is");
90
              if(!owVerify(portnum, FALSE))
91
                 printf(" not");
92
              printf(" present.\r\n");
93
#ifdef SOCKIT_OWM_ERR_ENABLE
94
              while(owHasErrors())
95
                 printf("  - Error %d\r\n", owGetErrorNum());
96
#endif
97
           }
98
 
99
        }
100
     }
101
     else
102
        printf("No temperature devices found!\r\n");
103
 
104
     printf("\r\nPress any key to continue\r\n");
105
     i = getchar();
106
  }
107
  while (i!='q');
108
 
109
  // release the 1-Wire Net
110
  owRelease(portnum);
111
 
112
  return 0;
113
}
114
// -------------------------------------------------------------------------------
115
// Read and print the serial number.
116
//
117
void DisplaySerialNum(uchar sn[8])
118
{
119
   int i;
120
   for (i = 7; i>=0; i--)
121
      printf("%02X", (int)sn[i]);
122
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.