Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: help needed ATMEGA16

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    6

    Default help needed ATMEGA16

    i'm trying to make an obstacle avoiding robot(auto.) using an ATMEGA16

    I'm using a couple of sensors....which give me floating point values for the cas of seeing an obstacle (0.15 v).....and not seeing any (4.56v){checked using a multi...)
    now, say i've connected them to A0.....my doubt is how to read the value from these sensors and act accordingly(say even an led toggle at port D2)
    i tried ....
    if(PINA0==1)
    PORTD=0xFF;
    else
    PORTD=0x00;

    but it seems it always goes in the else case......
    I'll be grateful for any help in troubleshootin this.....
    regards,
    Aakash

    P.s.....this is my first post at this group......i'm kinda high nd hopeful newbie..

  2. #2

    Default

    to read value from adc first u have to initialize the adc like the following code
    /* Activate ADC with Prescaler 16 --> 1Mhz/16 = 62.5kHz*/
    ADCSRA = _BV(ADEN) | _BV(ADPS2);
    ADMUX = 0;
    /*select ch0, with external AREF,ADCL having lower 8 bits and ADCH having upper two bits*/

    then u can read the value from selected channel using the following code
    unsigned int s1,s2;
    //Start conversion
    ADCSRA |= _BV(ADSC);
    // wait until converstion completed
    while (ADCSRA & _BV(ADSC) ) {};

    //take read value from adch and adcl to an integer no s2
    s1=ADCL;
    s2=ADCH;
    s1&=0x00ff;
    s2&=0x0003;
    s2<<=8;
    s2|=s1;
    after this the s2 will contain the read value it can be any thing from 0 to 1024 depending upon the voltage read (0 to 5 v);

  3. #3

    Default

    hey bmx

    Kindly post the complete code. which compiler are you using?

    in your case you need not necessarily use the ADC...since the values u said are in the logic 0 and 1 range.
    if u want to use the adc heres a good tutorial
    http://www.triindia.co.in/resources/?p=64

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    6

    Default

    ya, actually i also didn't want to use the ADC......as you said they're in fairly logical range of 0 and 1....
    I'm using gcc bundled with winavr-2007
    here's the code....it's still pretty raw only for a prototype....please tell me how to read if the sensor's high/low and act accordingly....
    thanks


    #include <avr/io.h>
    #include<avr/iom16.h>

    //defining macros

    #define HALT 0x00
    #define FWD 0x05
    #define REV 0x0A
    #define RT 0x09
    #define LT 0x06

    //Define functions
    //======================
    void ioinit(void); //Initializes IO
    //void delay_ms(uint16_t x); //General purpose delay
    //======================


    int main (void)
    {
    ioinit(); //Setup IO pins and defaults
    double c;
    int i;


    while(1)
    {
    //gen go forward
    PORTD=FWD;

    //OBSTACLE CASE

    if(PA0==1) //if front sensor is high
    {

    PORTD=HALT;
    while(PINA1==0) //second sensor is off
    {PORTD=RT;
    c++;
    }
    PORTD=HALT;
    while(PINA1==1)
    {PORTD=FWD;}
    PORTD=HALT;
    for(i=0;i<c;i++)
    {PORTD=LT;}
    PORTD=HALT;


    }
    }

    return(0);
    }

    void ioinit (void)
    {
    //1 = output, 0 = input
    //DDRB = 0b11111111; //All outputs
    DDRA = 0x00; //All inputs
    DDRD = 0xFF; //All outputs
    }

    /*General short delays
    void delay_ms(uint16_t x)
    {
    uint8_t y, z;
    for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 90 ; y++){
    for ( z = 0 ; z < 6 ; z++){
    asm volatile ("nop");
    }
    }
    }
    }*/

  5. #5

    Default

    /OBSTACLE CASE

    if(PA0==1) //if front sensor is high


    this should also be PINA0

    and i think in AVR series the ports are not bit addressable. u need to set individual bits by logical operations

    eg: to set pin 0 of PORTB
    PORTB= PORTB|0x01

    to set pin 4 of PORTB

    PORTB=PORTB|0x10

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    6

    Default

    first of all, thanks for the response
    but,
    I think u got me wrong....the problem is not with the assigning values..
    its only reading from the sensor that is creating problems......
    My question is just that how to determine if a pin is reading a logical high or low??
    that's the elusive thing for me right now, any help will be appreciated

  7. #7

    Default

    to check if pin(PINA1) is low (0) use following code

    if (bit_is_clear(PINA, 1))//check if pin no 1 ( PINA1 ) on port PINA is clear

    to check if pin is high (1)

    if (bit_is_set(PINA, 1))//check if pin no 1 ( PINA1 ) on port PINA is set

    here bit_is_clear(,) and bit_is_set(,) are the macros , to read more see avrlib documentation there are other such macros for looping like
    loop_until_bit_is_set(PIND, 0)

    to get more idea on using ports
    read

    http://winavr.scienceprog.com/avr-gc...inavr-gcc.html

  8. #8

    Default

    use the following macros

    #define BIT(x) (1 << (x)) //Set a particular bit mask
    #define CHECKBIT(x,b) x&b //Checks bit status
    #define SETBIT(x,b) x|=b; //Sets the particular bit
    #define CLEARBIT(x,b) x&=~b; //Sets the particular bit
    #define TOGGLEBIT(x,b) x^=b; //Toggles the particular bit

    eg:

    if(CHECKBIT(PINA,BIT(6))==1)
    {
    PORTB=0xff;
    }

  9. #9

    Default

    u can read the full port , PA0 won't read PortA0

    put this
    int a,b
    a=PINA
    b=a&0xfe(some what like this)


    I think you cannnot use if(PINA=1) or something like this, assign the port value to a variable(int a, a=PINA,if(a==1) ) and try(i don't remember this correctly)


    Bibin John
    www.bibinjohn.tk

  10. #10
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    6

    Default

    thanks all......i'll try all these variations nd see how it goes....thanks agn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •