Here is an example 16F877_spi_master_hw.jal, the user will be able to
select options:


-- Title: SPI example for communication via SPI
-- Author: Matthew Schinkel, William Welch, copyright (c) 2009, all
rights reserved.
-- Adapted-by:
-- Compiler: >=2.4l
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the BSD license (http://www.opensource.org/licenses/
bsd-license.php)
--
-- Description: spi example using spi library
--
-- Sources:
-- http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
-- http://ww1.microchip.com/downloads/en/devicedoc/spi.pdf
--

-- include chip
include 16f877                      -- target picmicro

-- this program assumes a 20 mhz resonator or crystal
-- is connected to pins osc1 and osc2.
pragma target osc hs               -- hs crystal or resonator
pragma target clock 20_000_000     -- oscillator frequency
--
pragma target wdt  disabled
pragma target lvp  disabled
--

enable_digital_io() -- disable all analog pins if any

include delay                 -- include the delay library

-- setup spi
ALIAS SPI_BUFFER        is SSPBUF
ALIAS SPI_BUFFER_STATUS IS SSPSTAT_BF
--
include spi_master_hw         -- includes the spi library
--
-- define the spi mode 0,0
SSPCON_CKP   = 0  -- idle state for clock is high level
SSPSTAT_CKE  = 0  -- clock edge select, data transmitted on falling
edge of sck
-- define other spi options
SSPSTAT_SMP  = 0  -- sample bit, input data sampled at middle of data
output time
SSPCON_SSPM  = 1  -- spi speed, clock = Fosc/16
SSPCON_SSPEN = 1  -- enables serial port and configures sck, sdo, sdi
and ss as source of the serial port pins
-- define spi inputs/outputs
pin_sdi_direction = input    -- spi input
pin_sdo_direction = output   -- spi output
pin_sck_direction = output   -- spi clock
-- Optional chip select pin
ALIAS chip_select_direction   is pin_SS_direction
ALIAS chip_select             is pin_SS
chip_select_direction = output    -- chip select/slave select pin
chip_select = low -- start chip slect low

-- main program

_usec_delay (100_000)    -- allow power to settle

chip_select = high       -- select the chip before we start

-- send 0xFF via spi interface
spi_master_hw = 0xFF     -- send 0xFF through

-- get data from spi interface
var byte x
x = spi_master_hw        -- get spi data

-- exchange data through spi interface
x = spi_master_hw_exchange(0xFF)

chip_select = low        -- de-select the chip when finnished

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to