Guest User

Untitled

a guest
Jul 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include "eeprom.h"
  2.  
  3. void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
  4. {
  5. /* Wait for completion of previous write */
  6. while(EECR & (1<<EEWE)) //ждем освобождения флага окончания последней операцией с памятью
  7. {}
  8. EEAR = uiAddress; //Устанавливаем адрес
  9. EEDR = ucData; //Пищем данные в регистр
  10. EECR |= (1<<EEMWE); //Разрешаем запись
  11. EECR |= (1<<EEWE); //Пишем байт в память
  12. }
  13.  
  14. unsigned char EEPROM_read(unsigned int uiAddress)
  15. {
  16. while(EECR & (1<<EEWE))
  17. {} //ждем освобождения флага окончания последней операцией с памятью
  18. EEAR = uiAddress; //Устанавливаем адрес
  19. EECR |= (1<<EERE); //Запускаем операцию считывания из памяти в регистр данных
  20. return EEDR; //Возвращаем результат
  21. }
  22.  
  23. void EEPROM_write_word(unsigned int uiAddress, uint16_t ucData)
  24. {
  25. EEPROM_write(uiAddress, (unsigned char) ucData);
  26. unsigned char dt = ucData>>8;
  27. EEPROM_write(uiAddress+1, dt);
  28. }
  29.  
  30. uint16_t EEPROM_read_word(unsigned int uiAddress)
  31. {
  32. uint16_t dt = EEPROM_read(uiAddress+1)*256;
  33. asm("nop");
  34. dt += EEPROM_read(uiAddress);
  35. return dt;
  36. }
  37.  
  38. void EEPROM_write_dword(unsigned int uiAddress, uint32_t ucData)
  39. {
  40. EEPROM_write_word(uiAddress, (uint16_t) ucData);
  41. uint16_t dt = ucData>>16;
  42. EEPROM_write_word(uiAddress+2, dt);
  43. }
  44.  
  45. uint32_t EEPROM_read_dword(unsigned int uiAddress)
  46. {
  47. uint32_t dt = EEPROM_read_word(uiAddress+2)*65536;
  48. asm("nop");
  49. dt += EEPROM_read_word(uiAddress);
  50. return dt;
  51. }
  52.  
  53. void EEPROM_write_string(unsigned int uiAddress, char str1[])
  54. {
  55. wchar_t n;
  56. for(n=0;str1[n]!='\0';n++)
  57. EEPROM_write(uiAddress+n,str1[n]);
  58. }
  59.  
  60. const char* EEPROM_read_string(unsigned int uiAddress, unsigned int sz)
  61. {
  62. unsigned int i;
  63. char* str1;
  64. str1 = (char *) realloc(NULL,sz);
  65. for (i=0;i<sz;i++)
  66. str1[i] = EEPROM_read(uiAddress+i);
  67. return str1;
  68. }
Add Comment
Please, Sign In to add comment