Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LFR with PID

Status
Not open for further replies.
OK, you mean to say the loop should not execute forever?
interrupt timer should be used?

Go to school for couple of years and then try again.
 
I have linked 5 different sources of perfectly good information about control systems and I have suggested that you slow down the PID update frequency.. you can do that by setting up a timed interrupt that executes at a constant (controlled) rate. Have you checked any of the links I posted? Have you tried to do anything to improve your code as I suggested? Are you just waiting for somebody to write code for you? Are you just going to post stupid questions until somebody feels sorry for you and does all the work for you?
 
... Are you just going to post stupid questions until somebody feels sorry for you and does all the work for you?

That M.O. seems to have paid off up to now...
 
I have written code with ISR....but not working....


Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

 int turnr,turnl,kp,tp,offsetr,offsetl,errorl;
 int errorr,powerr,powerl;
	int number;
	int number1;
int ReadADC(unsigned char ch);
void main(void){
	ADCON0=0b01000001;//000 = channel 0, (RA0/AN0)
	ADCON1=0b10000000;
TRISC=0X00;
unsigned int ch,b;
	T2CON = 0x1e;	
	PR2 = 129;							// timer preload value
	TMR2IE = 1;							// enable timer 2 interrupt
	PEIE = 1;							// enable peripheral interrupt
	GIE = 1;	



	PR2 = 0b11111001 ;
	T2CON = 0b00000100 ;   
	CCP1CON = 0b00111100; 
CCP2CON = 0b00111100;                  
	PORTC = 0; 


while(1){

  	__delay_ms(50);
CCPR2L =  powerl;  //pwm1 change                 
  	CCPR1L =powerr;  //pwm1 change                       
   if(powerl>0){
	

RC4=0;//forward a
RC3=1;
RC0=1;//forward b
RC6=0;
}
else{
powerl= powerl* (-1);
RC4=1;//forward a
RC3=0;
RC0=0;//forward b
RC6=1;

}

}
}





int ReadADC(unsigned char ch)
	{
	int ret = 0;
	ADCON0 = 0b10000001 + (ch<<3);	// set channel to read
	__delay_ms(5);
	GO_DONE = 1;					// start conversion
   	while(GO_DONE);					// wait for conversion
   	ret =  (ADRESH & 0x3) << 8;		// get
   	ret +=	ADRESL;					// result
	return ret;

	}


void interrupt ISR()		// This just swaps the buffer to the display
	{
	if(TMR2IF)				// make sure its the timer interrupt.
		{
number = ReadADC(0);	//sensor 1
number1 = ReadADC(1);//sensor 2
kp = 1000;                             
offsetr = 1;//1.67;sensor average value on black and wwhite
offsetl = 1;//1.57;                            
tp = 155;//random set pwm duty cycle
      
   errorr = number - offsetr;    
   errorl = number1 - offsetl;     
   turnr = kp * errorr;                     
   turnr = turnr/100 ;
   turnl = kp * errorl;                    
   turnl= turnl/100;               
   powerl = tp + turnr;  //pwm1 change                 
  powerr = tp - turnl;  //pwm1 change 
}
	TMR2IF = 0;
}
 
Hi,
i have found something unusual in LFR without using PID i am using L298 with single motor, total two motor of few mA.
I am using 20Mhz crystal with 877A and as we now the rate/freq of changing output is very very fast due to which the L298 get hot may be due to the operating frequency/i.e. changing of sensor value to output.
so, i want to now can we change the freq of uC without changing crystal or should i use 4Mhz crystal?
 
I was learning timer2 interrupt it is working fine but how to calculate at what time period the isr will be called?
at Pre scalier/post 1:1 or any point?
 
You can use the combination of the prescaler and PR2 register (Timer2 Period Register). I do not want to write any code or math for you. You need to figure things out on your own, so that you'll understand what you are doing. Read the datasheet. Timer2 documentation starts from page 61.
 
The code I gave you for the LED array used timer 2.... In an interrupt.... Well documented.... Shows hoe to use PR2 pre and post scalers....
yes i am using that on;ly but how to find exact timing i am searching.
 
yes i am using that on;ly but how to find exact timing i am searching.

Do you read the datasheet? Because it would be great to refer all info to the datasheet.. We just need to know if you read it or not.
 
At 20MHz you get a 5MHz cycle.... Thus 200nS time as 1/5000000 = 0.0000002.

So count the value in PR2 ( this is the pre-load of the timer ) 255 * 0.0000002 = 0.000051 or 51μS... That is 19607.84Hz or 19.60784KHz

If you pre-scale then you multiply this by 4 and 16 respectively

If you post-scale then you multiply the result of the previous calculation by 1 though 16 respectively.


So you are interrupting every 51μS as you have a maximum count and no scaling atall...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top