Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
adc_indexed/main.c

===== Description =====

This demo shows how to use the ADC (Analog to Digital) converter on SBC66 Netcruzer boards. The SBC66 Netcruzer boards have 12 Analog input ports, port 0-5 and 30-35 (old port names X0-X5, and Y0-Y5). For details see modtronix.com/product/sbc66ec/ For this demo, port 1 and 4 (X1 and X4) are configured as an Analog Inputs. By default the ADC is configured for voltages between 0 - 2.5V (To configure it for 3.3V inputs, see nz_analog.h file). For this demo:

===== Required Hardware =====

This project can be run on any of our SBC66 Netcruzer boards. For prototyping, we recommend combining this board with a Prototyping Board, like the PT66ECI for example. This low cost prototyping board makes all the I/O ports of the SBC66 board available via marked labels on the PCB. It also provides a reset and firmware button that simplifies prototyping.

===== Building Project =====

This project is located in the "src/demos/adc/adc_indexed" folder of the Netcruzer Download. To compile for Netcruzer Board, open this project in MPLAB X, and select the "Project Configuration" for desired board. For example "SBC66ECL_R2" for the SBC66ECL Revision 2 board. For details click here

===== Programming Board =====

After compiling (build), the board can be programmed via the USB Bootloader or a PIC Programmer. USB Programming is simplified when using the SBC board together with a Prototype Board.

===== File History =====

2012-08-08, David H. (DH):

#define THIS_IS_MAIN_FILE //Uniquely identifies this as the file with the main application entry function main()
#include "HardwareProfile.h" //Required for all Netcruzer projects
//For this demo, A1 (X1) is first channel (index 0), and A4 (X4) second channel (index 1)
#define ADC_INDEX_A1 0
#define ADC_INDEX_A4 1
int main(void)
{
WORD tmrFlashLed = 0; //Timer for flashing system LED
nzSysInitDefault(); //Default initialization. All ports inputs. All analog features disabled. Tick 1ms
DIR_SYSLED = OUTPUT_PIN; //Set System LED port as outputs
DIR_38 = OUTPUT_PIN; //Set Port 38 (old port name Y8) as an output pin
DIR_39 = OUTPUT_PIN; //Set Port 39 (old port name Y9) as an output pin
//Configure port 1 and 4 (old port names X1 and X4) as ADC channels
adcOpen(ADC_OPEN_A1 | ADC_OPEN_A4);
while(1)
{
nzSysTaskDefault(); //Main netcruzer task, call in main loop.
//If port 1 (X1) analog input is above 2.0V (2000mV), set 38 (old port name Y8) pin to 3.3V, else to 0V
//This funcion convertes the ADC value (0-1023), to a millivolt value
if (adcReadChanMvForIndex(ADC_INDEX_A1) > 2000)
PIN_38 = 1;
else
PIN_38 = 0;
//If port 4 (old port name X4) analog input value is above 900 (0-2.5V = 0-1023), set port 39 (old port name Y9)
//pin to 3.3V, else to 0V. This function gets te ADC's 10-bit value, which has a range from 0 - 1023.
if (adcReadChanForIndex(ADC_INDEX_A4) > 2000)
PIN_39 = 1;
else
PIN_39 = 0;
//Flash system LED every 500ms
if (tick16TestTmr(tmrFlashLed)) {
tick16UpdateTmrMS(tmrFlashLed, 200); //Update timer to expire in 200ms again
LAT_SYSLED = !LAT_SYSLED; //Toggle System LED
}
}//end while
return 0;
}//end main