0% found this document useful (0 votes)
42 views3 pages

Experiment#11: Question#1

The document describes an experiment using a microcontroller to read values from two potentiometers and a temperature sensor using an ADC and display the values through a USART serial interface. It includes code to initialize the ADC, timers and USART, read ADC values in a loop, convert them to voltages/temperature and send the output as strings to the USART. Delay functions are used to pace readings at 500ms intervals.

Uploaded by

jameeshudson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Experiment#11: Question#1

The document describes an experiment using a microcontroller to read values from two potentiometers and a temperature sensor using an ADC and display the values through a USART serial interface. It includes code to initialize the ADC, timers and USART, read ADC values in a loop, convert them to voltages/temperature and send the output as strings to the USART. Delay functions are used to pace readings at 500ms intervals.

Uploaded by

jameeshudson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment#11

Question#1
Code:
void Delay (int x) {
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
TIM2_ARR = x - 1;
TIM2_CR1 = 1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_SR = 0;
}

void main () {
RCC_APB2ENR |= 0x00000204; //Enable Port A and ADC 1
RCC_APB1ENR |= 0x00020001; //Enable Timer 2 and USART 2
GPIOA_CRL &= 0xFFFF8900; //Port A pin 0 & 1 as Analog I/P
GPIOA_CRL |= 0x00008900;
ADC1_CR2 = 1; //Enable ADC
ADC1_SMPR2 = 8; //ADC sample time 7.5 cycles
Delay (10);
GPIOA_BSRR = 0x8;
USART2_CR1 = 0x200C; //Enable USART and Tx/Rx
USART2_BRR = 0x45; //Set Baud Rate 115200
int x = 0, y = 0;
float f;
char z[5];
while (1) {
ADC1_SQR3 = 0; //1st conversion on PA0
ADC1_CR2 = 1;
while ((ADC1_SR & 0x2) == 0); //wait till TC flag is set
x = (ADC1_DR & 0xFFF);
f = x * 0.0008;
gcvt (f, 5, z); //C++ function to convert float to Array
usart1_string (“Potentiometer 1:”);
usart1_string (z);
usart1_sendByte ('\n'); //go to new line
usart1_sendByte ('\r'); //carrier return
Delay (500); //wait 500ms
ADC1_SQR3 = 1; //2nd conversion on PA1
ADC1_CR2 = 1;
while ((ADC1_SR & 0x2) == 0); //wait till TC flag is set
y = (ADC1_DR & 0xFFF);
f = 0.0008 * y;
gcvt (f, 5, z); //C++ function to convert float to Array
usart1_string (“Potentiometer 2:”);
usart1_string (z);
usart1_sendByte ('\n'); //go to new line
usart1_sendByte ('\r'); //carrier return
Delay (500); //wait 500ms
}
}

void usart1_sendByte (unsigned char c) {


USART2_DR = c;
while ((USART2_SR & 0x40) == 0); //wait until the TC flag is set
}

void usart1_string (unsigned char *word) {


while (*word) {
USART2_DR = word;
*word ++;
while ((USART2_SR & 0x40) == 0); //wait until TC flag is set
}
}
//END

Question#2
Code:
void Delay (int x) {
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
TIM2_ARR = x - 1;
TIM2_CR1 = 1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_SR = 0;
}

void main () {
RCC_APB2ENR |= 0x00000204; //Enable Port A and ADC 1
RCC_APB1ENR |= 0x00020001; //Enable Timer 2 and USART 2
GPIOA_CRL &= 0xFFFF8900; //Port A pin 0&1 as Analog I/P
GPIOA_CRL |= 0x00008900;
ADC1_CR2 = 1; //Enable ADC
ADC1_SMPR2 = 8; //ADC sample time 7.5 cycles
Delay (10);
GPIOA_BSRR = 0x8;
USART2_CR1 = 0x200C; //Enable USART and Tx/Rx
USART2_BRR = 0x45; //Set Baud Rate 115200
int x = 0;
float f;
char z[4];
while (1) {
ADC1_SQR3 = 0; //1st conversion on PA0
ADC1_CR2 = 1;
while ((ADC1_SR & 0x2) == 0); //wait till TC flag is set
x = (ADC1_DR & 0xFFF);
f = x * 0.0427;
gcvt (f, 5, z); //C++ function to convert int to Array
usart1_string (“Temperature:”); /
usart1_string (z);
usart1_sendByte ('\n'); //go to new line
usart1_sendByte ('\r'); //carrier return
Delay (500); //wait 500 ms
}
}

void usart1_sendByte (unsigned char c) {


USART2_DR = c;
while ((USART2_SR & 0x40) == 0); //wait until the TC flag is set
}

void usart1_string (unsigned char *word) {


while (*word) {
USART2_DR = *word++;
while ((USART2_SR & 0x40) == 0); //wait until TC flag is set
}
}
//END

Conclusion:

You might also like