UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


code:i2c_eeprom

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
code:i2c_eeprom [2008/11/27 02:34] laurencebcode:i2c_eeprom [2009/03/20 04:50] (current) laurenceb
Line 18: Line 18:
  u16 timeout=1;  u16 timeout=1;
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //send start  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //send start
- while (!(TWCR & (1<<TWINT)) && timeout);+ while (!(TWCR & (1<<TWINT)) && timeout)
  timeout++; //wait for a start to be transmitted  timeout++; //wait for a start to be transmitted
  if(!timeout)  if(!timeout)
Line 31: Line 31:
  u16 timeout=1;  u16 timeout=1;
  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
- while (!(TWCR & (1<<TWSTO)) && timeout);//wait for stop to be sent+ while (!(TWCR & (1<<TWSTO)) && timeout) //wait for stop to be sent
  timeout++;  timeout++;
  if(!timeout)  if(!timeout)
Line 42: Line 42:
  TWDR = c; //load the data  TWDR = c; //load the data
  TWCR = (1<<TWINT)|(1<<TWEN);  TWCR = (1<<TWINT)|(1<<TWEN);
- while (!(TWCR & (1<<TWINT)) && timeout);//wait for it to be sent+ while (!(TWCR & (1<<TWINT)) && timeout) //wait for it to be sent
  timeout++;  timeout++;
  if(!timeout)  if(!timeout)
Line 50: Line 50:
 } }
  
-u08 i2cread(u08 AK)+u08 i2cread(u08 ak)
 { {
  u16 timeout=1;  u16 timeout=1;
- TWCR=AK;+ TWCR=ak;
  while ((!(TWCR & (1<<TWINT))) && timeout)  while ((!(TWCR & (1<<TWINT))) && timeout)
  timeout++; //wait for it to be recieved  timeout++; //wait for it to be recieved
Line 94: Line 94:
  datasize--;  datasize--;
  i2cstart();  i2cstart();
- i2cwrite(SLA_R|(((*address)>>15)&0x02)); //set read to the correct block+ i2cwrite(SLA_R|((u08)((*address)>>15)&0x02)); //set read to the correct block
  for(n=0;n<datasize;n++) //the eeproms address pointer should have been setup already  for(n=0;n<datasize;n++) //the eeproms address pointer should have been setup already
  {  {
Line 105: Line 105:
  }This isn't needed on the Atmel chips*/  }This isn't needed on the Atmel chips*/
  }  }
- destination[datasize+1]=i2cread(_NAK_); //send NAK for the last byte recieved+ destination[datasize]=i2cread(_NAK_); //send NAK for the last byte recieved
  i2cstop();  i2cstop();
 + (*address)++;
 } }
  
Line 112: Line 113:
 u32 findtop() //Find the address of record number where we enter painted eeprom u32 findtop() //Find the address of record number where we enter painted eeprom
 { {
- u32 top=12*((u32)(((u32)1<<16)/12)); //half way through rounded down to nearest 12 + u32 top=12*((u32)1<<13); //half way through rounded down to 12 
- u08 n+ u08 n,endcond=TRUE; 
- u08 endcond=TRUE; //used to solve the problem of running with n=0 + u16 place=1<<13;
- u16 place=5461;+
  for(n=14;endcond;) //start by probing half way through - place is in records  for(n=14;endcond;) //start by probing half way through - place is in records
  {  {
Line 122: Line 122:
  n--; //need to decr here to get 1 byte resolution  n--; //need to decr here to get 1 byte resolution
  else  else
- endcond=FALSE; //terminate the loop after completion+ endcond=FALSE; //terminate the loop after completion //need to decr here to get 1 byte resolution
  i2cstart(); //read the slave  i2cstart(); //read the slave
  i2cwrite(SLA_R);  i2cwrite(SLA_R);
  if(i2cread(_NAK_)==MAGIC_NUMBER) //see if its painted  if(i2cread(_NAK_)==MAGIC_NUMBER) //see if its painted
- place&=~(1<<n); //set or clear the nth bit in the place holder + place&=~(1<<n); //clear the nth bit in the place holder 
- else + if(n) 
- place|=(1<<n);+ place|=(1<<(n-1)); //set the next bit
  i2cstop();  i2cstop();
 + if(place>10922)
 + place=10922;
  top = 12*( (u32)place ); //place is in units of 12  top = 12*( (u32)place ); //place is in units of 12
  }  }
 + set_address(&top);
 + i2cstart();
 + i2cwrite(SLA_R);
 + if(i2cread(_NAK_)!=MAGIC_NUMBER) //check the location to make sure its blank
 + place++;
 + top = 12*( (u32)place );
  return top;  return top;
 } }
Line 139: Line 147:
  u08 magic=MAGIC_NUMBER; //magic is our painting value  u08 magic=MAGIC_NUMBER; //magic is our painting value
  u32 n=0;  u32 n=0;
- set_address(&n); + for(;n<131072;n++/*n+=12*/) //this writes every (12th) byte
- for(;n<131072;n++) //this writes every (12th) byte+
  {  {
- /*set_address(&n);+ /* 
 + set_address(&n);
  i2cwrite(magic); //write the data  i2cwrite(magic); //write the data
  if((u08)n>243) //our next n will be across a page boundary  if((u08)n>243) //our next n will be across a page boundary
Line 149: Line 157:
  _delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );//delay 6 ms for page write  _delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );//delay 6 ms for page write
  }*/  }*/
- if(!((u08)n))+ if(!((u08)n)) //this code is for writing every byte
  {  {
  i2cstop();  i2cstop();
code/i2c_eeprom.1227753277.txt.gz · Last modified: 2008/11/27 02:34 by laurenceb

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki