encoder pulse generator
A & B signal.
PWM1A, PWM2A - Set Phase reg : 90 deg
/* ==============================================================================
System Name: YPEMC_encoder pulse generator
File Name: encoder_gneerator.c
Target: TMS320F28335
Note: encoder pulse generator
duty ratio 0.5, 1024 pulse, frequency change
a
/a
b
/b
z
U
V
W
=====================================================================================
History:
-------------------------------------------------------------------------------------
xxxx-xx-xx : xxxxxxxxxxxxxxxxxxxxxxxx (jyc)
================================================================================= */
// Include header files used in the main function
#include "DSP2833x_Device.h" /* DSP2833x Headerfile Include File */
#include "DSP2833x_Examples.h" /* DSP2833x Examples Include File */
#include <math.h>
interrupt void epwm1_isr(void); //
void Init_EPwm(void);
void Init_Gpio(void);
#define SYSCLK 150E6 /* 150MHz */
int PWM_FREQUENCY = 5000; /* */
void main(void)
{
DINT;
IER = 0x0000;
IFR = 0x0000;
InitSysCtrl();
//GPIO setting///////////////////////////////////
Init_Gpio();
InitPieCtrl();
IER = 0x0000; // Disable CPU interrupts and clear all CPU interrupt flags:
IFR = 0x0000;
InitPieVectTable();
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.EPWM1_INT = &epwm1_isr;
EDIS; // This is needed to disable write to EALLOW protect
IER |= M_INT3; //Enable CPU INT3 for EPWM1_INT
PieCtrlRegs.PIEIER3.bit.INTx1 = 1; //Enable PIE group 3 interrupt 1 for EPWM1_INT
Init_EPwm(); //
EPwm1Regs.CMPA.half.CMPA = EPwm1Regs.TBPRD * 0.5;
EPwm2Regs.CMPA.half.CMPA = EPwm1Regs.TBPRD * 0.5;
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
for(;;)
{
}
}
interrupt void epwm1_isr(void) // PMW 타이머 인터럽트 서비스 루틴.
{
EPwm1Regs.ETCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
void Init_EPwm(void)
{
//3000 pulse per resolution(PPR)
//1초동안 펄스의 개수 = (rpm/60)*PPR
//rpm/60 : 1초동안 몇번도는지
//2000rpm : 100000
//1000rpm : 50000
//500rpm : 25000
//100rpm : 5000
//10rpm : 500
PWM_FREQUENCY = 5000;
EPwm1Regs.TBPRD = (SYSCLK/PWM_FREQUENCY)-1; // Period = 1201 TBCLK counts
EPwm1Regs.CMPA.half.CMPA = ((EPwm1Regs.TBPRD+1)>>1); // Set 50% fixed duty for EPWM1A
EPwm1Regs.TBPHS.half.TBPHS = 0; // Set Phase register to zero
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Asymmetrical mode
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Master module
EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO; // Sync down-stream module
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero
EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET; // set actions for EPWM1A
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm2Regs.TBPRD = (SYSCLK/PWM_FREQUENCY)-1; // Period = 1201 TBCLK counts
EPwm2Regs.CMPA.half.CMPA = ((EPwm1Regs.TBPRD+1)>>1); // Set 50% fixed duty EPWM2A
EPwm2Regs.TBPHS.half.TBPHS = 0; // Set Phase register to zero initially
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Asymmetrical mode
EPwm2Regs.TBCTL.bit.PHSEN = TB_ENABLE; // Slave module
EPwm2Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_IN; // sync flow-through
EPwm2Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm2Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm2Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
EPwm2Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero
EPwm2Regs.AQCTLA.bit.ZRO = AQ_SET; // set actions for EPWM2A
EPwm2Regs.AQCTLA.bit.CAU = AQ_CLEAR;
// Set Phase reg to 300/1200 * 360 = 90 deg
EPwm2Regs.TBPHS.half.TBPHS = ((EPwm1Regs.TBPRD+1)) - ((EPwm1Regs.TBPRD+1)*0.25);
}
//---------------------------------------------------------------------------
void Init_Gpio(void)
{
/*
GPIO0/EPWM1A : A
GPIO1/EPWM1B/ECAP6/MFSRB : /A
GPIO2/EPWM2A : B
GPIO3/EPWM2B/ECAP5/MCLKRB : /B
GPIO34/ECAP1/XREADY : Z
GPIO24/ECAP1/EQEP2A/MDXB : U
GPIO25/ECAP2/EQEP2B/MDRB : V
GPIO26/ECAP3/EQEP2I/MCLKXB : W
*/
EALLOW;
//GPxMUXn : 핀 기능설정
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;
GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 0;
GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 0;
GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 0;
//GPxDIR : 방향 설정 (1: out, 0: in)
GpioCtrlRegs.GPADIR.all = 0xffffffff; // GPIO0-GPIO31
GpioCtrlRegs.GPBDIR.all = 0xffffffff; // GPIO32-GPIO63
GpioCtrlRegs.GPCDIR.all = 0xffffffff; // GPI064-GPIO95
// Pull-ups can be enabled or disabled.
GpioCtrlRegs.GPAPUD.all = 0x0000; // Pullup's enabled GPIO0-GPIO31
GpioCtrlRegs.GPBPUD.all = 0x0000; // Pullup's enabled GPIO32-GPIO63
GpioCtrlRegs.GPCPUD.all = 0x0000; // Pullup's enabled GPIO64-GPIO79
EDIS;
GpioDataRegs.GPACLEAR.all = 0xffffffff; //초기 상태를 클리어.(0)
GpioDataRegs.GPBCLEAR.all = 0xffffffff; //초기 상태를 클리어.(0)
GpioDataRegs.GPCCLEAR.all = 0xffffffff; //초기 상태를 클리어.(0)
}
'삽질기초 > HW' 카테고리의 다른 글
4층 pcb 설계시 주의사항 (0) | 2012.10.05 |
---|---|
3상 (0) | 2012.05.04 |
DSP library (0) | 2012.03.12 |
DSP2833x _IO 테스트 소스. (0) | 2011.11.08 |
dsp-errer (0) | 2011.07.29 |