please help with change in code

Thread Starter

thecoolsundar

Joined Aug 12, 2009
7
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

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:

Thread Starter

thecoolsundar

Joined Aug 12, 2009
7
Header file

Rich (BB code):
//******************************************************
//****  WHEEL-CHAIR CONTROL MAIN - SOURCE FILE  *******
//******************************************************
//Controller    : ATmega128 (Crystal: 16MHz)
//Compiler        : winAVR (AVRStudio)
//Author        : CC Dharmani, Chennai (India)
//Version        : 1.0 (beta)
//Date            : Dec 2011
//******************************************************

#ifndef     _MAIN_H_
#define        _MAIN_H_


#define     LED_ON        PORTE |= 0x80
#define     LED_OFF        PORTE &= ~0x80

#define     DIR_CENTER         (dir >= (-150)) && (dir <= 150)
#define     VEL_CENTER         (vel >= (-150)) && (vel <= 150)

#define     VEL_RVS            vel < (-150)
#define     VEL_FWD            vel > 150

#define     DIR_LEFT        dir < (-150)
#define     DIR_RIGHT        dir > 150

#define     STOP_PWM1       TCCR1B = 0x00; TCCR1A = 0x00
#define        START_PWM1_RVS  TCCR1B = 0x09; TCCR1A = 0x23
#define     START_PWM1_FWD  TCCR1B = 0x09; TCCR1A = 0x83
#define     SET1_FORWARD    PORTD |= 0x01; PORTD &= ~0x02    //D1_AHI=1, D1_BHI=0
#define     SET1_REVERSE    PORTD &= ~0x01; PORTD |= 0x02    //D1_AHI=0, D1_BHI=1
#define     HIP1_ENABLE        PORTD &= ~0x04
#define     HIP1_DISABLE    PORTD |= 0x04
#define     STOP_MOTOR1        HIP1_DISABLE; STOP_PWM1

#define     STOP_PWM2       TCCR3B = 0x00; TCCR3A = 0x00
#define        START_PWM2_RVS  TCCR3B = 0x09; TCCR3A = 0x23
#define     START_PWM2_FWD  TCCR3B = 0x09; TCCR3A = 0x83
#define     SET2_FORWARD    PORTD |= 0x08; PORTD &= ~0x10   //D2_AHI=1, D2_BHI=0
#define     SET2_REVERSE    PORTD &= ~0x08; PORTD |= 0x10    //D2_AHI=0, D2_BHI=1
#define     HIP2_ENABLE        PORTD &= ~0x20
#define     HIP2_DISABLE    PORTD |= 0x20
#define     STOP_MOTOR2        HIP2_DISABLE; STOP_PWM2
    

#define     HALT        0
#define     RVS            1
#define     FWD            2

#define     STRAIGHT    0
#define        LEFT        1
#define        RIGHT        2

#define     ON            1
#define     OFF            0


#endif
 
Last edited by a moderator:

Thread Starter

thecoolsundar

Joined Aug 12, 2009
7
ADC

Rich (BB code):
//**********************************************************
//********** ADC ROUTINES - SOURCE FILE ***********
//**********************************************************
//Controller    : ATmega128 (Crystal: 16MHz)
//Compiler        : winAVR (AVRStudio)
//Author        : CC Dharmani, Chennai (India)
//Version        : 1.0 (Beta_2)
//Date            : Dec 2009
//**********************************************************

#include <avr/io.h>
#include "ADC_routines.h"


//**********************************************************
//Function     : Initialize the ADC, Conversion time: 52uS
//Arguments    : None
//Return    : None
//**********************************************************
void ADC_init(void)
{
  ADCSRA = 0x00; //disable adc
  ADMUX  = 0x40;  //select adc input 0, ref:AVCC
  ADCSRA = 0x86; //prescaler:32, single conversion mode
}


//************************************************************************
//Function    : Do an Analog to Digital Conversion & read the Digital value
//Arguments    : None
//Return    : Integer value of the 10-bit ADC register
//************************************************************************
int ADC_read(void)
{
    char i;
    int ADC_temp, ADCH_temp;
    int ADC_var = 0;
    
    ADC_ENABLE;
    
    ADC_START_CONVERSION;         //do a dummy readout first
    
    while(!(ADCSRA & 0x10));     // wait for conversion done, ADIF flag active
    
    ADCSRA|=(1<<ADIF);
        
    for(i=0;i<8;i++)             // do the ADC conversion 8 times for better accuracy 
    {
         ADC_START_CONVERSION;
        while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
        ADCSRA|=(1<<ADIF);
        
        ADC_temp = ADCL;         // read out ADCL register
        ADCH_temp = ADCH;        // read out ADCH register        
        ADC_temp +=(ADCH_temp << 8);
        ADC_var += ADC_temp;      // accumulate result (8 samples) for later averaging
    }

    ADC_var = ADC_var >> 3;       // average the 8 samples
        
    ADC_DISABLE;
    
    return ADC_var;
}

//*************************************************************
//Function    : To read ADC Channels 0, 1 (Joystick pots) & 
//              assign the values to global variables
//Arguments    : None
//Return    : None
//*************************************************************
void readPots(void)
{
   ADMUX = 0x40;  //channel 0
   dir = ADC_read() - 511;
 
   ADMUX = 0x41;  //channel 1
   vel = ADC_read() - 511;

}


//*************************************************************
//Function    : To read ADC Channel 4 (Power Supply Voltage)
//Arguments    : None
//Return    : Status of supply: 0 - Battery OK, 1 - Battery Low
//*************************************************************
unsigned char checkSupply(void)
{
   int value;
   float voltage;
      
   ADMUX = 0x44;  //channel 
   value = ADC_read();

   voltage = (value * 5.0 / 1024.0); //getting ADC-pin voltage (0-5v)

   voltage = voltage * 5.9;    //getting battery voltage (0-30v)

   if(voltage >= BATTERY_LIMIT) return 0;
   else return 1;
}


//*********************  END OF FILE **************************
 
Last edited by a moderator:

Thread Starter

thecoolsundar

Joined Aug 12, 2009
7
ADC header

Rich (BB code):
//******************************************************
//******* ADC ROUTINES - HEADER FILE *******
//******************************************************
//Controller    : ATmega128 (Crystal: 16MHz)
//Compiler        : winAVR (AVRStudio)
//Author        : CC Dharmani, Chennai (India)
//Version        : 1.0 (Beta_2)
//Date            : Dec 2009
//******************************************************

#ifndef     _ADC_ROUTINES_H_
#define     _ADC_ROUTINES_H_    

#define        BATTERY_LIMIT            12


#define     DIR_CENTER         (dir >= (-150)) && (dir <= 150)
#define     VEL_CENTER         (vel >= (-150)) && (vel <= 150)

#define     VEL_RVS            vel < (-150)
#define     VEL_FWD            vel > 150

#define     DIR_LEFT        dir < (-150)
#define     DIR_RIGHT        dir > 150


#define     ADC_ENABLE                 ADCSR |= (1<<ADEN)
#define     ADC_DISABLE             ADCSR &= 0x7F
#define     ADC_START_CONVERSION    ADCSR |= (1<<ADSC)


volatile int vel, dir; 

void ADC_init(void);
int ADC_read(void);
void readPots(void);
unsigned char checkSupply(void);


#endif
 
Last edited by a moderator:
Top