PDA

View Full Version : 4-20mA using SPI Interface



gavo
- 28th March 2013, 17:23
Hi All,

I came across a 4-20mA board from MicroElektronika which uses the SPI to control the device, has anyone used such a board before?, I am looking for Code Examples, would anyone be able to assist?

Regards,

gavo

HenrikOlsson
- 28th March 2013, 17:49
Is it too much to ask to at least post a link to the board in question?
I could try to find it on their site but I won't so the best answer *I* can give you is that you can use SHIFTOUT to do bitbanged SPI or use the MSSP module (if the PIC has one) to do it "in hardware".

/Henrik.

alec
- 29th March 2013, 12:51
Hi

This is some code I wrote to use that board as a basic loop calibrator. Worked OK for me.

Take care

Alec


'************************************************* ***************
'* Name : Encoder test *
'* Author : alec noble *
'* Notice : Copyright (c) 2012 alec noble *
'* : All Rights Reserved *
'* Date : 9/27/2012 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG

'DEFINE OSC 20

'************************** setup LCD ****************************

DEFINE LCD_DREG PORTB ' Set Data to PortB
DEFINE LCD_DBIT 0 ' Set starting Data to Bit4
DEFINE LCD_RSREG PORTB ' Set Register Select to PortB
DEFINE LCD_RSBIT 4 ' Set RS line to PORTB.4
DEFINE LCD_EREG PORTB ' Set Enable to PortB
DEFINE LCD_EBIT 5 ' Set Enable line to PortB.5
DEFINE LCD_BITS 4 ' Set for 4 bit Bus
DEFINE LCD_LINES 2 ' Set number of lines to

'************************************************* ********************
CMCON = 7 'comparaters off - digital I/O on port A

trisb = 0 'all bits outputs
trisa = %00000111 'porta.0, 1, 2 input, all others output




cntr var word
cntr_1 var word
cntr_2 var word
preset var byte

sck var porta.6
sdi var porta.7
cs var porta.4


cntr = 200
preset = 0
cs = 1

lcdout $FE, $01
pause 5
lcdout "ahn_loop_cal"

pause 5000

lcdout $FE, $01
pause 5
lcdout dec cntr

'************************ Program **********************************

main:
if porta.2 = 1 then set
if porta.0 = 1 then CW
if porta.1 = 1 then CCW
goto main

set:
pause 5
if porta.2 = 0 then main
preset = preset + 1
while porta.2 = 1 : wend
if preset > 4 then preset = 0
lookup2 preset, [200, 400, 600, 800, 1000], cntr
goto display

CW:
pause 5
if porta.0 = 0 then main
if cntr = 1023 then main
cntr = cntr + 1
goto display


CCW:
pause 5
if porta.1 = 0 then main
if cntr < 190 then main
cntr = cntr - 1
goto display

display:
cntr_1 = cntr * 4
lcdout $FE, $01, dec cntr_1, " mV"
pause 5
cntr_2 = cntr_1/2
lcdout $FE, $C0, dec2 cntr_2/100, ".", dec2 cntr_2//100, " mA"
goto DAC_out

DAC_out:


cntr_1.15 = 0
cntr_1.14 = 0
cntr_1.13 = 1 'GAIN OF 1
cntr_1.12 = 1 'turn on DAC output
low CS
' pause 5
shiftout sdi, sck, 1, [cntr_1\16]
high cs

while porta.0 or porta.1 = 1
wend

goto main

gavo
- 10th April 2013, 19:51
Hi Alec,

Many Thanks!, will try it!,

Hendrik the link is http://www.mikroe.com/click/rs485-5v/

Regards,

Gavin