EmbDev.net

Forum: µC & Digital Electronics read DS2401 with ATmega 32 and a 1-Wire protocol


von ebrahim (Guest)


Rate this post
useful
not useful
DS2401 is a 64-bit ROM memory that used as a hardware-serial-number. It 
has only two pin. One of them is ground and one of them is Data/Power 
pin. So for communicate with this chip there is a protocol that called 
1-Wire.

I want to read my DS2401 chip with AVR ATmega32 , and I use 1wire.h 
library in codevision. As mentioned in Codevision AVR User Manual I must 
use w1_read() function to read a byte of DS2401. but it returns me only 
255 [On the LCD]!

This is my codevision code:
1
/*****************************************************
2
This program was produced by the
3
CodeWizardAVR V2.05.0 Professional
4
Automatic Program Generator
5
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
6
http://www.hpinfotech.com
7
8
Project : 
9
Version : 
10
Date    : 7/19/2014
11
Author  : 
12
Company : 
13
Comments: 
14
15
16
Chip type               : ATmega32A
17
Program type            : Application
18
AVR Core Clock frequency: 1.000000 MHz
19
Memory model            : Small
20
External RAM size       : 0
21
Data Stack size         : 512
22
*****************************************************/
23
24
#include <mega32a.h>
25
#include <delay.h>
26
#include <stdio.h>
27
28
// 1 Wire Bus interface functions
29
#include <1wire.h>
30
31
// Alphanumeric LCD Module functions
32
#include <alcd.h>
33
34
// Declare your global variables here
35
36
void main(void)
37
{
38
int init_res,read_byte,i;
39
char read_byte_char[64];
40
// Declare your local variables here
41
42
// Input/Output Ports initialization
43
// Port A initialization
44
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
45
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
46
PORTA=0x00;
47
DDRA=0x00;
48
49
// Port B initialization
50
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
51
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
52
PORTB=0x00;
53
DDRB=0x00;
54
55
// Port C initialization
56
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
57
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
58
PORTC=0x00;
59
DDRC=0x00;
60
61
// Port D initialization
62
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
63
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
64
PORTD=0x00;
65
DDRD=0x00;
66
67
// Timer/Counter 0 initialization
68
// Clock source: System Clock
69
// Clock value: Timer 0 Stopped
70
// Mode: Normal top=0xFF
71
// OC0 output: Disconnected
72
TCCR0=0x00;
73
TCNT0=0x00;
74
OCR0=0x00;
75
76
// Timer/Counter 1 initialization
77
// Clock source: System Clock
78
// Clock value: Timer1 Stopped
79
// Mode: Normal top=0xFFFF
80
// OC1A output: Discon.
81
// OC1B output: Discon.
82
// Noise Canceler: Off
83
// Input Capture on Falling Edge
84
// Timer1 Overflow Interrupt: Off
85
// Input Capture Interrupt: Off
86
// Compare A Match Interrupt: Off
87
// Compare B Match Interrupt: Off
88
TCCR1A=0x00;
89
TCCR1B=0x00;
90
TCNT1H=0x00;
91
TCNT1L=0x00;
92
ICR1H=0x00;
93
ICR1L=0x00;
94
OCR1AH=0x00;
95
OCR1AL=0x00;
96
OCR1BH=0x00;
97
OCR1BL=0x00;
98
99
// Timer/Counter 2 initialization
100
// Clock source: System Clock
101
// Clock value: Timer2 Stopped
102
// Mode: Normal top=0xFF
103
// OC2 output: Disconnected
104
ASSR=0x00;
105
TCCR2=0x00;
106
TCNT2=0x00;
107
OCR2=0x00;
108
109
// External Interrupt(s) initialization
110
// INT0: Off
111
// INT1: Off
112
// INT2: Off
113
MCUCR=0x00;
114
MCUCSR=0x00;
115
116
// Timer(s)/Counter(s) Interrupt(s) initialization
117
TIMSK=0x00;
118
119
// USART initialization
120
// USART disabled
121
UCSRB=0x00;
122
123
// Analog Comparator initialization
124
// Analog Comparator: Off
125
// Analog Comparator Input Capture by Timer/Counter 1: Off
126
ACSR=0x80;
127
SFIOR=0x00;
128
129
// ADC initialization
130
// ADC disabled
131
ADCSRA=0x00;
132
133
// SPI initialization
134
// SPI disabled
135
SPCR=0x00;
136
137
// TWI initialization
138
// TWI disabled
139
TWCR=0x00;
140
141
// 1 Wire Bus initialization
142
// 1 Wire Data port: PORTB
143
// 1 Wire Data bit: 2
144
// Note: 1 Wire port settings must be specified in the
145
// Project|Configure|C Compiler|Libraries|1 Wire IDE menu.
146
// w1_init(); <-------------------------
147
148
// Alphanumeric LCD initialization
149
// Connections specified in the
150
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
151
// RS - PORTA Bit 0
152
// RD - PORTA Bit 1
153
// EN - PORTA Bit 2
154
// D4 - PORTA Bit 4
155
// D5 - PORTA Bit 5
156
// D6 - PORTA Bit 6
157
// D7 - PORTA Bit 7
158
// Characters/line: 16
159
lcd_init(16);
160
161
while (1)
162
      {
163
      init_res = w1_init();
164
165
      if (init_res ==1)
166
      {
167
         lcd_clear(); 
168
         lcd_gotoxy(1,0);
169
         lcd_puts("Chip Connected");   
170
         delay_ms(1000);
171
172
         lcd_clear();
173
         lcd_gotoxy(1,0);
174
         lcd_puts("Serial Number:"); 
175
176
         for (i=1;i<9;i++)
177
            {
178
               read_byte=w1_read();
179
               lcd_gotoxy(i,1);
180
               sprintf(read_byte_char,"%i",read_byte); 
181
               lcd_puts(read_byte_char);   
182
               delay_ms(1000);
183
            };
184
185
186
      }
187
      else
188
      {
189
190
        lcd_clear();
191
        lcd_gotoxy(1,1);
192
        lcd_puts("Chip Not Found");
193
      } 
194
      delay_ms(2000);
195
      }
196
}

Q1: Why?

Q2: Each time that I call w1_read function, DS2401 send me next 1 byte? 
or the same first 1 byte? or total of the ROM?

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.