Ярлыки

_GetPixelIndex (1) _SetPixelIndex (1) 3-phase (1) 800x480 (1) АЦП (1) генератор (1) синхронный усилитель (2) структура (1) учебный курс (1) шаговый двигатель (1) ШИМ (2) accert (1) AD7608 (1) AD8429 (1) ADC (5) amplifer (1) arccos (1) arcsin (1) arctang (2) arctg (3) ARM (2) arm_sqrt_q15 (2) assembler (6) ASSERT (1) atan (2) bit (1) Bitband (1) boot (3) bootlloader (1) BUTTON (1) C (5) C# (1) CAN (2) CC2530 (5) CMSIS (4) command (1) Cordic (1) Core746I (1) CubeMX (4) DBGMCU (2) debug (2) debug.ini (1) delegate (1) Digital Potentiometers (1) DigitalPOT (1) Discovery (1) DMA (9) DMA2D (1) DSP (1) DSP library (1) DWT (1) EFM32 (5) EmWin (9) EXTI (1) FATFS (1) FMC (2) FreeRTOS (2) gl868-dual cmux (1) GPIO (4) GUI (2) GUIBuilder (1) GUIDRV_CompactColor_16 (1) HAL (3) HappyGecko (1) Hard Fault (2) heap (1) I2C (1) ID (1) ILI9320 (1) ILI9325 (1) Initialisation (1) InitLTDC (1) Instrumentithion (1) Interrupt (4) ITR (1) JTAG (1) Keil (5) LCDConf (2) lock-in (1) LTCD (1) LTDC (3) main (1) memory (1) MINI_STM32 Revision 01 (1) nBoot0 (1) NVIC (1) OnePulse (2) OSAL (4) pack (1) phase (1) printf (3) Pulse (1) PWM (12) RCC (2) RCR (1) Register (1) RESET (2) RS232 (3) RSS (1) RTC (3) RTOS-RTX (1) RTT (1) RTX-RTOS (1) SDCard (1) SDRAM (6) Segger (2) SPI (3) sqrt (3) SSD1298 (1) SSD1963 (1) Standart Peripherial Library (3) STANDBAY (1) startup (1) STemWin (8) stepper motor (1) STlink (2) STM32 (17) STM32429ZI (1) STM32Cube (1) STM32DBG.IN (1) STM32F (28) STM32F0 (4) STM32F1 (13) STM32F4 (10) STM32F4 Discovery (1) STM32F407ZG (1) STM32F429 (2) STM32F746 (1) STOP (1) string (1) struct (1) SWD (1) SWD JTAG (1) Synhronization (1) system_stm32f4xx.c (1) SystemInit (1) SysTick (1) task (4) telit (1) TIM (27) typedef (1) UART (1) USART (9) viewer (2) WM_PAINT (1) Z-stack (5) ZigBee (5)

суббота, 23 мая 2015 г.

2-х фазный ШИМ генератор 400 Герц на STM32F103

2-х фазный ШИМ генератор 400 Герц на STM32F103 с фазами, сдвинутыми на 90 градусов. Частота ШИМ генерации 40 кГц. Для генерации используется TIM4.





#include "stm32f10x.h"
#include "table.h"

const int table[126]={
0xbb8, 0xc74, 0xd30, 0xdea, 0xea2, 0xf57, 0x1008,0x10b5,
0x115d,0x11ff,0x129b,0x1330,0x13be,0x1443,0x14c0,0x1533,
0x159d,0x15fd,0x1652,0x169d,0x16dd,0x1712,0x173b,0x1758,
0x176a,0x1770,0x176a,0x1758,0x173b,0x1712,0x16dd,0x169d,
0x1652,0x15fd,0x159d,0x1533,0x14c0,0x1443,0x13be,0x1330,
0x129b,0x11ff,0x115d,0x10b5,0x1008,0xf57, 0xea2, 0xdea,
0xd30, 0xc74, 0xbb8, 0xafc, 0xa40, 0x986, 0x8ce, 0x819,
0x768, 0x6bb, 0x613, 0x571, 0x4d5, 0x440, 0x3b2, 0x32d,
0x2b0, 0x23d, 0x1d3, 0x173, 0x11e, 0xd3,  0x93,  0x5e,
0x35,  0x18,  0x6,   0x0,   0x6,   0x18,  0x35,  0x5e,
0x93,  0xd3,  0x11e, 0x173, 0x1d3, 0x23d, 0x2b0, 0x32d,
0x3b2, 0x440, 0x4d5, 0x571, 0x613, 0x6bb, 0x768, 0x819,
0x8ce, 0x986, 0xa40, 0xafc, 0xbb8,
0xbb8, 0xc74, 0xd30, 0xdea, 0xea2, 0xf57, 0x1008, 0x10b5,
0x115d,0x11ff,0x129b,0x1330,0x13be,0x1443,0x14c0,0x1533,
0x159d,0x15fd,0x1652,0x169d,0x16dd,0x1712,0x173b,0x1758,

0x176a};

void SystemInit1(void);
void TIM4_PWM_Init(void);
int t1,t2;
vu16 CCR1_Val=300;
vu16 CCR2_Val=1000;
vu16 CCR3_Val=2000;
vu16 CCR4_Val=6000;
TIM_OCInitTypeDef TIM_OCInitStructure;
void TIM4_GPIO_Configuration(void);
void TIM4_Mode_Configuration(void);

void TIM4_PWM_Init(void)
{
TIM4_GPIO_Configuration();
TIM4_Mode_Configuration();
// while(1);
}  

void TIM4_GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 //GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void TIM4_Mode_Configuration(void)
{
   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;


//TIM_DeInit(TIM4);
//TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructure.TIM_Period=6000;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
   TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM4, 7199,TIM_PSCReloadMode_Immediate );
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
      TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
   TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle2
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
      TIM_OCInitStructure.TIM_Pulse=CCR2_Val;
   TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
  //channle3
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
      TIM_OCInitStructure.TIM_Pulse=CCR3_Val;
   TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle4
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
      TIM_OCInitStructure.TIM_Pulse=CCR4_Val;
   TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
 
   TIM_ARRPreloadConfig(TIM4,ENABLE);
   TIM_Cmd(TIM4,ENABLE);  
}


void SystemInit1(void)
{
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);

}
}
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}

 
int main(void)
{                              
SystemInit1(); // Config system timer 72MHZ                                        
TIM4_PWM_Init(); // Init TIM4 PWM and out pwm and conig GPIO
SysTick_Config(1800);
__enable_irq ();
t1=0;
t2=0;
while(1)
{
// GPIOA->BSRR=GPIO_BSRR_BR0;
// GPIOA->BSRR=GPIO_BSRR_BS0;
};
}

void SysTick_Handler(void)
{

//nTime = 0;
//  while(nTime <= 100)
// {
t1++;
if(t1>=100){t1=0;}

t2++;
if(t2>=100){t2=0;}
// }
GPIOA->BSRR=GPIO_BSRR_BS0;
      TIM_OCInitStructure.TIM_Pulse=table[t2+25];
   TIM_OC1Init(TIM4, &TIM_OCInitStructure);
   TIM_OCInitStructure.TIM_Pulse=table[t1];
 TIM_OC2Init(TIM4, &TIM_OCInitStructure);

      TIM_OCInitStructure.TIM_Pulse=table[t2+25];
   TIM_OC3Init(TIM4, &TIM_OCInitStructure);
   TIM_OCInitStructure.TIM_Pulse=table[t1];
 TIM_OC4Init(TIM4, &TIM_OCInitStructure);
GPIOA->BSRR=GPIO_BSRR_BR0;
}




2 комментария:

  1. А вы могли бы добавить две кнопки для регулировки фазы влево / вправо?

    ОтветитьУдалить