本文目录一览:
- 1、急!!!RBF的PID控制的C程序,最好可以在单片机上实现的那种,悬赏还会追加的
- 2、关于PIC的C语言编程中定时器方面的资料,哪里有啊?求关于定时器的一个详细的例子。
- 3、用c语言编写RBF神经网络程序
- 4、C语言复制文件
急!!!RBF的PID控制的C程序,最好可以在单片机上实现的那种,悬赏还会追加的
PIC单片机的你看一下
//*********************************************************************************
#include pic.h
#include pic16f684.h
#include math.h
#include stdlib.h
void Init();
void PID();
void Set_Constants();
bit flag1,do_PID,int_flag;
signed char en0, en1, en2, en3, term1_char, term2_char, off_set;
unsigned char temp;
short int temp_int;
unsigned short int ki, kd, kp;
signed int SumE_Min, SumE_Max, SumE, integral_term, derivative_term, un;
signed long Cn;
// __CONFIG _CP_OFF _CPD_OFF _BOD_OFF _MCLRE_ON _WDT_OFF _INTRC_OSC_NOCLKOUT _FCMEN_ON
//***************************************************************************
// Positional PID 256 Hz
//***************************************************************************
//***************************************************************************
//Main() - Main Routine
//***************************************************************************
void main()
{
Init(); //Initialize 12F629 Microcontroller
Set_Constants(); //Get PID coefficients ki, kp and kd
while(1) //Loop Forever
{
if(do_PID){
PID();
}
}
}
//***************************************************************************
//Init - Initialization Routine
//***************************************************************************
void Init()
{
PORTA = 0;
TRISA = 0b00101101; // Set RA4 and RA2 as outputs
PORTC = 0;
TRISC = 0b00000011; // Set RC0 and RC1 as inputs, rest outputs
CMCON0 = 0x07; // Disable the comparator
IRCF0 = 1; // Used to set intrc speed to 8 MHz
IRCF1 = 1; // Used to set intrc speed to 8 MHz
IRCF2 = 1; // Used to set intrc speed to 8 MHz
CCP1CON = 0b01001100; // Full bridge PWM forward
ECCPAS = 0; // Auto_shutdown is disabled for now
PR2 = 0x3F; // Sets PWM Period at 31.2 kHz
T2CON = 0; // TMR2 Off with no prescale
CCPR1L = 0; // Sets Duty Cycle to zero
TMR2ON = 1; // Start Timer2
ANSEL = 0b00110101; // Configure AN0,AN2,AN4 and AN5 as analog
VCFG = 0; // Use Vdd as Ref
ADFM = 1; // Right justified A/D result
ADCS0 = 1; // 16 TOSC prescale
ADCS1 = 0;
ADCS2 = 1;
CHS0 = 0; // Channel select AN0
CHS1 = 0;
CHS2 = 0;
ADON = 1; //Turn A/D on
en0 = en1 = en2 = en3 = term1_char = term2_char =0;
ki = kd = 0;
kp = off_set = 0;
temp_int = integral_term = derivative_term = un =0;
SumE_Max = 30000;
SumE_Min = 1 - SumE_Max;
do_PID = 1; // Allowed to do PID function
T0CS = 0; // Timer0 as timer not a counter
TMR0 = 10; // Preload value
PSA = 0; // Prescaler to Timer0
PS0 = 0; // Prescale to 32 = 256 Hz
PS1 = 0;
PS2 = 1;
INTCON = 0;
PIE1 = 0;
T0IE = 1; // Enable Timer0 int
GIE = 1;
return;
}
void PID() // The from of the PID is C(n) = K(E(n) + (Ts/Ti)SumE + (Td/Ts)[E(n) - E(n-1)])
{
integral_term = derivative_term = 0;
// Calculate the integral term
SumE = SumE + en0; // SumE is the summation of the error terms
if(SumE SumE_Max){ // Test if the summation is too big
SumE = SumE_Max;
}
if(SumE SumE_Min){ // Test if the summation is too small
SumE = SumE_Min;
} // Integral term is (Ts/Ti)*SumE where Ti is Kp/Ki
// and Ts is the sampling period
// Actual equation used to calculate the integral term is
// Ki*SumE/(Kp*Fs*X) where X is an unknown scaling factor
// and Fs is the sampling frequency
integral_term = SumE / 256; // Divide by the sampling frequency
integral_term = integral_term * ki; // Multiply Ki
integral_term = integral_term / 16; // combination of scaling factor and Kp
// Calculate the derivative term
derivative_term = en0 - en3;
if(derivative_term 120){ // Test if too large
derivative_term = 120;
}
if(derivative_term -120){ // test if too small
derivative_term = -120;
} // Calculate derivative term using (Td/Ts)[E(n) - E(n-1)]
// Where Td is Kd/Kp
// Actual equation used is Kd(en0-en3)/(Kp*X*3*Ts)
derivative_term = derivative_term * kd; // Where X is an unknown scaling factor
derivative_term = derivative_term 5; // divide by 32 precalculated Kp*X*3*Ts
if(derivative_term 120){
derivative_term = 120;
}
if(derivative_term -120){
derivative_term = -120;
}
// C(n) = K(E(n) + (Ts/Ti)SumE + (Td/Ts)[E(n) - E(n-1)])
Cn = en0 + integral_term + derivative_term; // Sum the terms
Cn = Cn * kp / 1024; // multiply by Kp then scale
if(Cn = 1000) // Used to limit duty cycle not to have punch through
{
Cn = 1000;
}
if(Cn = -1000)
{
Cn = -1000;
}
if(Cn == 0){ // Set the speed of the PWM
DC1B1 = DC1B1 = 0;
CCPR1L = 0;
}
if(Cn 0){ // Motor should go forward and set the duty cycle to Cn
P1M1 = 0; // Motor is going forward
temp = Cn;
if(temp^0b00000001){
DC1B0 = 1;
}
else{
DC1B0 = 0;
}
if(temp^0b00000010){
DC1B1 = 1;
}
else{
DC1B1 = 0;
}
CCPR1L = Cn 2; // Used to stop the pendulum from continually going around in a circle
off_set = off_set +1; // the offset is use to adjust the angle of the pendulum to slightly
if(off_set 55){ // larger than it actually is
off_set = 55;
}
}
else { // Motor should go backwards and set the duty cycle to Cn
P1M1 = 1; // Motor is going backwards
temp_int = abs(Cn); // Returns the absolute int value of Cn
temp = temp_int; // int to char of LS-Byte
if(temp^0b00000001){
DC1B0 = 1;
}
else{
DC1B0 = 0;
}
if(temp^0b00000010){
DC1B1 = 1;
}
else{
DC1B1 = 0;
}
CCPR1L = temp_int 2; // Used to stop the pendulum from continually going around in a circle
off_set = off_set -1;
if(off_set -55){
off_set = -55;
}
}
en3 = en2; // Shift error signals
en2 = en1;
en1 = en0;
en0 = 0;
do_PID = 0; // Done
RA4 = 0; // Test flag to measure the speed of the loop
return;
}
void Set_Constants()
{
ANS2 = 1; // Configure AN2 as analog
ANS4 = 1; // Configure AN4 as analog
ANS5 = 1; // Configure AN5 as analog
ADFM = 1; // Right justified A/D result
CHS0 = 0; // Channel select AN4
CHS1 = 0;
CHS2 = 1;
temp = 200; // Gives delay
while(temp){
temp--;
}
GODONE = 1;
while(GODONE);{
temp = 0; // Does nothing.....
}
ki = ADRESH 8; // Store the A/D result to Integral Constant
ki = ki + ADRESL;
CHS0 = 1; // Channel select AN5
CHS1 = 0;
CHS2 = 1;
temp = 200; // Gives delay
while(temp){
temp--;
}
GODONE = 1;
while(GODONE);{
temp = 0; // Does nothing.....
}
kd = ADRESH 8; // Store the A/D result to Differential Constant
kd = kd + ADRESL;
CHS0 = 0; // Channel select AN2
CHS1 = 1;
CHS2 = 0;
temp = 200; // Gives delay
while(temp){
temp--;
}
GODONE = 1;
while(GODONE);{
temp = 0; // Does nothing.....
}
kp = ADRESH 8; // Store the A/D result to Proportional Constant
kp = kp + ADRESL;
CHS0 = 0; // Channel select AN0
CHS1 = 0;
CHS2 = 0;
}
void interrupt Isr()
{
if(T0IFT0IE){
TMR0 = 10; // Preload value
T0IF = 0; // Clear Int Flag
// flag1 = (!flag1);
RA4 = 1;
temp_int = 0;
temp_int = ADRESH 8; // Store the A/D result with offset
temp_int = temp_int + ADRESL - 512;
en0 = temp_int + off_set/8; // Store to error function asuming no over-flow
do_PID = 1; // Allowed to do PID function
GODONE = 1; // Start next A/D cycle
}
else
{
PIR1 = 0;
RAIF = 0;
INTF = 0;
}
if(temp_int 180){ //Check if error is too large (positive)
DC1B0 = DC1B1 = 0; // Stop PWM
CCPR1L = 0;
en0 = en1 = en2 = en3 = term1_char = term2_char = off_set = 0; // Clear all PID constants
Cn = integral_term = derivative_term = SumE = RA4 = 0;
do_PID = 0; // Stop doing PID
}
if(temp_int -180){ //Check if error is too large (negative)
DC1B0 = DC1B1 = 0; // Stop PWM
CCPR1L = 0;
en0 = en1 = en2 = en3 = term1_char = term2_char = off_set = 0; // Clear all PID constants
Cn = integral_term = derivative_term = SumE = RA4 = 0;
do_PID = 0; // Stop doing PID
}
}
关于PIC的C语言编程中定时器方面的资料,哪里有啊?求关于定时器的一个详细的例子。
网上有一个名为《PIC16F877单片机编程实例教程》的电子文档,PDF格式的。这里有PIC16F877的定时器的C语言样例程序。如果找不到,留下邮箱号可以给你传。
给你一个我最近写的PIC16F886的定时器程序,只要在main函数里调用init_T1()就能操作定时器1:
void init_T1(void) //初始化定时器1
{
TMR1H = 0xF4;TMR1L = 47; //定时3mS
T1CON = 0; //初始化T1
TMR1IE = 1; //开定时器中断
INTCON = 0XC0; //开总中断和PEIE外设中断
TMR1ON = 1;
}
void interrupt T1(void)
{
if(TMR1IF)
{ TMR1IF = 0;
INTCON = 0; //关中断清标志位
TMR1IE = 0;
rbf = 1; //具体操作
}
}
但你要先清楚:PIC单片机有很多种类的,虽然指令和架构都一样。但在某些功能上有区别的。比如上述的877和886的T1定时器带门控功能,而PIC16F716的T1则不带门控功能。有的PIC单片机甚至没有T1和T2定时器。这都需要你自己去看PIC单片机对应的数据手册(也是PDF格式,在PIC的生产商MICROCHIP的网站上有中文版下载)
用c语言编写RBF神经网络程序
RBF网络能够逼近任意的非线性函数,可以处理系统内的难以解析的规律性,具有良好的泛化能力,并有很快的学习收敛速度,已成功应用于非线性函数逼近、时间序列分析、数据分类、模式识别、信息处理、图像处理、系统建模、控制和故障诊断等。
简单说明一下为什么RBF网络学习收敛得比较快。当网络的一个或多个可调参数(权值或阈值)对任何一个输出都有影响时,这样的网络称为全局逼近网络。由于对于每次输入,网络上的每一个权值都要调整,从而导致全局逼近网络的学习速度很慢。BP网络就是一个典型的例子。
如果对于输入空间的某个局部区域只有少数几个连接权值影响输出,则该网络称为局部逼近网络。常见的局部逼近网络有RBF网络、小脑模型(CMAC)网络、B样条网络等。
附件是RBF神经网络的C++源码。
C语言复制文件
不应对非文本文件使用fgetc等易受干扰的函数,建议用fread,fwrite读写二进制文件
#include
"stdio.h"
/*
保护硬盘,绝对不要一个字节一个字节复制
*/
#define
SIZEOFBUFFER
256*1024L
/*
缓冲区大小,默认为256KB
*/
long
filesize(FILE
*stream)
{
long
curpos,
length;
curpos
=
ftell(stream);
fseek(stream,
0L,
SEEK_END);
length
=
ftell(stream);
fseek(stream,
curpos,
SEEK_SET);
return
length;
}
int
copyfile(const
char*
src,const
char*
dest)
{
FILE
*fp1,*fp2;
int
fsize,factread;
static
unsigned
char
buffer[SIZEOFBUFFER];
fp1=fopen(src,"rb");
fp2=fopen(dest,"wb+");
if
(!fp1
||
!fp2)
return
0;
for
(fsize=filesize(fp1);fsize0;fsize-=SIZEOFBUFFER)
{
factread=fread(buffer,1,SIZEOFBUFFER,fp1);
fwrite(buffer,factread,1,fp2);
}
fclose(fp1);
fclose(fp2);
return
1;
}
int
main()
{
copyfile("file1.txt","file2.txt");
return
0;
}