Multi-slave SPI using ATmega32s

Status
Not open for further replies.

Charisma

Newbie level 3
Joined
Aug 17, 2012
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,302
Hello everyone!
I'm trying to do a project in which I have to get two temperatures from two different locations using LM35s, do the appropriate signal conditioning with 2 Slave AT32s and then send the data to a 3rd AT32 ( the Master). The master has to take the average temperature and send it to a LCD.
here's my schematic and then follows my code:

Slave First (who said Masters come first?):

Code:
        .INCLUDE"M32DEF.INC"
	.EQU SS=4

	LDI R16,HIGH(RAMEND)	;STACK SETTINGS
	OUT SPH,R16
	LDI R16,LOW(RAMEND)
	OUT SPL,R16
;=================IO SETTINGS=================
	CBI DDRB,7
	SBI DDRB,6
	CBI DDRB,5
	CBI DDRB,4

	CBI DDRA,0
	
;=================SPI SETTINGS=====================
	LDI R16,(1<< SPE) 
	OUT SPCR,R16
;==================ADC SETTINGS====================
	LDI R16,( 1<<REFS1 | 1<<REFS0 | 1<<ADLAR )
	OUT ADMUX,R16

	LDI R16,( 1<<ADEN | 1<< ADPS2 | 1<<ADPS1 |1 << ADPS0 )
	OUT ADCSRA,R16
;==================================================

READ_TEMP:
	SBI ADCSRA,ADSC
WAIT_ADC:
	SBIS ADCSRA,ADIF
	RJMP WAIT_ADC
	SBI ADCSRA,ADIF
	IN R16,ADCH
WAIT_SPI:
	SBIS PINB,SS
	SBIS SPSR,SPIF
	RJMP WAIT_SPI
	IN R0,SPDR
	OUT SPDR,R16
	RJMP READ_TEMP


Master:


Code:
	.INCLUDE"M32DEF.INC"

	.EQU SS1 = 0
	.EQU SS2 = 1
	.EQU RS = 1
	.EQU EN = 0
	.EQU LCD_PORT = PORTD


	LDI R16,HIGH(RAMEND)	;STACK SETTINGS
	OUT SPH,R16
	LDI R16,LOW(RAMEND)
	OUT SPL,R16
;=================IO SETTINGS=================
	LDI R16,0xFF	;LCD
	OUT DDRD,R16
	SBI DDRC,0	
	SBI DDRC,1

	SBI DDRB,7		;SPI
	CBI DDRB,6
	SBI DDRB,5

	SBI DDRA,0		;SLAVE SELECT
	SBI DDRA,1
;=================SPI SETTINGS=====================
	SBI PORTA,SS1		;SLAVES OFF
	SBI PORTA,SS2
	
	LDI R16,(1<< SPE | 1<< MSTR ) 
	OUT SPCR,R16

;================LCD SETTINGS=================

	CBI PORTC,EN
	CALL DELAY2MS
	LDI R16,0x38
	CALL CW
	CALL DELAY2MS
	LDI R16,0x0E
	CALL CW
	LDI R16,0x01
	CALL CW
	CALL DELAY2MS

	CALL DELAY2MS
	CALL DELAY2MS
TRANSMIT:
	CBI PORTA,SS1 ;ENABLE SLAVE1
	LDI R16,0xFF
	OUT SPDR,R16 ;START THE TRANSMITION
WAIT1:
	SBIS SPSR,SPIF ;CHECK FOR TRANSMITION COMPLETION.
	RJMP WAIT1
	IN R20,SPDR
	SBI PORTA,SS1 ;DISABLE SLAVE1

	CBI PORTA,SS2 ;ENABLE SLAVE2
	LDI R16,0xFF
	OUT SPDR,R16 ;START THE TRANSMITION
WAIT2:
	SBIS SPSR,SPIF ;CHECK FOR TRANSMITION COMPLETION.
	RJMP WAIT2
	IN R21,SPDR
	SBI PORTA,SS2 ;DISABLE SLAVE2
	
	ADD R21,R20
	LSR R21
	CALL DISP
	RJMP TRANSMIT
	
;===============SUB-ROUTINES==================================
CW:
	OUT LCD_PORT,R16
	CBI PORTC,RS
	SBI PORTC,EN
	CALL SDELAY
	CBI PORTC,EN
	CALL DELAY100US
	RET
DW:
	OUT LCD_PORT,R16
	SBI PORTC,RS
	SBI PORTC,EN
	CALL SDELAY
	CBI PORTC,EN
	CALL DELAY100US
	RET

SDELAY:
	NOP
	NOP
	RET

DELAY100US:
	PUSH R17
	LDI R17,60
L1: CALL SDELAY
	DEC R17
	BRNE L1
	POP R17
	RET

DELAY2MS:
	PUSH R17
	LDI R17,20
L2:	CALL DELAY100US
	DEC R17
	BRNE L2
	POP R17
	RET

DISP:;==============EXTRACT DIGIGTS , CONVERT TO ASCII AND DISPLAY================
	CLR R11
	MOV R24,R21
DL1:CPI R24,10
	BRLO NEXT
	SUBI R24,10
	INC R25
	RJMP DL1
NEXT:
	LDI R16,48
	ADD R24,R16
	ADD R25,R16
	
	LDI R16,0x01
	CALL CW	
	LDI R16,'T'
	CALL DW
	LDI R16,'E'
	CALL DW
	LDI R16,'M'
	CALL DW
	LDI R16,'P'
	CALL DW
	LDI R16,':'
	
	MOV R16,R11
	CALL DW
	MOV R16,R10
	CALL DW

	RET

My problem is that I can't seem to get the SPI running. I get DATA OVERRUN and COLLISION errors every time. Could you give me a hand?
 

Attachments

  • schematic.png
    40.3 KB · Views: 80
Last edited:

there is problen in hardware as you shown.
there is problen in MISO and slave select pin.
which IC is master U3,U2 or U5.
 

Sorry, Originally the schematic was right, I made a change that messed it up. Anyway , I fixed the schematic and the errors persist. I fixed the schematic in the original post.Master is U5 , the other two are slaves.
 

A data collision will occur if you put the second data before the first byte transmit completes..
Or a new data arrives in the receive buffer before you read the last byte That may be some times called as a over run..

so wait after the first transmit and put the second data..
 

thanks , I knew that but that's percisely the reason for
Code:
SBIS SPSR,SPIF ;CHECK FOR TRANSMISSION COMPLETION.
	RJMP WAIT2
yet it doesn't work. could you please take a look at the code, spi section.
thanks in advance
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…