您尚未登录。

楼主 #1 2020-06-05 20:10:41

bunny
会员
注册时间: 2020-05-23
已发帖子: 154
积分: 154

BLDC开发板开车——02.小马达动起来!!

#include <NUC029xAN.h>
#include <stdio.h>
#include <stdint.h>

void sys_init(void) {
	SYS_UnlockReg();
	CLK_EnableModuleClock(CLK_PWRCON_XTL12M_EN_Msk);
	CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
	CLK_EnablePLL(CLK_PLLCON_PLL_SRC_HXT, FREQ_100MHZ);
	CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk);
	CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(2));
	SystemCoreClockUpdate();
}

void led_init(void) {
	SYS->P3_MFP &= ~(SYS_MFP_P32_Msk | SYS_MFP_P33_Msk | SYS_MFP_P34_Msk | SYS_MFP_P35_Msk);
	SYS->P3_MFP |= SYS_MFP_P32_GPIO | SYS_MFP_P33_GPIO | SYS_MFP_P34_GPIO | SYS_MFP_P35_GPIO;
	GPIO_SetMode(P3, BIT2 | BIT3 | BIT4 | BIT5, GPIO_PMD_OUTPUT);
}

void acmp_init(void) {
	SYS->P1_MFP &= ~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk | SYS_MFP_P16_Msk | SYS_MFP_P17_Msk);
	SYS->P1_MFP |= SYS_MFP_P14_ACMP0_N | SYS_MFP_P15_ACMP0_P | SYS_MFP_P16_ACMP2_N | SYS_MFP_P17_ACMP2_P;
	SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
	SYS->P3_MFP |= SYS_MFP_P30_ACMP1_N | SYS_MFP_P31_ACMP1_P;
	GPIO_DISABLE_DIGITAL_PATH(P1, BIT4 | BIT5 | BIT6 | BIT7);
	GPIO_DISABLE_DIGITAL_PATH(P3, BIT0 | BIT1);
	CLK_EnableModuleClock(ACMP01_MODULE);
	CLK_EnableModuleClock(ACMP23_MODULE);
	ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_PIN, ACMP_CR_HYSTERESIS_ENABLE);
	ACMP_Open(ACMP01, 1, ACMP_CR_VNEG_PIN, ACMP_CR_HYSTERESIS_ENABLE);
	ACMP_Open(ACMP23, 2, ACMP_CR_VNEG_PIN, ACMP_CR_HYSTERESIS_ENABLE);
}

void pwm_init(void) {
	SYS->P2_MFP &= ~(SYS_MFP_P20_Msk | SYS_MFP_P21_Msk | SYS_MFP_P22_Msk | SYS_MFP_P23_Msk | SYS_MFP_P24_Msk | SYS_MFP_P25_Msk);
	SYS->P2_MFP |= SYS_MFP_P20_PWM0 | SYS_MFP_P21_PWM1 | SYS_MFP_P22_PWM2 | SYS_MFP_P23_PWM3 | SYS_MFP_P24_PWM4 | SYS_MFP_P25_PWM5;
	GPIO_SetMode(P2, BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5, GPIO_PMD_OUTPUT);
	P20 = 0; P21 = 0; P22 = 0; P23 = 0; P24 = 0; P25 = 0;
	CLK_EnableModuleClock(PWM01_MODULE);
	CLK_EnableModuleClock(PWM23_MODULE);
	CLK_EnableModuleClock(PWM45_MODULE);
	CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HCLK, 0);
	CLK_SetModuleClock(PWM23_MODULE, CLK_CLKSEL1_PWM23_S_HCLK, 0);
	CLK_SetModuleClock(PWM45_MODULE, CLK_CLKSEL2_PWM45_S_HCLK, 0);
	PWMA->PPR = (10 << PWM_PPR_DZI23_Pos) | (10 << PWM_PPR_DZI01_Pos) | (1 << PWM_PPR_CP23_Pos) | (1 << PWM_PPR_CP01_Pos);
	PWMB->PPR = (10 << PWM_PPR_DZI01_Pos) | (1 << PWM_PPR_CP01_Pos);
	PWMA->CSR = (4 << PWM_CSR_CSR2_Pos) | (4 << PWM_CSR_CSR0_Pos);
	PWMB->CSR = (4 << PWM_CSR_CSR0_Pos);
	PWMA->PCR = PWM_PCR_PWM23TYPE_Msk | PWM_PCR_PWM01TYPE_Msk | PWM_PCR_CH2MOD_Msk | PWM_PCR_CH0MOD_Msk | PWM_PCR_DZEN23_Msk | PWM_PCR_DZEN01_Msk;
	PWMB->PCR = PWM_PCR_PWM01TYPE_Msk | PWM_PCR_CH0MOD_Msk | PWM_PCR_DZEN01_Msk;
	PWMA->CMR0 = 250;
	PWMA->CMR2 = 250;
	PWMB->CMR0 = 250;
	PWMA->CNR0 = 500;
	PWMA->CNR2 = 500;
	PWMB->CNR0 = 500;
	PWMA->POE = PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk | PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMB->POE = PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMA->PSCR = PWM_PSCR_PSSEN2_Msk | PWM_PSCR_PSSEN0_Msk;
	PWMB->PSCR = PWM_PSCR_PSSEN0_Msk;
	PWMA->PCR |= PWM_PCR_CH0EN_Msk;
}

void ah(int32_t duty) {
	PWMB->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMB->CMR0 = 250 - duty;
}

void al(int32_t duty) {
	PWMB->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMB->CMR0 = 250 + duty;
}

void az(void) {
	PWMB->POE &= ~(PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk);
}

void bh(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk;
	PWMA->CMR2 = 250 - duty;
}

void bl(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk;
	PWMA->CMR2 = 250 + duty;
}

void bz(void) {
	PWMA->POE &= ~(PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk);
}

void ch(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMA->CMR0 = 250 - duty;
}

void cl(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMA->CMR0 = 250 + duty;
}

void cz(void) {
	PWMA->POE &= ~(PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk);
}

int32_t wait_ap(volatile int32_t t) {
	int32_t i;
	P32 = 0;
	for ( ; t; t--) {
		if (0 == ACMP_GET_OUTPUT(ACMP01, 1)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (0 == ACMP_GET_OUTPUT(ACMP01, 1)) {
				break;
			}
		}
		if (0 == i) {
			P32 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_an(volatile int32_t t) {
	int32_t i;
	P32 = 0;
	for ( ; t; t--) {
		if (1 == ACMP_GET_OUTPUT(ACMP01, 1)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (1 == ACMP_GET_OUTPUT(ACMP01, 1)) {
				break;
			}
		}
		if (0 == i) {
			P32 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_bp(volatile int32_t t) {
	int32_t i;
	P33 = 0;
	for ( ; t; t--) {
		if (0 == ACMP_GET_OUTPUT(ACMP23, 2)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (0 == ACMP_GET_OUTPUT(ACMP23, 2)) {
				break;
			}
		}
		if (0 == i) {
			P33 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_bn(volatile int32_t t) {
	int32_t i;
	P33 = 0;
	for ( ; t; t--) {
		if (1 == ACMP_GET_OUTPUT(ACMP23, 2)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (1 == ACMP_GET_OUTPUT(ACMP23, 2)) {
				break;
			}
		}
		if (0 == i) {
			P33 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_cp(volatile int32_t t) {
	int32_t i;
	P34 = 0;
	for ( ; t; t--) {
		if (0 == ACMP_GET_OUTPUT(ACMP01, 0)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (0 == ACMP_GET_OUTPUT(ACMP01, 0)) {
				break;
			}
		}
		if (0 == i) {
			P34 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_cn(volatile int32_t t) {
	int32_t i;
	P34 = 0;
	for ( ; t; t--) {
		if (1 == ACMP_GET_OUTPUT(ACMP01, 0)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (1 == ACMP_GET_OUTPUT(ACMP01, 0)) {
				break;
			}
		}
		if (0 == i) {
			P34 = 1;
			break;
		}
	}
	return t;
}

void bldc_run(void) {
	int32_t t = 50000, duty = 10;
	for ( ;  ;  ) {
		cz();
		bl(duty);
		wait_cp(t); // 1 0 z
		t -= (t >> 12);
		az();
		ch(duty);
		wait_an(t); // z 0 1
		t -= (t >> 12);
		bz();
		al(duty);
		wait_bp(t); // 0 z 1
		t -= (t >> 12);
		cz();
		bh(duty);
		wait_cn(t); // 0 1 z
		t -= (t >> 12);
		az();
		cl(duty);
		wait_ap(t); // z 1 0
		t -= (t >> 12);
		bz();
		ah(duty);
		wait_bn(t); // 1 z 0
		t -= (t >> 12);
		if (duty < 60) {
			duty++;
		}
	}
}

int main(void) {
	sys_init();
	led_init();
	acmp_init();
	pwm_init();
	bldc_run();
	return 0;
}

最近编辑记录 bunny (2020-06-05 20:30:16)

离线

楼主 #2 2020-06-05 20:12:13

bunny
会员
注册时间: 2020-05-23
已发帖子: 154
积分: 154

Re: BLDC开发板开车——02.小马达动起来!!

离线

楼主 #3 2020-06-05 20:12:41

bunny
会员
注册时间: 2020-05-23
已发帖子: 154
积分: 154

Re: BLDC开发板开车——02.小马达动起来!!

离线

楼主 #4 2020-06-05 20:45:03

bunny
会员
注册时间: 2020-05-23
已发帖子: 154
积分: 154

Re: BLDC开发板开车——02.小马达动起来!!

精简一下代码

#include <NUC029xAN.h>
#include <stdio.h>
#include <stdint.h>

void sys_init(void) {
	SYS_UnlockReg();
	CLK_EnableModuleClock(CLK_PWRCON_XTL12M_EN_Msk);
	CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
	CLK_EnablePLL(CLK_PLLCON_PLL_SRC_HXT, FREQ_100MHZ);
	CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk);
	CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(2));
	SystemCoreClockUpdate();
}

void led_init(void) {
	SYS->P3_MFP &= ~(SYS_MFP_P32_Msk | SYS_MFP_P33_Msk | SYS_MFP_P34_Msk | SYS_MFP_P35_Msk);
	SYS->P3_MFP |= SYS_MFP_P32_GPIO | SYS_MFP_P33_GPIO | SYS_MFP_P34_GPIO | SYS_MFP_P35_GPIO;
	GPIO_SetMode(P3, BIT2 | BIT3 | BIT4 | BIT5, GPIO_PMD_OUTPUT);
}

void acmp_init(void) {
	SYS->P1_MFP &= ~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk | SYS_MFP_P16_Msk | SYS_MFP_P17_Msk);
	SYS->P1_MFP |= SYS_MFP_P14_ACMP0_N | SYS_MFP_P15_ACMP0_P | SYS_MFP_P16_ACMP2_N | SYS_MFP_P17_ACMP2_P;
	SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
	SYS->P3_MFP |= SYS_MFP_P30_ACMP1_N | SYS_MFP_P31_ACMP1_P;
	GPIO_DISABLE_DIGITAL_PATH(P1, BIT4 | BIT5 | BIT6 | BIT7);
	GPIO_DISABLE_DIGITAL_PATH(P3, BIT0 | BIT1);
	CLK_EnableModuleClock(ACMP01_MODULE);
	CLK_EnableModuleClock(ACMP23_MODULE);
	ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_PIN, ACMP_CR_HYSTERESIS_ENABLE);
	ACMP_Open(ACMP01, 1, ACMP_CR_VNEG_PIN, ACMP_CR_HYSTERESIS_ENABLE);
	ACMP_Open(ACMP23, 2, ACMP_CR_VNEG_PIN, ACMP_CR_HYSTERESIS_ENABLE);
}

void pwm_init(void) {
	SYS->P2_MFP &= ~(SYS_MFP_P20_Msk | SYS_MFP_P21_Msk | SYS_MFP_P22_Msk | SYS_MFP_P23_Msk | SYS_MFP_P24_Msk | SYS_MFP_P25_Msk);
	SYS->P2_MFP |= SYS_MFP_P20_PWM0 | SYS_MFP_P21_PWM1 | SYS_MFP_P22_PWM2 | SYS_MFP_P23_PWM3 | SYS_MFP_P24_PWM4 | SYS_MFP_P25_PWM5;
	GPIO_SetMode(P2, BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5, GPIO_PMD_OUTPUT);
	P20 = 0; P21 = 0; P22 = 0; P23 = 0; P24 = 0; P25 = 0;
	CLK_EnableModuleClock(PWM01_MODULE);
	CLK_EnableModuleClock(PWM23_MODULE);
	CLK_EnableModuleClock(PWM45_MODULE);
	CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HCLK, 0);
	CLK_SetModuleClock(PWM23_MODULE, CLK_CLKSEL1_PWM23_S_HCLK, 0);
	CLK_SetModuleClock(PWM45_MODULE, CLK_CLKSEL2_PWM45_S_HCLK, 0);
	PWMA->PPR = (10 << PWM_PPR_DZI23_Pos) | (10 << PWM_PPR_DZI01_Pos) | (1 << PWM_PPR_CP23_Pos) | (1 << PWM_PPR_CP01_Pos);
	PWMB->PPR = (10 << PWM_PPR_DZI01_Pos) | (1 << PWM_PPR_CP01_Pos);
	PWMA->CSR = (4 << PWM_CSR_CSR2_Pos) | (4 << PWM_CSR_CSR0_Pos);
	PWMB->CSR = (4 << PWM_CSR_CSR0_Pos);
	PWMA->PCR = PWM_PCR_PWM23TYPE_Msk | PWM_PCR_PWM01TYPE_Msk | PWM_PCR_CH2MOD_Msk | PWM_PCR_CH0MOD_Msk | PWM_PCR_DZEN23_Msk | PWM_PCR_DZEN01_Msk;
	PWMB->PCR = PWM_PCR_PWM01TYPE_Msk | PWM_PCR_CH0MOD_Msk | PWM_PCR_DZEN01_Msk;
	PWMA->CMR0 = 250;
	PWMA->CMR2 = 250;
	PWMB->CMR0 = 250;
	PWMA->CNR0 = 500;
	PWMA->CNR2 = 500;
	PWMB->CNR0 = 500;
	PWMA->POE = PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk | PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMB->POE = PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMA->PSCR = PWM_PSCR_PSSEN2_Msk | PWM_PSCR_PSSEN0_Msk;
	PWMB->PSCR = PWM_PSCR_PSSEN0_Msk;
	PWMA->PCR |= PWM_PCR_CH0EN_Msk;
}

void ah(int32_t duty) {
	PWMB->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMB->CMR0 = 250 - duty;
}

void al(int32_t duty) {
	PWMB->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMB->CMR0 = 250 + duty;
}

void az(void) {
	PWMB->POE &= ~(PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk);
}

void bh(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk;
	PWMA->CMR2 = 250 - duty;
}

void bl(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk;
	PWMA->CMR2 = 250 + duty;
}

void bz(void) {
	PWMA->POE &= ~(PWM_POE_PWM3_Msk | PWM_POE_PWM2_Msk);
}

void ch(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMA->CMR0 = 250 - duty;
}

void cl(int32_t duty) {
	PWMA->POE |= PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk;
	PWMA->CMR0 = 250 + duty;
}

void cz(void) {
	PWMA->POE &= ~(PWM_POE_PWM1_Msk | PWM_POE_PWM0_Msk);
}

int32_t wait_a_pn(volatile int32_t t, int32_t pn) {
	int32_t i;
	P32 = 0;
	for ( ; t; t--) {
		if (pn != ACMP_GET_OUTPUT(ACMP01, 1)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (pn != ACMP_GET_OUTPUT(ACMP01, 1)) {
				break;
			}
		}
		if (0 == i) {
			P32 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_b_pn(volatile int32_t t, int32_t pn) {
	int32_t i;
	P33 = 0;
	for ( ; t; t--) {
		if (pn != ACMP_GET_OUTPUT(ACMP23, 2)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (pn != ACMP_GET_OUTPUT(ACMP23, 2)) {
				break;
			}
		}
		if (0 == i) {
			P33 = 1;
			break;
		}
	}
	return t;
}

int32_t wait_c_pn(volatile int32_t t, int32_t pn) {
	int32_t i;
	P34 = 0;
	for ( ; t; t--) {
		if (pn != ACMP_GET_OUTPUT(ACMP01, 0)) {
			continue;
		}
		for (i = 100; i; i--) {
			if (pn != ACMP_GET_OUTPUT(ACMP01, 0)) {
				break;
			}
		}
		if (0 == i) {
			P34 = 1;
			break;
		}
	}
	return t;
}

void bldc_run(void) {
	int32_t t = 50000, duty = 10;
	for ( ;  ;  ) {
		cz();
		bl(duty);
		wait_c_pn(t, 1); // 1 0 z
		t -= (t >> 12);
		az();
		ch(duty);
		wait_a_pn(t, 0); // z 0 1
		t -= (t >> 12);
		bz();
		al(duty);
		wait_b_pn(t, 1); // 0 z 1
		t -= (t >> 12);
		cz();
		bh(duty);
		wait_c_pn(t, 0); // 0 1 z
		t -= (t >> 12);
		az();
		cl(duty);
		wait_a_pn(t, 1); // z 1 0
		t -= (t >> 12);
		bz();
		ah(duty);
		wait_b_pn(t, 0); // 1 z 0
		t -= (t >> 12);
		if (duty < 60) {
			duty++;
		}
	}
}

int main(void) {
	sys_init();
	led_init();
	acmp_init();
	pwm_init();
	bldc_run();
	return 0;
}

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn