#include "stc.h"
#include "nokia_5110.h"
#include "input.h"
#include "menu.h"
#include "delay.h"
#include "ad.h"
#include "uart.h"
#include "UartPro.h"
/*
**********************************************************
-------main函数--------
*函数名:主函数
*备 注:
**********************************************************
*/
sbit LED = P0^7;
sbit RelayArmature = P1^1;//B3 //yellow middle
sbit RelayExcitation = P1^2;//B5
#define Out P2
#define Pn 0.1f
#define In 0.1f
#define Dn 0
#define Pi 0.1f
#define Ii 0.1f
#define Di 0
#define OFFI 10.0f
#define MAXIPIDADDOUT 2.0f
#define MINIPIDADDOUT -2.0f
#define MINOUT 90.0f
#define MAXNPIDADDOUT 5.0f
#define MINNPIDADDOUT -5.0f
#define MAXSETI 580.0f
uchar selection;
uint ADgetref;
float ADgeti;
float ADsum;
uchar iii = 0;
uint SetSpeed;
uint NowSpeed;
//uint Count;
uchar timer1_interrupt_count;
uchar count = 0;
bit PID = 0;
uchar flag = 0;
float IPIDADDOUT = 0;
float NPIDADDOUT = 0;
float xdata errorn = 0;
float xdata errornp = 0;
float xdata errornpp = 0;
float xdata refi;
float xdata errori = 0;
float xdata errorip = 0;
float xdata erroripp = 0;
float xdata TEMP = 255.0f;
float u712;
uint SetI = 500;
uint IM;
void SwitchOnandOff();
void SetSpeedByKey();
void SetSpeedByRP();
void SwitchOn();
void SwitchOff();
void init_io();
void init_Timer0();
void init_Timer1();
void init_ints();
void main()
{
delayfor(1000);
LCD_Init();
LCD_Clear();
/*LCD_WriteString(0,0,"=Welcome=");
LCD_WriteString(0,1,"Motor");
LCD_WriteString(0,2,"Controlling");
LCD_WriteString(0,3,"System");
delayms(20000); */
AD_init();
init_io();
init_Timer0();
init_Timer1();
init_ints();
InitUart();
TEMP = 255;
P2 = 255;
SetI = 500;
EA = 1;
PID = 0;
while(1)
{
/* ADgeti = (AD_work(0)*16.5628f -43.1443f);
if(ADgeti < 0) ADgeti = 0;
u712 = AD_work(0);*/
selection = Menu_active((char **)mainMenuText, MAINMENUNUM);
switch (selection)
{
case 1: SwitchOnandOff();break;
case 2: SetSpeedByKey();break;
case 3: SetSpeedByRP();break;
default: break;
}
/* LCD_MyWriteNum(40,5,(uint)(ADgeti*1000),4);
LCD_WriteString(0,5,"712i:");
LCD_WriteString(0,4,"712u:");
LCD_MyWriteNum(40,4,(uint)(u712*1000),4);*/
//LCD_MyWriteNum(0,5,0,4);
}
}
void SwitchOnandOff()
{
while(1)
{
selection = Menu_active((char **)SwitchMenuText, SWITCHMENUNUM);
switch (selection)
{
case 1: SwitchOn();break;
case 2: SwitchOff();break;
case 10:return;
default: break;
}
}
}
void SetSpeedByKey()
{
/* while(1)
{
selection = Menu_active((char **)KeyMenuText, KEYMENUNUM);
switch (selection)
{
case 1:
{
SetI = ADgetref;
PID = 1;
break;
}
case 10:
{
P2 = 255;
TEMP = 255;
PID = 0;
return;
}
default: break;
}
//LCD_MyWriteNum(48,3,(uint)(ADgeti*1000),4); //Nowi
//LCD_MyWriteNum(48,1,(uint)(P2),4); //
LCD_MyWriteNum(0,2,ADgetref,4); //GIVEI
LCD_MyWriteNum(0,3,SetI,4); //SETI
OutPut_Data();
} */
// EA = 1; //开总中断允许位
LCD_Clear();
LCD_WriteString(0,0,"Set Speed");
LCD_WriteString(0,5,"Exit(S1)");
LCD_WriteString(0,4,"OK(S4)");
LCD_WriteString(24,1,"rpm");
LCD_WriteString(0,2,"Now Speed");
LCD_WriteString(24,3,"rpm");
delayms(1000);
while(1)
{
LCD_MyWriteNum(0,1,ADgetref,4); //SetSpeed
if(checkButton(4) == 1)
{
iii = 0;
while(1)
{
//SetI = ADgetref;
PID = 1;
LCD_MyWriteNum(0,3,NowSpeed,4); //NowSpeed
// LCD_MyWriteNum(48,3,(uint)(ADgeti*1000),4); //Nowi
LCD_MyWriteNum(48,1,(uint)(P2),4); //
OutPut_Data();
if(checkButton(1)==1)
{
//P2 = 255;
//TEMP = 255;
//PID = 0;
//EA = 0;
break;
}
}
}
else if(checkButton(1) == 1)
{
P2 = 255;
TEMP = 255;
//SetI = 500;
PID = 0;
//EA = 0;
break;
}
}
}
void SetSpeedByRP()
{
// EA = 0;
// PID = 0;
LCD_Clear();
LCD_WriteString(0,0,"Set Speed");
LCD_WriteString(0,5,"Exit(S1)");
LCD_WriteString(24,1,"rpm");
LCD_WriteString(0,2,"Now Speed");
LCD_WriteString(24,3,"rpm");
P2 = 255;
TEMP = 255;
PID = 1;
// EA = 1; //开总中断允许位
while(1)
{
//SetSpeed = ADgetref;
//P2 = ADgetref;
LCD_MyWriteNum(48,3,(uint)(ADgeti*1000),4); //Nowi
LCD_MyWriteNum(48,1,(uint)(P2),4); //u712 显示的于pid计算的电流测量值 也就是712输出电压
LCD_MyWriteNum(0,1,(uint)(ADgetref),4); //SetSpeed
LCD_MyWriteNum(0,3,NowSpeed,4); //NowSpeed
//LCD_MyWriteNum(48,4,(uint)(TEMP),4);
//LCD_MyWriteNum(48,5,(uint)(IPIDADDOUT),4);
// delayms(100);
/* LCD_MyWriteNum(0,1,ADgetref,4);
LCD_MyWriteNum(0,3,NowSpeed,4);*/
//OutData[3] = TEMP;
OutPut_Data();
if(checkButton(1)==1)
{
P2 = 255;
TEMP = 255;
PID = 0;
// EA = 0;
break;
}
}
}
void SwitchOn()
{
RelayExcitation = 0;
delayms(2000);
RelayArmature = 0;
}
void SwitchOff()
{
RelayArmature = 1;
delayms(20000);
RelayExcitation = 1;
}
void init_Timer0(void) //定时器0初始化
{
//计数个数:0
TMOD &= 0xf0;
TMOD |= 0x05; //设置定时器0模式为:16位计数模式
TH0 = 0x00;
TL0 = 0x00;
TR0 = 1; //启动定时器0
}
void init_Timer1(void) //定时器1初始化
{
//定时值(uS):50000
TMOD &= 0x0f;
TMOD |= 0x10; //设置定时器1模式为:16位定时模式
TH1 = 0xFC;
TL1 = 0x66;
TR1 = 1; //启动定时器1
}
void init_io(void) //io驱动模式
{
P0M1 = 0x00;
P0M0 = 0x00;
P1M1 = 0x00;
P1M0 = 0x00;
P2M1 = 0x00;
P2M0 = 0x00;
P3M1 = 0x00;
P3M0 = 0x00;
P4M1 = 0x00;
P4M0 = 0x00;
}
void init_ints(void) //外部中断初始化
{
IP=0;
ET1=1; //允许定时器1中断
}
///////////////////////////////////// interrupt ///////////////////////////
void timer_1(void) interrupt 3 //定时器1中断 1ms
{
TH1 = 0xFC; //重装载
TL1 = 0x66;
timer1_interrupt_count++;
ADsum += AD_pro(0);
//在此写定时器1中断服务程序 20ms
if(timer1_interrupt_count == 20)
{
count ++;
timer1_interrupt_count = 0;
ADsum/= 20; //用于pid计算的电流测量值 也就是712输出电压 未经处理,为0~1024
IM = ADsum;
// LED = 1;
if(PID == 1)
{
errori = (float)((float)(SetI) - (float)(IM)); //电流误差
IPIDADDOUT = Pi*(errori-errorip) + Ii*errori +Di*(errori-errorip-errorip+erroripp);//电流度控制输出,应限幅
if(IPIDADDOUT > MAXIPIDADDOUT)
{IPIDADDOUT = MAXIPIDADDOUT; }
if(IPIDADDOUT < MINIPIDADDOUT)
{IPIDADDOUT = MINIPIDADDOUT; }
TEMP -= IPIDADDOUT;
if(TEMP<MINOUT) TEMP = MINOUT;
P2 = (uint)(TEMP);
erroripp = errorip;
errorip = errori;
OutData[0] = P2;
OutData[1] = IPIDADDOUT;
OutData[2] = IM;
}
u712 = ADsum * 0.005068f;
ADgeti = (u712*17.1181f -44.1782f); //20041197
if(ADgeti < 0) ADgeti = 0; //用于显示的电流测量值
if(ADgeti > OFFI)
{
SwitchOff();
}
ADsum = 0;
// 0 1 2 3 4 5 6
//509.2323741 520.7591537 532.2859333 543.8127129 555.3394925 566.8662721 578.3930517
if (count == 5) //100ms
{
//if(SeiI == 500) SetI = 532;
/*ii++;
if(SetI==532) flag = 0; //2A
if(SetI==567) flag = 1; //5A
//pretend that nowspeed >500
if(ii > 100) {flag = 2;ii = 0;}
if(flag == 0) {SetI += 5;}
else if(flag == 1) {SetI = 566; ii = 0;}
else if(flag == 2) SetI -=5;