stm32实现波形输出,步进1hz

  • Post author:
  • Post category:其他




红叶何时落水

校赛第一部分

详见:


http://t.csdn.cn/8op0B

关键难点在于步进1hz,若使用定时器的PWM输出方波,步进1hz完全没有问题。但正弦与三角两种波形有一定的问题。

方案一,将方波利用比较器转换成正弦和方波

这一方案可以实现步进1hz,但是,峰峰值不能保证是5V.因为随频率的变化,峰峰值也会发生变化。

解决方案:我们使单片机输出的方波的峰峰值并不是定值,而是一个随着频率而变化的值。以此保证经过比较,放到后,峰峰值为定制5V。

方案二,利用DA输出波形

DA可以通过打点,来模拟波形。这个方案的难点在于如果我们只是单纯的该变定时器的值,那么很难实现步进1hz。

解决方案:利用算法,将打点数与定时器作为两个自变量,频率作为因变量。以此来增大步进的精度。

主函数

int main(void)
{
		u8 t=0;
		int i = 0;
    Init();
		
	while(1)
	{
		t=KEY_Scan(0);		//µÃµ½¼üÖµ
		switch(t)
		{				 
			case KEY0_PRES:
				depending = (depending + 1) % 7;
				LCD_ShowxNum(80,00,depending,1,16,0);
				break;
			case KEY1_PRES:
				if(depending == 0) {
					pwm_hz += 100;
					tim1 = 72000000 / pwm_hz;
					Init();
				}
				if(depending == 1) {
					pwm_hz += 10;
					tim1 = 72000000 / pwm_hz;
					Init();
				}
				if(depending == 2) {
					pwm_hz += 1;
					tim1 = 72000000 / pwm_hz;
					Init();
				}
				if(depending == 3) {
					k += 100;
					tim2 = 1000000 / k;
					TIM2_Config(tim2, 0);
				}
				if(depending == 4) {
					k += 10;
					tim2 = 1000000 / k;
					TIM2_Config(tim2, 0);
				}
				if(depending == 5) {
					k += 1;
					tim2 = 1000000 / k;
					TIM2_Config(tim2, 0);
				}
				if(depending == 6) {
					Dac3_Init();
					MYDMA1_Config( (u32) & (DAC->DHR12R1), (u32)WAVE72, 72);
					TIM1_PWM_Init(tim1,0); 	
					TIM_SetCompare1(TIM1,(tim1 + 1) / 2);
					TIM2_Config(tim2, 0);
					LCD_ShowString(0,40,200,16,16,"      Triangle ");
				}
				LCD_ShowxNum(120,20,pwm_hz,5,16,0);
				LCD_ShowxNum(120,40,k,5,16,0);
				break;
			case WKUP_PRES:		
				if(depending == 0) {
					pwm_hz -= 100;
					tim1 = 72000000 / pwm_hz;
					Init();
				}
				if(depending == 1) {
					pwm_hz -= 10;
					tim1 = 72000000 / pwm_hz;
					Init();
				}
				if(depending == 2) {
					pwm_hz -= 1;
					tim1 = 72000000 / pwm_hz;
					Init();
				}
				if(depending == 3) {
					k -= 100;
					tim2 = 1000000 / k;
					TIM2_Config(tim2, 0);
				}
				if(depending == 4) {
					k -= 10;
					tim2 = 1000000 / k;
					TIM2_Config(tim2, 0);
				}
				if(depending == 5) {
					k -= 1;
					tim2 = 1000000 / k;
					TIM2_Config(tim2, 0);
				}
				if(depending == 6) {
					Dac3_Init();
					MYDMA1_Config( (u32) & (DAC->DHR12R1), (u32)SIN72, 72);
					TIM1_PWM_Init(tim1,0); 	
					TIM_SetCompare1(TIM1,(tim1 + 1) / 2);
					TIM2_Config(tim2, 0);
					LCD_ShowString(0,40,200,16,16,"      SIN     ");
				}
				LCD_ShowxNum(120,20,pwm_hz,5,16,0);
				LCD_ShowxNum(120,40,k,5,16,0);
				break;
			default:
				delay_ms(10);
		} 
		
	}
}

void Init(void)
{
		uart_init(9600);
		delay_init();	
		KEY_Init();
		LCD_Init();
		POINT_COLOR=BLUE;//ÉèÖÃ×ÖÌåΪÀ¶É«    
		Dac3_Init();
    MYDMA1_Config( (u32) & (DAC->DHR12R1), (u32)SIN72, 72);
		
		TIM1_PWM_Init(tim1,0); 	
		TIM_SetCompare1(TIM1,(tim1 + 1) / 2);
		TIM2_Config(tim2, 0);
		LCD_ShowString(0,0,200,16,16,"PENDING: ");
		LCD_ShowString(0,20,200,16,16,"WAVE: Square ");
		LCD_ShowString(200,20,200,16,16,"Hz");
		LCD_ShowString(200,40,200,16,16,"Hz");
		LCD_ShowxNum(80,00,depending,1,16,0);
		LCD_ShowString(0,40,200,16,16,"      SIN     ");
}



版权声明:本文为m0_59140170原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。