robbe55
- 21st April 2016, 13:32
Hi all,
For a university project I have written a PICBASIC code to read digital signals from the encoder on my MCU and send them to both another MCU (to graphically plot the result on PC via Python) and to an LCD screen (for debugging). Now, since i'm completely new to picbasic i have no idea whether my code will work. Since we have limited time to actually work with the hardware, we have to make sure the software is carefully prepared beforehand. That's why i would kindly ask some of you guys, with a lot more experience, to look through my code and make me aware of certain mistakes.
The MCU i'm using is PIC12F683 (datasheet: http://akizukidenshi.com/download/ds/microchip/pic12f683.pdf). It's an 8 pin MCU.
The encoder output (which is the MCU input) exists out of 2 square waves, which are the same but shifted 90° in phase (to check which way the encoder is turning).
What i want to get as output is a 3 digit number, because that is what to next MCU requires as input. So i wanted to take number 0 to 499 for clockwise turning and 501 to 999 for counterclockwise turning.
I'm pretty convinced the idea behind my method is correct, but i have no idea whether i have written it all in a correct way, without too many syntax errors.
Here's what i've written so far, i hope i did a decent job of explaining what i want to achieve, if not you can always ask me.
It would be great if some people with actual PICBASIC knowledge could help me :)
Thanks!
'************************************************* ***************
'* Name : RotaryEncoder.BAS *
'* Author : Robbe Degezelle *
'* Notice : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 12/04/2016 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'initialisation------
NewVal = (GPIO & 000) >> 3
OldVal = NewVal
ANSEL = 00000 ' all pins digital
TRISIO = 1111 'set all ports to inputs
'Enc_1 VAR GPIO.5 ' first encoder input
'Enc_2 var GPIO.4 ' second encoder input
LCD_pin var GPIO.1
'variables-----------
NewVal VAR BYTE
OldVal VAR BYTE
Counter VAR BYTE
CounterCC var byte
Counter = 000
CounterCC = 500
word1 var word 'C
word1=000
word2 var word 'CC
word2=000
P4800 CON 188 '4800 baud true
N9600 con $4054 '9600 baud rate inverted
'Program----------------
Main
NewVal = (GPIO & 000) >> 3 ; zou 0,1,2 of 3 moeten geven
If NewVal <> OldVal Then
Branch CurVal, [S0,S1,S2,S3] ; indien waarde veranderd, vergelijken met oud
EndIF
AfterBranch
Goto Main
S0
If OldVal = 2 Then
GoSub Clockwise
ELSE
GoSub CounterClockwise
EndIF
Goto AfterBranch
S1
if OldVal = 0 Then
GoSub Clockwise
ELSE
GoSub CounterClockwise
EndIF
Goto AfterBranch
S2
if OldVal = 3 Then
GoSub Clockwise
Else
GoSub CounterClockwise
EndIf
Goto AfterBranch
S3
if OldVal = 1 Then
GoSub Clockwise
Else
GoSUb CounterClockwise
Endif
Goto AfterBranch
Clockwise
OldVal = CurVal
Counter = Counter + 1
word1 = Counter
serout2 GPIO.0,p4800, ["B1", DEC3 word1]
gosub disp_lcd
Return
CounterClockwise
OldVal = CurVal
CounterCC = CounterCC + 1
word2 = CounterCC
serout2 GPIO.0,p4800, ["B1", DEC3 word2]
gosub disp_lcd
return
'LCD
disp_lcd
serout2 LCD_pin, N9600, [$FE, 1$] 'clear display
pause 1
serout2 LCD_pin, N9600, [$FE, 2$] 'cursor returns home
pause 1
serout LCD_pin, N9600, ["TellerC= ", DEC Counter]
pause 1
SEROUT2 LCD_pin,N9600, [$FE, $C0] 'cursor to 2nd line
pause 1
SEROUT2 LCD,N9600, ["TellerCC= ", dec CounterCC]
return
end
For a university project I have written a PICBASIC code to read digital signals from the encoder on my MCU and send them to both another MCU (to graphically plot the result on PC via Python) and to an LCD screen (for debugging). Now, since i'm completely new to picbasic i have no idea whether my code will work. Since we have limited time to actually work with the hardware, we have to make sure the software is carefully prepared beforehand. That's why i would kindly ask some of you guys, with a lot more experience, to look through my code and make me aware of certain mistakes.
The MCU i'm using is PIC12F683 (datasheet: http://akizukidenshi.com/download/ds/microchip/pic12f683.pdf). It's an 8 pin MCU.
The encoder output (which is the MCU input) exists out of 2 square waves, which are the same but shifted 90° in phase (to check which way the encoder is turning).
What i want to get as output is a 3 digit number, because that is what to next MCU requires as input. So i wanted to take number 0 to 499 for clockwise turning and 501 to 999 for counterclockwise turning.
I'm pretty convinced the idea behind my method is correct, but i have no idea whether i have written it all in a correct way, without too many syntax errors.
Here's what i've written so far, i hope i did a decent job of explaining what i want to achieve, if not you can always ask me.
It would be great if some people with actual PICBASIC knowledge could help me :)
Thanks!
'************************************************* ***************
'* Name : RotaryEncoder.BAS *
'* Author : Robbe Degezelle *
'* Notice : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 12/04/2016 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'initialisation------
NewVal = (GPIO & 000) >> 3
OldVal = NewVal
ANSEL = 00000 ' all pins digital
TRISIO = 1111 'set all ports to inputs
'Enc_1 VAR GPIO.5 ' first encoder input
'Enc_2 var GPIO.4 ' second encoder input
LCD_pin var GPIO.1
'variables-----------
NewVal VAR BYTE
OldVal VAR BYTE
Counter VAR BYTE
CounterCC var byte
Counter = 000
CounterCC = 500
word1 var word 'C
word1=000
word2 var word 'CC
word2=000
P4800 CON 188 '4800 baud true
N9600 con $4054 '9600 baud rate inverted
'Program----------------
Main
NewVal = (GPIO & 000) >> 3 ; zou 0,1,2 of 3 moeten geven
If NewVal <> OldVal Then
Branch CurVal, [S0,S1,S2,S3] ; indien waarde veranderd, vergelijken met oud
EndIF
AfterBranch
Goto Main
S0
If OldVal = 2 Then
GoSub Clockwise
ELSE
GoSub CounterClockwise
EndIF
Goto AfterBranch
S1
if OldVal = 0 Then
GoSub Clockwise
ELSE
GoSub CounterClockwise
EndIF
Goto AfterBranch
S2
if OldVal = 3 Then
GoSub Clockwise
Else
GoSub CounterClockwise
EndIf
Goto AfterBranch
S3
if OldVal = 1 Then
GoSub Clockwise
Else
GoSUb CounterClockwise
Endif
Goto AfterBranch
Clockwise
OldVal = CurVal
Counter = Counter + 1
word1 = Counter
serout2 GPIO.0,p4800, ["B1", DEC3 word1]
gosub disp_lcd
Return
CounterClockwise
OldVal = CurVal
CounterCC = CounterCC + 1
word2 = CounterCC
serout2 GPIO.0,p4800, ["B1", DEC3 word2]
gosub disp_lcd
return
'LCD
disp_lcd
serout2 LCD_pin, N9600, [$FE, 1$] 'clear display
pause 1
serout2 LCD_pin, N9600, [$FE, 2$] 'cursor returns home
pause 1
serout LCD_pin, N9600, ["TellerC= ", DEC Counter]
pause 1
SEROUT2 LCD_pin,N9600, [$FE, $C0] 'cursor to 2nd line
pause 1
SEROUT2 LCD,N9600, ["TellerCC= ", dec CounterCC]
return
end