Apr 102013
 

Overview:

In this project you will learn to use a basic functionality of chipKIT board in MPLAB X IDE, which is to read an analog value and output the corresponding digital reading for it. A potentiometer is a  three-terminal resistor with a sliding contact that forms an adjustable voltage divider. Using a potentiometer measuring instrument, you could measure the electric potential. In this example we use the potentiometer in the chipKIT Basic IO shield. Find the same project using MPIDE here.

Hardware Used:

To do the analog read project, you require the following hardware devices.

  • chipKIT™ Uno 32
  • chipKIT™ Basic IO shield
  • PICkit® 3 In-Circuit Debugger/Programmer
  • USB cable
  • 6 pin header to connect chipKIT™ board and PICkit® 3

Reference:

The reference guide for each board, schematics and other resources are available on their individual homepages:

Procedure:

  1. Connect the hardware devices. The PICkit® 3 should be connected to the chipKIT™ Uno32 board through a suitable header and use the USB cable to connect the PICkit® 3 to your PC.
  2. Place the IO shield on top of the Uno32 board with a bit of a firm press. A0-potentiometer of the IO shield will be used as peripheral for Uno32 board, in this excercise.
  3. Once the hardware setup is made and the device drivers are updated for PICkit® 3 on your computer, launch MPLAB X (Start>All Programs>Microchip>MPLAB X IDE>MPLAB X IDE vx.xx on Window® Machines ).
  4. Create a new project through File>New Project. In the pop up new project window, select Standalone Project  under Projects  and click Next. Under Family scroll and select 32-bit MCUs (PIC32). Note the processor number from the chipKIT board’s reference manual and enter it under the Device  option. Click Next.
  5. Select PICkit3 in the Hardware Tools, click Next. Select XC32 (v1.20) under Compiler Toolchains, click Next. Give a project name to create the project.
  6. In the Project tab on the left side of the MPLAB X IDE, right click on Source Files > New > C Main File.. and give a file name for main source code. Once the dot c file opens, select all the codes in the file and replace it with the codes below.
    #include <plib.h>
    #include <xc.h>
    
    #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
    #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
    #define SYS_FREQ (80000000L)
    
    unsigned int channel4;	// conversion result as read from result buffer
    unsigned int offset;	// buffer offset to point to the base of the idle buffer
    
    int main(void)
    {
        // Configure the device for maximum performance but do not change the PBDIV
        // Given the options, this function will change the flash wait states and
        // enable prefetch cache but will not change the PBDIV. The PBDIV value
        // is already set via the pragma FPBDIV option above..
        SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    
        // configure and enable the ADC
        CloseADC10();    // ensure the ADC is off before setting the configuration
    
        // define setup parameters for OpenADC10
                    // Turn module on | ouput in integer | trigger mode auto | enable autosample
        #define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
    
        // define setup parameters for OpenADC10
                    // ADC ref external    | disable offset test    | disable scan mode | perform 2 samples | use dual buffers | use alternate mode
        #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_9 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    
        // define setup parameters for OpenADC10
        //                   use ADC internal clock | set sample time
        #define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
    
        // define setup parameters for OpenADC10
        // do not assign channels to scan
        #define PARAM4    SKIP_SCAN_ALL
    
        // define setup parameters for OpenADC10
                    // set AN2 as analog inputs
        #define PARAM5    ENABLE_AN2_ANA
    
            // use ground as neg ref for A | use AN2 for input A
    
         // configure to sample AN2
        SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2); // configure to sample AN2
        OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above
    
        EnableADC10(); // Enable the ADC
    
      while ( ! mAD1GetIntFlag() ) { } // wait for the first conversion to complete so there will be vaild data in ADC result registers
    
        // the result of the conversion is available in channel4.
        while (1)
        {
    
            channel4 = ReadADC10(0);          // read the result of channel 4 conversion from the idle buffer
    
        }
    
        return 0;
    }
  7. To read the potentiometer value, access the watch window from Window > Debugging > Watches or you can use the shortcut key Alt+Shift+2. In the watch window panel that opens on the MPLAB X IDE, enter the new watch as channel4, in the blank <Enter new watch> area as shown below in the figure.                                                                                                                                                          analogread mplab1
  8. In the program editor window, put a breakpoint by clicking on the line number after the code line
     channel4 = ReadADC10(0);          // read the result of channel 4 conversion from the idle buffer
    That is on the line with the closing braces of the while loop. This should create a red box in the number line as shown in the figure below,                                                     analogread mplab2
  9. Click on Debug Project icon shown below,                                                 analogread mplab3
  10. Once the program halts its execution at the line with the breakpoint, click on Watches output window, right click on the expand button under Value column and select Display value column as > Decimal. The value that appear in this column for channel4 variable corresponds to the potentiometer reading which is read in the range of 0 to 1023. A value 0 corresponds to 0 volts and a value 1023 corresponds to 3.3 volts.
  11. Now, click on Continue button from the top pane and observe the value of channel4 to change and reflect the potentiometer reading as described before.
  12. If you would like to change the potentiometer knob and see a different value, then, change the potentiometer knob and click on the Reset button on the top pane. This will initialize the variables back to zero. Clicking on the Play button on the top pane, will get your changed  potentiometer value on the Watches window. Below is a screenshot showing the potentiometer value for 3.3 volts being read in the Watch window.  analogread mplab4
VN:F [1.9.22_1171]
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
Mar 152013
 

Overview:

In this project you will learn to use a basic functionality of chipKIT board, which is to read a analog value and output the corresponding digital reading for it. A potentiometer is a  three-terminal resistor with a sliding contact that forms an adjustable voltage divider. Using a potentiometer measuring instrument, you could measure the electric potential.

Hardware Used:

The hardware used in this tutorial will be a chipKIT™ Uno32 along with a chipKIT™ Basic I/O Shield both manufactured by Digilent Inc.

Reference:

The reference guide for each board, schematics and other resources are available on their individual homepages:

Procedure:

  1. Open MPIDE software on your PC and go to File > Examples > Basics > AnalogReadSerial to open the code which reads the analog signal on serial window.
  2. The code should look like this:
    /*
    AnalogReadSerial
    Reads an analog input on pin 0, prints the result to the serial monitor
    
    This example code is in the public domain.
    */
    
    void setup() {
    Serial.begin(9600);
    }
    
    void loop() {
    int sensorValue = analogRead(A0);
    Serial.println(sensorValue, DEC);
    }
    The function Serial.begin(), sets the data rate in bits per second (baud rate) for serial data transmission. In this case, the baud rate for communication between the computer and the chipKIT board is set to 9600. Using analogRead function, enables the chipKIT board to read the input voltage at port A0 ranging from 0 to 3.3 volts into values ranging from 0 to 1023 (as chipKIT board has a 10 bit A/D converter in it). Serial.println function, prints in a new line each time the values from sensorValue variable in decimal format.
  3. Make sure you connect the Uno32 board via the mini-B connector on board to an available port on your computer.
  4. Place the IO shield on top of the Uno32 board with a bit of a firm press. LEDs and A0-potentiometer of the IO shield will be used as peripherals for Uno32 board, in this excercise.
  5. Once connected, in your MPIDE window, select Tools>Board>chipKIT UNO32 to identify the UNO32 Board as the target
  6. Next, select Tools>Serial Port>COMxx. This will depend on which port was assigned to the chipKIT UNO32 Board when first connected.
  7. To load your sketch onto the chipKIT UNO32 board’s Microcontroller, press the upload button. This will compile your sketch (check for errors) and then send to the Microcontroller. Unless you type your own code in the sketch, you wouldn’t need to verify them before uploading.
  8. Once the code is done uploading, open the serial monitor from the toolbar shown below,                                                                                                                analogread96
  9. A new serial monitor window will appear as shown below,                                                                          analogread2
  10. The potentiometer on IO shield which provides input voltage to analog input A0 of UNO32 board, can be turned to get the varying voltage levels. Serial monitor window will show the decimal value change (between 0 to 1023) correspondingly.
  11. As a next step, the Organic LED present in the IO shield can be used to display the potentiometer values. For this, go to the chipKIT™ Basic I/O Shield homepage mentioned in Reference section.
  12. Download the zip file that contains libraries and documentation for using the Basic I/O Shield™ with the chipKIT MPIDE. Open the downloaded zip folder and copy the three IOShield folders in it. These three folders have the libraries and example programs for EEPROM, OLED and Temperature Sensor peripherals on IO shield.
  13. Now open the drive where you installed the mpide software, inside your mpide folder navigate through hardware > pic32 > libraries and paste the three IOShield folders in it.
  14. Close your mpide software, for the libraries to get included and the changes to take effect.
  15. On restarting the mpide software and navigating through File > Examples; you can find IOShieldEEPROM, IOShieldOled and IOShieldTemp folders with their demo examples being included.
  16. You can run the IOShield_Oled_Demo example as a new sketch, with the existing hardware setup, to observe the change in OLED display of IO shield.
  17. To make the ADC values display on OLED, create a new sketch, save it by giving a name and insert the code below in the sketch space.
    #include <IOShieldOled.h>
    
    int adc_data = 0;
    int thousands = 0;
    int hundreds = 0;
    int tens = 0;
    int ones = 0;
    
    void setup()
    {
      IOShieldOled.begin();
      IOShieldOled.displayOn();
    }
    
    void loop()
    {
      adc_data = analogRead(0);
    
      //parse data
      ones = adc_data%10;
      adc_data = adc_data/10;
      tens = adc_data%10;
      adc_data = adc_data/10;
      hundreds = adc_data%10;
      adc_data = adc_data/10;
      thousands = adc_data%10;
    
      //convert data to ASCII
       thousands = thousands+48;
       hundreds = hundreds+48;
       tens = tens+48;
       ones = ones+48;
    
      //Clear the virtual buffer
      IOShieldOled.clearBuffer();
    
      //Draw a rectangle over wrting then slide the rectagle
      //down slowly displaying all writing
        IOShieldOled.clearBuffer();
        IOShieldOled.setCursor(0, 0);
        IOShieldOled.putString("Analog read");
        IOShieldOled.setCursor(0, 1);
        IOShieldOled.putString("result is");
        IOShieldOled.setCursor(0, 3);
        IOShieldOled.putChar(thousands);
        IOShieldOled.setCursor(1, 3);
        IOShieldOled.putChar(hundreds);
        IOShieldOled.setCursor(2, 3);
        IOShieldOled.putChar(tens);
        IOShieldOled.setCursor(3, 3);
        IOShieldOled.putChar(ones);
    
      delay(100);
    
    }
  18. The above code gets the decimal valued data from the potentiometer in the IO shield, separates each number by modulo operation, converts the decimals into ASCII characters which are suitable OLED to read and displays it in OLED by assigning the cursor position.
  19. To get more details on each function used with respect to OLED, open the IOShieldOLED folder which you downloaded from Digilent website and refer IOShield Library – Oeld.pdf
  20. To get the display, verify the code and upload it to the board. Once uploaded, you can see the potentiometer value displayed on OLED. Turn the knob, to get values changing correspondingly.
VN:F [1.9.22_1171]
Rating: 9.5/10 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS