University
Published

# Potentio-buzzer

We measure the resistance you've always wanted to measure in the most efficient and user-friendly way that you could imagine.

BeginnerFull instructions provided5 hours36

## Things used in this project

### Hardware components

×1
 Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
 Rotary potentiometer (generic)
×1
 Tilt Switch, SPST
×1
 Buzzer, Piezo
×1
 Jumper wires (generic)
×6
 Resistor 10k ohm
×1

### Software apps and online services

 Texas Instruments Code Composer Studio

## Code

C/C++
```/*
*
*  Created on: Mar 28, 2016
*      Author: Ray
*  Edited by team I.we6pm
*/

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

// 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

// Step 7: Specify the number of ADC conversions per second.  This value corresponds to 125 ksps

// 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.

// Step 9: Ensure that the sample sequencer 3 is disabled by clearing the corresponding ASEN3
// bit (bit 3) in the ADCACTSS  register.

// 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.

// 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.

// 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.

// 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.

// 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).

// Done with the initialization of the ADC.  Time to do some converting!

// This is a busy-wait or polling based approach to talking to a typically slow peripheral.  In this case,
// the peripheral happens to be the ADC we have set up.  0 to 3.3V maps to the sample ranges of
// 0 to 4095 (2^12 - 1)

// Do a battery test using PE4...
// Then do a version using the temperature probe...

}
```

### main.c

C/C++
```/*
* main.c
*
*  Edited by team I.we6pm
*
* Code for Lecture 18...
*
* This is the configuration we will use:
* 		+ 	Our input pin will be PE4.
*			Not used as a GPIO, but using its Alternate Function
*		+	We will configure it for a sample rate of 125 kHz.
*		+	We will start sampling via software and poll to determine when data is ready.
*			This will be how we synchronize the CPU with ADC0.
*
* 	This example will follow the configuration used by Valvano in section 10.4.1 TM4C ADC details
*
* Date 27 March 2016
*/

extern void System_Clock_Init(void);

int main(void) {

unsigned int volatile *pGPIODATA_PortE = (unsigned int *) (0x40024000 + 0x3FC);

System_Clock_Init();

// Nice function that sets up the ADC for you :)

// Set up the pointers to all of our registers.
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);

// Set up Port F
*pRCGCGPIO = *pRCGCGPIO | 0x0020;
while ( (*pRCGCGPIO & 0x0020) == 0 ) ; // Good thing this is volatile!

*pGPIOLOCK_PortF        = 0x4C4F434B;
*pGPIOCR_PortF = *pGPIOCR_PortF | 0x1F;

// SET UP PIN 3 for the speaker.
*pGPIODIR_PortF = *pGPIODIR_PortF | 0x08;  // 0x08 looks familiar. This would SET a 1 (because of the OR) on the 3rd bit of the register. This sets Pin F3 to be an output.
*pGPIOAFSEL_PortF = *pGPIOAFSEL_PortF & ~0x08; // No alternative functions for this pin
*pGPIODEN_PortF = *pGPIODEN_PortF | 0x08; // Digital enable the pin

while(1)
{
sample_voltage = (3.3/4095.0) * (float) sample_ADC0;  // This would be good to be in the main.c that you downloaded, but I forgot to include it. Sorry :(

// THE ONLY THING YOU NEED TO DO IS BELOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// What is the Resistance of the photoresistor????????????????? PART 1

float sample_resistor = (10000 * sample_voltage) / (3.3 - sample_voltage);
//Do a square wave! (Turn on the pin, wait, turn off the pin, wait (Pin F3 (like we did with the blinking of the LED (you remember? from like 3 labs ago?)))) (PART 2)

*pGPIODATA_PortF = *pGPIODATA_PortF | 0x08;
for (i = 0; i < sample_ADC0; i++){}
*pGPIODATA_PortF = *pGPIODATA_PortF & ~0x08;
for (i = 0; i < sample_ADC0; i++){}
*pGPIODATA_PortF = *pGPIODATA_PortF | 0x08;
}
return 0;
}
```

## Credits

### Lionel Hu

1 project • 0 followers

### Tarushi R. Mittal

1 project • 0 followers

### Xinyue Li

1 project • 0 followers

### Jason Zhu

1 project • 0 followers

### Chen Qingge

1 project • 0 followers