The Sample program to interface AT93C56 EEPROM IC with STM32F103RB controller.

Dependencies:   mbed-rtos mbed

Committer:
akhilpanayamparambil
Date:
Fri Oct 31 07:01:08 2014 +0000
Revision:
0:d6477e83445b
Interface code for AT93C56A;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akhilpanayamparambil 0:d6477e83445b 1 #include "mbed.h"
akhilpanayamparambil 0:d6477e83445b 2
akhilpanayamparambil 0:d6477e83445b 3 #define EE_READ 0x400 // 10 XXXXXXXXX(A8-A0)
akhilpanayamparambil 0:d6477e83445b 4 #define EE_WRITE 0x200 // 01 XXXXXXXXX(A8-A0)
akhilpanayamparambil 0:d6477e83445b 5 #define EE_EWEN 0x1FF // 00 11XXXXXXX(X is DONT CARE)
akhilpanayamparambil 0:d6477e83445b 6 #define EE_EWDS 0x000 // 00 00XXXXXXX(X is DONT CARE)
akhilpanayamparambil 0:d6477e83445b 7 #define EE_ERASE 0x600 // 11 XXXXXXXXX(A8-A0)
akhilpanayamparambil 0:d6477e83445b 8
akhilpanayamparambil 0:d6477e83445b 9 Serial device(PA_9, PA_10); // tx, rx
akhilpanayamparambil 0:d6477e83445b 10 DigitalOut CS(PC_0);
akhilpanayamparambil 0:d6477e83445b 11 DigitalOut CK(PB_13);
akhilpanayamparambil 0:d6477e83445b 12 DigitalIn DI(PB_14);
akhilpanayamparambil 0:d6477e83445b 13 DigitalOut DO(PB_15);
akhilpanayamparambil 0:d6477e83445b 14
akhilpanayamparambil 0:d6477e83445b 15 void vSend(unsigned short usCommand)
akhilpanayamparambil 0:d6477e83445b 16 {
akhilpanayamparambil 0:d6477e83445b 17 signed char i=10;
akhilpanayamparambil 0:d6477e83445b 18 DO=1; //SB
akhilpanayamparambil 0:d6477e83445b 19 CS=1; // Chip Select High
akhilpanayamparambil 0:d6477e83445b 20 wait_us(1); // SB Clock Generation
akhilpanayamparambil 0:d6477e83445b 21 CK=1;
akhilpanayamparambil 0:d6477e83445b 22 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 23 CK=0;
akhilpanayamparambil 0:d6477e83445b 24 while(i>=0)
akhilpanayamparambil 0:d6477e83445b 25 {
akhilpanayamparambil 0:d6477e83445b 26 DO = (usCommand>>i)&0x01;
akhilpanayamparambil 0:d6477e83445b 27 i--;
akhilpanayamparambil 0:d6477e83445b 28 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 29 CK=1;
akhilpanayamparambil 0:d6477e83445b 30 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 31 CK=0;
akhilpanayamparambil 0:d6477e83445b 32 }
akhilpanayamparambil 0:d6477e83445b 33 }
akhilpanayamparambil 0:d6477e83445b 34 void vWriteEnable()
akhilpanayamparambil 0:d6477e83445b 35 {
akhilpanayamparambil 0:d6477e83445b 36 vSend(EE_EWEN);
akhilpanayamparambil 0:d6477e83445b 37 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 38 CS=0; //Chip Select Low
akhilpanayamparambil 0:d6477e83445b 39 }
akhilpanayamparambil 0:d6477e83445b 40 void vWriteDisable()
akhilpanayamparambil 0:d6477e83445b 41 {
akhilpanayamparambil 0:d6477e83445b 42 vSend(EE_EWDS);
akhilpanayamparambil 0:d6477e83445b 43 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 44 CS=0; //Chip Select Low
akhilpanayamparambil 0:d6477e83445b 45 }
akhilpanayamparambil 0:d6477e83445b 46 void vErase(unsigned short usAddr)
akhilpanayamparambil 0:d6477e83445b 47 {
akhilpanayamparambil 0:d6477e83445b 48 vSend(EE_ERASE|usAddr);
akhilpanayamparambil 0:d6477e83445b 49 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 50 CS=0;
akhilpanayamparambil 0:d6477e83445b 51 /** wait busy flag clear */
akhilpanayamparambil 0:d6477e83445b 52 wait_us(1); // tcs > 250ns @2.7V
akhilpanayamparambil 0:d6477e83445b 53 CS=1;
akhilpanayamparambil 0:d6477e83445b 54 wait_us(1); // tsv < 250ns @2.7V
akhilpanayamparambil 0:d6477e83445b 55 while(DI==0); // 0.1ms < twp < 10ms
akhilpanayamparambil 0:d6477e83445b 56 CS=0;
akhilpanayamparambil 0:d6477e83445b 57 }
akhilpanayamparambil 0:d6477e83445b 58 void vWrite(unsigned short usAddr, unsigned char ucData)
akhilpanayamparambil 0:d6477e83445b 59 {
akhilpanayamparambil 0:d6477e83445b 60 signed char i=7;
akhilpanayamparambil 0:d6477e83445b 61 vSend(EE_WRITE|usAddr);
akhilpanayamparambil 0:d6477e83445b 62 for(i=7;i>=0;i--)
akhilpanayamparambil 0:d6477e83445b 63 {
akhilpanayamparambil 0:d6477e83445b 64 DO = (int)( (ucData>>i)&0x0001 );
akhilpanayamparambil 0:d6477e83445b 65 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 66 CK=1;
akhilpanayamparambil 0:d6477e83445b 67 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 68 CK=0;
akhilpanayamparambil 0:d6477e83445b 69 }
akhilpanayamparambil 0:d6477e83445b 70 CS=0;
akhilpanayamparambil 0:d6477e83445b 71 /** wait busy flag clear */
akhilpanayamparambil 0:d6477e83445b 72 wait_us(1); // tcs > 250ns @2.7V
akhilpanayamparambil 0:d6477e83445b 73 CS=1;
akhilpanayamparambil 0:d6477e83445b 74 wait_us(1); // tsv < 250ns @2.7V
akhilpanayamparambil 0:d6477e83445b 75 while(DI==0); // 0.1ms < twp < 10ms
akhilpanayamparambil 0:d6477e83445b 76 CS=0;
akhilpanayamparambil 0:d6477e83445b 77 }
akhilpanayamparambil 0:d6477e83445b 78 unsigned char ucRead(unsigned short usAddr)
akhilpanayamparambil 0:d6477e83445b 79 {
akhilpanayamparambil 0:d6477e83445b 80 unsigned char data=0;
akhilpanayamparambil 0:d6477e83445b 81 signed char i=7;
akhilpanayamparambil 0:d6477e83445b 82
akhilpanayamparambil 0:d6477e83445b 83 vSend(EE_READ|usAddr);
akhilpanayamparambil 0:d6477e83445b 84 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 85
akhilpanayamparambil 0:d6477e83445b 86 for(i=7;i>=0;i--)
akhilpanayamparambil 0:d6477e83445b 87 {
akhilpanayamparambil 0:d6477e83445b 88 CK=1;
akhilpanayamparambil 0:d6477e83445b 89 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 90 CK=0;
akhilpanayamparambil 0:d6477e83445b 91 data = data | (DI<<i);
akhilpanayamparambil 0:d6477e83445b 92 wait_us(1);
akhilpanayamparambil 0:d6477e83445b 93 }
akhilpanayamparambil 0:d6477e83445b 94 CS=0;
akhilpanayamparambil 0:d6477e83445b 95
akhilpanayamparambil 0:d6477e83445b 96 return data;
akhilpanayamparambil 0:d6477e83445b 97 }
akhilpanayamparambil 0:d6477e83445b 98 int main()
akhilpanayamparambil 0:d6477e83445b 99 {
akhilpanayamparambil 0:d6477e83445b 100 unsigned char ucI=0;
akhilpanayamparambil 0:d6477e83445b 101
akhilpanayamparambil 0:d6477e83445b 102 CS = 0; //default state for Chip Select and Clock
akhilpanayamparambil 0:d6477e83445b 103 CK = 0;
akhilpanayamparambil 0:d6477e83445b 104
akhilpanayamparambil 0:d6477e83445b 105 device.baud(19200);
akhilpanayamparambil 0:d6477e83445b 106 device.printf("The Program demostrating the IC AT93C56 EEPROM \r\n");
akhilpanayamparambil 0:d6477e83445b 107
akhilpanayamparambil 0:d6477e83445b 108 device.printf("Going to Write Enable \r\n");
akhilpanayamparambil 0:d6477e83445b 109 vWriteEnable();
akhilpanayamparambil 0:d6477e83445b 110
akhilpanayamparambil 0:d6477e83445b 111 device.printf("Going to Erase Addr 0 \r\n");
akhilpanayamparambil 0:d6477e83445b 112 vErase(0);
akhilpanayamparambil 0:d6477e83445b 113 device.printf("Going to Write Value AA to Addr 0 \r\n");
akhilpanayamparambil 0:d6477e83445b 114 vWrite(0,0xAA);
akhilpanayamparambil 0:d6477e83445b 115 device.printf("Read Back Value %04X\r\n,",ucRead(0));
akhilpanayamparambil 0:d6477e83445b 116
akhilpanayamparambil 0:d6477e83445b 117 device.printf("Going to Erase Addr 1 \r\n");
akhilpanayamparambil 0:d6477e83445b 118 vErase(1);
akhilpanayamparambil 0:d6477e83445b 119 device.printf("Going to Write Value BB to Addr 1 \r\n");
akhilpanayamparambil 0:d6477e83445b 120 vWrite(1,0xBB);
akhilpanayamparambil 0:d6477e83445b 121 device.printf("Read Back Value %04X\r\n,",ucRead(1));
akhilpanayamparambil 0:d6477e83445b 122
akhilpanayamparambil 0:d6477e83445b 123 device.printf("Going to Erase Addr 2 \r\n");
akhilpanayamparambil 0:d6477e83445b 124 vErase(2);
akhilpanayamparambil 0:d6477e83445b 125 device.printf("Going to Write Value CC to Addr 2 \r\n");
akhilpanayamparambil 0:d6477e83445b 126 vWrite(2,0xCC);
akhilpanayamparambil 0:d6477e83445b 127 device.printf("Read Back Value %04X\r\n,",ucRead(2));
akhilpanayamparambil 0:d6477e83445b 128
akhilpanayamparambil 0:d6477e83445b 129 device.printf("Going to Write Disable \r\n");
akhilpanayamparambil 0:d6477e83445b 130
akhilpanayamparambil 0:d6477e83445b 131 vWriteDisable();
akhilpanayamparambil 0:d6477e83445b 132
akhilpanayamparambil 0:d6477e83445b 133 while(1)
akhilpanayamparambil 0:d6477e83445b 134 {
akhilpanayamparambil 0:d6477e83445b 135 for(ucI=0;ucI<64;ucI++)
akhilpanayamparambil 0:d6477e83445b 136 {
akhilpanayamparambil 0:d6477e83445b 137 device.printf("%04X ============== %d\r\n,",ucRead(ucI),ucI);
akhilpanayamparambil 0:d6477e83445b 138 wait(0.5);
akhilpanayamparambil 0:d6477e83445b 139 }
akhilpanayamparambil 0:d6477e83445b 140 }
akhilpanayamparambil 0:d6477e83445b 141 }