Use of Opto_Encoder without the Mikrobus

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
thunderer
Posts: 54
Joined: 07 Sep 2011 15:55

Use of Opto_Encoder without the Mikrobus

#1 Post by thunderer » 25 Sep 2020 23:54

I try to use the Opto-Encoder library with my own hardware.

While I believe I understand some of the library usage, I have a question about this code line, what does it do?

Code: Select all

optoencoder_gpioDriverInit(T_OPTOENCODER_P(@_MIKROBUS1_GPIO)) 
The original example is here:

Code: Select all

'
'Example for Opto_Encoder Click
'
'    Date          : Feb 2018.
'    Author        : MikroE Team
'
'Test configuration PIC :
'    
'    MCU             : P18F87K22
'    Dev. Board      : EasyPIC PRO v7
'    PIC Compiler ver : v7.1.0.0
'
'---
'
'Description :
'
'The application is composed of three sections :
'
'- System Initialization - Initializes PWM pin and INT pin as INPUT.
'- Application Initialization - Initializes Driver init and opto encoder init.
'- Application Task - (code snippet) - Detects the change of step and logs to usb uart number of stepped steps.
'
program Click_Opto_Encoder_PIC

include Click_Opto_Encoder_types
include Click_Opto_Encoder_config

dim
    newStep as int16_t 
    oldStep as int16_t 
    text as char[256] 
    
sub procedure systemInit() 

    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_INT_PIN, _GPIO_INPUT) 
    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_PWM_PIN, _GPIO_INPUT) 
    mikrobus_logInit(_LOG_USBUART, 9600) 
    mikrobus_logWrite("--- System Inti --- ", _LOG_LINE) 
    Delay_ms(100) 

end sub

sub procedure applicationInit() 

    optoencoder_gpioDriverInit(T_OPTOENCODER_P(@_MIKROBUS1_GPIO)) 
    optoencoder_init() 

end sub

sub procedure applicationTask() 

    newStep = optoencoder_getPosition() 
    if ((oldStep <> newStep)) then 
        IntToStr(newStep, text) 
        mikrobus_logWrite(" Step: ", _LOG_TEXT) 
        mikrobus_logWrite(text, _LOG_LINE) 
        oldStep = newStep 
    end if

end sub

main :
    oldStep = 0

    systemInit() 
    applicationInit() 
    while (1) 
        applicationTask() 
    wend

end.
The adapted code that I want to use is below. Not tested as I do not understand the line above (optoencoder_gpioDriverInit(T_OPTOENCODER_P(@_MIKROBUS1_GPIO))) - in the types.mbas we have typedef T_OPTOENCODER_P as ^const uint8_t.

Code: Select all

'
'Example for Opto_Encoder Click
'

program Click_Opto_Encoder_PIC_test

include Click_Opto_Encoder_types
include Click_Opto_Encoder_config

dim
    newStep, oldStep as int16_t 
    
sub procedure systemInit() 
dim Rotary_Encoder_A      as sbit at RC0_bit
    Rotary_Encoder_B      as sbit at RC1_bit 
    Delay_ms(100) 
end sub

sub procedure applicationInit() 
    optoencoder_gpioDriverInit(T_OPTOENCODER_P(@_MIKROBUS1_GPIO)) 
    optoencoder_init() 
end sub

sub procedure applicationTask() 
    newStep = optoencoder_getPosition() 
    if ((oldStep <> newStep)) then 
        ' do something here
        oldStep = newStep 
    end if

end sub

main :
    oldStep = 0

    systemInit() 
    applicationInit() 
    while (1) 
        applicationTask() 
    wend
end.
Thank you for your help.
The one who asks, learns and explores may look stupid for a second. The one who does not do that, stays stupid forever.

User thunderer on: RHCforum, elforum, mikroelektronika/Libstock, HOD, fanowar

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Use of Opto_Encoder without the Mikrobus

#2 Post by filip » 30 Sep 2020 07:41

Hi,

This line initializes the click board to run on mikroBUS1 of EasyPIC PRO v7.

If you want, I can attach the library source file for better understanding.

Regards,
Filip.

Post Reply

Return to “mikroBasic PRO for PIC General”