Ejemplo n.º 1
0
void setup_adc(){

    /* Setting reference voltage to 2.5  and enabling reference */
    REF_A_setReferenceVoltage(REF_A_VREF2_5V);
    MAP_REF_A_enableReferenceVoltage();

    MAP_ADC14_enableModule();
    //~47 KHz
    MAP_ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_32, ADC_DIVIDER_2,  0);
    /* Configuring GPIOs for Analog In */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,
            GPIO_PIN5 | GPIO_PIN4 | GPIO_PIN3 | GPIO_PIN2, GPIO_TERTIARY_MODULE_FUNCTION);

    /* Configuring ADC Memory (ADC_MEM0 - ADC_MEM7 (A0 - A1)  with no repeat)
     * with internal 2.5v reference */

    MAP_ADC14_configureMultiSequenceMode(ADC_MEM0, ADC_MEM3, true);
    MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_INTBUF_VREFNEG_VSS, ADC_INPUT_A0, false);
    MAP_ADC14_configureConversionMemory(ADC_MEM1, ADC_VREFPOS_INTBUF_VREFNEG_VSS, ADC_INPUT_A1, false);
    MAP_ADC14_configureConversionMemory(ADC_MEM2, ADC_VREFPOS_INTBUF_VREFNEG_VSS, ADC_INPUT_A2, false);
    MAP_ADC14_configureConversionMemory(ADC_MEM3, ADC_VREFPOS_INTBUF_VREFNEG_VSS, ADC_INPUT_A3, false);

    /* Setting up the sample timer to automatically step through the sequence convert.*/
    MAP_ADC14_enableSampleTimer(ADC_AUTOMATIC_ITERATION);

    /* Triggering the start of the sample */
    MAP_ADC14_enableConversion();
    MAP_ADC14_toggleConversionTrigger();

}
Ejemplo n.º 2
0
void main()
{
	int err = 0, count = 0, bytesWritten = 0;
	char addr[5], i = '0';
	char buf[32], data[64];
	uint8_t status;
	uint32_t ACLKfreq;
	BYTE temp[100];
	UINT bw = 0, btw;
	FRESULT iFResult;
	DWORD sizeBuf = 0;

//Initialization Functions
	err = InitFunction();
	err |= ADC_InitFunction();
	err |= I2C_InitFunc(calData);
	err |= InitOneWire();

//Initialize the SD card
	Use_SD_CARD = YES;
	if(SD_CardInit())
	{
		Use_SD_CARD = NO;
	}

	//Initialize the nRF wireless module
	ACLKfreq = MAP_CS_getACLK();  // get ACLK value to verify it was set correctly

	rf_crc = RF24_CRCO;
	rf_addr_width      = (uint8_t)ADDRESS_WIDTH;
	rf_speed_power     = RF24_SPEED_MIN | RF24_POWER_MAX;
	rf_channel         = 120;
	msprf24_init();  // All RX pipes closed by default
	msprf24_open_pipe(0, 1);  // Open pipe#0 with Enhanced ShockBurst enabled for receiving Auto-ACKs
	msprf24_set_pipe_packetsize(0, (uint8_t)ADDRESS_WIDTH);  // Dynamic payload length enabled (size=0)

	// Transmit to 'rad01' (0x72 0x61 0x64 0x30 0x31)
	msprf24_standby();
	memcpy(addr, "\xDE\xAD\xBE\xEF\x01", 5);
//	addr[0] = 'r'; addr[1] = 'a'; addr[2] = 'd'; addr[3] = '0'; addr[4] = '2';
	w_tx_addr(addr);
	w_rx_addr(0, addr);  // Pipe 0 receives auto-ack's, autoacks are sent back to the TX addr so the PTX node
						 // needs to listen to the TX addr on pipe#0 to receive them.
	buf[0] = '1';
	buf[1] = '\0';
//	w_tx_payload(1, buf);
	w_tx_payload_noack(1, buf);
	msprf24_activate_tx();

	if (rf_irq & RF24_IRQ_FLAGGED)
	{
			msprf24_get_irq_reason();  // this updates rf_irq
			if (rf_irq & RF24_IRQ_TX)
					status = 1;
			if (rf_irq & RF24_IRQ_TXFAILED)
					status = 0;
			msprf24_irq_clear(RF24_IRQ_MASK);  // Clear any/all of them
	}
	memset(buf, 0, sizeof(buf));

	MAP_Interrupt_enableMaster();
//	MAP_Interrupt_setPriority(INT_TA0_0, 0x00);

	//Entering the main loop
	while(1)
	{
		while(Refresh == 0)
		{	//Keep the process locked here until 'Refresh' is set high.
		}
		MAP_ADC14_toggleConversionTrigger();
		Refresh = 0;

		//Get light value
	    GetLightValue(&lux, &lightIndex);

	    //Get temperature and pressure
	    GetBaroTemp(calData, &temperature, &pressure);

	    //Get temperature and humidity
	    __delay_cycles(100);
	    dht_start_read();
	    int t = dht_get_temp();
	    int h = dht_get_rh();
		__delay_cycles(100);

		//For debug purposes
		printf("Temperature: %d\n", temperature);
		printf("Pressure: %d\n", pressure);
		printf("Lux: %d\n", lux);
		printf("Humidity: %d\n", h);

		//Form the string to send to base station
		sprintf(buf, "<T%003dP%000006dH%003dL%00005d>", temperature, pressure, h, lux);

		//transmit data
		GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
		w_tx_payload((uint8_t)ADDRESS_WIDTH, buf);
		msprf24_activate_tx();
		GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);

		//open SD card
		if(Use_SD_CARD == YES)
		{
			iFResult = f_open(&g_sFileObject, "data.txt", FA_OPEN_EXISTING | FA_WRITE);
			count = 0;
			//if the first open fails, unmount and try again for 5 times
			while ((iFResult != FR_OK) && (count < 5))
			{
				iFResult = f_mount(0, 0);
				count++;
				__delay_cycles(10000);
				iFResult = f_mount(0, &g_sFatFs);
				__delay_cycles(10000);
				iFResult |= f_open(&g_sFileObject, "data.txt", FA_WRITE | FA_CREATE_NEW);
			}
//			sizeBuf = strlen(temp) + 1;
			sizeBuf = sizeBuf + bw;

			sprintf(temp, "\n%d, %d, %d, %d ", temperature, pressure, lux, h);
			btw = strlen(temp);
			if(count < 5)
			{
				//Find the end of the file and write new data here
				iFResult = f_lseek(&g_sFileObject, sizeBuf);
				iFResult = f_write(&g_sFileObject, temp, btw, &bw);
				//close the file
				iFResult = f_close(&g_sFileObject);
			}
		}
	}
}
Ejemplo n.º 3
0
int main(void) 
{
    WDT_A_holdTimer();

    //Configure Timer
    unsigned int dcoFrequency = 3E+6;
    MAP_CS_setDCOFrequency(dcoFrequency);
    MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1); // ( 3Mhz / 64 ) / (Period = 46875) = 1 second

    //Set LED's as outputs and turn off
    MAP_GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0);
    MAP_GPIO_setAsOutputPin(GPIO_PORT_P2,GPIO_PIN0);
    MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
    MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);


	////////////////////////////////////////////////////////////////////////////////////////////////////
    //			ADC INITILIZATION P5.5
	////////////////////////////////////////////////////////////////////////////////////////////////////

    /* Initializing ADC */
    MAP_ADC14_enableModule();

    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN5, GPIO_TERTIARY_MODULE_FUNCTION); //writes 0's in PXSEL registers for pins 1.2 and 1.3
    MAP_ADC14_setResolution(ADC_10BIT); // initialize to use a 10 Bit ADC
    MAP_ADC14_initModule(ADC_CLOCKSOURCE_SMCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_1,0);

    /* Configuring ADC Memory */
    MAP_ADC14_configureSingleSampleMode(ADC_MEM0, false); //put results in this 16 bit register location to hold results, false means we are manually triggering
    MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A0, false); //pin 5.5 is tied to channel 0 (A0)
    /* Configuring Sample Timer */
    MAP_ADC14_enableSampleTimer(ADC_MANUAL_ITERATION);

    //Timer A setup
    MAP_Timer_A_configureUpMode(TIMER_A0_MODULE, &upConfig0);
    Interrupt_enableInterrupt(INT_TA0_0);
	Timer_A_startCounter(TIMER_A0_MODULE, TIMER_A_UP_MODE);


    /* Enabling/Toggling Conversion */
    MAP_ADC14_enableConversion();
    MAP_ADC14_toggleConversionTrigger();

	////////////////////////////////////////////////////////////////////////////////////////////////////
    //			END ADC
	////////////////////////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////////////////////////
	//            Timer A 2 and PWM on P5.6
	////////////////////////////////////////////////////////////////////////////////////////////////////

    GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION); //writes 0's in PXSEL registers for pins 1.2 and 1.3


    // Set Timer A period (PWM signal period)
    TA2CCR0 = 10000 ; // i think i wrote this wring, his example he showed in class is is 10000
    // Set Duty cycle
    TA2CCR1 = DUTY_CYCLE * 10000; //inital DutyCycle of 0%.. Duty_Cycle is macro
    // Set output mode to Reset/Set
    TA2CCTL1 = OUTMOD_7 ;    // Macro which is equal to 0x00e0, defined in msp432p401r.h
        // Initialize Timer A
    TA2CTL = TASSEL__SMCLK | MC__UP | TACLR ;  // this bitwise or’s multiple settings at the same time (this just sets different bits in the register to set these functionalities/settings)
    											//Does this start the timer?? (is next line necessary?)

	Timer_A_startCounter(TIMER_A2_MODULE, TIMER_A_UP_MODE);
	////////////////////////////////////////////////////////////////////////////////////////////////////
	//          END Timer A 2 and PWM on 5.6
	////////////////////////////////////////////////////////////////////////////////////////////////////


    Interrupt_enableMaster() ;


    while(1){

    	while(MAP_ADC14_isBusy()==0){ // poll the busy flag. we have a conversion going bc we already triggerted it above, so we are waiting for the conversion to be done, when it is, we are going to call the get result function. this just returns the 16 bit value rsults
    		ADC_result = (uint16_t)MAP_ADC14_getResult(ADC_MEM0); //just goes to memory location takes results and returns the 16 bit value... this just continues to get repopulted.
    		MAP_ADC14_toggleConversionTrigger(); ////once we got the result we start a new conversion and we go back to top and begin wiaitng until its dones again
    	}//end nested while
    }//end inf while
}//end main