📄 tempcontrol02.c
字号:
#pragma CODE_SECTION(vect,"vect")
#include <stdio.h>
void TEMPcontrol(void);
void testint1(void);
#define IMR *(pmem+0x0000)
#define IFR *(pmem+0x0001)
#define PMST *(pmem+0x001D)
#define SWCR *(pmem+0x002B)
#define SWWSR *(pmem+0x0028)
#define AL *(pmem+0x0008)
#define CLKMD 0x0058 /* clock mode reg*/
/* timer 0 */
#define TIM0 *(pmem+0x0024) /* timer0 register */
#define PRD0 *(pmem+0x0025) /* timer0 period register */
#define TCR0 *(pmem+0x0026) /* timer0 control register */
#define Len1 1800
#define Len2 60
int r = 180;//165--195
unsigned int *pmem=0;
unsigned int flagc,flagt;
int i,j,n,m;
int TIMER1,TIMER2; /* 1s counter and 5s counter */
unsigned int pw,nw; /* positive pulse width and negative pulse width */
int feedback = 0; /* INT1 counter to count the speed */
int *feed =(int *)0x1000;
int temp1[Len1],temp2[Len2]; /* To store the speed in different PWM during 1s */
double deta,duty;
double e0,e1,e2,y0,y1,y2,x;
double Kp = 5.0;
double Ti = 1.0;
double Td = 0.2;
double T = 1;
double a0,a1,a2;
void cpu_init()
{
*(unsigned int*)CLKMD=0x0; //switch to DIV mode clkout= 1/2 clkin
while(((*(unsigned int*)CLKMD)&01)!=0);
*(unsigned int*)CLKMD=0x27ff; //switch to PLL
PMST=0x3FA0;
SWWSR=0x7fff;
SWCR=0x0000;
IMR=0;
IFR=IFR;
}
int cnt = 0;
ioport unsigned char port8001;
unsigned char data[10];
void sort(unsigned char * array,int length);
unsigned char sum_ave(unsigned char * array,int length);
unsigned int average;
interrupt void int1()
{
data[cnt] = port8001;
data[cnt] &= 0x00FF;
cnt++;
if (cnt >= 10) {
cnt = 0;
}
}
void set_int()
{
asm(" ssbx intm");
TCR0=0x0b1b;
PRD0=0x9c3f; //0x4e1f;
IMR=IMR|0x000c;
IFR=IFR;
TCR0=0x0b29;
asm(" rsbx intm");
}
interrupt void tint0()
{
TIMER1++;
TIMER2++;
if(TIMER1==50*5)/* 1s=(1)PRD0=0x4e1f; (2)#0xf007, CLKMD (10MHz)(3)TIMER1==50)*/
{
TIMER1 = 0;
sort(data,10);
average = sum_ave( &data[2],6);
feedback = average;
temp1[m] = feedback;
m++;
y2 = y1;
y1 = y0;
y0 = feedback;
e0 = r - y0;
e1 = r - y1;
e2 = r - y2;
duty = 1.0/(2*(a0+a2));
deta = 0.5*(a0*e0 - a1*e1 + a2*e2)*duty; //duty;
}
if (TIMER2==1500*5) /* 30s */
{
if (n<Len2)
{
temp2[n] = feedback;
feedback = TIMER2 = 0;
n++;
}
}
feedback = 0;
}
void TEMPcontrol()
{
asm(" ssbx xf");
for(i=0;i<pw;i++)
{
j=0;j=0;j=0;j=0;j=0;j=0;j=0;j=0;
j=0;j=0;j=0;j=0;j=0;j=0;j=0;j=0;
j=0;j=0;j=0;j=0;j=0;j=0;j=0;j=0;
j=0;j=0;j=0;j=0;j=0;j=0;j=0;j=0;
j=0;j=0;j=0;j=0;j=0;j=0;j=0;j=0;
j=0;j=0;j=0;j=0;j=0;j=0;j=0;j=0;
}
asm(" rsbx xf");
for(j=0;j<nw;j++)
{
i=0;i=0;i=0;i=0;i=0;i=0;i=0;i=0;
i=0;i=0;i=0;i=0;i=0;i=0;i=0;i=0;
i=0;i=0;i=0;i=0;i=0;i=0;i=0;i=0;
i=0;i=0;i=0;i=0;i=0;i=0;i=0;i=0;
i=0;i=0;i=0;i=0;i=0;i=0;i=0;i=0;
i=0;i=0;i=0;i=0;i=0;i=0;i=0;i=0;
}
}
void main(void)
{
cpu_init();
a0 = Kp*(1.0 + T/Ti + Td/T);
a1 = Kp*(1.0 + 2.0*Td/T);
a2 = Kp*Td/T;
e0=e1=e2=y0=y1=y2=0;
pw = 2*10000*0.9;//0.1
nw = 2*10000*0.1;//0.9
for (i=0; i<Len1; i++)
{temp1[i]=0;}
for (i=0; i<Len2; i++)
{temp2[i]=0;}
flagc = flagt = 0;
i=j=m=n=feedback = 0;
TIMER1 = TIMER2 =0;
set_int();
for(;;)
{
/* if(flagt==1) {asm(" ssbx xf");}
else {asm(" rsbx xf");}*/
pw = pw + 20000*deta;//10000
nw = nw - 20000*deta;//10000
if (pw>=20000) pw=20000;
if (nw>=20000) nw=20000;
if (pw<=0) pw=0;
if (nw<=0) nw=0;
TEMPcontrol();
/* if(m>=1800){
pw = 10000*1.0;
nw = 10000*0;
}
*/
}
}
// char array[10] = {0x112,0xa3,0x11,4,0x55,6,0x55,7,0x22,0x33};
void sort(unsigned char * array,int length)
{
int i, ii;
unsigned char max;
// for (i = 0; i < length; i++) {
// array[i] &= 0xFF;
// }
for (i = 0; i < length; i++) {
max = array[i];
for ( ii = i; ii < length; ii++) {
if (array[ii] > max) {
max = array[ii];
array[ii] = array[i];
array[i] = max;
}
}
}
}
unsigned char sum_ave(unsigned char * array,int length)
{
register int i;
unsigned int sum = 0;
for (i = 0; i < length; i++) {
sum += array[i];
}
return ((unsigned char)(sum/length));
}
void vect()
{
asm(" .ref _c_int00");/*pseudoinstruction*/
asm(" .ref _int1");
asm(" b _c_int00");/* reset */
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* int0 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete");
asm(" nop"); /* int1 */
asm(" nop");
asm(" nop");
// asm(" rete"); /* int2 */
asm(" b _int1");
asm(" nop");
asm(" nop");
// asm(" nop");
asm(" b _tint0"); /* tint0 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* brint0 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* bxint0 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* dmac0 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* tint1 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* int3 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* hpint */
asm(" nop");
asm(" nop");
asm(" rete"); /* brint1 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* bxint1 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* dmac4 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" rete"); /* dmac5 */
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
asm(" nop");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -