Sei sulla pagina 1di 7

IOWA STATE UNIVERSITY

TDMA Code Explanation


Written in C for the PIC18F46J11
Bear Tracking Team (SDMAY1010)
3/19/2010

The purpose of this document is to keep a working record of the code written for the PIC18F46J11. This
document applies to the Revision A hardware design.

Contents
Introduction .................................................................................................................................................. 4
Code Discussion ............................................................................................................................................ 4
main.c........................................................................................................................................................ 4
main() .................................................................................................................................................... 4
activate_unit() ....................................................................................................................................... 4
maintenance() ....................................................................................................................................... 4
init.c .......................................................................................................................................................... 5
eusart_init()........................................................................................................................................... 5
osc_init() ............................................................................................................................................... 5
io_init() .................................................................................................................................................. 5
vhf_init() ................................................................................................................................................ 5
handler.c ................................................................................................................................................... 5
ds_handler() .......................................................................................................................................... 5
dpslp_chk() ............................................................................................................................................ 5
go_to_sleep() ........................................................................................................................................ 5
send_eusart() ........................................................................................................................................ 5
send_gpio() ........................................................................................................................................... 5
eusart_test().......................................................................................................................................... 6
sleep_count() ........................................................................................................................................ 6
power_33()............................................................................................................................................ 6
power_5().............................................................................................................................................. 6
vhf_trx()................................................................................................................................................. 6
switch_ctrl() .......................................................................................................................................... 6
interrupts.c................................................................................................................................................ 6
high_vector() ......................................................................................................................................... 6
low_vector() .......................................................................................................................................... 6
high_vector_table()............................................................................................................................... 6
low_vector_table() ................................................................................................................................ 6
eusart_tx_int() ...................................................................................................................................... 7
eusart_rx_int() ...................................................................................................................................... 7

Introduction
This document describes the files as well as the high level and low level functions written for the
PIC18F46J11 microcontroller for Revision A of the Bear Tracking Project (SDMAY1010). The
tools used to program this microcontroller are the following:

IDE: MPLAB
Programmer: PICKIT2
Compiler: MPLAB C for PIC18 MCUs (MCC18)

The MPLAB project currently used is the following:

TDMANetwork.mcp

The files currently created for this project are the following:

main.c with main.h header


init.c with init.h header
handler.c with handler.h header
interrupts.c with interrupts.h header

These files and the functions declared will be explained in detail in following sections.

Code Discussion
This section discusses the files and functions used in the TDMANetwork.mcp project.

main.c
This file configures and initializes the PIC18F46J11, and serves as the skeleton for the TDMA
network.

main()
This function calls the initialization functions and then runs the deep sleep handler function. It is
then decided whether the power on reset (POR) was pure, meaning it is the first power up, or was
an awake from deep sleep. Proper actions are then taken based on the decision.

activate_unit()
This function is called when the PIC has fully wakened from deep sleep. It is where
communication with the GPS, VHF transceiver, and EEPROM will take place. This is where
communication with other units will take place (one frame in the TDMA network). After all
networking logic is complete, data is written to the deep sleep save registers and the PIC goes
back to sleep.

maintenance()
This function performs maintenance on the peripherals of the PIC. Its purpose is to make sure
all devices are working correctly.

init.c
This file contains functions that initialize communication ports, I/O ports, the oscillator, etc.

eusart_init()
This function initializes all registers required to serially communicate. What is initialized here is
required to transmit RF data using the VHF transceiver.

osc_init()
This function initializes all registers required for the PICs internal oscillator to oscillate
properly.

io_init()
This function initializes all GPIO pins to be either digital or analog inputs or outputs. In Revision
A, there should only be one pin set as input (for the battery monitor) and it should be analog.

vhf_init()
This function calls a function called send_gpio() that sends a 32 element array of ones and zeros,
specified by the user, using GPIO pins and following the procedure described in the ADF7021
datasheet. This will properly configure the VHF transceiver to be either in TX or RX mode,
depending on the argument called by the user.

handler.c
This file contains functions that perform any type of data handling. This involves the deep sleep
functionality, sending eusart data, sending data via I/O ports, and other functions for peripheral
testing purposes.

ds_handler()
This function is called to decide whether or not the power on reset (POR) was pure or if it was
from a deep sleep wake.

dpslp_chk()
This function is called to determine whether or not the power on reset (POR) was pure or if it
was from a deep sleep wake.

go_to_sleep()
This function is called if a wake from a deep sleep has occurred, but the wake is not at the
beginning of a TDMA network frame. When this function is called, it increments a counter by
calling the sleep_count() function. It then writes this counter information to the deep sleep save
registers, and puts the PIC back into deep sleep.

send_eusart()
This function has an integer as a parameter and sends this integer on the TX pin of the PIC.

send_gpio()
This function has an unsigned character array as a parameter and sends each element of the array
over I/O pins. The array can only contain ones or zeros and must be a length of 32. This function
provides the functionality required to configure the VHF transceiver.

eusart_test()
This function tests the eusart port by calling the send_eusart function to send specific values
(from 0x00 to 0x03).

sleep_count()
This function increments the counter variables count1 and count2. These variables are set at zero
when a pure POR has occurred. When counter1 passes 255, it resets to zero and increments
counter 2. So, the maximum value these counters can increment to is 2552 or 65,025.

power_33()
This function sets the proper I/O pin high or low to turn the 3.3V power supply on or off
respectively.

power_5()
This function sets the proper I/O pin high or low to turn the 5V power supply on or off
respectively.

vhf_trx()
This function sets the proper I/O pin high or low to turn the VHF transceiver on or off
respectively.

switch_ctrl()
This function is called to control the RF switch to be in either receive or transmit mode. This
function has an unsigned character as a parameter. If the argument passed through this function
is 0, the switch is off. If the argument passed through this function is 1, the switch is in transmit
mode. If the argument passed through this function is 2, the switch is in receive mode.

interrupts.c
This file contains all functions needed for handling interrupts on the PIC18F46J11.

high_vector()
When a high priority interrupt occurs, the program arrives at this function. This function then
directs the program counter (PC) to the high_vector_table() function using assembly code.

low_vector()
When a low priority interrupt occurs, the program arrives at this function. This function then
directs the program counter (PC) to the low_vector_table() function using assembly code.

high_vector_table()
If an eusart receive interrupt has occurred, this function calls the eusart_rx_int() function. If an
eusart transmit interrupt has occurred, this function calls the eusart_tx_int() function. If a value is
sent using eusart, this interrupt will have occurred.

low_vector_table()
This function is not implemented yet because no low priority interrupts are set up.

eusart_tx_int()
This function releases the tx enable pin to clear the tx interrupt flag and allow for another
transmission.

eusart_rx_int()
This function releases the tx enable pin to clear the tx interrupt flag and allow for another
transmission. It then stores the value that was received in an array called eusart_buff. If the data
received exceeds the length of eusart_buff, the rx pointer is reset to the beginning of the buffer so
that data is overwritten.

Potrebbero piacerti anche