示例#1
0
void readControlData(void)
{
    char a0 = 0x0;
    char v0 = 0x0;

    a0 = sampleADC();

    v0 |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) != 0x0 ? 0x0 : 0x1;
    v0 |= (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2) != 0x0 ? 0x0 : 0x1) << 0x1;
    v0 |= (a0 << 0x2);

    if(v0 != values[0])
    {
        values[0] = v0;

        writeControlData();
    }
}
示例#2
0
int readTemp() {
  return sampleADC(INCH_TEMP);
}
void main()
{
    volatile int status = 0;
    uint16_t i;
    volatile FILE *fid;
    
    // If running from flash copy RAM only functions to RAM   
#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif      

    // Initialize System Control:
    // PLL, WatchDog, enable Peripheral Clocks
    // This example function is found in the F2806x_SysCtrl.c file.
       InitSysCtrl();



    // For this example, only init the pins for the SCI-A port.
    EALLOW;
    GpioCtrlRegs.GPCMUX2.bit.GPIO84 = 1;
    GpioCtrlRegs.GPCMUX2.bit.GPIO85 = 1;
    GpioCtrlRegs.GPCGMUX2.bit.GPIO84 = 1;
    GpioCtrlRegs.GPCGMUX2.bit.GPIO85 = 1;
    EDIS;


    //  Clear all interrupts and initialize PIE vector table:
    // Disable CPU interrupts
       DINT;

    // Initialize PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    // This function is found in the F2806x_PieCtrl.c file.
       InitPieCtrl();

    // Disable CPU interrupts and clear all CPU interrupt flags:
       IER = 0x0000;
       IFR = 0x0000;

    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    // This will populate the entire table, even if the interrupt
    // is not used in this example.  This is useful for debug purposes.
    // The shell ISR routines are found in F2806x_DefaultIsr.c.
    // This function is found in F2806x_PieVect.c.
       InitPieVectTable();


    // Initialize SCIA
    scia_init();

    //Initialize GPIOs for the LEDs and turn them off
    EALLOW;
    GpioCtrlRegs.GPADIR.bit.GPIO12 = 1;
    GpioCtrlRegs.GPADIR.bit.GPIO13 = 1;
    GpioDataRegs.GPADAT.bit.GPIO12 = 1;
    GpioDataRegs.GPADAT.bit.GPIO13 = 1;
    EDIS;


    // Enable global Interrupts and higher priority real-time debug events:
    EINT;   // Enable Global interrupt INTM
    ERTM;   // Enable Global realtime interrupt DBGM

    
    // Configure the ADC:
    // Initialize the ADC
	EALLOW;

	//write configurations
	AdcaRegs.ADCCTL2.bit.PRESCALE = 6; //set ADCCLK divider to /4
	AdcbRegs.ADCCTL2.bit.PRESCALE = 6; //set ADCCLK divider to /4
    AdcSetMode(ADC_ADCA, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);
    AdcSetMode(ADC_ADCB, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);

	//Set pulse positions to late
	AdcaRegs.ADCCTL1.bit.INTPULSEPOS = 1;
	AdcbRegs.ADCCTL1.bit.INTPULSEPOS = 1;

	//power up the ADCs
	AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1;
	AdcbRegs.ADCCTL1.bit.ADCPWDNZ = 1;

	//delay for 1ms to allow ADC time to power up
	DELAY_US(1000);


   //ADCA
   EALLOW;
   AdcaRegs.ADCSOC0CTL.bit.CHSEL = 0x0E;  //SOC0 will convert pin ADCIN14
   AdcaRegs.ADCSOC0CTL.bit.ACQPS = 25; //sample window is acqps + 1 SYSCLK cycles
   AdcaRegs.ADCSOC1CTL.bit.CHSEL = 0x0E;  //SOC1 will convert pin ADCIN14
   AdcaRegs.ADCSOC1CTL.bit.ACQPS = 25; //sample window is acqps + 1 SYSCLK cycles
   AdcaRegs.ADCINTSEL1N2.bit.INT1SEL = 1; //end of SOC1 will set INT1 flag
   AdcaRegs.ADCINTSEL1N2.bit.INT1E = 1;   //enable INT1 flag
   AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //make sure INT1 flag is cleared



    
    //Redirect STDOUT to SCI
    status = add_device("scia", _SSA, SCI_open, SCI_close, SCI_read, SCI_write, SCI_lseek, SCI_unlink, SCI_rename);
    fid = fopen("scia","w");
    freopen("scia:", "w", stdout);
    setvbuf(stdout, NULL, _IONBF, 0);
    
    //Print a TI Logo to STDOUT
    drawTILogo();

    //Twiddle LEDs
    GpioDataRegs.GPADAT.bit.GPIO12 = 0;
    GpioDataRegs.GPADAT.bit.GPIO13 = 1;


    for(i = 0; i < 50; i++){

        GpioDataRegs.GPATOGGLE.bit.GPIO12 = 1;
        GpioDataRegs.GPATOGGLE.bit.GPIO13 = 1;
        DELAY_US(50000);


    }

    //LEDs off
    GpioDataRegs.GPADAT.bit.GPIO12 = 1;
    GpioDataRegs.GPADAT.bit.GPIO13 = 1;
        
    
    //Clear out one of the text boxes so we can write more info to it
    clearTextBox();
    
    currentSample = sampleADC();
    

    //Main program loop - continually sample temperature
    for(;;) {
        

        //Sample ADCIN14
        currentSample = sampleADC();
        
        //Update the serial terminal output
        updateDisplay();
        
        //If the sample is above midscale light one LED
        if(currentSample > 2048){
    	    GpioDataRegs.GPADAT.all = 0x2000;
        }else{
            //Otherwise light the other
    	    GpioDataRegs.GPADAT.all = 0x1000;
        }

        DELAY_US(1000000);
        
    }
}
void configureAndTest(void)
{
   struct TestData testData;
   char            btName[20];
   
   
// Configure Bluetooth module
   
   SET_LED_STAT;
   
   // USART setup at 9600 baud (default in Bluetooth module)
   UBRR0 = 77;        // = [F_CPU / (16*baud)] - 1  (9600 bps @ 12MHz)
   UCSR0B = B(TXEN0); // Enable TX and keep default 8n1 serial data format
   
   sei();   // To enable UDRE interrupt to send UART data
   btStatMode = true;
   
   _delay_ms(1000); // Bluetooth module startup delay
   sendProgmemStr(cmdName);
   
   _delay_ms(1000); // Bluetooth module delay between commands
   sendProgmemStr(cmdBaud);
   
   CLR_LED_STAT;  // give time for the LDRs on test system to stabilize
   
   _delay_ms(1000); // Bluetooth module delay between commands
   
   cli();
   
   
// System test (with test system)
   
   // ADC setup
   ADCSRA = B(ADEN) | B(ADPS2) | B(ADPS1);   // Enable ADC with 1/64 prescaler - fAD = 12e6/64 = 187.5 kHz @ 12MHz (must be < 200 kHz)
   
   // SPI module setup to communicate with test system
   SPCR = B(SPE) | B(MSTR) | B(SPR0); // Enable SPI master mode, SCK freq = fosc/8 = 1.5 MHz @ 12 MHz (must be < 12 MHz / 4)
   SPSR = B(SPI2X);
   
   testData.inputs0 = ISSET_I2 | ISSET_INT;  // should be low
   testData.inputs1 = ISSET_I1;  // should be high (button not pressed)
   
   CLR_SS;   // set /SS output low
   
   // initiate communication with test system
   SPDR = 0x34;
   loop_until_bit_is_set(SPSR, SPIF);
   testData.init = SPDR;
   
   // Rest values
   
   //  voltage levels
   //_delay_us(10);  // give time to test system set I2
   testData.vBat = sampleADC(BITPOS_ABAT);   // this will also give time to test system set I2
   sampleADC(0x0E); // pre-select bandgap voltage reference to allow it to stabilize
   testData.inputs1 |= ISSET_I2; // should be high
   
   testData.dv1_1 = recvSPI();          // 1.1 V internal reference on test system (powered by DVcc)
   testData.av1_1 = sampleADC(0x0E);    // 1.1 V internal reference; also give time to test system set /INT

   //_delay_us(10);  // give time to test system set /INT
   testData.inputs1 |= ISSET_INT; // should be high
   
   testData.vRef     = recvSPI();
   testData.emgRef_2 = recvSPI();
   testData.ecgRef_2 = recvSPI();
   testData.eegRef_2 = recvSPI();
   
   //  outputs
   testData.ldrSta00  = recvSPI();
   testData.ldrBat00  = recvSPI();
   testData.ldrLedOff = recvSPI();
   testData.pwmOff    = recvSPI();
   
   //  inputs
   testData.edaShort = sampleADC(BITPOS_A3);   // EDA
   testData.accXoff  = recvSPI();
   testData.accYoff  = recvSPI();
   testData.accZoff  = sampleADC(BITPOS_A5);   // ACC
   testData.luxOff   = sampleADC(BITPOS_A6);   // LUX
   
   // Start PWM stimulus: Timer 0 setup for PWM output
   OCR0B = 0xC0;                             // set PWM duty cycle as 3/4
   TCCR0A = B(COM0B1) | B(WGM01) | B(WGM00); // Non-inverting Fast PWM mode on OC0B
   TCCR0B = B(CS00);                         // Start timer with no prescaling
   
   // Status LED on
   SET_LED_STAT;
   _delay_ms(110); // give time to test system reply
   testData.ldrSta10 = recvSPI();
   testData.ldrBat10 = recvSPI();
   CLR_LED_STAT;
   
   // Battery LED on
   SET_LED_BAT;
   _delay_ms(110); // give time to test system reply
   testData.ldrSta01 = recvSPI();
   testData.ldrBat01 = recvSPI();
   CLR_LED_BAT;
   
   // O1 LED on
   SET_O1;
   _delay_ms(110); // give time to test system reply
   testData.ldrLedOn = recvSPI();
   CLR_O1;
   
   // Button pressed
   _delay_ms(110); // give time to test system press button
   testData.inputs0 |= ISSET_I1;  // should be low (button pressed)
   
   // ACC ST on
   testData.accXon = recvSPI();
   testData.accYon = recvSPI();
   testData.accZon = sampleADC(BITPOS_A5);   // ACC

   // LUX on
   //_delay_ms(100); // wait 100 ms for the LDRs to stabilize
   //testData.luxOn = sampleADC(BITPOS_A6);   // LUX
   
   // PWM on (~400 ms have elapsed since start of PWM stimulus)
   //_delay_ms(10); // give time to test system reply
   testData.pwmOn = recvSPI();
   OCR0B = 0;
   
   // EDA on
   //testData.edaOn = sampleADC(BITPOS_A3);   // EDA
   
   ADCSRA = B(ADIF);    // Disable ADC and clear pending interrupt flag
   SPCR = 0;   // Disable SPI
   
   SET_SS;   // set /SS output high
   
   
// Send test data
   
   UBRR0 = 12;                                // = [F_CPU / (8*baud)] - 1  with U2X0 = 1  (115.2kbps @ 12MHz)
   UCSR0A = B(U2X0);
   UCSR0B = B(RXEN0) | B(TXEN0); // Enable RX and TX and keep default 8n1 serial data format
   
   loop_until_bit_is_set(UCSR0A, RXC0);   // wait for incoming byte (and ignore it)
   
   for(byte i = 0; i < sizeof testData; i++)
   {
      loop_until_bit_is_set(UCSR0A, UDRE0);
      UDR0 = ((byte *)&testData)[i];
   }
   
// Receive Set Bluetooth name command
   
   do
   {
      loop_until_bit_is_set(UCSR0A, RXC0);   // wait for incoming byte
   } while (UDR0 != 0x63); // loop until Set Bluetooth name command is received
   
   for(byte i = 0; i < sizeof btName; i++)
   {
      loop_until_bit_is_set(UCSR0A, RXC0);   // wait for incoming byte
      btName[i] = UDR0;
      if (btName[i] == 0)  break;
   }
   
   // send command reply
   loop_until_bit_is_set(UCSR0A, UDRE0);
   UDR0 = 0x36;
   
// Change Bluetooth name
   
   // wait for Bluetooth connection drop
   while (ISSET_CTS) ;
   
   _delay_ms(100); // Add a delay between connection drop and command
   
   // send "AT+NAME"
   for(byte i = 0; i < 7; i++)
   {
      loop_until_bit_is_set(UCSR0A, UDRE0);
      UDR0 = pgm_read_byte(cmdName+i);
   }
   
   for(byte i = 0; i < sizeof btName; i++)
   {
      if (btName[i] == 0)  break;
      
      loop_until_bit_is_set(UCSR0A, UDRE0);
      UDR0 = btName[i];
   }
   
   _delay_ms(1000); // Bluetooth module delay between commands
   
   //UCSR0A = B(TXC0) | B(U2X0);   // clear TXC0
   //loop_until_bit_is_set(UCSR0A, TXC0);   // wait for end of transmission
   
   UCSR0B = 0; // flush RX buffer
}
void testAndConfigure()
{
   // 1: Test LEDs and send results to test system through SPI
   
   // SPI IOs setup
   SET_DO1;   // set SS output high
   DDRB = BIT_DO1 | BIT_DO2 | BIT_DO4; // Set output for SS, MOSI and SCK
   
   // Internal ADC setup
   DIDR0 = B(ADC5D) | B(ADC4D);             // Digital Input Disable ADC5/SCL and ADC4/SDA
   ADCSRA = B(ADEN) | B(ADPS2) | B(ADPS1);   // Enable ADC with 1/64 prescaler - fAD = 8e6/64 = 125 kHz @ 8MHz
   
   // SPI module setup to transmit data to test system
   SPCR = B(SPE) | B(MSTR) | B(SPR1); // Enable SPI master mode, SCK freq = fosc/64 = 125 kHz @ 8 MHz
   
   CLR_DO1;   // set SS output low
   
   uint8_t valLdrBat = sampleADC(4);
   uint8_t valLdrSta = sampleADC(5);
   
   // send LDR values with both LEDs off
   SPDR = valLdrBat;
   loop_until_bit_is_set(SPSR, SPIF);
   SPDR = valLdrSta;
   loop_until_bit_is_set(SPSR, SPIF);
   
   SET_LED_BAT;
   
   _delay_ms(100); // wait 100 ms for the LDR to stabilize
   
   valLdrBat = sampleADC(4);
   valLdrSta = sampleADC(5);
   
   // send LDR values with battery LED on
   SPDR = valLdrBat;
   loop_until_bit_is_set(SPSR, SPIF);
   SPDR = valLdrSta;
   loop_until_bit_is_set(SPSR, SPIF);
   
   CLR_LED_BAT;
   SET_LED_STAT;
   
   _delay_ms(100); // wait 100 ms for the LDRs to stabilize
   
   valLdrBat = sampleADC(4);
   valLdrSta = sampleADC(5);
   
   ADCSRA = B(ADIF);    // Disable ADC and clear pending interrupt flag
   
   if (!ISSET_GP)   // GP should be high by now
      valLdrBat = 0xFF;   // identify GP error
   
   // send LDR values with status LED on
   SPDR = valLdrBat;
   loop_until_bit_is_set(SPSR, SPIF);
   SPDR = valLdrSta;
   loop_until_bit_is_set(SPSR, SPIF);

   SPCR = 0;   // Disable SPI
   
   
   // 2: Configure Bluetooth module
   
   // USART setup at 9600 baud (default in Bluetooth module)
   UBRR0 = 51;        // = [F_CPU / (16*baud)] - 1  (9600 bps @ 8MHz)
   UCSR0B = B(TXEN0); // Enable TX and keep default 8n1 serial data format
   
   _delay_ms(1000); // Bluetooth module startup delay
   sendProgmemStr(cmdName);
   
   _delay_ms(1000); // Bluetooth module delay between commands
   sendProgmemStr(cmdBaud);

   _delay_ms(1000); // Bluetooth module delay between commands

   CLR_LED_STAT;
}