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
Interface 2 off LS7166 quadrature encoders to Atmega16
General discussion on mikroBasic for AVR.
Author
Message
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
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
Return to “mikroBasic for AVR General”
Jump to
- NECTO Studio
- ↳ IDE
- ↳ IDE General
- ↳ IDE Beta Testing
- ↳ IDE Wish List
- ↳ ARM AI Compilers
- ↳ mikroC AI for ARM
- ↳ mikroC AI for ARM General
- ↳ mikroC AI for ARM Beta Testing
- ↳ mikroC AI for ARM Wish List
- ↳ PIC AI compilers
- ↳ mikroC AI for PIC
- ↳ mikroC AI for PIC General
- ↳ mikroC AI for PIC Beta Testing
- ↳ mikroC AI for PIC Wish List
- ↳ PIC32 AI Compilers
- ↳ mikroC AI for PIC32
- ↳ mikroC AI for PIC32 General
- ↳ mikroC AI for PIC32 Beta Testing
- ↳ mikroC AI for PIC32 Wish List
- ↳ dsPIC AI Compilers
- ↳ mikroC AI for dsPIC
- ↳ mikroC AI for dsPIC General
- ↳ mikroC AI for dsPIC Beta Testing
- ↳ mikroC AI for dsPIC Wish List
- ↳ AVR AI Compilers
- ↳ mikroC AI for AVR
- ↳ mikroC AI for AVR General
- ↳ mikroC AI for AVR Beta Testing
- ↳ mikroC AI for AVR Wish List
- ↳ Additional Software
- ↳ Visual TFT AI
- ↳ Visual TFT AI General
- ↳ Visual TFT AI Beta Testing
- ↳ Visual TFT AI Wish List
- PRO Compilers
- ↳ PIC PRO Compilers
- ↳ PIC Compilers General
- ↳ mikroC PRO for PIC
- ↳ mikroC PRO for PIC General
- ↳ mikroC PRO for PIC Beta Testing
- ↳ mikroC PRO for PIC Wish List
- ↳ mikroBasic PRO compiler for PIC
- ↳ mikroBasic PRO for PIC General
- ↳ mikroBasic PRO for PIC Beta Testing
- ↳ mikroBasic PRO for PIC Wish List
- ↳ mikroPascal PRO for PIC
- ↳ mikroPascal PRO for PIC General
- ↳ mikroPascal PRO for PIC Beta Testing
- ↳ mikroPascal PRO for PIC Wish List
- ↳ dsPIC PRO Compilers
- ↳ dsPIC Compilers General
- ↳ mikroC PRO for dsPIC30/33 and PIC24
- ↳ mikroC PRO for dsPIC30/33 and PIC24 General
- ↳ mikroC PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroC PRO for dsPIC30/33 and PIC24 Wish List
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 General
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 Wish List
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 General
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 Wish List
- ↳ PIC32 PRO Compilers
- ↳ PIC32 Compilers General
- ↳ mikroC PRO for PIC32
- ↳ mikroC PRO for PIC32 General
- ↳ mikroC PRO for PIC32 Beta Testing
- ↳ mikroC PRO for PIC32 Wish List
- ↳ mikroBasic PRO for PIC32
- ↳ mikroBasic PRO for PIC32 General
- ↳ mikroBasic PRO for PIC32 Beta Testing
- ↳ mikroBasic PRO for PIC32 Wish List
- ↳ mikroPascal PRO for PIC32
- ↳ mikroPascal PRO for PIC32 General
- ↳ mikroPascal PRO for PIC32 Beta Testing
- ↳ mikroPascal PRO for PIC32 Wish List
- ↳ AVR PRO Compilers
- ↳ AVR Compilers General
- ↳ mikroC PRO for AVR
- ↳ mikroC PRO for AVR General
- ↳ mikroC PRO for AVR Beta Testing
- ↳ mikroC PRO for AVR Wish List
- ↳ mikroBasic PRO compiler for AVR
- ↳ mikroBasic PRO for AVR General
- ↳ mikroBasic PRO for AVR Beta Testing
- ↳ mikroBasic PRO for AVR Wish List
- ↳ mikroPascal PRO for AVR
- ↳ mikroPascal PRO for AVR General
- ↳ mikroPascal PRO for AVR Beta Testing
- ↳ mikroPascal PRO for AVR Wish List
- ↳ FT90x PRO Compilers
- ↳ FT90x Compilers General
- ↳ mikroC PRO for FT90x
- ↳ mikroC PRO for FT90x General
- ↳ mikroC PRO for FT90x Beta Testing
- ↳ mikroC PRO for FT90x Wish List
- ↳ mikroBasic PRO compiler for FT90x
- ↳ mikroBasic PRO for FT90x General
- ↳ mikroBasic PRO for FT90x Beta Testing
- ↳ mikroBasic PRO for FT90x Wish List
- ↳ mikroPascal PRO for FT90x
- ↳ mikroPascal PRO for FT90x General
- ↳ mikroPascal PRO for FT90x Beta Testing
- ↳ mikroPascal PRO for FT90x Wish List
- ↳ 8051 PRO Compilers
- ↳ 8051 Compilers General
- ↳ mikroC PRO for 8051
- ↳ mikroC PRO for 8051 General
- ↳ mikroC PRO for 8051 Beta Testing
- ↳ mikroC PRO for 8051 Wish List
- ↳ mikroBasic PRO compiler for 8051
- ↳ mikroBasic PRO for 8051 General
- ↳ mikroBasic PRO for 8051 Beta Testing
- ↳ mikroBasic PRO for 8051 Wish List
- ↳ mikroPascal PRO for 8051
- ↳ mikroPascal PRO for 8051 General
- ↳ mikroPascal PRO for 8051 Beta Testing
- ↳ mikroPascal PRO for 8051 Wish List
- ↳ ARM PRO Compilers
- ↳ ARM Compilers General
- ↳ mikroC PRO for ARM
- ↳ mikroC PRO for ARM General
- ↳ mikroC PRO for ARM Beta Testing
- ↳ mikroC PRO for ARM Wish List
- ↳ mikroBasic PRO for ARM
- ↳ mikroBasic PRO for ARM General
- ↳ mikroBasic PRO for ARM Beta Testing
- ↳ mikroBasic PRO for ARM Wish List
- ↳ mikroPascal PRO for ARM
- ↳ mikroPascal PRO for ARM General
- ↳ mikroPascal PRO for ARM Beta Testing
- ↳ mikroPascal PRO for ARM Wish List
- ↳ Older versions of Compilers
- ↳ Older versions of mikroC Compilers
- ↳ mikroC for PIC
- ↳ mikroC General
- ↳ mikroC Beta Testing
- ↳ mikroC Wish List
- ↳ mikroC for dsPIC30/33 and PIC24
- ↳ mikroC for dsPIC30/33 and PIC24 General
- ↳ mikroC for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroC for dsPIC30/33 and PIC24 Wish List
- ↳ RSC-4x mikroC
- ↳ RSC-4x mikroC General
- ↳ RSC-4x mikroC Wish List
- ↳ mikroC Beta testing
- ↳ Older versions of mikroBasic Compilers
- ↳ mikroBasic compiler for PIC
- ↳ mikroBasic General
- ↳ mikroBasic Beta Testing
- ↳ mikroBasic Wish List
- ↳ mikroBasic compiler for dsPIC30/33 and PIC24
- ↳ mikroBasic for dsPIC30/33 and PIC24 General
- ↳ mikroBasic for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroBasic for dsPIC30/33 and PIC24 Wish List
- ↳ mikroBasic for dsPIC30/33 and PIC24 Bug List
- ↳ Obsolete Project: mikroBasic compiler for AVR
- ↳ mikroBasic for AVR General
- ↳ mikroBasic for AVR Wish List
- ↳ mikroBasic Beta testing
- ↳ Older versions of mikroPascal Compilers
- ↳ mikroPascal compiler for PIC
- ↳ mikroPascal FAQ
- ↳ mikroPascal General
- ↳ mikroPascal Beta Testing
- ↳ mikroPascal Wish List
- ↳ mikroPascal Bug List
- ↳ mikroPascal compiler for dsPIC30/33 and PIC24
- ↳ mikroPascal for dsPIC30/33 and PIC24 General
- ↳ mikroPascal for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroPascal for dsPIC30/33 and PIC24 Wish List
- ↳ mikroPascal for dsPIC30/33 and PIC24 Bug List
- ↳ Obsolete Project: mikroPascal compiler for AVR
- ↳ mikroPascal for AVR General
- ↳ mikroPascal for AVR Wish List
- ↳ mikroPascal Beta testing
- ↳ Library Development Discussion
- ↳ Code Requests
- Additional Software
- ↳ Visual TFT Software
- ↳ Visual TFT General
- ↳ Visual TFT Beta Testing
- ↳ Visual TFT Wish List
- ↳ Visual GLCD Software
- ↳ Visual GLCD General
- ↳ Visual GLCD Beta Testing
- ↳ Visual GLCD Wish List
- ↳ Package Manager Software
- ↳ Package Manager General
- ↳ Package Manager Beta Testing
- ↳ Package Manager Wishlist
- ↳ GLCD Font Creator Software
- ↳ SpeakUp Software
- ↳ Timer Calculator
- ↳ CAN Calculator
- ↳ mikroBootloader
- Hardware
- ↳ Development Boards
- ↳ CODEGRIP programmer and debugger
- ↳ Mikromedia
- ↳ mikroProg™ programmer and debugger
- ↳ Development Boards Wishlist
- ↳ Review
- ↳ Click Boards release
- ↳ Hexiwear
- Website & Forums
- ↳ Libstock Discussion
- ↳ Product Announcements
- ↳ Website & Forums General Discussion
- ↳ Website & Forums Wishlist
- ↳ User Projects