Skip to content

Commit

Permalink
add encoder driver with another main -fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
gehadelkoumy committed Apr 15, 2023
1 parent 0f678f0 commit c51987d
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ARM-HAL-drivers/Encoder-driver/include/ENCODER_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Date : 9 FEB 2023 */
/*****************************************/

#ifndef ENCODER_CONFIG_H
#define ENCODER_CONFIG_H

//#define channel1 GPIOA,8
//#define channel2 GPIOA,9

#define EncoderResolution 230
#define WheelRadius 5


#endif
19 changes: 19 additions & 0 deletions ARM-HAL-drivers/Encoder-driver/include/ENCODER_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Date : 9 FEB 2023 */
/*****************************************/

#ifndef ENCODER_INTERFACE_H
#define ENCODER_INTERFACE_H

u8 HENCODER_u8GetMotorDirection(u8 Copy_u8Channel2Port , u8 Copy_u8Channel2Pin);
void HENCODER_voidEncoderCounts(u8 Copy_u8Channel2Port ,u8 Copy_u8Channel2Pin);
s32 HENCODER_s32GetEncoderCounts(void);
s32 HENCODER_s32GetRevPerMin(s32 Copy_s32EncoderCounts);
f32 HENCODER_f32GetDistance( s32 Copy_s32EncoderCounts );

#define ClockwiseDirection 1
#define CounterClockwiseDirection 2

#endif
16 changes: 16 additions & 0 deletions ARM-HAL-drivers/Encoder-driver/include/ENCODER_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Date : 9 FEB 2023 */
/*****************************************/

#ifndef ENCODER_PRIVATE_H
#define ENCODER_PRIVATE_H




#define Pi 3.14


#endif
77 changes: 77 additions & 0 deletions ARM-HAL-drivers/Encoder-driver/src/ENCODER_program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Date : 9 FEB 2023 */
/*****************************************/

#include "STD_TYPES.h"
#include "BIT_MATH.h"

#include "DIO_interface.h"

#include "ENCODER_interface.h"
#include "ENCODER_private.h"
#include "ENCODER_config.h"

static s32 Global_s32NumOfCounts = 0 ;

/*input parameter : encoder channels */
u8 HENCODER_u8GetMotorDirection(u8 Copy_u8Channel2Port , u8 Copy_u8Channel2Pin)
{

u8 Local_u8MotorDirection = 0;
u8 Local_u8ChannelState;

Local_u8ChannelState = MGPIO_u8GetPinValue(Copy_u8Channel2Port ,Copy_u8Channel2Pin+1);
if(Local_u8ChannelState == HIGH)
{
Local_u8MotorDirection = ClockwiseDirection;
}
else if (Local_u8ChannelState == LOW)
{
Local_u8MotorDirection = CounterClockwiseDirection;
}

return Local_u8MotorDirection;
}


void HENCODER_voidEncoderCounts(u8 Copy_u8Channel2Port ,u8 Copy_u8Channel2Pin)
{
u8 MotorDirection ;

MotorDirection = HENCODER_u8GetMotorDirection(Copy_u8Channel2Port , Copy_u8Channel2Pin);

if(MotorDirection == ClockwiseDirection)
{
Global_s32NumOfCounts++;
}
else
{
Global_s32NumOfCounts--;
}

}


s32 HENCODER_s32GetEncoderCounts(void)
{
return Global_s32NumOfCounts;
}


s32 HENCODER_s32GetRevPerMin(s32 Copy_s32EncoderCounts)
{
s32 Local_s32RPM = 0;
Local_s32RPM = (Copy_s32EncoderCounts*60)/EncoderResolution; // need to handle if not 1sec
return Local_s32RPM;
}


f32 HENCODER_f32GetDistance( s32 Copy_s32EncoderCounts )
{
f32 Local_f32Distance = 0;
Local_f32Distance = (Copy_s32EncoderCounts*2*Pi*WheelRadius)/EncoderResolution;
return Local_f32Distance;
}

100 changes: 100 additions & 0 deletions ARM-HAL-drivers/Encoder-driver/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* main.c
*
* Created on: Feb 11, 2023
* Author: Gehad
*/

#include"STD_TYPES.h"
#include"BIT_MATH.h"

#include"RCC_interface.h"
#include"DIO_interface.h"
#include"NVIC_interface.h"
#include"AFIO_interface.h"
#include"EXTI_interface.h"
#include"STK_interface.h"
//#include"TIM2_interface.h"
#include"ENCODER_interface.h"


volatile u8 MotorDirection;
volatile s32 counts = 0;
volatile s32 countsPerSec = 0;
volatile f32 RevPerMin = 0;


/*count 1sec : RCC = HSE crystal(8MHz) + systick clock = AHB/8 = (1tick -- 1usec)*/

/*ISR of Systick*/
void EncoderCounts(void)
{
static s32 LastCounts = 0;

MotorDirection = HENCODER_u8GetMotorDirection(GPIOA,PIN8);

counts = HENCODER_s32GetEncoderCounts();
countsPerSec = counts - LastCounts;

LastCounts = counts;

RevPerMin = HENCODER_s32GetRevPerMin(countsPerSec);
MGPIO_VoidSetPinValue(GPIOC,PIN13,!MGPIO_u8GetPinValue(GPIOC,PIN13));

}

/*ISR of EXTI8*/
void GetReading (void)
{
HENCODER_voidEncoderCounts(GPIOA,PIN8);
}


void main(void)
{
/*initialize RCC*/
RCC_voidInitSysClock();
/*Enable GPIOA ,B ,C clock*/
RCC_voidEnableClock(RCC_APB2,2);
RCC_voidEnableClock(RCC_APB2,3);
RCC_voidEnableClock(RCC_APB2,4);
/*Enable AFIOA clock */
RCC_voidEnableClock(RCC_APB2,0);

/*AFIO*/
MAFIO_voidSetEXTIConfiguration(LINE8 , AFIOA);
//MAFIO_voidSetEXTIConfiguration(LINE9 , AFIOA);

/*pin directions*/
/*input floating --- 0 or 1 (encoder pins)*/
MGPIO_VoidSetPinDirection(GPIOA,PIN8,INPUT_FLOATING);
MGPIO_VoidSetPinDirection(GPIOA,PIN9,INPUT_FLOATING);
/*motor*/
MGPIO_VoidSetPinDirection(GPIOA,PIN2,OUTPUT_2MHZ_PP);
MGPIO_VoidSetPinDirection(GPIOA,PIN3,OUTPUT_2MHZ_PP);
/*output pin for test*/
MGPIO_VoidSetPinDirection(GPIOC,PIN13,OUTPUT_2MHZ_PP);


/*EXTI initialize*/
/*before initializing EXTI , initiate call back*/
EXTI_voidSetCallBack(GetReading,LINE8);
MEXTI_voidInit();
MEXTI_voidSetSignalLatch(LINE8,RISING);
//MEXTI_voidSetSignalLatch(LINE9,RISING);

/*Enable EXTI(5:9) interrupt from NVIC*/
MNVIC_voidEnableInterrupt(23);

/*systick initialize*/
MSTK_voidInit();
/*start timer 1sec*/
MSTK_voidSetIntervalPeriodic(1000000,EncoderCounts);

while(1)
{
/*cytron pins*/
MGPIO_VoidSetPinValue(GPIOA,PIN2,HIGH);
MGPIO_VoidSetPinValue(GPIOA,PIN3,HIGH);
}
}

0 comments on commit c51987d

Please sign in to comment.