0% found this document useful (0 votes)
4 views2 pages

Encoder Speed Measurement Code

This document contains C code for an encoder that measures speed. It includes functions to initialize peripherals like GPIO pins and timers, configure interrupt handling, and measure velocity by counting clock pulses within a set time period. The main function sets the system clock, calls the initialization functions, then loops continuously calculating the elapsed time between clock pulses and determining speed from that time.

Uploaded by

Mishell Domingo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Encoder Speed Measurement Code

This document contains C code for an encoder that measures speed. It includes functions to initialize peripherals like GPIO pins and timers, configure interrupt handling, and measure velocity by counting clock pulses within a set time period. The main function sets the system clock, calls the initialization functions, then loops continuously calculating the elapsed time between clock pulses and determining speed from that time.

Uploaded by

Mishell Domingo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Encoder.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/tm4c123gh6pm.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
1#include "inc/hw_timer.h"
#define Tmpmax 39999999
#define dØ 0.314159
volatile float contador = 0;
volatile float dt = 0.00000;
volatile float W;

void PERIFERICOS(void){
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
}

void GPIO(void){
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1); 25}

void TIMER(void){
TimerConfigure(TIMER0_BASE, TIMER_CFG_ONE_SHOT_UP);
TimerLoadSet(TIMER0_BASE, TIMER_A, Tmpmax);
}

void INTERRUPCION(void){
GPIOIntTypeSet(GPIO_PORTD_BASE, GPIO_PIN_0, GPIO_FALLING_EDGE);
GPIOIntEnable(GPIO_PORTD_BASE, GPIO_INT_PIN_0);
IntEnable(INT_GPIOD);
IntMasterEnable();
}

//PIN PD0 --> CLK


//PIN PD1 --> DT
void MEDIR_VELOCIDAD(void){
GPIOIntClear(GPIO_PORTD_BASE, GPIO_INT_PIN_0);
contador = TimerValueGet(TIMER0_BASE, TIMER_A); // Se determina
cuantos numeros se han contado
HWREG(TIMER0_BASE + TIMER_O_TAV) = 0; //Reiniciar el contador
TimerEnable(TIMER0_BASE, TIMER_A);
}

int main(void){
SysCtlClockSet(SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_1
6MHZ| SYSCTL_SYSDIV_5);//40Mhz
PERIFERICOS();
GPIO();
TIMER();
INTERRUPCION();
while(1){
dt = contador/40000000;
W = dØ/dt;
}
}

Page 2

You might also like