please help with change in code

Discussion in 'The Projects Forum' started by thecoolsundar, Jun 21, 2012.

  1. thecoolsundar

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1.  
    2. //******************************************************
    3. //****  CONTROL MAIN - SOURCE FILE  *******
    4. //******************************************************
    5. //Controller    : ATmega128 (Crystal: 16MHz)
    6. //Compiler        : winAVR (AVRStudio)
    7. //Author        : CC Dharmani, Chennai (India)
    8. //Version        : 1.0 (beta)
    9. //Date            : Dec 2011
    10. //******************************************************
    11.  
    12. #include <avr/io.h>
    13. #include <util/delay.h>
    14. #include <avr/interrupt.h>
    15. #include "main.h"
    16. #include "ADC_routines.h"
    17.  
    18. //******************************************************
    19.  
    20. //******************************************************
    21. void port_init(void)
    22. {
    23.  PORTA = 0x00; //not used
    24.  DDRA  = 0x00;
    25.  PORTB = 0x00;
    26.  DDRB  = 0x60; //PB6,PB5: PWM o/p
    27.  PORTC = 0x00;
    28.  DDRC  = 0x03; //PC0,PC1: Ultrasonic sensors; PC7: Inhibit signal
    29.  PORTD = 0x24;
    30.  DDRD  = 0xFF; //PD6,PD7: breaks; re,aining signals for HIP drivers
    31.  PORTE = 0x80;
    32.  DDRE  = 0x98; //PE7: LED; PE3,PE4: PWM o/p
    33.  PORTF = 0x00;
    34.  DDRF  = 0x00; //reserved for ADC
    35.  PORTG = 0x00;
    36.  DDRG  = 0x00; //not used
    37. }
    38.  
    39. //******************************************************
    40. //TIMER1 initialize - prescale:1
    41. // WGM: 7) PWM 10bit fast, TOP=0x03FF
    42. // desired value: 15KHz
    43. // actual value: 15.625KHz (4.0%)
    44. //******************************************************
    45. void timer1_init(void)
    46. {
    47.  TCCR1B = 0x00; //stop
    48.  TCNT1 = 0x0000; //setup
    49.  OCR1A  = 0x01ff;
    50.  OCR1B  = 0x0000;
    51.  TCCR1A = 0xa3;
    52.  TCCR1B = 0x09; //start Timer
    53. }
    54.  
    55.  
    56. //******************************************************
    57. //TIMER3 initialize - prescale:1
    58. // WGM: 7) PWM 10bit fast, TOP=0x03FF
    59. // desired value: 15KHz
    60. // actual value: 15.625KHz (4.0%)
    61. //******************************************************
    62. void timer3_init(void)
    63. {
    64.  TCCR3B = 0x00; //stop
    65.  TCNT3 = 0x0000; //setup
    66.  //OCR3A  = 0x03ff;
    67.  //OCR3B  = 0x03ff;
    68.  //TCCR3A = 0xa3;
    69.  TCCR3B = 0x09; //start Timer
    70. }
    71.  
    72. //******************************************************
    73. //call this routine to initialize all peripherals
    74. //******************************************************
    75. void init_devices(void)
    76. {
    77.  //stop errant interrupts until set up
    78.  cli(); //disable all interrupts
    79.  XDIV  = 0x00; //xtal divider
    80.  XMCRA = 0x00; //external memory
    81.  port_init();
    82.  timer1_init();
    83.  timer3_init();
    84.  ADC_init();
    85.  
    86.  MCUCR = 0x00;
    87.  EICRA = 0x00; //extended ext ints
    88.  EICRB = 0x00; //extended ext ints
    89.  EIMSK = 0x00;
    90.  TIMSK = 0x00; //timer interrupt sources
    91.  ETIMSK = 0x00; //extended timer interrupt sources
    92.  //sei(); //re-enable interrupts
    93.  //all peripherals are now initialized
    94. }
    95.  
    96. //******************************************************
    97.  
    98. //******************************************************
    99. int main(void)
    100. {
    101.  
    102.  init_devices();
    103.  
    104.     OCR1B = 0;
    105.     OCR3B = 0;
    106.  
    107.      STOP_MOTOR1;
    108.      STOP_MOTOR2;
    109.  
    110.  while(1)
    111.  {
    112.  
    113.     readPots();
    114.  
    115.     if(DIR_CENTER)
    116.     {
    117.         if(VEL_CENTER)
    118.         {
    119.             STOP_MOTOR1;
    120.              STOP_MOTOR2;
    121.             OCR1A = 0;
    122.             OCR1B = 0;
    123.             OCR3A = 0;
    124.             OCR3B = 0;
    125.         }
    126.         else if(VEL_FWD)
    127.         {
    128.               vel = vel * 2;
    129.             OCR1A = vel;
    130.             OCR3A = vel;
    131.             SET1_FORWARD;
    132.             SET2_FORWARD;
    133.             START_PWM1_FWD;
    134.             START_PWM2_FWD;
    135.             HIP1_ENABLE;
    136.             HIP2_ENABLE;
    137.         }
    138.         else
    139.         {
    140.               vel = vel * (-2);
    141.             OCR1B = vel;
    142.             OCR3B = vel;
    143.             SET1_REVERSE;
    144.             SET2_REVERSE;
    145.             START_PWM1_RVS;
    146.             START_PWM2_RVS;
    147.             HIP1_ENABLE;
    148.             HIP2_ENABLE;
    149.         }
    150.      }
    151.  
    152.     else if(DIR_RIGHT)
    153.     {
    154.         dir = dir * 2;
    155.  
    156.         if(VEL_CENTER)
    157.         {
    158.             OCR1B = dir;
    159.             OCR3A = dir;
    160.             SET1_REVERSE;
    161.             SET2_FORWARD;
    162.             START_PWM1_RVS;
    163.             START_PWM2_FWD;
    164.             HIP1_ENABLE;
    165.             HIP2_ENABLE;
    166.         }
    167.         else if(VEL_FWD)
    168.         {
    169.               vel = vel * 2;
    170.             OCR3A = vel;    //velocity of motor2 depends only on speed pot
    171.             vel = vel - dir;
    172.             if(vel<0) vel = 0;
    173.             OCR1A = vel;    //motor1 has reduced velocity
    174.             SET1_FORWARD;
    175.             SET2_FORWARD;
    176.             START_PWM1_FWD;
    177.             START_PWM2_FWD;
    178.             HIP1_ENABLE;
    179.             HIP2_ENABLE;
    180.         }OCR3A = vel;
    181.         else
    182.         {
    183.               vel = vel * (-2);
    184.             OCR3B = vel;    //velocity of motor2 depends only on speed pot
    185.             vel = vel - dir;
    186.             if(vel<0) vel = 0;
    187.             OCR1B = vel;    //motor1 has reduced velocity
    188.             SET1_REVERSE;
    189.             SET2_REVERSE;
    190.             START_PWM1_RVS;
    191.             START_PWM2_RVS;
    192.             HIP1_ENABLE;
    193.             HIP2_ENABLE;
    194.         }
    195.     }
    196.  
    197.     else
    198.     {
    199.         dir = dir * (-2);
    200.         if(VEL_CENTER)
    201.         {
    202.             OCR1A = dir;
    203.             OCR3B = dir;
    204.             SET1_FORWARD;
    205.             SET2_REVERSE;
    206.             START_PWM1_FWD;
    207.             START_PWM2_RVS;
    208.             HIP1_ENABLE;
    209.             HIP2_ENABLE;
    210.         }
    211.         else if(VEL_FWD)
    212.         {
    213.               vel = vel * 2;
    214.             OCR1A = vel;    //velocity of motor1 depends only on speed pot
    215.             vel = vel - dir;
    216.             if(vel<0) vel = 0;
    217.             OCR3A = vel;    //motor2 has reduced velocity
    218.             SET1_FORWARD;
    219.             SET2_FORWARD;
    220.             START_PWM1_FWD;
    221.             START_PWM2_FWD;
    222.             HIP1_ENABLE;
    223.             HIP2_ENABLE;
    224.         }
    225.         else
    226.         {
    227.               vel = vel * (-2);
    228.             OCR1B = vel;    //velocity of motor1 depends only on speed pot
    229.             vel = vel - dir;
    230.             if(vel<0) vel = 0;
    231.             OCR3B = vel;    //motor2 has reduced velocity
    232.             SET1_REVERSE;
    233.             SET2_REVERSE;
    234.             START_PWM1_RVS;
    235.             START_PWM2_RVS;
    236.             HIP1_ENABLE;
    237.             HIP2_ENABLE;
    238.         }
    239.     }
    240.  
    241.     _delay_ms(50);
    242.  } //end of while(1)
    243.  
    244.  return 0;
    245. }
    246.  
     
    Last edited by a moderator: Jun 21, 2012
  2. thecoolsundar

    Thread Starter New Member

    Aug 12, 2009
    7
    0
    Header file

    Code ( (Unknown Language)):
    1.  
    2. //******************************************************
    3. //****  WHEEL-CHAIR CONTROL MAIN - SOURCE FILE  *******
    4. //******************************************************
    5. //Controller    : ATmega128 (Crystal: 16MHz)
    6. //Compiler        : winAVR (AVRStudio)
    7. //Author        : CC Dharmani, Chennai (India)
    8. //Version        : 1.0 (beta)
    9. //Date            : Dec 2011
    10. //******************************************************
    11.  
    12. #ifndef     _MAIN_H_
    13. #define        _MAIN_H_
    14.  
    15.  
    16. #define     LED_ON        PORTE |= 0x80
    17. #define     LED_OFF        PORTE &= ~0x80
    18.  
    19. #define     DIR_CENTER         (dir >= (-150)) && (dir <= 150)
    20. #define     VEL_CENTER         (vel >= (-150)) && (vel <= 150)
    21.  
    22. #define     VEL_RVS            vel < (-150)
    23. #define     VEL_FWD            vel > 150
    24.  
    25. #define     DIR_LEFT        dir < (-150)
    26. #define     DIR_RIGHT        dir > 150
    27.  
    28. #define     STOP_PWM1       TCCR1B = 0x00; TCCR1A = 0x00
    29. #define        START_PWM1_RVS  TCCR1B = 0x09; TCCR1A = 0x23
    30. #define     START_PWM1_FWD  TCCR1B = 0x09; TCCR1A = 0x83
    31. #define     SET1_FORWARD    PORTD |= 0x01; PORTD &= ~0x02    //D1_AHI=1, D1_BHI=0
    32. #define     SET1_REVERSE    PORTD &= ~0x01; PORTD |= 0x02    //D1_AHI=0, D1_BHI=1
    33. #define     HIP1_ENABLE        PORTD &= ~0x04
    34. #define     HIP1_DISABLE    PORTD |= 0x04
    35. #define     STOP_MOTOR1        HIP1_DISABLE; STOP_PWM1
    36.  
    37. #define     STOP_PWM2       TCCR3B = 0x00; TCCR3A = 0x00
    38. #define        START_PWM2_RVS  TCCR3B = 0x09; TCCR3A = 0x23
    39. #define     START_PWM2_FWD  TCCR3B = 0x09; TCCR3A = 0x83
    40. #define     SET2_FORWARD    PORTD |= 0x08; PORTD &= ~0x10   //D2_AHI=1, D2_BHI=0
    41. #define     SET2_REVERSE    PORTD &= ~0x08; PORTD |= 0x10    //D2_AHI=0, D2_BHI=1
    42. #define     HIP2_ENABLE        PORTD &= ~0x20
    43. #define     HIP2_DISABLE    PORTD |= 0x20
    44. #define     STOP_MOTOR2        HIP2_DISABLE; STOP_PWM2
    45.    
    46.  
    47. #define     HALT        0
    48. #define     RVS            1
    49. #define     FWD            2
    50.  
    51. #define     STRAIGHT    0
    52. #define        LEFT        1
    53. #define        RIGHT        2
    54.  
    55. #define     ON            1
    56. #define     OFF            0
    57.  
    58.  
    59. #endif
    60.  
     
    Last edited by a moderator: Jun 21, 2012
  3. thecoolsundar

    Thread Starter New Member

    Aug 12, 2009
    7
    0
    ADC

    Code ( (Unknown Language)):
    1.  
    2. //**********************************************************
    3. //********** ADC ROUTINES - SOURCE FILE ***********
    4. //**********************************************************
    5. //Controller    : ATmega128 (Crystal: 16MHz)
    6. //Compiler        : winAVR (AVRStudio)
    7. //Author        : CC Dharmani, Chennai (India)
    8. //Version        : 1.0 (Beta_2)
    9. //Date            : Dec 2009
    10. //**********************************************************
    11.  
    12. #include <avr/io.h>
    13. #include "ADC_routines.h"
    14.  
    15.  
    16. //**********************************************************
    17. //Function     : Initialize the ADC, Conversion time: 52uS
    18. //Arguments    : None
    19. //Return    : None
    20. //**********************************************************
    21. void ADC_init(void)
    22. {
    23.   ADCSRA = 0x00; //disable adc
    24.   ADMUX  = 0x40;  //select adc input 0, ref:AVCC
    25.   ADCSRA = 0x86; //prescaler:32, single conversion mode
    26. }
    27.  
    28.  
    29. //************************************************************************
    30. //Function    : Do an Analog to Digital Conversion & read the Digital value
    31. //Arguments    : None
    32. //Return    : Integer value of the 10-bit ADC register
    33. //************************************************************************
    34. int ADC_read(void)
    35. {
    36.     char i;
    37.     int ADC_temp, ADCH_temp;
    38.     int ADC_var = 0;
    39.    
    40.     ADC_ENABLE;
    41.    
    42.     ADC_START_CONVERSION;         //do a dummy readout first
    43.    
    44.     while(!(ADCSRA & 0x10));     // wait for conversion done, ADIF flag active
    45.    
    46.     ADCSRA|=(1<<ADIF);
    47.        
    48.     for(i=0;i<8;i++)             // do the ADC conversion 8 times for better accuracy
    49.     {
    50.          ADC_START_CONVERSION;
    51.         while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
    52.         ADCSRA|=(1<<ADIF);
    53.        
    54.         ADC_temp = ADCL;         // read out ADCL register
    55.         ADCH_temp = ADCH;        // read out ADCH register        
    56.         ADC_temp +=(ADCH_temp << 8);
    57.         ADC_var += ADC_temp;      // accumulate result (8 samples) for later averaging
    58.     }
    59.  
    60.     ADC_var = ADC_var >> 3;       // average the 8 samples
    61.        
    62.     ADC_DISABLE;
    63.    
    64.     return ADC_var;
    65. }
    66.  
    67. //*************************************************************
    68. //Function    : To read ADC Channels 0, 1 (Joystick pots) &
    69. //              assign the values to global variables
    70. //Arguments    : None
    71. //Return    : None
    72. //*************************************************************
    73. void readPots(void)
    74. {
    75.    ADMUX = 0x40;  //channel 0
    76.    dir = ADC_read() - 511;
    77.  
    78.    ADMUX = 0x41;  //channel 1
    79.    vel = ADC_read() - 511;
    80.  
    81. }
    82.  
    83.  
    84. //*************************************************************
    85. //Function    : To read ADC Channel 4 (Power Supply Voltage)
    86. //Arguments    : None
    87. //Return    : Status of supply: 0 - Battery OK, 1 - Battery Low
    88. //*************************************************************
    89. unsigned char checkSupply(void)
    90. {
    91.    int value;
    92.    float voltage;
    93.      
    94.    ADMUX = 0x44;  //channel
    95.    value = ADC_read();
    96.  
    97.    voltage = (value * 5.0 / 1024.0); //getting ADC-pin voltage (0-5v)
    98.  
    99.    voltage = voltage * 5.9;    //getting battery voltage (0-30v)
    100.  
    101.    if(voltage >= BATTERY_LIMIT) return 0;
    102.    else return 1;
    103. }
    104.  
    105.  
    106. //*********************  END OF FILE **************************
    107.  
     
    Last edited by a moderator: Jun 21, 2012
  4. thecoolsundar

    Thread Starter New Member

    Aug 12, 2009
    7
    0
    ADC header

    Code ( (Unknown Language)):
    1.  
    2. //******************************************************
    3. //******* ADC ROUTINES - HEADER FILE *******
    4. //******************************************************
    5. //Controller    : ATmega128 (Crystal: 16MHz)
    6. //Compiler        : winAVR (AVRStudio)
    7. //Author        : CC Dharmani, Chennai (India)
    8. //Version        : 1.0 (Beta_2)
    9. //Date            : Dec 2009
    10. //******************************************************
    11.  
    12. #ifndef     _ADC_ROUTINES_H_
    13. #define     _ADC_ROUTINES_H_    
    14.  
    15. #define        BATTERY_LIMIT            12
    16.  
    17.  
    18. #define     DIR_CENTER         (dir >= (-150)) && (dir <= 150)
    19. #define     VEL_CENTER         (vel >= (-150)) && (vel <= 150)
    20.  
    21. #define     VEL_RVS            vel < (-150)
    22. #define     VEL_FWD            vel > 150
    23.  
    24. #define     DIR_LEFT        dir < (-150)
    25. #define     DIR_RIGHT        dir > 150
    26.  
    27.  
    28. #define     ADC_ENABLE                 ADCSR |= (1<<ADEN)
    29. #define     ADC_DISABLE             ADCSR &= 0x7F
    30. #define     ADC_START_CONVERSION    ADCSR |= (1<<ADSC)
    31.  
    32.  
    33. volatile int vel, dir;
    34.  
    35. void ADC_init(void);
    36. int ADC_read(void);
    37. void readPots(void);
    38. unsigned char checkSupply(void);
    39.  
    40.  
    41. #endif
    42.  
     
    Last edited by a moderator: Jun 21, 2012
Loading...