Name:
hspisetup
Syntax:

HSPISETUP OFF HSPISETUP mode, spispeed  

Mode - is a constant/variable to define the mode

spimode00 (mode 0,0 - input sampled at middle of data time)
spimode01 (mode 0,1 - input sampled at middle of data time)
spimode10 (mode 1,0 - input sampled at middle of data time)
spimode11 (mode 1,1 - input sampled at middle of data time)
spimode00e (mode 0,0 - input sampled at end of data time)
spimode01e (mode 0,1 - input sampled at end of data time)
spimode10e (mode 1,0 - input sampled at end of data time)
spimode11e (mode 1,1 - input sampled at end of data time)

Spispeed - is a constant/variable to define the clock speed

spifast (clock freq / 4) (= 1MHz with 4MHz resonator)
spimedium (clock freq / 16) (= 250kHz with 4MHz resonator)
spislow (clock freq / 64) (= 63 kHz with 4MHz resonator)
Description:

The hpisetup command sets the microcontroller's hardware pins to SPI mode. This command setups the microcontroller for SPI transmission via the microcontroller's SPI hardware pins. This method is faster and more code efficient than using the 'bit-banged' spiout (shiftout) command. When connecting SPI devices (e.g. EEPROM) remember that the data-in (SDI) of the EEPROM connects to the data-out (SDO) of the PICAXE, and vice versa.

Advanced Technical Information

Users familiar with assembler code programming may find the following microcontroller information useful (also see the Logic Analyser screenshots in section 2 of the manual).

spimode00 (CKP=0, CKE=1, SMP=0) Mode (0,0)
spimode01 (CKP=0, CKE=0, SMP=0) Mode (0,1)
spimode10 (CKP=1, CKE=1, SMP=0) Mode (1,0)
spimode11 (CKP=1, CKE=0, SMP=0) Mode (1,1)
spimode00e (CKP=0, CKE=1, SMP=1)  
spimode01e (CKP=0, CKE=0, SMP=1)  
spimode10e (CKP=1, CKE=1, SMP=1)  
spimode11e (CKP=1, CKE=0, SMP=1)  
Applies To:
20X2, 28X1, 28X2, 40X1, 40X2
See Also:
Related Create:
Share:
Print:

Reading and writing a 25LC160 EEPROM using 28X2 or 40X2.

Pin connection of the EEPROM is as follows:

1CSpicaxe output 7 (B.7)
2SOpicaxe input 4 (C.4)
3WP+5V
4Vss0V
5SIpicaxe input 5 (C.5)
6SCKpicaxe input 3 (C.3)
7HOLD+5V
8Vdd+5V

Code Example:
init:	hspisetup spimode11e, spimedium	; spi mode 1,1

	low cs				; enable chip select
	hspiout (6)			; send write enable
	high cs				; disable chip select
	low cs				; enable chip select
	hspiout (1,0)			; remove block protection
	high cs				; disable chip select
	pause 5				; wait write time

main:	low cs				; enable chip select
	hspiout (6)			; send write enable
	high cs				; disable chip select

	low cs				; enable chip select
	hspiout (2,0,5,25)		; write 25 to address 5
	high cs				; disable chip select
	pause 5				; wait write time of 5ms
	low cs				; enable chip select
	hspiout (6)			; send write enable
	high cs				; disable chip select

	low cs				; enable chip select
	hspiout (3,0,5)			; send read command, address 5
	hspiin (b1)			; shift in the data
	high cs				; disable chip select
	low cs				; enable chip select
	hspiout (4)			; send write disable
	high cs				; disable chip select
	debug
	pause 1000
	goto main
Copy Code Submit an Example

Reading and writing a 25LC160 EEPROM using 20X2.

Pin connection of the EEPROM is as follows:

1CSpicaxe output (B.6)
2SOpicaxe output (B.5)
3WP+5V
4Vss0V
5SIpicaxe input (C.1)
6SCKpicaxe output (B.7)
7HOLD+5V
8Vdd+5V

Code Example:
init:	hspisetup spimode11e, spimedium	; spi mode 1,1

	low cs				; enable chip select
	hspiout (6)			; send write enable
	high cs				; disable chip select
	low cs				; enable chip select
	hspiout (1,0)			; remove block protection
	high cs				; disable chip select
	pause 5				; wait write time

main:	low cs				; enable chip select
	hspiout (6)			; send write enable
	high cs				; disable chip select

	low cs				; enable chip select
	hspiout (2,0,5,25)		; write 25 to address 5
	high cs				; disable chip select
	pause 5				; wait write time of 5ms
	low cs				; enable chip select
	hspiout (6)			; send write enable
	high cs				; disable chip select

	low cs				; enable chip select
	hspiout (3,0,5)			; send read command, address 5
	hspiin (b1)			; shift in the data
	high cs				; disable chip select
	low cs				; enable chip select
	hspiout (4)			; send write disable
	high cs				; disable chip select
	debug
	pause 1000
	goto main
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.