📄 c_pwm1.c
字号:
/* include C standard file*/
#include <stdio.h>
#include <math.h>
/* include DSP standard file*/
#include "F243_c.h"
/* include variable define and I/O adress define*/
#include "c_pwm1.h"
void kick_dog(void)
{
*WDKEY=0x5555;
*WDKEY=0xAAAA;
}
void delay(uint time)
{
uint i;
for(i=0;i<time;i++)
{
i++; i--;
kick_dog();
}
}
void freeze(void)
{
asm(" SETC INTM");
while(1)
{
DISABLE_PWM;
kick_dog();
DEAL_PDP_BUG;
*EVIFRA = 0x0001;
*ACTR = 0x0000; /*When urgency occured, CPU halted here, closing all PWM output*/
ERROR;
FBUZ_ON;
}
}
void over_current(void)
{
asm(" SETC INTM");
while(1)
{
DISABLE_PWM;
*ACTR = 0x0000;
kick_dog();
WAITING;
FBUZ_ON;
delay(50000);
ERROR;
FBUZ_OFF;
delay(50000);
}
}
main()
{
sint i;
/***disable global interrupts***/
asm(" SETC INTM");
/*DEAL PDPINT BUG*/
DEAL_PDP_BUG;
/*Enable the watchdog timer*/
*WDCR = 0x0028;
kick_dog();
WSGR = 0xFE40; /*one wait state for IO space*/
/*configure system register*/
*SCSR = 0x4001;
/*Setup shared I/O pins*/
*OCRA = 0x0560; /*100,96,91:PWM_EN(OPA7,OPB1,3);102,98,94:PWM;83:CAP3;87:AD1_BUSY(IPB7)*/
/*85:AD2_BUSY(IPB6);128:AD1_CNS(OPB5);130:AD2_CNS(OPB4)*/
*OCRB = 0x0000;
*PADATDIR = 0x8202; /*58 output high,100 output low */
*PBDATDIR = 0x3A00; /* 设置96,91,128,130为输出,四个管脚输出都为低电平*/
/* 设置85,87为输入*/
*PDDATDIR = 0x3000;
*PCDATDIR = 0XC000;
/* Setup SCI module */
*SCICCR = 0x07; /* 1 stop bit,no parity,8 char bits, async mode, idle line protocal*/
*SCICTL1 = 0x02; /*Enable TX; Disalbe RX,RX ERR ,SLEEP,TXWAKE*/
*SCICTL2 = 0x00; /* Disable RX INT, Disable TX INT */
*SCIHBAUD = 0x01;
*SCILBAUD = 0x03; /* Baud rat =9600 bps */
/*BRR=259,波特率=20MHz/[(259+1)*8]=9600 bps*/
temp = *SCITXBUF; /* Clear the buffers */
temp = *SCITXBUF;
/*CAN initialization*/
*MCR = 0x1000; /*change configuration request,set CCR=1*/
while (!(*GSR & 0x0010)); /*wait for change configuration enable CCE=1*/
*BCR2 = 0x0001; /*set BRP=1,TQ=(BRP+1)/Iclk*/
*BCR1 = 0x0523; /*resynchronize on the falling edge only,SJW=1,sample only once,
TSEG1=5,TSEG2=4,Baud rate=1 Mbit/s */
*MCR = 0x0000; /*return in normal operation*/
while (*GSR & 0x0010); /*wait for change configuration disable CCE=0*/
/*End CAN Initialization*/
/*Disable timer1 and 2, and the PWM configuration*/
*T1CNT = 0x0000; /*Clear timer counter*/
*T1CON = 0x0900; /*set T1 in continuous UP/DOWN mode,时钟预定标系数x/2,不使能定时器操作*/
*T1PR = T1_PER; /*Set timer period, switch frequency is 5 KHz*/
*GPTCON = 0x0000; /*configure GPTCON, if PDPINT is active,the sixth bit is set to zero*/
*DBTCON = 0x0000; /*Disable deadband units*/
*CMPR1 = T1_PER;
*CMPR2 = T1_PER;
*CMPR3 = T1_PER;
*ACTR = 0x0666; /*Configure PWM outputs,PWM1、3、5高有效*/
*COMCON = 0x8200; /*Enable PWM outputs&compare operation*/
/*Setup the core interrupts*/
*IMR = 0x0000; /*Clear the IMR register */
*IFR = 0x00FF; /*Clear any pending interrups */
*IMR = 0x0002; /*Enable INT4 for CAP3, enable INT2 for T1 in CAP3 ISR later*/
/*Setup the event manager interrupts*/
*EVIFRA = 0xFFFF; /* Clear Event Manager IFR and set up the EM IMR regs */
*EVIFRB = 0xFFFF;
*EVIFRC = 0xFFFF;
*EVIMRA = 0x0200; /* Enable T1 UnderFlow INT in INT2 and PDPINT in INT1*/
*EVIMRB = 0x0000; /* Disable T2 int */
*EVIMRC = 0x0000; /* Disable CAP3 INT */
/* 变量清零和初始化 */
t_step = 0;
p_u_dc_sam = 0; /*p_u_dc_sam,用来指向下一个直流电压采样值放在队列中的那个位置*/
for (i = 0; i < 16; i++)
{
u_dc_sam_array[i] = U_DC_START;
}
u_dc.err_I = 0;
u_dc.control = 0;
u_dc.set = U_DC_SET;
k_i_p = K_I_P_INIT;
phase.angle = 50;
/*采样直流测电压*/
ADC12_START;
while (!(ADC1_BUSY)); /*confirm ADC1 running*/
while (!(ADC2_BUSY));
do
{
kick_dog();
u_a.former = u_a.nonce;
while (ADC1_BUSY); /*ADC1 complete*/
while (ADC2_BUSY);
u_dc.sam = (sint)(ADC1_DATA);
temp = (sint)(ADC1_DATA);
temp = (sint)(ADC1_DATA);
temp = (sint)(ADC1_DATA);
u_a.nonce = ADC2_DATA;
temp = ADC2_DATA;
temp = ADC2_DATA;
temp = ADC2_DATA;
ADC12_STOP;
u_dc.sam += U_DC_E;
u_a.nonce += U_A_E;
u_dc.sam &= 0x0FFF;
u_a.nonce &= 0x0FFF;
if (u_dc.sam >= 2048) u_dc.sam -= 4096;
u_dc.sam = SLONGMUL((u_dc.sam) , (sint)K_V_DC , 12);
if (u_a.nonce >= 2048) u_a.nonce -= 4096;
if ((u_a.former < 0) && (u_a.nonce >= 0)) phase.angle = 0; /*A相电压过零点*/
ADC12_START;
while(!(ADC1_BUSY));
while(!(ADC2_BUSY));
}
while ((u_dc.sam <= (sint)U_DC_START) || (phase.angle != 0));
ADC12_STOP;
/* 直流侧电压达到可以进行PFC控制*/
FBUZ_ON;
delay(30000);
FBUZ_OFF;
*T1CNT = 0x0000;
*T1CON |= 0x0040; /* 使能T1并开始计数 */
asm(" CLRC INTM " ); /*Enable global interrupts*/
DISABLE_PWM;
while (1);
}
/* T1 周期中断服务程序 */
interrupt void timer1_per_int(void)
{
uint EVIFRA_temp;
sint i, j = 0;
asm(" SETC INTM " );
kick_dog();
/* 判断是不是PDPINT中断,若是则调用freeze()函数*/
/*EVIFRA_temp = *EVIFRA;
if(EVIFRA_temp & 0x0001) freeze();*/
*EVIFRA = 0x0200; /* 清T1周期中断标志 */
*COMCON = 0x8200; /*Enable PWM outputs&compare operation*/
while (u_dc.control < CNTL_START)
{
DISABLE_PWM;
j++;
if(j <= 20) WAITING;
if((j > 20) && (j <= 40)) ERROR;
if(j > 40) j = 0;
kick_dog();
t_step = 0;
p_u_dc_sam = 0; /*p_u_dc_sam,用来指向下一个直流电压采样值放在队列中的那个位置*/
for (i = 0; i < 16; i++) u_dc_sam_array[i] = U_DC_START;
u_dc.err_I = 0;
u_dc.set = U_DC_SET;
phase.angle = 50;
ADC1_START;
while(!(ADC1_BUSY));
while (ADC1_BUSY);
temp = ADC1_DATA; /*read ADC1 and ADC2 result in sequence*/
temp = ADC1_DATA;
temp = ADC1_DATA;
u_dc.control = ADC1_DATA;
ADC1_STOP;
u_dc.control &= 0x0FFF;
if (u_dc.control >= 2048) u_dc.control -= 4096;
while(phase.angle == 50)
{
kick_dog();
ADC2_START;
while(!(ADC2_BUSY));
u_a.former = u_a.nonce;
while (ADC2_BUSY);
u_a.nonce = ADC2_DATA;
temp = ADC2_DATA;
temp = ADC2_DATA;
temp = ADC2_DATA;
ADC2_STOP;
u_a.nonce += U_A_E;
u_a.nonce &= 0x0FFF;
if (u_a.nonce >= 2048) u_a.nonce -= 4096;
if ((u_a.former < 0) && (u_a.nonce >= 0)) phase.angle = 0; /*A相电压过零点*/
}
}
/* 启动所有AD转换,ADC1(4,3,2,1通道):u_dc_temp,i_a,i_b,u_dc.control; ADC2(4,3,2通道):u_a,u_b,u_c*/
ADC12_START; /*start ADC1 and ADC2 simultaneously*/
while(!(ADC1_BUSY)); /*confirm converting*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -