/**8 ***************************************************************************
ID: 1813131 • Letter: #
Question
/**8
*****************************************************************************
**
**
*****************************************************************************
*/
/*Includes*/
#include "stm32f10x.h"
/*Defines*/
#define GPIO_CNF_INPUT_ANALOG 0
#define GPIO_CNF_INPUT_FLOATING 1
#define GPIO_CNF_INPUT_PULLUPDOWN 2
#define GPIO_CNF_OUTPUT_PUSHPULL 0
#define GPIO_CNF_OUTPUT_OPENDRAIN 1
#define GPIO_CNF_AFIO_PUSHPULL 2
#define GPIO_CNF_AFIO_OPENDRAIN 3
#define GPIO_MODE_INPUT 0
#define GPIO_MODE_OUTPUT10MHz 1
#define GPIO_MODE_OUTPUT2MHz 2
#define GPIO_MODE_OUTPUT50MHz 3
#define GPIOCONF(mode, cnf) ((cnf << 2) | (mode))
#define GPIOPINCONFL(pin, conf) (conf << (pin * 4))
#define GPIOPINCONFH(pin, conf) (conf << ((pin - 8) * 4))
#define CONFMASKL(pin) ((u32)~(15 << (pin * 4)))
#define CONFMASKH(pin) ((u32)~(15 << ((pin - 8) * 4)))
#define SAMPLE_TIME_1_5 0
#define SAMPLE_TIME_7_5 1
#define SAMPLE_TIME_13_5 2
#define SAMPLE_TIME_28_5 3
#define SAMPLE_TIME_41_5 4
#define SAMPLE_TIME_55_5 5
#define SAMPLE_TIME_71_5 6
#define SAMPLE_TIME_239_5 7
/*Define ADC Ports*/
#define ADC_SAMPLE_TIME0(x) (x << 0)
#define ADC_SAMPLE_TIME1(x) (x << 3)
#define ADC_SAMPLE_TIME2(x) (x << 6)
#define ADC_SAMPLE_TIME3(x) (x << 9)
#define ADC_SAMPLE_TIME4(x) (x << 12)
#define ADC_SAMPLE_TIME5(x) (x << 15)
#define ADC_SAMPLE_TIME6(x) (x << 18)
#define ADC_SAMPLE_TIME7(x) (x << 21)
#define ADC_SAMPLE_TIME8(x) (x << 24)
#define ADC_SAMPLE_TIME9(x) (x << 27)
#define ADC_SAMPLE_TIME10(x) (x << 0)
#define ADC_SAMPLE_TIME11(x) (x << 3)
#define ADC_SAMPLE_TIME12(x) (x << 6)
#define ADC_SAMPLE_TIME13(x) (x << 9)
#define ADC_SAMPLE_TIME14(x) (x << 12)
#define ADC_SAMPLE_TIME15(x) (x << 15)
#define ADC_SAMPLE_TIME16(x) (x << 18)
#define ADC_SAMPLE_TIME17(x) (x << 21)
#define ADC_SEQUENCE_LENGTH(x) (x << 20)
// SQR3
#define ADC_SEQ1(x) (x << 0)
#define ADC_SEQ2(x) (x << 5)
#define ADC_SEQ3(x) (x << 10)
#define ADC_SEQ4(x) (x << 15)
#define ADC_SEQ5(x) (x << 20)
#define ADC_SEQ6(x) (x << 25)
// SQR2
#define ADC_SEQ7(x) (x << 0)
#define ADC_SEQ8(x) (x << 5)
#define ADC_SEQ9(x) (x << 10)
#define ADC_SEQ10(x) (x << 15)
#define ADC_SEQ11(x) (x << 20)
#define ADC_SEQ12(x) (x << 25)
// SQR1
#define ADC_SEQ13(x) (x << 0)
#define ADC_SEQ14(x) (x << 5)
#define ADC_SEQ15(x) (x << 10)
#define ADC_SEQ16(x) (x << 15)
/*User Pushbutton on Pin A0*/
#define SW_USER_GPIO GPIOA
#define SW_USER_PIN 0
/*Analog to Digital Converter Input Pin A1*/
#define AIN0_GPIO GPIOA
#define AIN0_PIN 1
/*USART TX Line Pin A9 */
#define USART_TX_GPIO GPIOA
#define USART_TX_PIN 9
/*USART RX Line Pin A10 */
#define USART_RX_GPIO GPIOA
#define USART_RX_PIN 10
/*Blue LED on Pin C8 */
#define LED_BLUE_GPIO GPIOC
#define LED_BLUE_PIN 8
/*Green LED on Pin C9 */
#define LED_GREEN_GPIO GPIOC
#define LED_GREEN_PIN 9
/*
*
*
*
*/
int main(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN | RCC_APB2ENR_ADC1EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
LED_BLUE_GPIO->CRH = (LED_BLUE_GPIO->CRH & CONFMASKH(LED_BLUE_PIN)) | GPIOPINCONFH(LED_BLUE_PIN, GPIOCONF(GPIO_MODE_OUTPUT2MHz, GPIO_CNF_AFIO_PUSHPULL));
AIN0_GPIO->CRH = (AIN0_GPIO->CRH & CONFMASKH(AIN0_PIN)) | GPIOPINCONFH(AIN0_PIN, GPIOCONF(GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG));
AFIO->MAPR = AFIO_MAPR_TIM3_REMAP; // Full TIM3 remap
TIM3->PSC = 23; // Set prescaler to 24 (PSC + 1)
TIM3->ARR = 4096; // Auto reload value 4096 (PWM resolution 12-bits)
TIM3->CCR3 = 0; // Start value channel 3
TIM3->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1; // PWM mode on channel 3
TIM3->CCER = TIM_CCER_CC3E; // Enable compare on channel 3
TIM3->CR1 = TIM_CR1_CEN; // Enable timer
ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_CONT; // Turn on ADC, enable continuos mode
ADC1->SQR1 = ADC_SEQUENCE_LENGTH(0); // One channel in sequence
ADC1->SQR3 = ADC_SEQ1(1); // ADC channel 1 is first in sequence
ADC1->SMPR2 = ADC_SAMPLE_TIME0(SAMPLE_TIME_239_5); // sample time for first channel
ADC1->CR1 = ADC_CR1_EOCIE; // Enable interrupt form End Of Conversion
NVIC_EnableIRQ(ADC1_IRQn); // Enable interrupt form ACD1 peripheral
ADC1->CR2 |= ADC_CR2_ADON; // Turn on conversion
while (1) {}
}
//=============================================================================
// ADC1 Interrupt handler
//=============================================================================
void ADC1_IRQHandler (void)
{
if(ADC1->SR & ADC_SR_EOC)
{
TIM3->CCR3 = ADC1->DR;
}
}
Provide any comments you have about the code above after compiling and running this code.
Explanation / Answer
1) good use of pre processor directives and macros.
2) Macro definition is correct
3) Final result resides in the accumulator
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.