📄 tempcontrol01.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)
/* 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--191
unsigned int *pmem=0;
unsigned int flagc,flagt;
int i,j,n,m;
int TIMER1,TIMER2; /* 1s counter and 5s counter */
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()
{
asm(" NOP");
asm(" STM #0, CLKMD");
asm(" STM #0, CLKMD");
asm(" STM #0x9007, CLKMD");
asm(" rpt #0ffffh");
asm(" nop");
PMST=0x3FA0;
SWWSR=0x7fff;
SWCR=0x0000;
IMR=0;
IFR=IFR;
/* asm(" stm 1000h,ar1");
asm(" stm 100ch,ar6");
asm(" st #10,*ar6");*/
}
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;
sort(data,10);
average = sum_ave( &data[2],6);
feedback = average;
}
}
/*void null(){
asm (" portr 8001h , *ar1");
asm (" andm #0ffh , *ar1+");
asm(" banz loop,*ar6- ");
asm(" nop");
asm(" stm 1000h,ar1");
asm(" st #10,*ar6");
asm("loop:");
// asm(" ldm *ar1, a ");
// AL = *feed;
// *feed++;
// feedback = *feed;
// cmps
// flagc=flagc^1;
/* if(flagc==1)
{ asm(" st 0h,*ar7");
asm(" portw *ar6,800ah");}
else
{ asm(" st 0ffffh,*ar7");
asm(" portw *ar6,800ah");}*/
//}
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 */
{
// flagt = flagt^1;
// TIMER2++;
TIMER1 = 0;
// feedback = *feed; /* Read AD, to get the feedback */
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 = (a0*e0 - a1*e1 + a2*e2)*0.042; //duty;
}
if (TIMER2==1500*5*2) /* 30s */
{
if (n<Len2)
{
temp2[n] = feedback;
feedback = TIMER2 = 0;
n++;
}
}
feedback = 0;
}
void TEMPcontrol()
{
/* asm(" st 0ffh,*ar2");
asm( " portw *ar2,800bh");*/
for(i=0;i<pw;i++)
{
asm(" ssbx xf");
j=0;
j=0;
j=0;
j=0;
// j=0;
}
/* asm(" st 80h,*ar2");
asm( " portw *ar2,800bh");*/
for(j=0;j<nw;j++)
{
asm(" rsbx xf");
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;
m=0;
pw = 10000*0.2;
nw = 10000*0.8;
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 - 10000*deta;
nw = nw + 10000*deta;
if (pw>=10000) pw=10000;
if (nw>=10000) nw=10000;
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 + -