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.
Thanks.
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.