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.
Finally we need to call this function from the main function or any other function.
UART1TX('a');
This will start executing our function with the char attribute set as 'a'. It will send the 'a' ASCII character to the UART1TX buffer.
Basic Configuration
#include <p24fxxxx.h> _CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE & I2C1SEL_PRI) // Internal FRC OSC = 8MHz _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) //turn off junk we don't need #define VREG_DIR TRISAbits.TRISA0 //sets VREG33_DIR to TRISBIT A0 #define VREG_EN LATAbits.LATA0 //sets VREG_EN to LAT bit A0 (output latch) #define MODE_LED_DIR TRISAbits.TRISA1 //sets the MODE_LED_DIR to TRISBIT A1 #define MODE_LED LATAbits.LATA1 //sets the MODE_LED to LAT bit A! (output latch) #pragma code
First line sets up the internal oscillator to produce the 32 MHz clock needed for the 16MIPS maximum speed of the BUS Pirate uC. Second line turns off all the uC options we wont use in this tutorial. This is followed by #define statements which allow us to use VREG_DIR, VREG_EN, MODE_LED_DIR, and MODE_LED names for the TRIS and LAT registers associated with VREG and MODE LED pins.
Function Declarations
void initADC(); unsigned int getADC(); unsigned char UART1RX(); void UART1TX(char c); void InitializeUART1(void); void initADC(void);
Here we declare all the function we will use in the subsequent code. The 'getADC' simply reads one ADC value from the ADC pin and returns that value as unsigned int. 'unsigned char UART1RX()' reads one character form the UART1RX buffer and returns that value. 'void UART1TX(char c)' sends the 'c' character to the UART1TX buffer. 'void InitializeUART(void) simply configures the UART1 module. While the 'void initADC(void)' function configures the ADC peripheral.
