Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************** (C) COPYRIGHT *******************************
- * File Name : main.c
- * Author : WCH
- * Version : V1.0.0
- * Date : 2022/08/08
- * Description : Main program body.
- *********************************************************************************
- * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- * Attention: This software (modified or not) and binary are used for
- * microcontroller manufactured by Nanjing Qinheng Microelectronics.
- *******************************************************************************/
- #include <ch32v00x.h>
- //#include "debug.h"
- /* Global define */
- /* Global Variable */
- //Положение фаз в таблице синуса.
- uint8_t Fase1= 0;
- uint8_t Fase2= 20;
- uint8_t Fase3= 40;
- //Продолжительность аварийного торможения.
- uint16_t cycle_stop= 0;
- //Таблица ШИМ, минимальное значение 75 и максимальное не должно превышать 8000-75=7925,
- //в 75 это 1/24000000*75= 3 микросекунды находится: 1 микросекунда это Dead-time + 2 микросекунды
- //продолжительность HIN для IR2136( Data Sheet No. PD60166 revU, стр 5 под первой таблицей).*/
- uint16_t Sin_pwm_tab[60]={ 4000, 4410, 4816, 5213, 5596, 5963, 6307, 6626, 6917, 7175, 7399, 7586, 7733, 7839, 7903, 7925,
- 7903, 7839, 7733, 7586, 7399, 7175, 6917, 6626, 6307, 5963, 5596, 5213, 4816, 4410, 4000, 3590, 3184, 2787, 2404, 2037, 1693,
- 1374, 1083, 825, 601, 414, 267, 161, 97, 75, 97, 161, 267, 414, 601, 825, 1083, 1374, 1693, 2037, 2404, 2787, 3184, 3590};
- uint8_t cnt = 0;
- void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
- void EXTI7_0_IRQHandler (void) __attribute__((interrupt()));
- void Gpio_Init(void)
- {
- RCC->APB2PCENR |= RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD;
- // T1CH1 (PD2 GPIO_Mode_AF_OUT)
- GPIOD->CFGLR = (GPIOD->CFGLR & ~GPIO_CFGLR_CNF2) | GPIO_CFGLR_CNF2_1;
- GPIOD->CFGLR |= GPIO_CFGLR_MODE2;
- // T1CH1N (PD0 GPIO_Mode_AF_OUT)
- GPIOD->CFGLR = (GPIOD->CFGLR & ~GPIO_CFGLR_CNF0) | GPIO_CFGLR_CNF0_1;
- GPIOD->CFGLR |= GPIO_CFGLR_MODE0;
- // T1CH2 (PA1 GPIO_Mode_AF_OUT)
- GPIOA->CFGLR = (GPIOA->CFGLR & ~GPIO_CFGLR_CNF1) | GPIO_CFGLR_CNF1_1;
- GPIOA->CFGLR |= GPIO_CFGLR_MODE1;
- // T1CH2N (PA2 GPIO_Mode_AF_OUT)
- GPIOA->CFGLR = (GPIOA->CFGLR & ~GPIO_CFGLR_CNF2) | GPIO_CFGLR_CNF2_1;
- GPIOA->CFGLR |= GPIO_CFGLR_MODE2;
- // T1CH3 (PC3 GPIO_Mode_AF_OUT)
- GPIOC->CFGLR = (GPIOC->CFGLR & ~GPIO_CFGLR_CNF3) | GPIO_CFGLR_CNF3_1;
- GPIOC->CFGLR |= GPIO_CFGLR_MODE3;
- // T1CH3N (PD1 GPIO_Mode_AF_OUT)
- GPIOD->CFGLR = (GPIOD->CFGLR & ~GPIO_CFGLR_CNF1) | GPIO_CFGLR_CNF1_1;
- GPIOD->CFGLR |= GPIO_CFGLR_MODE1;
- //PC1 (GPIO_Mode_OUT_PP)
- GPIOC->CFGLR = (GPIOC->CFGLR & ~(GPIO_CFGLR_CNF1_0 | GPIO_CFGLR_CNF1_1));
- GPIOC->CFGLR |= GPIO_CFGLR_MODE1;
- GPIOC->BSHR = GPIO_BSHR_BR1;
- }
- void TIM1_Dead_Time_Init(u16 arr, u16 psc, u16 ccp)
- {
- GPIO_InitTypeDef GPIO_InitStructure = {0};
- TIM_OCInitTypeDef TIM_OCInitStructure = {0};
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
- TIM_BDTRInitTypeDef TIM_BDTRInitStructure = {0};
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1, ENABLE);
- /* TIM1_CH1 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* TIM1_CH1N */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- TIM_TimeBaseInitStructure.TIM_Period = arr;
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = ccp;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
- TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
- TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
- TIM_BDTRInitStructure.TIM_DeadTime = 0xFF;
- TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
- TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
- TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
- TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
- TIM_ARRPreloadConfig(TIM1, ENABLE);
- TIM_Cmd(TIM1, ENABLE);
- }
- void TIM1_Init(void){
- RCC->APB2PCENR |= RCC_APB2Periph_TIM1;
- TIM1->PSC = 2-1; // 48000000/2 = 24MHz -> i.e 1 tick = 41,67 ns
- TIM1->ATRLR = 8000-1; // 48000000/(2*8000) = 3000 Гц
- TIM1->RPTCR = 0;
- TIM1->CTLR1 = TIM_ARPE;
- // конфигурация входов и режим работы PWM2
- TIM1->CHCTLR1 = ((TIM1->CHCTLR1) & ~TIM_CC1S) | TIM_OC1M;
- // определили активный уровень, включили выход
- TIM1->CCER = TIM_CC1P | TIM_CC1E | TIM_CC1NP | TIM_CC1NE;
- // определили уровни в свободном состоянии
- TIM1->CTLR2 = ((TIM1->CTLR2) & ~TIM_OIS1N) | TIM_OIS1;
- TIM1->CH1CVR = 4000;
- TIM1->BDTR |= TIM_MOE;
- TIM1->DMAINTENR |= TIM_UIE;
- TIM1->CTLR1 |= TIM_CEN;
- NVIC_EnableIRQ(TIM1_UP_IRQn);
- //SetVTFIRQ((u32)TIM1_UP_IRQHandler, TIM1_UP_IRQn,1,ENABLE);
- //////////////////////////////////////////////////////////////
- /****** PWM out TIM2_CH3 (PC0) ********************/
- //RCC->APB1PCENR |= RCC_APB2Periph_GPIOC;
- //GPIOC->CFGLR |= GPIO_CFGLR_MODE0;
- //GPIOC->CFGLR = (GPIOC->CFGLR & ~(GPIO_CFGLR_CNF0)) | GPIO_CFGLR_CNF0_1;
- //TIM2->CH3CVR = 500;
- //TIM2->CCER |= TIM_CC3E | TIM_CC3P;
- //PWM mode 2, прямой ШИМ 3 канал
- //TIM2->CHCTLR2 |= TIM_OC3M;
- /////////////////////////////////////////////////////////////
- }
- void EXTI7_0_IRQHandler(void){
- EXTI->INTFR &= ~EXTI_INTF_INTF0;
- cnt += 10;
- //asm ("NOP");
- }
- void TIM1_UP_IRQHandler(){
- TIM1->INTFR &= ~TIM_UIF;
- GPIOC->OUTDR ^= GPIO_OUTDR_ODR1;
- EXTI->SWIEVR |= EXTI_SWIEVR_SWIEVR0;
- cnt++;
- }
- void EXTI_init(){
- RCC->APB2PCENR |= RCC_APB2Periph_AFIO;
- //AFIO->EXTICR |= AFIO_EXTICR1_EXTI0;
- EXTI->EVENR |= EXTI_EVENR_MR0;
- EXTI->INTENR |= EXTI_INTENR_MR0;
- NVIC_EnableIRQ(EXTI7_0_IRQn);
- }
- /*********************************************************************
- * @fn main
- *
- * @brief Main program.
- *
- * @return none
- */
- int main(void)
- {
- Gpio_Init();
- //TIM1_Dead_Time_Init(100, 48 - 1, 50);
- TIM1_Init();
- EXTI_init();
- for(;;){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement