Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] Getting garbage data from external eeprom on STK600

Status
Not open for further replies.

Mandar Joshi

Member level 2
Joined
Mar 4, 2015
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
445
Hello,
I am using STK600 and I have written code to read data from external dataflash using SPI interrupt and then put the same data on hyperterminal in PWM interrupt. But I get only first value of data byte as correct rest as 0x00. I also tried by using status register read command after receiving every single byte of data. There also I get the value of status register as 0x00.
My both PWM and SPI interrupts are working properly. Please tell me what is wrong with my code. Here is my code



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <asf.h>
 
//Slave select low
 
#define cs_low() \
delay_us(10);\
PORTB=(0<<PB0);\
delay_us(10);
 
//Slave select high
 
#define cs_high() \
delay_us(10);\
PORTB=(1<<PB0);\
delay_us(10);
 
 
 
char opcode_cnt=0;               //Counts number bytes required before start reading actual data  
unsigned char data;              //Variable to save save received data
volatile char rd_cmplete_flg=0;  //Flag is set after read operation completed
int pg_addr;                     //Indicates address of page
int buff_addr;                   //Indicates starting address of buffer
volatile unsigned int i=0;       //Counts number bytes read
char pwm_init_flg=0;             //Used only once in init_pwm() function
char sample_count=4;
unsigned char continuous_rd_flg=0,status_rd_flg=0;      //continuous_rd_flg to trigger data read in ISR and status_rd_flg to trigger status register read
 
 
void init_pwm(void);
void spimstr_init(void);
void spi_tx(unsigned char byte);
void spi_tx_ISR(unsigned char byte);
unsigned char spi_rx_ISR(void);
unsigned char spi_rx(void);
unsigned char get_status(void);
void init_uart(void);
void continuous_pg_read(unsigned int page_address,unsigned int buffer_address);
 
 
 
int main (void)
{
    // Insert system clock initialization code here (sysclk_init()).
 
    board_init();
    
    init_uart();
    
    spimstr_init();
    
    continuous_pg_read(0x00,0x00);
    
    while(1)
   {
      if(rd_cmplete_flg)
     {
       rd_cmplete_flg=0;     
       
       init_pwm();
     } 
      
      if(i>=23923)
     {
       break;
     } 
   }
    
    // Insert application code here, after the board has been initialized.
}
 
 
 
 
void init_pwm()
{
  if(!pwm_init_flg)
 { 
  pwm_init_flg=0;
 
  DDRB = (1<<PB5);                          // set OC1A as output
 
  TCCR1A = (1<<COM1A1)|(1<<COM1A0)|(1<<WGM10);// set OC1A on compare match, clear OC1A at bottom
     // 8 bit fast PWM
  TCCR1B = (1<<WGM12)|(1<<CS10);                // 8bit fast PWM and clock=8Mhz
 }
 
  TIMSK1 |= (1<<TOIE1);                         // enable timer 1 overflow interrupt
}
 
 
 
void spimstr_init()
{
    //Master Mode Selected
    //MSB out first
    //Clock Polarity and Clock Phase selected as 1
    //Clock Frequency 8MHz/128 selected
 
    SPCR|=(1<<SPE)|(0<<DORD)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA)|(0<<SPR1)|(0<<SPR0);
    
    SPSR=(1<<SPI2X);
}
 
 
 
 
void spi_tx(unsigned char byte)
{
    SPDR=byte;
    
    while(!(SPSR&(1<<SPIF)));        //wait until transmission is completed
}
 
 
 
void spi_tx_ISR(unsigned char byte)
{
    SPDR=byte;
}
 
 
 
unsigned char spi_rx()
{
    while(!(SPSR&(1<<SPIF)));        //wait until reception is completed
    
    return SPDR;
}
 
 
unsigned char spi_rx_ISR()
{
    return SPDR;
}
 
 
 
 
unsigned char get_status()
{
    unsigned char status;
    
    //high to low transition made on SS
    
    cs_low();
    
    spi_tx(0xd7);            //opcode to read status register of EEPROM
    
    status=spi_rx();         //Received the value of status register of EEPROM
    
    status=status & 0x80;
 
    //low to high transition made on SS
    
    cs_high();
    
    return status;
}
 
 
void init_uart()
{
    UBRR1H=0x00;
 
    UBRR1L=0x0c;
    
    UCSR1B|=(1<<RXEN1)|(1<<TXEN1);
    
    UCSR1C|=(1<<UCSZ11)|(1<<UCSZ10);
}
 
 
void continuous_pg_read(unsigned int page_address,unsigned int buffer_address)
{
    pg_addr=page_address;
    
    buff_addr=buffer_address;
    
    while(get_status()==0x00);
    
    cs_low();
    
    spi_tx(0xe8);                           //opcode to read page
    
    SREG=0x80;
    
    SPCR|=(1<<SPIE);
}
 
 
//ISR for Continuous Array Read mode
 
ISR(SPI_STC_vect)
{
  //PORTA=0xff;                            //LED port to ensure the working of interrupt
  
  //delay_ms(1500);
  
  if(!continuous_rd_flg)
 { 
      opcode_cnt++;
  
     switch(opcode_cnt)
    {
       case 1:
      
              spi_tx_ISR((0x00)|(pg_addr>>7));
    
              break;
    
       case 2:
     
              spi_tx_ISR((pg_addr<<1)|(buff_addr>>8));
    
              break;
    
       case 3:
             
              spi_tx_ISR(buff_addr & 0x00ff);
    
              break;
    
       case 4:
      
              spi_tx_ISR(0x00);
    
              break;
    
       case 5:
      
              spi_tx_ISR(0x00);
    
              break;
    
       case 6:
      
              spi_tx_ISR(0x00);
    
              break;
    
       case 7:
      
              spi_tx_ISR(0x00);
    
              break;
      
       case 8:
    
              spi_tx_ISR(0x00);
      
              break;   
  }
 
 
      if(opcode_cnt>=9)
     {
              opcode_cnt=0;                //Reset to zero to repeat switch case
     
              if(rd_cmplete_flg==0)
             {
                    data=spi_rx_ISR();
        
                    rd_cmplete_flg=1;
             }
              //Important Note
     
              //The buffer address and page address has to be incremented before cs_high() only then only this mode works
     
              if(buff_addr<263)            
             {
                    buff_addr++;
             }
      
              else
             {
                    buff_addr=0;         
     
                    pg_addr++;
             }
     
              cs_high();
             
              continuous_rd_flg=1;
     }
 }
  if(status_rd_flg)
 {
      unsigned char status;
    
      status=spi_rx_ISR();  
      
      status=status & 0x80;        
      
      cs_high();
      
      status_rd_flg=0;
      
      continuous_rd_flg=0;
      
      cs_low();
      
      spi_tx_ISR(0xe8);                 //Resend continuous read opcode to read next byte 
 }
 
 //PORTA=~PORTA;
 
 //delay_ms(1500);
}
 
 
ISR(TIMER1_OVF_vect)
{
  //PORTA=0x55;                //LED port to ensure the working of interrupt
  
  //delay_ms(1500);
  
  sample_count--;
 
  if(sample_count==0)
 {
   sample_count=4;
   
   TIMSK1=(0<<TOIE1); 
   
   UDR1=data;
   
   while(!(UCSR1A & (1<<UDRE1)));
   
   i++;
   
   status_rd_flg=1;
   
   cs_low();
   
   spi_tx_ISR(0xd7);       //Send opcode to read status register 
}   
 
  //PORTA=~PORTA;
  
  //delay_ms(1500);
}
 
 
 
void board_init(void)
{
 DDRA|=0xff;                          //optput port for LED
  
 DDRB|=(1<<DDB2)|(1<<DDB1)|(1<<DDB0);     //Direction of SS,MISO,SCK,MOSI pin selected
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top