当前位置:首页 > 单片机 > 单片机
[导读]//主程序======================================================#include "Main.h"#include "PID_f1.h"/* ............................................................... *//* ..............................

//主程序======================================================
#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 < PID_MIN)
{
Control_new = PID_MIN;
}
}

// Store 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 -------------------------------------------------
-*------------------------------------------------------------------*/


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

单片机内部有很多的特殊功能寄存器,每个寄存器在单片机内部都分配有唯一的地址,一般我们会根据寄存器功能的不同给寄存器赋予各自的名称,当我们需要在程序中操作这些特殊功能寄存器时,必须要在程序的最前面将这些名称加以声明,声明的...

关键字: C51 数据类型 扩充定义

数据元(Data Element),也称为数据元素,是用一组属性描述其定义、标识、表示和允许值的数据单元,在一定语境下,通常用于构建一个语义正确、独立且无歧义的特定概念语义的信息单元。数据元可以理解为数据的基本单元,将若...

关键字: C51 数据类型

▼点击下方名片,关注公众号▼欢迎关注【玩转单片机与嵌入式】公众号,回复关键字获取更多免费资料。回复【加群】,限时免费进入知识共享群;回复【3D封装库】,常用元器件的3D封装库;回复【电容】,获取电容、元器件选型相关的内容...

关键字: C51 MDK RealView

在Keil C51软件中51单片机的中断服务和外设驱动程序的开发

关键字: keil5 编译 C51

Intel公司1980年推出了MCS-51系列单片机:集成 8位CPU、4K字节ROM、128字节RAM、4个8位并口、1个全双工串行口、2个16位定时/计数器。寻址范围64K,并有控制功能较强的布尔处理器。 80C5...

关键字: C51 KEIL 编程

c上标3下标5怎么算用计算机,c上标3下标5怎么算

关键字: C51 KEIL

▼点击下方名片,关注公众号▼大家好,很高兴和各位一起分享我的第16篇原创文章,喜欢和支持我的工程师,一定记得给我点赞、收藏、分享。加微信[xyzn3333]与作者沟通交流,免费获取更多单片机与嵌入式的海量电子资料。很多初...

关键字: 51单片机 C51

常看见初学者要求使用_at_,这是一种谬误,把C当作ASM看待了。在C中变量的定位是编译器的事情,初学者只要定义变量和变量的作 用域,编译器就把一个固定地址给这个变量。

关键字: C51 单片机 误区 注意事项

简介:编程首要是要考虑程序的可行性,然后是可读性、可移植性、健壮性以及可测试性。这是总则。但是很多人忽略了可读性、可移植性和健壮性(可调试的方法可能歌不相同),这是不对的。

关键字: C51 编程规范 文件配置

如果你用 Keil C51 进行编译,记住一点:它不区分大小写!!!卧槽,今天编程序那个调错啊,就因为一个数组名和一个变量名完全一样,只是大小写不一样罢了,标准 C 我怎么记得这样可以啊……上网一查,卧槽,Keil C5...

关键字: C51 单片机 编程要点
关闭