In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Mike_W wrote: [4] After reading through examples and text about EEPROM I made an attempt at a program to write and read from a ST M93C46 MICROWIRE serial access EEPROM. I did not have much luck. I listed the program and a schematic of the part I have. The EEPROM is mounted to a small board and is configured for 16 bit operation. If anyone could give me an idea where I am going wrong I would appreciate it. Mike DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 ' ------------------------------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------------------------------- Chip_Select VAR RC.1 ' S M93C46 Pin 1 Purple Serial_Data_Input VAR RC.2 ' D M93C46 Pin 2 Blue Serial_Data_Output VAR RC.3 ' Q M93C46 Pin 3 Orange Serial_Clock VAR RC.4 ' C M93C46 Pin 4 Yellow ' ------------------------------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------------------------------- ' Device Op-codes plus a 1 start bit Read_Device CON 110 ' read Write_Device CON 101 ' write Erase_Device CON 111 ' erase a byte or word EWEN CON 10011 ' erase/write enable EWDS CON 10000 ' erase/write disable ERAL CON 10010 ' erase all memory WRAL CON 10001 ' write all memory ' ------------------------------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------------------------------- tmpW1 VAR WORD ' work variable tmpW2 VAR WORD ' data written to device tmpB1 VAR BYTE ' address for reads or writes tmpB2 VAR BYTE ' data read from device ' ------------------------------------------------------------------------------------------------- ' Sub Declarations ' ------------------------------------------------------------------------------------------------- Device_Busy SUB ' check if device is writing Device_Enable SUB ' enable the device - erase/write Device_Disable SUB ' disable the device - erase/write Write_to_Device SUB ' write to device Read_from_Device SUB ' read from device Erase_Part SUB ' erase byte or word Erase_All SUB ' erase all memory Write_All SUB ' write all memory with same data Delay_MS SUB 1, 2 ' delay in milliseconds Delay_US SUB 1, 2 ' delay in microseconds ' ================================================================================================= PROGRAM Start ' ================================================================================================= WATCH tmpB2,16,UDEC WATCH tmpW2,16,UDEC ' value 251 ' ------------------------------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------------------------------- Start: LOW Chip_Select ' deactivate device Delay_Ms 1000 ' pause 1000 milliseconds 1 second Main: tmpW2 = 251 ' value to write tmpB1 = %000001 ' address value GOSUB Device_Enable ' enable device for writes Delay_US 1 GOSUB Write_to_Device ' write the data Delay_US 1 GOSUB Device_Busy ' wait for write to stop Delay_US 1 GOSUB Device_Disable ' disable device only write/erase affected Delay_US 1 GOSUB Read_from_Device ' read the data LOW Chip_Select ' deactivate device END ' end program ' ================================================================================================= ' Subroutines / Jump Table ' ================================================================================================= Device_Busy: DO WHILE Serial_Data_Output = 1 ' loop until write finishes LOOP LOW Chip_Select ' deactivate device RETURN ' ------------------------------------------------------------------------------------------------- Device_Enable: ' erase/write enable LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, EWEN \5 ' send enable opcode LOW Chip_Select ' deactivate device RETURN ' ------------------------------------------------------------------------------------------------- Device_Disable: ' erase/write disable LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, EWDS \5 ' send disable opcode LOW Chip_Select ' deactivate device RETURN ' ------------------------------------------------------------------------------------------------- Write_to_Device: ' write to device LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, Write_Device \3 ' send write opcode SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, tmpB1 \6 ' send the address SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, tmpW2 ' send the data LOW Chip_Select ' deactivate device RETURN ' ------------------------------------------------------------------------------------------------- Read_from_Device: ' read from device LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, Read_Device \3 ' send read opcode SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, tmpB1 \6 ' send the address SHIFTIN Serial_Data_Output, Serial_Clock, MSBPRE, tmpB2 \4 ' read the data data is msb first LOW Chip_Select ' deactivate device RETURN ' ------------------------------------------------------------------------------------------------- Erase_Part: LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, Erase_Device \3 ' send write opcode SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, tmpB1 \6 ' send the address LOW Chip_Select RETURN ' ------------------------------------------------------------------------------------------------- Erase_All: LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, ERAL \5 ' send write opcode SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, tmpB1 \6 ' send the address LOW Chip_Select RETURN ' ------------------------------------------------------------------------------------------------- Write_All: LOW Serial_Clock ' hold clock low to start instruction HIGH Chip_Select ' activate device SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, WRAL \5 ' send write opcode SHIFTOUT Serial_Data_Input, Serial_Clock, MSBFIRST, tmpB1 \6 ' send the address LOW Chip_Select RETURN ' ------------------------------------------------------------------------------------------------- ' Use: DELAY_MS mSecs (one thousandth of a second) ' -- delays program in milliseconds Delay_MS: IF __PARAMCNT = 1 THEN ' get delay value tmpW1 = __PARAM1 ' ELSE ' store delay value tmpW1 = __WPARAM12 ' ENDIF ' do delay PAUSE tmpW1 ' pause for tmpW1 milliseconds RETURN ' ------------------------------------------------------------------------------------------------- ' Use: DELAY_US uSecs (one millionth of a second) ' -- delays program in microseconds DELAY_US: IF __PARAMCNT = 1 THEN tmpW1 = __PARAM1 ELSE tmpW1 = __WPARAM12 ENDIF PAUSEUS tmpW1 RETURN [/4] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=208841 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2007 (http://www.dotNetBB.com)