Interface 2 off LS7166 quadrature encoders to Atmega16

General discussion on mikroBasic for AVR.
Post Reply
Author
Message
Tobben
Posts: 1
Joined: 24 Jan 2007 19:27
Location: Bergen, Norway
Contact:

Interface 2 off LS7166 quadrature encoders to Atmega16

#1 Post by Tobben » 24 Jan 2007 20:08

This is a program that reads the 24 bit registers of the LS7166 chip and transfers the data to PC via RS232

It is used to read X/Y axes on an instrument and run motors for each axis.
It will start/stop three motors according to content in a recived byte from PC
It will stop all motors if communication to PC is lost.
It sends data to PC in follwing format:

Axxxxxxxx ' encoder 0
Bxxxxxxxx ' encoder 1
Cxxx ' status for motors and some end stop switches. (tips: convert to binary)
Dxxxx ' analog reading (pressure, Temperature, etc.


program Read2encoders

dim i, index, Status, ProxyStatus, kommando, Motorstatus, cmdmissing as byte
dim enc as longint[3]
dim encoder as longint[2]
dim txt as string[15]
dim temp as word

sub procedure write7x66
Clearbit (PORTC,1) 'write7x66puls start
Delay_us (10)
Setbit (PORTC,1) ' End of write7x66puls
Delay_us (10)
end sub

sub procedure ReadEncoder(dim byref index as byte)
DDRB = $FF 'Databus is output (AVR)
'TRISB = $0 'Databus is output(PIC)
if index = 1 then
PORTC = %00011110 'Do ready for write to Master control: C/D high, RD high, WR high
else
PORTC = %00001111 'Do ready for write to Master control: C/D high, RD high, WR high
end if
PORTB = %00000011 'Reset Adr Pointer and transfer 24 bits to output latch
write7x66
if index = 1 then
PORTC = %00010110 'Select encoder 1 Do ready for read: Set C/D control low (bit3)
else
PORTC = %00000111 'Select encoder 2 Do ready for read: Set C/D control low (bit3)
end if
'Delay_ms (200)
DDRB = 0 'Set PORTB (databus) as input
'TRISB = $FF
for i=0 to 2
Clearbit (PORTC,2) 'Read pulse start
Delay_us (50)
enc = PINB
'Delay_ms (100) ' Testing delay
Setbit (PORTC,2) 'End of read pulse
'Delay_ms (200)
next i
'Delay_ms (200)
enc[1]=enc[1] << 8
enc[2]=enc[2] << 16
encoder[index] = enc[0] + enc[1] + enc[2]
end sub

main:
'preset = 8388608
Delay_ms(1000)
Usart1_Init(14400)'AVR

'Usart_Init(14400) 'PIC

DDRB = $FF 'AVR Databus as output
DDRC = $1F 'AVR 5 Lsb of Controlbus as output.3 Msb as input (Proximity switches)
DDRD = $FC
'TRISB = $0 'PIC Databus as output
'TRISC = $F0 'PIC 4 Lsb of Controlbus as output.4 Msb as input (Proximity switches)
PORTC = %00001110 '$E ' C/D high, RD high, WR high
PORTB = %00100000 '$3F 'Select master control register. Master RESET
write7x66
PORTB = %01111000 'Input control register
write7x66
PORTB = %10010000 'Output control Register
write7x66
PORTB = %11000011 'Quadrature register
write7x66
PORTC = %00000110 'X X 0 1 U 0 (SetC/D low, ready to write to Preset register)
PORTB = %00000000 'lsb
write7x66
'PORTB = %00000000 'byte 2
write7x66
PORTB = %10000000 'msb
write7x66
PORTC = %00001110 'X X 1 1 U 0 (SetC/D high)
write7x66
PORTB = %00001000 'Transfer preset to counter
write7x66

while true 'main program loop
for index=0 to 1
ReadEncoder(index)
next index
ProxyStatus = PINC 'Read PORTC
ProxyStatus = ProxyStatus >> 5 'discard lower 5 bits
Motorstatus = PIND
Motorstatus = Motorstatus and %11111000 'set lower 3 bits to 0
Status = ProxyStatus or Motorstatus
USart1_Write_Char(65) 'A
LongintToStr(encoder[0], txt)
USart1_Write_Text(txt)'AVR
LongintToStr(encoder[1], txt)
USart1_Write_Char(66) 'B
USart1_Write_Text(txt)
ByteToStr(Status, txt)
USart1_Write_Char(67) 'C
USart1_Write_Text(txt)
temp = Adc_Read(0) ' read channel 0 temprature on pcb
WordToStr(temp, txt)
USart1_Write_Char(68) 'D
USart1_Write_Text(txt)
Delay_ms(50)
if Usart1_Data_Ready > 0 then
kommando = Usart1_Read
Kommando = Kommando << 3
PORTD = Kommando
cmdmissing = 0
'Delay_ms(1000)
else
cmdmissing = cmdmissing + 1
if cmdmissing >30 then
Kommando = 0
PORTD = Kommando
cmdmissing = 0
end if
end if
wend
end.

Connections to ATMEGA16:

Pin Name in/out
1 PB0 I/O Data to and from LS7166
2 PB1 I/O Data to and from LS7166
3 PB2 I/O Data to and from LS7166
4 PB3 I/O Data to and from LS7166
5 PB4 I/O Data to and from LS7166
6 PB5 I/O Data to and from LS7166
7 PB6 I/O Data to and from LS7166
8 PB7 I/O Data to and from LS7166
9 RST
10 VCC
11 GND
12 XT2
13 XT1
14 PD0 I Serial Recive Data
15 PD1 O Serial Send Data
16 PD2 Available
17 PD3 O Toggle deploy instrument
18 PD4 O Y Forward
19 PD5 O Y Reverse
20 PD6 O X Forward
21 PD7 O X Reverse
22 PC0 O Pin 2 on LS 7166 Chip Select 1
23 PC1 O Pin 1 on LS 7166 Write Pulse
24 PC2 O Pin 19 on LS 7166 Read Pulse
25 PC3 O Pin 18 on LS7166 Control / Data
26 PC4 O Pin 2 on LS 7166 Chip Select 2
27 PC5 I In / Out Proximity Switch
28 PC6 I X Proximity Switch
29 PC7 I Y Proximity Switch
30 AVCC Available
31 AGND Available
32 AREF Available
33 PA7 Available
34 PA6 Available
35 PA5 Available
36 PA4 Available
37 PA3 Available
38 PA2 Available
39 PA1 Available
40 PA0 I Temperature censor

You has to hardwire the two LS7166 according to table and get two quadrature encoders to test this program.

IT JUST WORKS FINE!!!!!!
Tobben

mullins42
Posts: 74
Joined: 27 Jun 2009 12:24

Re: Interface 2 off LS7166 quadrature encoders to Atmega16

#2 Post by mullins42 » 24 Oct 2011 12:47

Hi Tobben,

I'm playing around with the LS7266 and have tried out your code on a PIC 18f2550.

I'm having some problems as it looks like the output isn't getting latched into the output registers. I know this is an old topic and you might not be around anymore, but if you are, can you dig up your code again?

Cheers,
James

Post Reply

Return to “mikroBasic for AVR General”