Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ir Pulse Generator Pseudocode

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

//

module: IR pulse generator

// function: main

// initialize PIC32 hardware

// initialize and read the team select pin. This pin tells us which team we are on, to set
comms frequency
// set as digital input
// read pin value
// if we are team A, set pulse length to 300us. Otherwise set to 500us

// set up the ‘request data transfer’ pin, so we can initiate messages to the gameplay
node
// set as digital output

// call initialize the IR LED (see “LED_pin_setup” function below)

// initialize output compare hardware to generate IR pulses (see “IR_OC_HW_init” function


below)
// do not turn on the OC module (we only want to transmit when we are close to another
‘bot)

// initialize input capture hardware to capture IR pulses (see “IR_IC_HW_init” function


below)
// turn on IC module (we should always be looking for a signal)

// set up SPI communications


// upon command from game master node, begin IR transmission
// once hall sensor senses other ‘bot moving away (i.e., it has received the
message), terminate transmission

/*---------------------------- Module Functions ---------------------------*/

LED_pin_setup
// set as digital output
// set pin low to start

IR_OC_HW_init // uses OC1 and TMR3


// set pin as output compare
// map to correct peripheral
// disable output compare while setting up
// use single compare mode
// select timer 3
// set up timer:
// clear on bit
// select internal PBCLK
// set prescale
// load / clear timer register
// set the match value (to desired pulse length)
// set the period value (match to timer size for modulo arithmetic)
// set up interrupts
// clear interrupt flag
// set input capture priority
// turn on multiple interrupt vectors (MVEC)
// enable global interrupts
// enable local interrupt

Interrupt service routine for output compare:


__ISR(_OUTPUT_COMPARE_1_VECTOR, IPL6SOFT) IR_OC_Handler(void)
// clear flag
// increment transmission edge flag

// if we are in the ‘preamble’ portion of the message, set the timing to 1.5x

// else:
// if the transmission edge count is less than the number of edges we want to
transmit:
// set the timer for one period, to transmit the next edge
// else, we need to rest the edge count and transmit an end-of-message gap
// reset the transmission edge count
// set the timer for 8x period, to generate the pulse gap

IR_IC_HW_init
// set up interrupts
// clear interrupt flag
// set input capture priority
// turn on multiple interrupt vectors (MVEC)
// enable global interrupts
// enable local interrupt

// set up input capture pin as digital input and map peripheral

// set up timer (timer 2)

// pick correct IC setup:


// rising edge first
// capture every falling edge (inversion from comparator)
// interrupt on every capture

Interrupt service routine for input capture:


void __ISR(_INPUT_CAPTURE_2_VECTOR, IPL6SOFT) IR_IC_Handler(void)

// read buffer to clear it

// clear interrupt flag

// set a one-shot timer. This will reset the receiver_edge_count when we get the
pulse gap

// if we are *not* in active receiving mode, check that the period matches the
preamble period
// if it does:
// enter active receiving mode
// set the edge count to 1 - this is the first edge of the message

// otherwise:
// take no action

// if we are in active receiving


// calculate the period
// increment the receiver edge count

// capture the IC ticks for reference


IR_IC_one_shot_timer_init // This timer is used to check for a pulse-gap and reset


receiver edge count
// disable the timer (Timer 1) before configuring
// Choose the internal clock as the source
// set up the pre-scale so that the count fits into 16 bits
// set timeout to 1500us, because our signal will always be less than 1100us

// clear flag
// set up for multiple interrupt vectors
// enable global interrupts
// enable local interrupts
// configure priority and sub-priority

// clear any pending flag


// zero timer
// turn timer on

Interrupt service routine for one-shot (pulse gap) timer


__ISR(_TIMER_1_VECTOR, IPL6SOFT) IR_IC_OneShot_Handler
// clear flag
// when timer expires, the current edge number is the last baton value
// set last baton value variable to receiver edge count
// increment the baton number (for the next transmission)
// clear active receiving flag
// toggle “request for data” pin, to transmit baton value to game master note
// reset receiver edge count to zero

You might also like