void adcInit(void) { /* * Note: This function initializes only ADC2, and only for single channel, single conversion mode. No DMA, no interrupts, no bells or whistles. */ /* Note that this de-initializes registers for all ADCs (ADCx) */ ADC_DeInit(); /* Define ADC init structures */ ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; /* Populates structures with reset values */ ADC_StructInit(&ADC_InitStructure); ADC_CommonStructInit(&ADC_CommonInitStructure); /* enable ADC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); /* init ADCs in independent mode, div clock by two */ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; /* HCLK = 168MHz, PCLK2 = 84MHz, ADCCLK = 42MHz (when using ADC_Prescaler_Div2) */ ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* Init ADC2: 12bit, single-conversion. For Arduino compatibility set 10bit */ analogReadResolution(12); /* Enable ADC2 */ ADC_Cmd(ADC2, ENABLE); }
void setup() { Serial.begin(115200); for(int a = 0; a < 12; a++){ pinMode(output[a], OUTPUT); } analogReadResolution(10); }
void FPR_control::set_precision(int pwmBits, int adcBits) { _pwmBits = pwmBits; _adcBits = adcBits; //ADC and PWM precision analogWriteResolution(_pwmBits); analogReadResolution(_adcBits); }
void LipoSingleCellMonitor::Initialize(uint8_t expectedCells, uint8_t analogReadReso) { _debug = false; _cellsInUse = expectedCells; _expectedCells = expectedCells; analogReadResolution(analogReadReso); analogReference(DEFAULT); _pins = new int32_t[_MAXCELLS]; _totalVoltage = BufferedSmoothValue<float>(Coefficients::Hamming, 21); for (int i = 0; i < _MAXCELLS; i++) { _pins[i] = i; _cellVoltages[i] = BufferedSmoothValue<float>(Coefficients::Hamming, 21); } }
uint16_t hwCPUVoltage() { analogReference(DEFAULT); analogReadResolution(12); analogReadAveraging(32); #if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0/3.1/3.2 return 1195 * 4096 / analogRead(39); #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // Teensy 3.6 return 1195 * 4096 / analogRead(71); #elif defined(__MKL26Z64__) // Teensy LC // not supported return FUNCTION_NOT_SUPPORTED; #else // not supported return FUNCTION_NOT_SUPPORTED; #endif }
void setup() { pinMode(IO13, OUTPUT); /* LED fixed on board */ analogReadResolution(10); analogWriteResolution(8); #ifdef ENABLE_GROVE_KIT Wire.begin(); lcd.begin(16,2); /* set LCD to 16 columns, 2 lines */ lcd.setRGB(0x10, 0x10, 0xe0); /* LCD backlight goes blue */ #endif /* ENABLE_GROVE_KIT */ #ifdef ENABLE_BLUEMIX /* Connect to IBM Watson IoT */ BluemixClient.connect(); #ifndef ENABLE_BLUEMIX_REGISTERED Serial.println((char *)"Connected to IBM Watson IoT (Quickstart mode)"); #else Serial.println((char *)"Connected to IBM Watson IoT (Registered mode)"); #endif /* ENABLE_BLUEMIX_REGISTERED */ #endif /* ENABLE_BLUEMIX */ }
//////////////////////////////////////////////////////////////////////////////// // AUDIO //////////////////////////////////////////////////////////////////////////////// void audioInit() { pinMode(AUDIO_INPUT_PIN, INPUT); analogReadResolution(AUDIO_READ_RESOLUTION); analogReadAveraging(AUDIO_READ_AVERAGING); }
TeensyUnit::TeensyUnit(): Wire(0), FPWM_pin {FPWM_1_pin, FPWM_2_pin, FPWM_3_pin, FPWM_4_pin, FPWM_5_pin, FPWM_6_pin}, SPWM_pin {SPWM_1_pin, SPWM_2_pin, SPWM_3_pin, SPWM_4_pin, SPWM_5_pin, SPWM_6_pin}, Analog_pin {Analog_1_pin, Analog_2_pin, Analog_3_pin, Analog_4_pin, Analog_5_pin, Analog_6_pin}, sound0(*this, 0), sound1(*this, 1), sound2(*this, 2), sound3(*this, 3), sound4(*this, 4), sound5(*this, 5), sound{sound0, sound1, sound2, sound3, sound4, sound5} { //=============================================== //==== pin initialization ==== //=============================================== uint8_t num_ports = 0; uint8_t num_pins = 0; //--- Teensy On-Board --- pinMode(indicator_led_pin, OUTPUT); //--- Programming Pin --- pinMode(PGM_DO_pin, OUTPUT); //--- FPWM pins --- num_ports = sizeof(FPWM_pin)/sizeof(FPWM_pin[0]); for (uint8_t j = 0; j<num_ports; j++){ num_pins = sizeof(FPWM_pin[j])/sizeof(FPWM_pin[j][0]); for (uint8_t i = 0; i<num_pins; i++){ pinMode(FPWM_pin[j][i], OUTPUT); } } //--- Analogue pins --- num_ports = sizeof(Analog_pin)/sizeof(Analog_pin[0]); for (uint8_t j = 0; j<num_ports; j++){ num_pins = sizeof(Analog_pin[j])/sizeof(Analog_pin[j][0]); for (uint8_t i = 0; i<num_pins; i++){ pinMode(Analog_pin[j][i], INPUT); } } //--- Analogue settings --- analogReadResolution(12); analogReadAveraging(32); analogWriteResolution(8); analogWriteFrequency(0, 1600); analogWriteFrequency(1, 1600); analogWriteFrequency(2, 1600); //--- Slow PWM driver ---- spwm = PWMDriver(0x40); //--- Multiplexer pins --- num_pins = sizeof(I2C_MUL_ADR_pin)/sizeof(I2C_MUL_ADR_pin[0]); for (uint8_t i = 0; i<num_pins; i++){ pinMode(I2C_MUL_ADR_pin[i], OUTPUT); } //--- I2C initialization ---- Wire.begin(I2C_MASTER,0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, I2C_RATE_400); }
void RealMouse::setup(){ analogReadResolution(12); digitalWrite(LEDR,LOW); digitalWrite(LEDG,LOW); digitalWrite(LEDB,LOW); digitalWrite(LEDGO,LOW); digitalWrite(SDCS,LOW); digitalWrite(BUZZER,LOW); digitalWrite(IREMITTER1,LOW); digitalWrite(IREMITTER2,LOW); digitalWrite(IREMITTER3,LOW); digitalWrite(IREMITTER4,LOW); digitalWrite(IREMITTER5,LOW); digitalWrite(IREMITTER6,LOW); digitalWrite(VL6180EN1,LOW); digitalWrite(VL6180EN2,LOW); digitalWrite(VL6180EN3,LOW); digitalWrite(VL6180EN4,LOW); digitalWrite(VL6180EN5,LOW); digitalWrite(VL6180EN6,LOW); digitalWrite(MOTOR1A,LOW); digitalWrite(MOTOR1B,LOW); digitalWrite(MOTOR2A,LOW); digitalWrite(MOTOR2B,LOW); digitalWrite(MOTORDIR1,LOW); digitalWrite(MOTORDIR2,LOW); pinMode(IREMITTER1,OUTPUT); pinMode(IREMITTER2,OUTPUT); pinMode(IREMITTER3,OUTPUT); pinMode(IREMITTER4,OUTPUT); pinMode(IREMITTER5,OUTPUT); pinMode(IREMITTER6,OUTPUT); pinMode(IRRECEIVER1,INPUT); pinMode(IRRECEIVER2,INPUT); pinMode(IRRECEIVER3,INPUT); pinMode(IRRECEIVER4,INPUT); pinMode(IRRECEIVER5,INPUT); pinMode(IRRECEIVER6,INPUT); pinMode(BATTERYSENSE,INPUT); pinMode(VL6180EN1,OUTPUT); pinMode(VL6180EN2,OUTPUT); pinMode(VL6180EN3,OUTPUT); pinMode(VL6180EN4,OUTPUT); pinMode(VL6180EN5,OUTPUT); pinMode(VL6180EN6,OUTPUT); pinMode(ENCODER1A,INPUT); pinMode(ENCODER1B,INPUT); pinMode(ENCODER2A,INPUT); pinMode(ENCODER2B,INPUT); pinMode(MOTOR1A,OUTPUT); pinMode(MOTOR1B,OUTPUT); pinMode(MOTOR2A,OUTPUT); pinMode(MOTOR2B,OUTPUT); pinMode(MOTORDIR1,OUTPUT); pinMode(MOTORDIR2,OUTPUT); pinMode(LEDR,OUTPUT); pinMode(LEDG,OUTPUT); pinMode(LEDB,OUTPUT); pinMode(LEDGO,OUTPUT); pinMode(BUTTONGO,INPUT_PULLUP); pinMode(BUTTON1,INPUT_PULLUP); pinMode(BUTTON2,INPUT_PULLUP); pinMode(SDCARDDETECT,INPUT_PULLUP); pinMode(SDCS,OUTPUT); pinMode(BUZZER,OUTPUT); digitalWrite(LEDR,LOW); digitalWrite(LEDG,LOW); digitalWrite(LEDB,LOW); digitalWrite(LEDGO,LOW); digitalWrite(SDCS,LOW); digitalWrite(BUZZER,LOW); digitalWrite(IREMITTER1,LOW); digitalWrite(IREMITTER2,LOW); digitalWrite(IREMITTER3,LOW); digitalWrite(IREMITTER4,LOW); digitalWrite(IREMITTER5,LOW); digitalWrite(IREMITTER6,LOW); digitalWrite(VL6180EN1,LOW); digitalWrite(VL6180EN2,LOW); digitalWrite(VL6180EN3,LOW); digitalWrite(VL6180EN4,LOW); digitalWrite(VL6180EN5,LOW); digitalWrite(VL6180EN6,LOW); digitalWrite(MOTOR1A,LOW); digitalWrite(MOTOR1B,LOW); digitalWrite(MOTOR2A,LOW); digitalWrite(MOTOR2B,LOW); digitalWrite(MOTORDIR1,LOW); digitalWrite(MOTORDIR2,LOW); goButton.attach(BUTTONGO); goButton.interval(10); aButton.attach(BUTTON1); aButton.interval(200); bButton.attach(BUTTON2); bButton.interval(200); //Start I2C library Wire.begin(); Wire1.begin(); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); // initialize with the I2C addr 0x3D (for the 128x64) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.display(); delay(200); /* Initialise the sensor */ if(!imu.begin()) { /* There was a problem detecting the BNO055 ... check your connections */ display.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); display.display(); exit(0); } int left_rangefinder_init_status = left_rangefinder.initMouse(); int middle_rangefinder_init_status = middle_rangefinder.initMouse(); int right_rangefinder_init_status = right_rangefinder.initMouse(); if(left_rangefinder_init_status != 0 || middle_rangefinder_init_status != 0 || right_rangefinder_init_status != 0 ){ display.println("Ooops, rangefinder broken"); display.print(left_rangefinder_init_status); display.print(middle_rangefinder_init_status); display.print(right_rangefinder_init_status); display.display(); exit(0); } int range_interval = 20; left_rangefinder.startRangeContinuous(range_interval); middle_rangefinder.startRangeContinuous(range_interval); right_rangefinder.startRangeContinuous(range_interval); float voltage = analogRead(BATTERYSENSE) * (3.3 / 4095) * (49 / 10.0) / 3.0; if (voltage < 3.6){ display.clearDisplay(); display.setCursor(0, 0); display.print("Voltage is too low!"); display.println(voltage); display.display(); exit(0); } Serial.print("voltage :"); Serial.println(voltage); imu.setExtCrystalUse(true); motL.setPID(0.09,0.01,0.01,0); motR.setPID(0.09,0.01,0.01,0); kc.setSampleTime(20); kc.setAcceleration(12000,4*M_PI,6000,10*M_PI); kc.setup(); }
// Public Methods ////////////////////////////////////////////////////////////// void DualMC33926::init() { // Define pinMode for the pins and set the frequency for timer1. pinMode(_M1DIR,OUTPUT); pinMode(_M1PWM,OUTPUT); // pinMode(_M1FB,INPUT); pinMode(_M2DIR,OUTPUT); pinMode(_M2PWM,OUTPUT); // pinMode(_M2FB,INPUT); pinMode(_nD2,OUTPUT); digitalWrite(_nD2,HIGH); // default to on pinMode(_nSF,INPUT); #ifdef __SAM3X8E__ //DUE analogReadResolution(12); //0-4095 // #undef PWM_FREQUENCY //#define PWM_FREQUENCY 20000 //20khz #else //mega // PWM frequency calculation // 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz //TCCR1A = 0b10100000; // TCCR1B = 0b00010001; // ICR1 = 400; // TCCR2A settings //--------------------------------------------------------------------- // These bits control the Output Compare pin (OC2A) behavior. If one or // both of the COM2A1:0 bits are set, the OC2A output overrides the // normal port functionality of the I/O pin it is connected to. // However, note that the Data Direction Register (DDR) bit // corresponding to the OC2A pin must be set in order to enable the // output driver. // When OC2A is connected to the pin, the function of the COM2A1:0 bits // depends on the WGM22:0 bit setting. // // Fast PWM Mode // COM2A1 COM2A0 // 0 0 Normal port operation, OC2A disconnected. // 0 1 WGM22 = 0: Normal Port Operation, OC0A Disconnected. // WGM22 = 1: Toggle OC2A on Compare Match. // 1 0 Clear OC2A on Compare Match, set OC2A at BOTTOM // 1 1 Clear OC2A on Compare Match, clear OC2A at BOTTOM cbi(TCCR2A,COM2A1); cbi(TCCR2A,COM2A0); sbi(TCCR2A,COM2B1); cbi(TCCR2A,COM2B0); // Combined with the WGM22 bit found in the TCCR2B Register, these bits // control the counting sequence of the counter, the source for maximum // (TOP) counter value, and what type of waveform generation to be used // Modes of operation supported by the Timer/Counter unit are: // - Normal mode (counter), // - Clear Timer on Compare Match (CTC) mode, // - two types of Pulse Width Modulation (PWM) modes. // // Mode WGM22 WGM21 WGM20 Operation TOP // 0 0 0 0 Normal 0xFF // 1 0 0 1 PWM 0xFF // 2 0 1 0 CTC OCRA // 3 0 1 1 Fast PWM 0xFF // 4 1 0 0 Reserved - // 5 1 0 1 PWM OCRA // 6 1 1 0 Reserved - // 7 1 1 1 Fast PWM OCRA cbi(TCCR2B,WGM22); sbi(TCCR2A,WGM21); sbi(TCCR2A,WGM20); //--------------------------------------------------------------------- // TCCR2B settings //--------------------------------------------------------------------- // The FOC2A bit is only active when the WGM bits specify a non-PWM // mode. // However, for ensuring compatibility with future devices, this bit // must be set to zero when TCCR2B is written when operating in PWM // mode. When writing a logical one to the FOC2A bit, an immediate // Compare Match is forced on the Waveform Generation unit. The OC2A // output is changed according to its COM2A1:0 bits setting. Note that // the FOC2A bit is implemented as a strobe. Therefore it is the value // present in the COM2A1:0 bits that determines the effect of the // forced compare. // A FOC2A strobe will not generate any interrupt, nor will it clear // the timer in CTC mode using OCR2A as TOP. // The FOC2A bit is always read as zero. cbi(TCCR2B,FOC2A); cbi(TCCR2B,FOC2B); // The three Clock Select bits select the clock source to be used by // the Timer/Counter. // CS22 CS21 CS20 Prescaler // 0 0 0 No clock source (Timer/Counter stopped). // 0 0 1 No prescaling // 0 1 0 8 // 0 1 1 32 // 1 0 0 64 // 1 0 1 128 // 1 1 0 256 // 1 1 1 1024 cbi(TCCR2B,CS22); cbi(TCCR2B,CS21); sbi(TCCR2B,CS20); #endif }