PDA

View Full Version : 32-bit Quadrature Counter With Serial Interface



precision
- 9th June 2008, 08:39
Can enybody convert this code into picbasic using SHIFTIN ? I dont know ASM
LS7366R is a 32-bit CMOS counter, with direct interface for quadrature clocks from incremental encoders.For communications with microprocessors or microcontrollers, it provides a 4-wire SPI/MICROWIRE bus.The four standard bus I/Os are SS/, SCK, MISO and MOSI.



;Sample routines for PIC18CXXX interface
/************************************************** **********************************/
;Initialize PIC18Cxxx portc in LS7366 compatible SPI
;Setup: master mode, SCK idle low, SDI/SDO datashift on high to low transition of SCK
;SS/ assertion/deassertion made with direct manipulation of RA5
;Initialize portc
CLRF PORTC ;Clear portc
CLRF LATC ;Clear data latches
MOVLW 0x10 ;RC4 is input, RC3 & RC5 are outputs
MOVWF TRISC ;RC3=CLK, RC4=SDI, RC5=SDO
BCF TRISA, 5 ;RA5=output
BSF PORTA, 5 ;RA5=SS/=high
CLRF SSPSTAT ;SMP=0 => SDI data sampled at mid-data
BSF SSPSTAT, CKE ;CKE=1 => data shifts on active to idle SCK transitions
MOVLW 0x21 ;SPI mode initialization data
MOVWF SSPCON ;Master mode, CLK/16, CKP=0 => CLK idles low
;data shifted on active to idle CLK edge
/************************************************** ******************/
; WR_MDR0
BSF PORTA, 5 ;SS/=high
BCF PORTA, 5 ;SS/=low
MOVLW 0x88 ;LS7366 WR_MDR0 command
MOVWF SSPBUF ;Transmit command byte
LOOP1 BTFSS SSPSTAT, BF ;Transmission complete with BF flag set?
BRA LOOP1 ;No, check again
MOVF SSPBUF, W ;Dummy read to clear BF flag.
MOVLW 0xA3 ;MDR0 data:fck/2, synchronous index. index=rcntr, x4
MOVWF SSPBUF ;Transmit data
LOOP2 BTFSS SSPSTAT, BF ;BF set?
BRA LOOP2 ;No, check again
BSF PORTA, 5 ;SS/=high
/************************************************** ******************/
;RD_MDR0
BSF PORTA, 5 ;SS/=high
BCF PORTA, 5 ;SS/=low
MOVLW 0x48 ;LS7366 RD_MDR0 command
MOVWF SSPBUF ;Transmit command byte
LOOP1 BTFSS SSPSTAT, BF ;BF flag set?
BRA LOOP1 ;No, check again
MOVWF SSPBUF ;Send dummy byte to generate clock & receive data
LOOP2 BTFSS SSPSTAT, BF ;BF flag set?
BRA LOOP2 ;No, check again
MOVF SSPBUF, W ;Recieved data in WREG.
MOVWF RXDATA ;Save received data in RAM
BSF PORTA, 5 ;SS/=high



Datasheet: http://www.lsicsi.com/pdfs/Data_Sheets/LS7366R.pdf

.

precision
- 9th June 2008, 14:04
I wrote this code but not working




DEFINE OSC 20
INCLUDE "MODEDEFS.BAS"
INCLUDE "MY_LCD.bas
CMCON = 7
ADCON1 = 15
CVRCON = %00000000 'CVref turned off

OUTPUT PORTA.5
OUTPUT PORTC.3
OUTPUT PORTC.5
INPUT PORTC.4

Symbol HC_latch = PORTA.5
symbol HC_SDI = PORTC.4 ;RC4=SDI
symbol HC_SDO = PORTC.5 ;RC5=SDO
SYMBOL HC_Clk = PORTC.3 ;RC3=CLK

RD_MDR0 var word
WR_MDR0 var word

pause 1000

start:
HC_Latch = 0
shiftin HC_SDI, HC_Clk, MSBFIRST,[RD_MDR0]
pauseus 1
HC_Latch = 1

Lcdout $FE,1
lcdout "Count =", DEC5 RD_MDR0

pause 200
goto start



.

skimask
- 9th June 2008, 14:17
I wrote this code but not working.

And this is for which PIC?

precision
- 9th June 2008, 18:35
for PIC 18F4550

.

RodSTAR
- 10th June 2008, 02:49
I don't ASM. BTW, A good choice you may consider is to get a cheap and powerfull PIC18Fxx31: 2331 2431 4331 or 4431, wich provide a powerfull hardware Quadrature Encoder Interface (QEI) onchip module, with a confortable wide set of interrupts focused on presition and fast rotary decoding. Hope this pics help you.