Connect with us

Need help with code.

Discussion in 'Microcontrollers, Programming and IoT' started by bigone5500, Jun 2, 2015.

Scroll to continue with content
  1. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I found this code online and want to try to build this clock. However, when I try to compile the code in arduino, I get errors. One is on line 7 and says 'initializer-string for array of chars is too long [-fpermissive]. I can change the 3 to a 6 and it goes past that. I then get an error on line 61 that says 'PORTA' was not declared in this scope. I'm thinking that I need to do something with a library but not sure.

    Code:
    #include<avr/io.h>
    #include<stdio.h>
    #define F_CPU 8000000UL
    #include<util/delay.h>
    #include<avr/interrupt.h>
    char value[3];
    char days[7][3]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
    char position[8]={0x00,0x88,0x85,0x8d,0xc5,0xc8,0xcb};
    int i,time;
    int j=0;
    
    void i2c_init()
    {
      TWSR=0x00;
      TWBR=0x47;
      TWCR=0x04;
    }
    
    
    void i2c_start()
    {
      TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
      while((TWCR &(1<<TWINT))==0);
    }
    
    void i2c_stop()
    {
      TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
    }
    
    void i2c_write(char data)
    {
      TWDR=data;
      TWCR=(1<<TWINT)|(1<<TWEN);
      while((TWCR &(1<<TWINT))==0);
    }
    
    unsigned char i2c_read()
    {
      TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
      while(!(TWCR &(1<<TWINT)));
      return TWDR;
    }
    
    
    void data(char *txt)
    {
      PORTB|=(1<<1);
      while(*txt!='\0')
      {          
      PORTA=*txt++;
      PORTB|=(1<<0);
      _delay_ms(1);
      PORTB&=~(1<<0);
      _delay_ms(1);
      }
    }
    
    void cmd(char comm)
    { 
       PORTA=comm;
       PORTB&=~(1<<1);
       PORTB|=(1<<0);
       _delay_ms(1);
       PORTB&=~(1<<0);
       _delay_ms(1);
    }
    
    void time_set(void)
    {
      i2c_init();
      i2c_start();
      i2c_write(0xD0);
      i2c_write(0x00);
      i2c_write(0x00);
      i2c_write(0x00);
      i2c_write(0x16);
      i2c_write(0x00);
      i2c_write(0x31);
      i2c_write(0x05);
      i2c_write(0x15);
      i2c_stop();
      _delay_ms(2);
    }
    
    
    void time_write()
    {
    if((i==2)|(i==4)|(i==5))
       {
        cmd(position[i]);
        data(value);
        data("-");
       }
      else if(i==3)
      {
        cmd(position[i]);
        data(&days[time][0]);
      }
    
      else if(i==7){}
    
      else
       {
        cmd(position[i]);
        data(value);
       }
    }
    
    
    int main(void)
    {
    DDRB=0x03;
    DDRA=0xff;
    DDRC=0x00;
    cmd(0x38);
    cmd(0x0c);
    cmd(0x01);
    cmd(0x80);
    data("TIME:");
    cmd(0xc0);
    data("DATE:");
    time_set();
    j=0;
    while(1)
       {
       i2c_start();
       i2c_write(0xD0);
       i2c_write(0x01);
       i2c_stop();
       _delay_ms(2);
       i2c_start();
       i2c_write(0xD1);
       for(i=1;i<=7;i++)
          {
          time=i2c_read();
          sprintf(&value[0],"%x",time&0xF0);
          sprintf(&value[1],"%x",time&0x0F);
          time_write();
          }
       i2c_stop();
       _delay_ms(2);
       }
    return 0;
    }
    Thanks.
     
  2. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I played around with it a bit more and found out that if I leave the '3' and change the double quotes to single, I do not get the first error.

    I still am trying to figure out how to get around the 'PORTA' was not declared in this scope issue.


    EDIT*

    Here's why i get the problem...LOL!!!!!
    http://www.saetechnologies.com/error-porta-was-not-declared-in-this-scope/
    Seems I have malware on my arduino. Possibly even spyware.
     
  3. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    I doubt it's malware... same error, but the error only presents itself in the compiler.
    Often 'where' something pops up is just as important as what is being said. This is not a 'Windows' error, but a compiler error correct?
     
  4. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    Can you please link the site you got the code from?
    Also , you are running windows correct? Can you tell us what version of the Arduino IDE you are running?

    http://stackoverflow.com/questions/17809083/how-to-compile-avr-code-in-arduino
     
  5. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I'm sorry Gryd3. I wasn't being serious. That was one of those sites that you get when you do a Google search which take your query and turn it into a web page that seems like it is what you are looking for. It tries to convince you that the information on the page relates to your issue and that they have just the solution you need to fix your ailment.

    I thought it was funny.

    Here's the site:
    http://www.gadgetronicx.com/digital-clock-using-avr-microcontroller/
    I'm running the latest IDE, 1.6.4.
    Windows 8.1 Pro x64.

    I slacked off for a while on my Arduino stuff as I am in the process of moving but I have found more time lately to get back into learning it.
     
    Last edited: Jun 6, 2015
  6. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    When I did the searching a while back I read that some Arduino don't actually have 'PORTA' and that you need to modify the code to use a different port.
    I could be wrong, what Arduino are you using... and you 'are' using an Arduino with AVR code correct?
     
  7. flat5

    flat5

    4
    1
    Jun 11, 2015
    I can not test the program but can remove the PORTA error message by

    char PORTA=*txt++;

    and

    char PORTA=comm;

    However I get another error message

    'DDRA' was not declared in this scope

    Can't easily give the line number because I added a setup() which did nothing useful :)
     
  8. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    Same question as Bigone... are you using an Arduino with AVR code? (If so , which arduino)
    Take a look at the Arduino you are using and ensure that PORTA actually exists.

    Look here : http://www.arduino.cc/en/Reference/PortManipulation
    Ports B, C, D only... no Port A hence, DDRA would not exist either.
     
  9. flat5

    flat5

    4
    1
    Jun 11, 2015
    Actually I was answering his question, or thought I was.
    It looked safe (codewise) to just assign a type.

    Thank you for explaining why PORTA is a problem but PORTB is not.
    "no Port A hence, DDRA would not exist either." Ahh.

    I used the Arduino compiler v.1.6.4 AVRISP mkII
     
    Gryd3 likes this.
  10. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    Thanks for the replies guys. I have taken to trying to find a sketch that I can use to get my 1602 LCD to work in I2C mode. I have yet to find a decent one that works with my hardware. Maybe I'm just too old to learn this stuff...
     
  11. flat5

    flat5

    4
    1
    Jun 11, 2015
    I'm 67 and discovered the Arduino about 6 months ago.
    Learning the Arduino compiler's 'C++'...slowly :)
     
  12. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    While I've read studies that the mind becomes less malleable with age, it only seems to be when the owner stops learning, it's hard to pick up again.
    Nothing is hard, just time consuming.
    So if you can't figure something out, look over the program and if there are parts you dont understand look them up. Each element is pretty simple, it's when you start to add it together that it starts to get complicated.
    I'm currently 27, and the only reason I find things difficult to learn is because I seem to have less free time to dedicate to playing with something.
     
  13. flat5

    flat5

    4
    1
    Jun 11, 2015
    "While I've read studies..."
    Lots of things are 'hard'.
    Intelligence & talent helps...and of course study & time at it (practice).
    Being old does not seem to help but may give one more free time.
     
    Last edited: Jun 12, 2015
  14. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I would just like to find a sketch that will integrate my I2C LCD and DS1307 to make a clock. My way of learning is to find something that works and dissect it. Then I can realize how it works and begin to understand how modifications can change the behavior. I would be very grateful if anyone has this long sought-after sketch.

    *sighs*...
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-