SPI EEPROM

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nikhiledutech
Posts: 117
Joined: Wed Dec 27, 2017 8:52 am

SPI EEPROM

Post by nikhiledutech » Tue May 15, 2018 9:08 am

Hello,
I am currently interfacing stepper motor with stm32f407 disc board. I have created a list of functions. This functions are basically for setting different GPIO pins.

So problem that i face is one of this : it's not properly iterating or the pins are not getting reset for the appropriate time. As i saw i oscilloscope the duration of pins value going low is very less as compared to the desired result.

I have given another piece of code in last, which worked. there i just pass functions directly instead of passing functions from the list and it worked. So do you have any idea, where i am going wrong in ASK25_SM_Send_Sequence() function.



So here goes my function for setting pins up.

Code: Select all


#Clearing the Stepper Motor Pins
def pins_reset():
	st_motor_pins[0].value(0)
	st_motor_pins[1].value(0)	
	st_motor_pins[2].value(0)
	st_motor_pins[3].value(0)




#Sequence of Stepper Motor Pins
def Zero_A():
	st_motor_pins[0].value(1)
	st_motor_pins[1].value(1)		


def Eight_Eight():
	st_motor_pins[1].value(1)
	st_motor_pins[3].value(1)		


def A_Zero():
	st_motor_pins[2].value(1)
	st_motor_pins[3].value(1)		


def Two_Two():
	st_motor_pins[0].value(1)
	st_motor_pins[2].value(1)	


Now i have made a function to test the directions, and if its clockwise it will iterate through clockwise functions list and vice versa for anti clockwise.


This function rotates stepper motor in desired direction:

Code: Select all

def ASK25_SM_Rotate (StMotorDirection, Angle, Delay):
	i = Count = 0
	Count = ((Angle * 10) / CAL_ANGLE)
	for x in range (Count):
		ASK25_SM_Send_Sequence(StMotorDirection, Delay)



And here is the function that will be sending Sequence of pins to Set and reset

Code: Select all


def ASK25_SM_Send_Sequence(StMotorDirection, Delay):

	temp = 0
	SmAntClk = [Zero_A, Eight_Eight, A_Zero, Two_Two]
	SmClk = [Two_Two, A_Zero, Eight_Eight, Zero_A]
	while True:
		if (temp < 4):
			if (StMotorDirection == MotorClockwise):
				pins_reset()
				SmClk[temp]()

			elif (StMotorDirection == MotorAntiClockwise):
				pins_reset()
				SmAntClk[temp]()	#Sending the appropriate sequence from list 

			pyb.delay(60)
			temp+=1
			if (temp == 4): 
				temp = 0	





And here goes my main function . Passing clockwise diection with 360 angle and 60ms delay

Code: Select all

def main():
	# Initialize Stepper Motor pins 
	ASK25_SM_Init()

	# Rotate stepper motor 360 degree with delay 60 
	ASK25_SM_Rotate(MotorClockwise,360,60)


main()


Here is the function that worked.

Code: Select all



def ASK25_SM_Send_Sequence(StMotorDirection, Delay):

	if (StMotorDirection == MotorClockwise):
		pins_reset()		#Clearing the pins
		Two_Two()			#sending step 0x22
		pyb.delay(Delay)	#Delay 
		pins_reset()
		A_Zero()			#Sending step 0xA0
		pyb.delay(Delay)
		pins_reset()
		Eight_Eight()		#Sending step 0x88
		pyb.delay(Delay)
		pins_reset()
		Zero_A()			#Sending step 0x0A
		pyb.delay(Delay)


	elif (StMotorDirection == MotorAntiClockwise):
		pins_reset()		#Clearing the pins
		Zero_A()			#sending step 0x0A
		pyb.delay(Delay)	#Delay
		pins_reset()
		Eight_Eight()		#sending step 0x88
		pyb.delay(Delay)
		pins_reset()
		A_Zero()			#sending step 0xA0
		pyb.delay(Delay)
		pins_reset()
		Two_Two()			#sending step 0x22
		pyb.delay(Delay)


Last edited by nikhiledutech on Wed May 16, 2018 9:33 am, edited 1 time in total.

User avatar
pythoncoder
Posts: 2584
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem in Iterating through list

Post by pythoncoder » Tue May 15, 2018 5:52 pm

If you do a forum search on the keyword "stepper" you'll find a number of solutions to driving stepper motors.
Peter Hinch

nikhiledutech
Posts: 117
Joined: Wed Dec 27, 2017 8:52 am

Re: Problem in Iterating through list

Post by nikhiledutech » Wed May 16, 2018 5:09 am

Hey,

I already have successfully interfaced Stepper Motor. Its just that my loop was not working properly.

Sir I have searched in forum regarding how to read and write from SPI based EEPROM. I haven't found anything related to SPI.

So can you give any tip to interface SPI based EEPROM.

User avatar
pythoncoder
Posts: 2584
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

SPI EEPROM

Post by pythoncoder » Wed May 16, 2018 5:38 am

This file might provide some hints, although it is for a Flash device requiring block erasure. EEPROM chips usually can be byte-erased: if your chip is such the driver could be substantially simplified. But the code shows how to implement the block protocol which is required to enable MicroPython to mount the device as a filesystem.
Peter Hinch

nikhiledutech
Posts: 117
Joined: Wed Dec 27, 2017 8:52 am

Re: Problem in Iterating through list

Post by nikhiledutech » Wed May 16, 2018 9:33 am

Okay here goes my short code, to read write and read byte to EEPROM. In the below code , i have used a byte array to store eeprom address along with data. It works fine, but if i pass a hex value as my data to be written, and later on read it gives me decimal of the hex value written. So for that reason i have used hex conversion with return in read byte function. Without hex conversion, only decimal value written are read properly.


Also problem here is if i want to send a character, than it gives me error. As byte array don't accept "string" elements. So if i want to use the same code to send a character suppose " A" using spi_driver.ASK25_25AA160A_Write_Byte(0x0008, "A")

Can you suggest me, how can I send a character using same function ?

EEP_WRITE = 0x02 # Write bit of IR
EEP_READ = 0x03 # Read bit of IR
EEP_WREN = 0x06 #Write Enable Latch
EEP_WRDI = 0x04 # Write Disable Latch
EEP_RDSR = 0x05 # Read Status Reg
EEP_WRSR = 0x01 # Write Status Reg


# brief Write Byte value at desired address(0x000 to 0x7FF)
# eep_address EEPROM 16bit address
# param byte_data byte data to be written

def ASK25_25AA160A_Write_Byte(eep_address,byte_data):
Tx_Buf = bytearray(4)
WE = EEP_WREN

Tx_Buf[0] = EEP_WRITE
Tx_Buf[1] = eep_address>>8 # 1st byte extract
Tx_Buf[2] = eep_address # 2nd byte extract
Tx_Buf[3] = byte_data

SPI2_CS_Force(DISABLE)
status = spi.send(WE, timeout=500)
if(status == None):
SPI2_CS_Force(ENABLE)
else:
return(status)

SPI2_CS_Force(DISABLE)
status = spi.send(Tx_Buf, timeout=500)
if(status == None):
SPI2_CS_Force(ENABLE)
delay(5)




# Read Byte value at desired address(0x000 to 0x7FF)
# param eep_address EEPROM 16bit address
# Read Byte is returned
def ASK25_25AA160A_Read_Byte (eep_address):
Tx_Buf = bytearray(3)
Rx_Buf = bytearray(1)

Tx_Buf[0] = EEP_READ
Tx_Buf[1] = (eep_address>>8) # // 1st byte extract
Tx_Buf[2] = eep_address # 2nd byte extract

SPI2_CS_Force(DISABLE)
status = spi.send(Tx_Buf, timeout=500)
status = spi.recv(Rx_Buf, timeout=500)
if(status == None):
SPI2_CS_Force(ENABLE);
delay(5) # Delay for 5ms Read Cycle

return(hex(Rx_Buf[0]))
And this goes my main function.

Code: Select all



def main():

	#data can be any hex value 	
	spi_driver.ASK25_25AA160A_Write_Byte(0x0008,0x46)		#Write Data Address = 0x0005 Data = 0x41
	read_data = spi_driver.ASK25_25AA160A_Read_Byte(0x0008)	#Read Data from eeeprom address = 0x05
	print("Read Data : " ,read_data)				#read data will be printed on UART 


main()

Thanks

User avatar
pythoncoder
Posts: 2584
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: SPI EEPROM

Post by pythoncoder » Thu May 17, 2018 7:37 am

You need to read up on the way Python 3 handles Unicode. A string is not the same as a bytes object as a Unicode character can occupy multiple bytes. But they can be converted. Hints:

Code: Select all

>>> a = 'the quick brown fox'
>>> type(a)
<class 'str'>
>>> b = a.encode('UTF8')
>>> type(b)
<class 'bytes'>
>>> c = bytearray(b)
>>> c
bytearray(b'the quick brown fox')
>>> chr(c[0])
't'
Peter Hinch

nikhiledutech
Posts: 117
Joined: Wed Dec 27, 2017 8:52 am

Re: SPI EEPROM

Post by nikhiledutech » Fri May 18, 2018 12:32 pm

Okay thank you sir

Post Reply