Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
can u please tell how to decrease its freq...?
Set up an interrupt that executes at a constant interval.
OK, you mean to say the loop should not execute forever?
interrupt timer should be used?
... Are you just going to post stupid questions until somebody feels sorry for you and does all the work for you?
#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;
}
how to decrease micro controller freq without changing crystal.
yes i am using that on;ly but how to find exact timing i am searching.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.
T2CON = 0x04;
PR2 = 255; with 20Mhz