Below is the code which my friend wrote, but unfortunately he's not working on the project now
Here's the problem. When i move the joystick to center, I want Ahi and bhi both on continously, instead of them being disabled. I think this might solve a problem, i'm having with inductive feedback and also brake the vehicle.
Thanks
Here's the problem. When i move the joystick to center, I want Ahi and bhi both on continously, instead of them being disabled. I think this might solve a problem, i'm having with inductive feedback and also brake the vehicle.
Thanks
Rich (BB code):
//******************************************************
//**** CONTROL MAIN - SOURCE FILE *******
//******************************************************
//Controller : ATmega128 (Crystal: 16MHz)
//Compiler : winAVR (AVRStudio)
//Author : CC Dharmani, Chennai (India)
//Version : 1.0 (beta)
//Date : Dec 2011
//******************************************************
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "main.h"
#include "ADC_routines.h"
//******************************************************
//******************************************************
void port_init(void)
{
PORTA = 0x00; //not used
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x60; //PB6,PB5: PWM o/p
PORTC = 0x00;
DDRC = 0x03; //PC0,PC1: Ultrasonic sensors; PC7: Inhibit signal
PORTD = 0x24;
DDRD = 0xFF; //PD6,PD7: breaks; re,aining signals for HIP drivers
PORTE = 0x80;
DDRE = 0x98; //PE7: LED; PE3,PE4: PWM o/p
PORTF = 0x00;
DDRF = 0x00; //reserved for ADC
PORTG = 0x00;
DDRG = 0x00; //not used
}
//******************************************************
//TIMER1 initialize - prescale:1
// WGM: 7) PWM 10bit fast, TOP=0x03FF
// desired value: 15KHz
// actual value: 15.625KHz (4.0%)
//******************************************************
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1 = 0x0000; //setup
OCR1A = 0x01ff;
OCR1B = 0x0000;
TCCR1A = 0xa3;
TCCR1B = 0x09; //start Timer
}
//******************************************************
//TIMER3 initialize - prescale:1
// WGM: 7) PWM 10bit fast, TOP=0x03FF
// desired value: 15KHz
// actual value: 15.625KHz (4.0%)
//******************************************************
void timer3_init(void)
{
TCCR3B = 0x00; //stop
TCNT3 = 0x0000; //setup
//OCR3A = 0x03ff;
//OCR3B = 0x03ff;
//TCCR3A = 0xa3;
TCCR3B = 0x09; //start Timer
}
//******************************************************
//call this routine to initialize all peripherals
//******************************************************
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
timer1_init();
timer3_init();
ADC_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
//sei(); //re-enable interrupts
//all peripherals are now initialized
}
//******************************************************
//******************************************************
int main(void)
{
init_devices();
OCR1B = 0;
OCR3B = 0;
STOP_MOTOR1;
STOP_MOTOR2;
while(1)
{
readPots();
if(DIR_CENTER)
{
if(VEL_CENTER)
{
STOP_MOTOR1;
STOP_MOTOR2;
OCR1A = 0;
OCR1B = 0;
OCR3A = 0;
OCR3B = 0;
}
else if(VEL_FWD)
{
vel = vel * 2;
OCR1A = vel;
OCR3A = vel;
SET1_FORWARD;
SET2_FORWARD;
START_PWM1_FWD;
START_PWM2_FWD;
HIP1_ENABLE;
HIP2_ENABLE;
}
else
{
vel = vel * (-2);
OCR1B = vel;
OCR3B = vel;
SET1_REVERSE;
SET2_REVERSE;
START_PWM1_RVS;
START_PWM2_RVS;
HIP1_ENABLE;
HIP2_ENABLE;
}
}
else if(DIR_RIGHT)
{
dir = dir * 2;
if(VEL_CENTER)
{
OCR1B = dir;
OCR3A = dir;
SET1_REVERSE;
SET2_FORWARD;
START_PWM1_RVS;
START_PWM2_FWD;
HIP1_ENABLE;
HIP2_ENABLE;
}
else if(VEL_FWD)
{
vel = vel * 2;
OCR3A = vel; //velocity of motor2 depends only on speed pot
vel = vel - dir;
if(vel<0) vel = 0;
OCR1A = vel; //motor1 has reduced velocity
SET1_FORWARD;
SET2_FORWARD;
START_PWM1_FWD;
START_PWM2_FWD;
HIP1_ENABLE;
HIP2_ENABLE;
}OCR3A = vel;
else
{
vel = vel * (-2);
OCR3B = vel; //velocity of motor2 depends only on speed pot
vel = vel - dir;
if(vel<0) vel = 0;
OCR1B = vel; //motor1 has reduced velocity
SET1_REVERSE;
SET2_REVERSE;
START_PWM1_RVS;
START_PWM2_RVS;
HIP1_ENABLE;
HIP2_ENABLE;
}
}
else
{
dir = dir * (-2);
if(VEL_CENTER)
{
OCR1A = dir;
OCR3B = dir;
SET1_FORWARD;
SET2_REVERSE;
START_PWM1_FWD;
START_PWM2_RVS;
HIP1_ENABLE;
HIP2_ENABLE;
}
else if(VEL_FWD)
{
vel = vel * 2;
OCR1A = vel; //velocity of motor1 depends only on speed pot
vel = vel - dir;
if(vel<0) vel = 0;
OCR3A = vel; //motor2 has reduced velocity
SET1_FORWARD;
SET2_FORWARD;
START_PWM1_FWD;
START_PWM2_FWD;
HIP1_ENABLE;
HIP2_ENABLE;
}
else
{
vel = vel * (-2);
OCR1B = vel; //velocity of motor1 depends only on speed pot
vel = vel - dir;
if(vel<0) vel = 0;
OCR3B = vel; //motor2 has reduced velocity
SET1_REVERSE;
SET2_REVERSE;
START_PWM1_RVS;
START_PWM2_RVS;
HIP1_ENABLE;
HIP2_ENABLE;
}
}
_delay_ms(50);
} //end of while(1)
return 0;
}
Last edited by a moderator: