- 浏览: 13028 次
最新评论
C51的PID程序
- 博客分类:
- 技术杂绘
C51的PID程序
2011年12月22日
//主程序================================================== ====
#include "Main.h"
#include "PID_f1.h"
/* .................................................. ............. */
/* .................................................. ............. */
void main(void)
{
float x,y,z;
while(1)
{
x = PID_Control(y,z);
}
}
//PID子程序================================================== ===
/*------------------------------------------------ ------------------*-
PID_f1.C (v1.00)
-------------------------------------------------- ----------------
Simple PID control implementation.
See Chapter 35 for details.
COPYRIGHT
---------
This code is from the book:
PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
[Pearson Education, 2001; ISBN: 0-201-33138-1].
This code is copyright (c) 2001 by Michael J. Pont.
See book for copyright details and other information.
-*------------------------------------------------ ------------------*/
#include "PID_f1.h"
// ------ Private constants ----------------------------------------
#define PID_KP (0.2f) // Proportional gain
#define PID_KI (0.01f) // Integral gain
#define PID_KD (0.01f) // Differential gain
#define PID_MAX (1.0f) // Maximum PID controller output
#define PID_MIN (0.0f) // Minimum PID controller output
// ------ Private variable definitions------------------------------
static float Sum_G; // Integrator component
static float Old_error_G; // Previous error value
/*------------------------------------------------ ------------------*-
PID_Control()
Simple floating-point version.
See text for details.
-*------------------------------------------------ ------------------*/
float PID_Control(float Error, float Control_old)
{
// Proportional term
float Control_new = Control_old + (PID_KP * Error);
// Integral term
Sum_G += Error;
Control_new += PID_KI * Sum_G;
// Differential term
Control_new += (PID_KD * SAMPLE_RATE * (Error - Old_error_G));
// Control_new cannot exceed PID_MAX or fall below PID_MIN
if (Control_new > PID_MAX)
{
Control_new = PID_MAX;
}
else
{
if (Control_new error value
Old_error_G = Error;
return Control_new;
}
/*------------------------------------------------ ------------------*-
---- END OF FILE -------------------------------------------------
-*------------------------------------------------ ------------------*/
//头文件Main.H============================================ =============
/*------------------------------------------------ ------------------*-
Main.H (v1.00)
-------------------------------------------------- ----------------
'Project Header' (see Chap 9) for project PID_f1 (see Chap 35)
COPYRIGHT
---------
This code is from the book:
PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
[Pearson Education, 2001; ISBN: 0-201-33138-1].
This code is copyright (c) 2001 by Michael J. Pont.
See book for copyright details and other information.
-*------------------------------------------------ ------------------*/
#ifndef _MAIN_H
#define _MAIN_H
//------------------------------------------------ ------------------
// WILL NEED TO EDIT THIS SECTION FOR EVERY PROJECT
//------------------------------------------------ ------------------
// Must include the appropriate microcontroller header file here
#include
// Include oscillator / chip details here
// (essential if generic delays / timeouts are used)
// -
// Oscillator / resonator frequency (in Hz) e.g. (11059200UL)
#define OSC_FREQ (12000000UL)
// Number of oscillations per instruction (4, 6 or 12)
// 12 - Original 8051 / 8052 and numerous modern versions
// 6 - Various Infineon and Philips devices, etc.
// 4 - Dallas, etc.
//
// Take care with Dallas devices
// - Timers default to *12* osc ticks unless CKCON is modified
// - If using generic code on a Dallas device, use 12 here
#define OSC_PER_INST (12)
//------------------------------------------------ ------------------
// SHOULD NOT NEED TO EDIT THE SECTIONS BELOW
//------------------------------------------------ ------------------
typedef unsigned char tByte;
typedef unsigned int tWord;
typedef unsigned long tLong;
// Misc #defines
#ifndef TRUE
#define FALSE 0
#define TRUE (!FALSE)
#endif
#define RETURN_NORMAL (bit) 0
#define RETURN_ERROR (bit) 1
//------------------------------------------------ ------------------
// Interrupts
// - see Chapter 13.
//------------------------------------------------ ------------------
// Generic 8051/52 timer interrupts (used in most schedulers)
#define INTERRUPT_Timer_0_Overflow 1
#define INTERRUPT_Timer_1_Overflow 3
#define INTERRUPT_Timer_2_Overflow 5
// Additional interrupts (used in shared-clock schedulers)
#define INTERRUPT_EXTERNAL_0 0
#define INTERRUPT_EXTERNAL_1 2
#define INTERRUPT_UART_Rx_Tx 4
#define INTERRUPT_CAN_c515c 17
//------------------------------------------------ ------------------
// Error codes
// - see Chapter 14.
//------------------------------------------------ ------------------
#define ERROR_SCH_TOO_MANY_TASKS (1)
#define ERROR_SCH_CANNOT_DELETE_TASK (2)
#define ERROR_SCH_WAITING_FOR_SLAVE_TO_ACK (3)
#define ERROR_SCH_WAITING_FOR_START_COMMAND_FROM_MASTER (3)
#define ERROR_SCH_ONE_OR_MORE_SLAVES_DID_NOT_START (4)
#define ERROR_SCH_LOST_SLAVE (5)
#define ERROR_SCH_CAN_BUS_ERROR (6)
#define ERROR_I2C_WRITE_BYTE (10)
#define ERROR_I2C_READ_BYTE (11)
#define ERROR_I2C_WRITE_BYTE_AT24C64 (12)
#define ERROR_I2C_READ_BYTE_AT24C64 (13)
#define ERROR_I2C_DS1621 (14)
#define ERROR_USART_TI (21)
#define ERROR_USART_WRITE_CHAR (22)
#define ERROR_SPI_EXCHANGE_BYTES_TIMEOUT (31)
#define ERROR_SPI_X25_TIMEOUT (32)
#define ERROR_SPI_MAX1110_TIMEOUT (33)
#define ERROR_ADC_MAX150_TIMEOUT (44)
#endif
/*------------------------------------------------ ------------------*-
---- END OF FILE -------------------------------------------------
-*------------------------------------------------ ------------------*/ //头文件 PID_f1.H
// ------ Public function prototypes -------------------------------
float PID_Control(float,float);
// ------ Public constants -----------------------------------------
#define SAMPLE_RATE (1) // Hz
/*------------------------------------------------ ------------------*-
---- END OF FILE -------------------------------------------------
-*------------------------------------------------ ------------------*/
发表评论
-
c++模版函数中应用typename
2012-01-20 11:04 820c++模版函数中应用typename 2011年06月21日 ... -
在 Visual C++ 中使用内联汇编(三)
2012-01-20 11:04 869在 Visual C++ 中使用内联汇编(三) 2011年0 ... -
寻找Nenia C’alladhan中的女声――Constance (Cuirina)
2012-01-20 11:03 800寻找Nenia C’alladhan中的 ... -
嗯哼,27°C
2012-01-20 11:03 1483嗯哼,27°C 14小时前 紧锣密鼓地忙完了各种考试,又 ... -
C1203018小时测试
2012-01-20 11:03 557C1203018小时测试 18小时前 C1203018小 ... -
Android网络编程实践之旅(一):网络状态检测
2012-01-19 15:57 727Android网络编程实践之旅 ... -
error: No curses/termcap library found的解决办法
2012-01-19 15:57 951error: No curses/termcap librar ... -
开机提示 A disk read error occurred ……
2012-01-19 15:57 925开机提示 A disk read error occurred ... -
ELAU运动控制器的应用――Watch3
2012-01-19 15:57 707ELAU运动控制器的应用――Watch3 2011年12月1 ... -
北京自考专科接考本科要分清专业类别
2012-01-17 05:50 592北京自考专科接考本科 ... -
新目标英语八年级上册unit9 sectionA 1a-grammar导学案
2012-01-17 05:50 518新目标英语八年级上册unit9 sectionA 1a-gra ... -
符合用户体验是获得好的关键词排名的关键
2012-01-17 05:50 585符合用户体验是获得好 ... -
文化传媒成大盘下跌小棉袄
2012-01-17 05:50 500文化传媒成大盘下跌小 ... -
大叔遇上御姐丈母娘【十一】
2012-01-17 05:50 544大叔遇上御姐丈母娘【十一】 2011年11月25日 ... -
《中庸》
2012-01-16 04:37 495《中庸》 2010年02月26日 【原文】 子曰 ... -
第十四章 我是商鞅(上)
2012-01-16 04:36 463第十四章 我是商鞅( ... -
礼乐文明与和谐社会的构建
2012-01-16 04:36 659礼乐文明与和谐社会的 ...
相关推荐
运行于单片机的PID程序,C51,就是个PID调整函数,推荐给大家。
增量式PID控制算法C51程序
C51程序+Proteus仿真 PID C51程序+Proteus仿真 PID
用51单片机写的PID算法控制电机的转身PWM。可以使用哈
用C51弄的一个整形变量来实现PID算法,由于是用整形来做的所以在精度上有所欠缺!
PID计算,讲解PID功能原理,具体源代码,初学者可以参照阅读,
增量式PID控制算法C51程序。从学校大神那里抄的。
基于c51单片机的三相异步电动机调速系统,有源程序,已编译无错误,并且在电机上实践过的哦!
单片机c51写的PID控制程序,下载试试吧
本文给大家分享了一个单片机PID程序源代码。
帮助你理解PID的两种算法 位置式PID 增量式PID 里面有2个例子
pid温度控制程序,采用C51开发自创,温度传感器为DS18B20
热电偶测温C51程序,可以同时测7路不同种类的热电偶,用一路铜电阻做零点补偿
真正要用PID算法的时候,发现书上的代码在我们51上来实现还不是那么容易的事情。简单的说来,就是不能直接调用。仔细分析你可以发现,教材上的、网上现行的PID实现 的C语言代码几乎都是用浮点型的数据来做的,可以...
直流电机单片机调速基于单片机直流电机pid调速程序
倒立摆C51程序,使用电机自选,根据自己的电机性能修改相应的参数,基本功能已经实现。
C51单片机实现温度数据的采集,通过串口传至上位机以及控制温度软件设计源代码,单片机实现温度传感器测温,通过串口在电脑端显示,功能实现:数码管显示同时通过串口发送到电脑端显示,注释详细,可供学习设计参考...
51单片机循迹小车PID算法.zip
单片机C51语言基于模糊控制的PID温度控制程序
实现了51单片机的pid算法,使用增量式PID算法,进行算法离散数字化。