The PD3 multiplexing function is TIM2_CC2, which can be used to test the PWM function. This routine adjusts the PWM pulse width by the potentiometer to adjust the brightness of LED1 connected to PD3.
#include
Void CLK_init(void)
{
CLK_CKDIVR = 0x08; // 16M internal RC is divided by 2 and the system clock is 8M
}
Void GPIO_init(void)
{
PD_DDR = 0x08; // Configure the PD3 direction register PD3 output
PD_CR1 = 0x08; // Set PD3 to push-pull output
}
Void TIM2_init(void)
{
TIM2_CCMR2 = 0x70; // PWM mode 2
TIM2_CCER1 = 0x30; // CC2 is configured as output
TIM2_ARRH = 0x03; / / configure the PWM resolution to 10 bits, ARR = 0x3FF
TIM2_ARRL = 0xFF; / / PWM frequency = 8M / 0x03FF = 7820Hz
TIM2_CR1 = 0x01; // The counter is enabled and starts counting
}
This article refers to the address: http://
Void ADC_init(void)
{
ADC_CSR = 0x03; // ADC input pin AIN3
ADC_CR1 = 0x01; // ADC is on
}
Void Run(void)
{
Unsigned int value;
ADC_CSR &= ~0x80;//Clear EOC conversion end flag
ADC_CR1 |= 0x01; //Start a single conversion
While(!(ADC_CSR&0x80)); //waiting for a single conversion to complete
Value = ((int)ADC_DRH "2); // Read the upper 8 bits first, the default setting data is left aligned
Value |= ADC_DRL; // read the lower 8 bits again
TIM2_CCR2H = (unsigned char)(value》"8); // Update CC2 compare register
TIM2_CCR2L = (unsigned char)(value);
}
Void init_devices(void)
{
Asm ("sim"); / / prohibit interrupt
CLK_init();
GPIO_init();
TIM2_init();
ADC_init();
Asm("rim"); //Enable interrupt
}
Void main( void )
{
Init_devices();
While(1) Run();
}
Sewing Machine Servo Motor,Dorna Servo Motor,Industrial Motor,Brushless Sewing Machine Motor
LISHUI SHUANGZHENG MOTOR CO.,LTD. , https://www.szservomotor.com