#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;
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
20131126.zip (67个子文件)
20131126
INTRINS.H 492B
2013-12-13-1823TEST.TXT 8KB
motorcontrol.plg 2KB
delay.OBJ 1KB
UARTProANO.LST 7KB
input.OBJ 3KB
2013-12-11-21-03.txt 8KB
uart.h 183B
nokia_5110.OBJ 18KB
UARTANO.c 797B
uart.c 321B
2013-12-12-1843currentokaddv.txt 8KB
STC12C5A.H 45KB
input.LST 4KB
AD.h 217B
nokia_5110.c 9KB
STARTUP.OBJ 749B
AD.c 2KB
menu.LST 5KB
motorcontrol 55KB
UARTProANO.c 3KB
main201312111250.txt 6KB
motorcontrol.uvopt 59KB
UartPro.c 1KB
2013-12-13-1844WRONG.TXT 8KB
input.c 1KB
motorcontrol.hex 19KB
motorcontrol.M51 55KB
STC.H 19KB
UartPro.LST 3KB
AD.LST 5KB
motorcontrol_uvopt.bak 59KB
STARTUP.A51 6KB
motorcontrol_uvproj.bak 15KB
font.h 5KB
2013-12-12-10-49.TXT 8KB
menu.h 751B
uart.OBJ 3KB
main.LST 18KB
menu.c 3KB
motorcontrol.uvproj 14KB
UARTANO.h 268B
2013-12-14-1107good.txt 7KB
UARTANO.LST 2KB
input.h 141B
mian20131211.txt 7KB
2013-12-12-14-42currentok.txt 8KB
UartPro.h 138B
STARTUP.LST 14KB
basic.h 0B
delay.c 223B
2013-12-13-2233currentreallyok.txt 8KB
delay.LST 1KB
define.h 202B
nokia_5110.LST 19KB
main1.txt 6KB
uart.LST 1KB
main.OBJ 22KB
UartPro.OBJ 5KB
menu.OBJ 7KB
UARTProANO.h 513B
delay.h 131B
AD.OBJ 4KB
motorcontrol.lnp 211B
nokia_5110.h 1KB
2013-12-13-1135currenttest.txt 9KB
main.c 7KB
共 67 条
- 1
资源评论
- dltpwrlydd2014-05-18有仿真有程序挺不错的,不过和我的方案不太吻合
- cc64502017-07-17不错,有一定的参考价值
- a714562014-03-27注释不是很多,要画时间才能明白 、
stevewongbuaa
- 粉丝: 71
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功