Isabella EstesAdam TaoRebecca YeeSamuel Yu
Published

Attendance RAY-dar

Ray's Baes are the face of the future, bringing higher class turnout for Dr. Simar.

BeginnerFull instructions provided1 hour67
Attendance RAY-dar

Things used in this project

Story

Read more

Schematics

Circuit Diagram

Code

main.c

C/C++
main file for our project
int main(void) {

    // Helper functions from Lab 7
    System_Clock_Init();
    ADC0_InitSWTriggerSeq3_PE4();

    // Configure Port F
    unsigned int volatile *pRCGCGPIO = (unsigned int *) (0x400FE000 + 0x608);
    unsigned int volatile *pGPIOLOCK_PortF = (unsigned int *)(0x40025000 + 0x520);
    unsigned int volatile *pGPIOCR_PortF = (unsigned int *)(0x40025000 + 0x524);
    unsigned int volatile *pGPIODIR_PortF = (unsigned int *) (0x40025000 + 0x400);
    unsigned int volatile *pGPIOAFSEL_PortF = (unsigned int *) (0x40025000 + 0x420);
    unsigned int volatile *pGPIODEN_PortF = (unsigned int *) (0x40025000 + 0x51C);
    unsigned int volatile *pGPIODATA_PortF = (unsigned int *) (0x40025000 + 0x3FC);

    // Unlock and set up Port F
    *pRCGCGPIO = *pRCGCGPIO | 0x0020;
    while ( (*pRCGCGPIO & 0x0020) == 0 );
    *pGPIOLOCK_PortF = 0x4C4F434B;
    *pGPIOCR_PortF = *pGPIOCR_PortF | 0x1F;

    *pGPIODIR_PortF = *pGPIODIR_PortF | 0x08;
    *pGPIOAFSEL_PortF = *pGPIOAFSEL_PortF & ~0x08;
    *pGPIODEN_PortF = *pGPIODEN_PortF | 0x08;

    unsigned int sample_ADC0;

    // Infinite loop
    while(1) {

        sample_ADC0 = ADC0_Sample_Seq3();

        *pGPIODATA_PortF = *pGPIODATA_PortF | 0x08;

        // Delay
        int i;
        for (i = 0; i < sample_ADC0; i++) {}
        // Toggle the 3rd bit of Port F
        *pGPIODATA_PortF = *pGPIODATA_PortF & ~0x08;
        // Delay some more
        int j;
        for (j = 0; j < sample_ADC0; j++) {}

    }
    return 0;
}

System_Clock_Init.c

C/C++
helper for main to start clocks, made by Dr. Simar
/*
 * System_Clock_init.c
 *
 *  Created on: Mar 28, 2016
 *      Author: Ray
 */

// Handles initialization of the system level clocks.

#include "ADC0_registers.h"

void
System_Clock_Init(void)
{
	/*
	 * In order for the ADC module to be used, the PLL must be enabled and programmed to a supported
	 * crystal frequency in the RCC register (see page 260).  We will use the example found in Valvano
	 * Sec. 4.3
	 *
	 * Our processor on the LaunchPad had a 16 MHz main oscillaor and we enable it to run at 80 MHz.
	 */

	// Step 1: enable the use of the RCC2 register fields.
	*pRCC2 |= 0x80000000;	//	DS p 260

	// Step 2:
	*pRCC2 |= 0x00000800;	// 1) bypass PLL while initializing

	// Step 3: Select the crystal value and oscillator source
	*pRCC2 = (*pRCC2 & ~0x000007C0)	// Clear bits 10-6
			+ 0x00000540;			// 10101, configure for 16 MHz crystal
	*pRCC2 &= ~0x00000070;	// Configure for main oscillator sources

	// Step 4: Activate PLL by clearing PWRDN
	*pRCC2 &= ~0x00002000;

	// Step 5: Set the desired system divider
	*pRCC2 |= 0x40000000;	// use 400 MHz PLL
	*pRCC2 = (*pRCC2 & ~0x1FC00000) + (4 << 22);	// 80 MHz

	while ((*pRIS & 0x00000040) == 0){}

	// Step 6: Enable use of PLL by clearing BYPASS
	*pRCC2 &= ~0x00000300;

	// Done enabling the PLL.
}

ADC0_InitSWTriggerSeq3_PE4.c

C/C++
helper function for main, made by Dr. Simar
/*
 * ADC0_InitSWTriggerSeq3_PE4.c
 *
 *  Created on: Mar 28, 2016
 *      Author: Ray
 */

#include "ADC0_registers.h"


void
ADC0_InitSWTriggerSeq3_PE4( void )
{
	/*
	 * Now step through the steps for setting up the ADC module.
	 */

	// Steps 1 - 5 follow that in Valvano Sec 10.4.1

	// Step 6a: Enable the ADC clock for ADC0 by setting bit 0 of the RCGCADC register
	*pRCGCADC |= 0x0001;	// DS p 352

	// Step 1a: Turn on the clocks for port E.
	//	Note:	port E clocks are controlled by bit 4 (R4) of *pRCGCGPIO.
	//			In _binary_ this would correspond to a mask of 0b01.0000.
	//			In _hex_ this would correspond to a mask of 0x10
	*pRCGCGPIO |= 0x0010;	// DS p 340

	// Step 1b: Check to be sure the clocks have started.
	//			This can take a few clock cycles.
	//			Keep checking bit 4 until it is no longer 0.

	while((*pPRGPIO & 0x10) != 0x10){}	// DS p 406

	// Let's use PE4 as the analog input to ADC0.

	// Step 2:  Set the direction of the pin to be used.  The pins
	//			can be configured as either input or output.
	//			In _binary_ the bit position mask would correspond to a mask of 0b1.0000
	//			In _hex_ this would correspond ot a mask of 0x10;
	//			To make PE4 an input, we must clear this bit.  Thus:
	*pGPIODIR_PortE &= ~0x0010;	// DS p 663

	// Step 3: 	Enable the Alternate Function on pin PE4.
	//			This means we set the bit corresponding to PE4.
	*pGPIOAFSEL_PortE |= 0x0010;	// DS p 672

	// Step 4:	Disable the digital function on pin PE4.
	//			This means we clear the bit corresponding to PE4.
	*pGPIODEN_PortE &= ~0x0010;	// DS p 682

	// Step 5:	Enable the analog function on pin PE4.
	//			This means we set the bit corresponding to PE4.
	*pGPIOAMSEL_PortE |= 0x0010;	// DS p 687

	// Completed configuring PE4 for use as an analog input

	// Step 6:14  configure ADC0.

	while ((*pPRADC & 0x01) != 0x01){}	// Wait for the ADC0 to be ready.

	// Step 7: Specify the number of ADC conversions per second.  This value corresponds to 125 ksps
	*pADCPC_ADC0 = 0x0001;	// DS p 892

	// Step 8: Set the priorities of each of the four sequencers.  We set sample sequencer 3 as the highest
	// prioirity and set the others with lower, unique priorities.
	*pADCSSPRI_ADC0 = 0x0123;	// DS p 842

	// Step 9: Ensure that the sample sequencer 3 is disabled by clearing the corresponding ASEN3
	// bit (bit 3) in the ADCACTSS  register.
	*pADCACTSS_ADC0 &= ~0x0008;	// DS p 822

	// Step 10: Select the event that initiates sampling for sample sequencer 3.  Configure the
	// 'trigger event' to be initiated by the processor setting the SSn bit in the ADCPSSI register.
	*pADCEMUX_ADC0 &= 0x0FFF;	// DS p 834

	// Step 11:  For each sample in the sample sequence, configure the corresponding input source in the
	// ADCSSMUXn register.  For our case we sample channel AIN9, which is PE4.  This is shown in Table 13-1 on
	// page 802.  We clear the SS3 field and set channel to AIN9.
	*pADCSSMUX3_ADC0 = (*pADCSSMUX3_ADC0 & 0XFFFFFFFF0) + 0X9;	// DS p 876

	// Step 12: For each sample in the sample sequence, configure the sample control bits in the
	// corresponding nibble in the ADCSSCTL3  register.  Our setting is:
	// TS0 = 0, IE0 = 1, END0 = 1,  D0 = 0 or, the nibble is 0b0110 = 6
	// This is the register to use to pick temperature sampling.

	*pADCSSCTL3_ADC0 = 0x0006;	// DS p 877

	// Step 13:  If interrupts are to be used, set the corresponding MASK bit in the ADCIM  register.
	// With software start, we do not want ADC interrupts, so we clear bit 3.
	*pADCIM_ADC0 &= ~0x0008;		// DS p 826

	// Step 14:	Enable the sample sequencer logic by setting the corresponding ASENn  bit in the
	// ADCACTSS register.  To enable sequencer 3, we write a 1 to bit 3 (ASEN3).

	*pADCACTSS_ADC0 |= 0x0008;	// DS p 822

}

ADC0_Sample_Seq3.c

C/C++
helper for main, collects data from breadboard, made by Dr. Simar
/*
 * credits to dr simar, we're yoinking this
 *
 * ADC0_Sample_Seq3.c
 *
 *  Created on: Mar 28, 2016
 *      Author: Ray
 */

#include "ADC0_registers.h"

unsigned int
ADC0_Sample_Seq3(void)

{
	unsigned int sample_ADC0;

	// Step 1:  Start collecting a single sample by writing a 1 to bit 3 of the ADCPSSI register.
	*pADCPSSI_ADC0 = 0x08;	// DS p 846

	// Step 2: Wait for the conversion of the single sample we have requested.  Bit 3 will be set when
	// a sample has completed conversion. DS p 824
	while ((*pADCRIS_ADC0 & 0x08) == 0){};

	// Step 3: Now that conversion is complete, read the 12-bit digital sample from the FIFO for
	// Sample Sequencer 3, the ADCSSFIFO3 register!!!
	sample_ADC0 = *pADCSSFIFO3_ADC0 & 0x0FFF; // DS p 861

	// Step 4: Acknowledge the we have complete the read.  This will allow for the system to
	// return to the state where it can process another sample, when requested.  (DS p 829)

	*pADCISC_ADC0 = 0x08;

	return sample_ADC0;
}

Credits

Isabella Estes

Isabella Estes

1 project • 1 follower
Adam Tao

Adam Tao

0 projects • 2 followers
Rebecca Yee

Rebecca Yee

0 projects • 2 followers
Samuel Yu

Samuel Yu

0 projects • 1 follower

Comments