BPv3 read ADC via terminal demo
From DP
Read a voltage from the ADC pin and send it to UART FTDI com terminal
Contents |
Overview
The Bus Pirate uses the PIC24FJ64GA002 microcontroller, and as such it can be used as a development board for the same, with some limitations.This demo is a combination reading a ADC value and communicating with PC terminal via FTDI USB(UART). A value will read via the internal ADC peripheral and sent to the PC terminal via the UART peripheral and the onboard FTDI UART to USB IC.
In the text below, the code is broken up and explained section by section. If you want you can download the full MPLAB 8 project from our SVN, or copy/paste the full code for main.c that will be displayed at the bottom of the page.
Using #define statements
In this tutorial we will use #define statements to make the code easier to read. Basically we will use #define statements to supplement one line of text with another.
Syntax
#define text1 text2This tells the compiler to replace text1 with text2 in the source code. text1 and text 2 must be a non-spaced string of characters.
Usage
#define VREG_DIR TRISAbits.TRISA0In our code we thell the compiler to replace any instance of VREG33_DIR with TRISAbits.TRISA0. This enables us to write VREG33_DIR instead of TRISAbits.TRISA0 throughout the code, which is easier to understand then trying to figure out which bit of the TRIS register is connected to which pin. This way we know that VREG_DIR controls the direction of the pin associated with the voltage regulator.
Using Functions
Another concept that will be used in this tutorial are functions. They are pieces of code that are written separately from the main function, and can be called within it.
Basic Syntax
[type of return_value] name_fo_function([type of argument1] argument1,...) { //code to be executed in the function return return_value; }
Usage
Fist we need to declare the function we will use before the main function.
void UART1TX(char c);
Here we declare taht the function doesn't have a return value (void) and that it takes one argument of the type char named c.
Next we need to define our function anywhere in our source code.
void UART1TX(char c){ while(U1STAbits.UTXBF == 1); //if buffer is full, wait U1TXREG = c; }
This function waits while the TX buffer is full, and when it is not, the char argument c to the buffer.
