A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wheel.cpp Source File

Wheel.cpp

00001 /*******************************************************************************
00002  *
00003  *  Wheel.c - Driver for the scroll wheel
00004  *
00005  *  Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *    Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  *    Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  *    Neither the name of Texas Instruments Incorporated nor the names of
00020  *    its contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************/
00036 
00037 /***************************************************************************//**
00038  * @file       HAL_Wheel.c
00039  * @addtogroup HAL_Wheel
00040  * @{
00041  ******************************************************************************/
00042 #include "mbed.h"
00043 //#include "msp430.h"
00044 #include "Wheel.h"
00045 
00046 //#define WHEEL_PORT_DIR P8DIR
00047 //#define WHEEL_PORT_OUT P8OUT
00048 //#define WHEEL_ENABLE  BIT0
00049 //#define ADC_PORT_SEL  P6SEL
00050 //#define ADC_INPUT_A5  BIT5
00051 
00052 uint16_t positionData;
00053 uint16_t positionDataOld;
00054 
00055 /***************************************************************************//**
00056  * @brief   Set up the wheel
00057  * @param   None
00058  * @return  None
00059  ******************************************************************************/
00060 
00061 void Wheel_init(void)
00062 {
00063    LPC_PINCON->PINSEL3 |=  (3UL << 30);                  /* P1.31 is AD0.5, attached to pot1*/
00064    LPC_SC->PCONP       |=  (1 << 12);                    /* Enable power to ADC block */
00065    LPC_ADC->ADCR        =  (1 << 5) |                    /* select AD0.5 pin b100000*/
00066                            (4 << 8) |                    /* ADC clock is 25MHz/5 */
00067                            (1 << 21);                    /* enable ADC */ 
00068 
00069     //WHEEL_PORT_DIR |= WHEEL_ENABLE;
00070     //WHEEL_PORT_OUT |= WHEEL_ENABLE;                    // Enable wheel
00071 
00072     //ADC12CTL0 = ADC12SHT02 + ADC12ON;                  // Sampling time, ADC12 on
00073     //ADC12CTL1 = ADC12SHP;                              // Use sampling timer
00074     //ADC12MCTL0 = ADC12INCH_5;                          // Use A5 (wheel) as input
00075     //ADC12CTL0 |= ADC12ENC;                             // Enable conversions
00076     //ADC_PORT_SEL |= ADC_INPUT_A5;                      // P6.5 ADC option select (A5)
00077 }
00078 
00079 /***************************************************************************//**
00080  * @brief   Determine the wheel's position
00081  * @param   None
00082  * @return  Wheel position (0~7)
00083  ******************************************************************************/
00084 
00085 uint8_t Wheel_getPosition(void)
00086 {
00087     uint8_t position = 0;
00088 
00089     Wheel_getValue();
00090 
00091     //determine which position the wheel is in
00092     if (positionData > 0x0806)
00093         position = 7 - (positionData - 0x0806) / 260;  //scale the data for 8 different positions
00094     else
00095         position = positionData / 260;
00096 
00097     return position;
00098 }
00099 
00100 /***************************************************************************//**
00101  * @brief   Determine the raw voltage value across the potentiometer
00102  * @param   None
00103  * @return  Value
00104  ******************************************************************************/
00105 
00106 uint16_t Wheel_getValue(void)
00107 {
00108 
00109     LPC_ADC->ADCR |=  (1<<24);                     /* start conversion */
00110     while (!(LPC_ADC->ADGDR & (1UL<<31)));         /* Wait for Conversion end */
00111     positionData = ((LPC_ADC->ADGDR >> 4) & 0xFFF);         /* read converted value */
00112                                                    /* read value needs scaling*/
00113     LPC_ADC->ADCR &= ~(7<<24);                     /* stop conversion */
00114     //printf("ADC Value %i\r\n",positionData);
00115     //measure ADC value
00116     //ADC12IE = 0x01;                                    // Enable interrupt
00117     //ADC12CTL0 |= ADC12SC;                              // Start sampling/conversion
00118     //__bis_SR_register(LPM0_bits + GIE);                // LPM0, ADC12_ISR will force exit
00119     //ADC12IE = 0x00;                                    // Disable interrupt
00120 
00121     //add hysteresis on wheel to remove fluctuations
00122     if (positionData > positionDataOld)
00123         if ((positionData - positionDataOld) > 10)
00124             positionDataOld = positionData;            //use new data if change is beyond
00125                                                        // fluctuation threshold
00126         else
00127             positionData = positionDataOld;            //use old data if change is not beyond
00128                                                        // fluctuation threshold
00129     else
00130     if ((positionDataOld - positionData) > 10)
00131         positionDataOld = positionData;                //use new data if change is beyond
00132                                                        // fluctuation threshold
00133     else
00134         positionData = positionDataOld;                //use old data if change is not beyond
00135                                                        // fluctuation threshold
00136 
00137     return positionData;
00138 }
00139 
00140 /***************************************************************************//**
00141  * @brief   Disable wheel
00142  * @param   None
00143  * @return  none
00144  ******************************************************************************/
00145 
00146 void Wheel_disable(void)
00147 {
00148   
00149     //WHEEL_PORT_OUT &= ~WHEEL_ENABLE;                   //disable wheel
00150     //ADC12CTL0 &= ~ADC12ENC;                            // Disable conversions
00151     //ADC12CTL0 &= ~ADC12ON;                             // ADC12 off
00152 }
00153 
00154 /***************************************************************************//**
00155  * @brief   Enable wheel
00156  * @param   None
00157  * @return  none
00158  ******************************************************************************/
00159 
00160 void Wheel_enable(void)
00161 {
00162 
00163     //WHEEL_PORT_OUT |= WHEEL_ENABLE;                    //disable wheel
00164     //ADC12CTL0 |= ADC12ON;                              // ADC12 on
00165     //ADC12CTL0 |= ADC12ENC;                             // Enable conversions
00166 }
00167 
00168 /***************************************************************************//**
00169  * @brief Handles ADC interrupts.
00170  *
00171  *        Stores result of single ADC conversion for reading position of the scroll wheel.
00172  * @param  none
00173  * @return none
00174  ******************************************************************************/
00175 /*
00176 #pragma vector = ADC12_VECTOR
00177 __interrupt void ADC12_ISR(void)
00178 {
00179     switch (__even_in_range(ADC12IV, ADC12IV_ADC12IFG15))
00180     {
00181         // Vector  ADC12IV_NONE:  No interrupt
00182         case  ADC12IV_NONE:
00183             break;
00184 
00185         // Vector  ADC12IV_ADC12OVIFG:  ADC overflow
00186         case  ADC12IV_ADC12OVIFG:
00187             break;
00188 
00189         // Vector  ADC12IV_ADC12TOVIFG:  ADC timing overflow
00190         case  ADC12IV_ADC12TOVIFG:
00191             break;
00192 
00193         // Vector  ADC12IV_ADC12IFG0: ADC12IFG0:
00194         case  ADC12IV_ADC12IFG0:
00195             positionData = ADC12MEM0;                  // ADC12MEM = A0 > 0.5AVcc?
00196             __bic_SR_register_on_exit(LPM0_bits);      // Exit active CPU
00197             break;
00198 
00199         // Vector  ADC12IV_ADC12IFG1:  ADC12IFG1
00200         case  ADC12IV_ADC12IFG1:
00201             break;
00202 
00203         // Vector ADC12IV_ADC12IFG2:  ADC12IFG2
00204         case ADC12IV_ADC12IFG2:
00205             break;
00206 
00207         // Vector ADC12IV_ADC12IFG3:  ADC12IFG3
00208         case ADC12IV_ADC12IFG3:
00209             break;
00210 
00211         // Vector ADC12IV_ADC12IFG4:  ADC12IFG4
00212         case ADC12IV_ADC12IFG4:
00213             break;
00214 
00215         // Vector ADC12IV_ADC12IFG5:  ADC12IFG5
00216         case ADC12IV_ADC12IFG5:
00217             break;
00218 
00219         // Vector ADC12IV_ADC12IFG6:  ADC12IFG6
00220         case ADC12IV_ADC12IFG6:
00221             break;
00222 
00223         // Vector ADC12IV_ADC12IFG7:  ADC12IFG7
00224         case ADC12IV_ADC12IFG7:
00225             break;
00226 
00227         // Vector ADC12IV_ADC12IFG8:  ADC12IFG8
00228         case ADC12IV_ADC12IFG8:
00229             break;
00230 
00231         // Vector ADC12IV_ADC12IFG9:  ADC12IFG9
00232         case ADC12IV_ADC12IFG9:
00233             break;
00234 
00235         // Vector ADC12IV_ADC12IFG10:  ADC12IFG10
00236         case ADC12IV_ADC12IFG10:
00237             break;
00238 
00239         // Vector ADC12IV_ADC12IFG11:  ADC12IFG11
00240         case ADC12IV_ADC12IFG11:
00241             break;
00242 
00243         // Vector ADC12IV_ADC12IFG12:  ADC12IFG12
00244         case ADC12IV_ADC12IFG12:
00245             break;
00246 
00247         // Vector ADC12IV_ADC12IFG13:  ADC12IFG13
00248         case ADC12IV_ADC12IFG13:
00249             break;
00250 
00251         // Vector ADC12IV_ADC12IFG14:  ADC12IFG14
00252         case ADC12IV_ADC12IFG14:
00253             break;
00254 
00255         // Vector ADC12IV_ADC12IFG15:  ADC12IFG15
00256         case ADC12IV_ADC12IFG15:
00257             break;
00258 
00259         default:
00260             break;
00261     }
00262 }
00263 */
00264 /***************************************************************************//**
00265  * @}
00266  ******************************************************************************/
00267