Skip to content

Commit

Permalink
Add servo pulse measurement program (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
laneboysrc committed Feb 27, 2014
1 parent 1248974 commit 4d01b9b
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
25 changes: 25 additions & 0 deletions firmware/pulse-measurement/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.PHONY: all
all: pulse-measurement


#############################################################################
.PHONY: pulse-measurement
pulse-measurement: pulse-measurement.hex

pulse-measurement.hex: pulse-measurement.o servo-input.o uart-output.o
sdcc --use-non-free -mpic14 -p16f1825 -o $@ $^


#############################################################################
.c.o:
sdcc --use-non-free -mpic14 -p16f1825 -c $<


clean :
$(RM) *.hex
$(RM) *.lst
$(RM) *.map
$(RM) *.cod
$(RM) *.o
$(RM) *.asm

49 changes: 49 additions & 0 deletions firmware/pulse-measurement/pulse-measurement.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <pic16f1825.h>
#include <stdint.h>

static __code uint16_t __at (_CONFIG1) configword1 = _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF;
static __code uint16_t __at (_CONFIG2) configword2 = _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF;

extern void Init_input(void);
extern void Read_input(void);
extern void Init_output(void);
extern void Output_result(void);


/*****************************************************************************
Init_hardware()
Initializes all used peripherals of the PIC chip.
****************************************************************************/
static void Init_hardware(void) {
//-----------------------------
// Clock initialization
// FIXME: do 32 MHz HW
OSCCON = 0b01111010; // 4x PLL disabled, 16 MHz HF, Internal oscillator

//-----------------------------
// IO Port initialization
// FIXME: do 16F1825 ports
PORTA = 0;
LATA = 0;
ANSELA = 0;
TRISA = 0b11101000; // Make all ports that are not used for the motor input
}


/*****************************************************************************
main()
No introduction needed ...
****************************************************************************/
void main(void) {
Init_hardware();
Init_output();
Init_input();

while (1) {
Read_input();
Output_result();
}
}

53 changes: 53 additions & 0 deletions firmware/pulse-measurement/servo-input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <pic16f1825.h>


/*****************************************************************************
Init_input()
Called once during start-up of the firmware.
****************************************************************************/
void Init_input(void) {
// FIXME: Timer 1 at max speed; Single pulse gate mode

TMR1H = 0;
TMR1L = 0;
}


/*****************************************************************************
Read_input
Called once per mainloop to read the input signal.
This function shall set the global variable winch_mode depending on the
state transition logic desired.
****************************************************************************/
void Read_input(void) {
// FIXME: use gate mode

++TMR1H;

#if 0
CCP1CON = 0b00000000; // Compare mode off

// Wait until servo signal is LOW
// This ensures that we do not start in the middle of a pulse
while (RA5 != 0) ;

// Wait until servo signal is high; start of pulse
while (RA5 != 1) ;

// Start the time measurement
TMR1ON = 1;

// Wait until servo signal is LOW again; end of pulse
while (RA5 != 0) ;

// Start the time measurement
TMR1ON = 0;

// Check if the measured pulse time is between 600 and 2500 us. If it is
// outside we consider it an invalid servo pulse and stop further execution.
//(TMR1H << 8) | TMR1L;
#endif
}

73 changes: 73 additions & 0 deletions firmware/pulse-measurement/uart-output.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <pic16f1825.h>

static unsigned char tx_value;

static unsigned int val;

#define FOSC 32 // Osc frequency in MHz
#define BAUDRATE 38400 // Desired baudrate
#define BRGH_VALUE 1 // Either 0 or 1
#define SPBRG_VALUE (((10*FOSC*1000000/((64-(48*BRGH_VALUE))*BAUDRATE))+5)/10)-1

static void UART_send(void);

/*****************************************************************************
Init_output()
Called once during start-up of the firmware.
****************************************************************************/
void Init_output(void) {
SPBRG = SPBRG_VALUE; // Baud Rate register, calculated by macro
BRGH = BRGH_VALUE;

SYNC=0; // Disable Synchronous/Enable Asynchronous
SPEN=1; // Enable serial port
TXEN=1; // Enable transmission mode
CREN=0; // Disable reception mode

TXREG = '\n';
}


/*****************************************************************************
Output_result()
FIXME: implement and document
****************************************************************************/
void Output_result(void) {
val = (TMR1H << 8) + TMR1L;

tx_value = val / 10000;
UART_send();
val = val % 10000;
tx_value = val / 1000;
UART_send();
val = val % 1000;
tx_value = val / 100;
UART_send();
val = val % 100;
tx_value = val / 10;
UART_send();
tx_value = val % 10;
UART_send();

tx_value = '\n';
UART_send();

for (val = 0; val < 1000; val++) {
;
}

}


/*****************************************************************************
Send tx_value out via the UART
****************************************************************************/
void UART_send(void) {
/* Wait for TSR register being empty */
while (!TRMT);

TXREG = tx_value;
}

0 comments on commit 4d01b9b

Please sign in to comment.