NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » error help

July 21, 2010
by cinta51
cinta51's Avatar

ok I made this program for a clock and then to have a buzzer go off at certain times but not sure why I am getting these errors when tring to load it.

// realtimeclock1.c // for NerdKits with ATmega168 // mrobbins@mit.edu

define F_CPU 14745600

define FEED_HOUR 12

define FEED_MIN 0

define ALERT_SECONDS 10 /how many seconds should the buzzer sound /

include <stdio.h>

include <stdlib.h>

include <avr/io.h>

include <avr/interrupt.h>

include <avr/pgmspace.h>

include <inttypes.h>

include "../libnerdkits/delay.h"

include "../libnerdkits/lcd.h"

include "../libnerdkits/uart.h"

// PIN DEFINITIONS: void realtimeclock_setup() { // setup Timer0: // CTC (Clear Timer on Compare Match mode) // TOP set by OCR0A register TCCR0A |= (1<<WGM01); // clocked from CLK/1024 // which is 14745600/1024, or 14400 increments per second TCCR0B |= (1<<CS02) | (1<<CS00); // set TOP to 143 // because it counts 0, 1, 2, ... 142, 143, 0, 1, 2 ... // so 0 through 143 equals 144 events OCR0A = 143; // enable interrupt on compare event // (14400 / 144 = 100 per second) TIMSK0 |= (1<<OCIE0A); }

// the_time will store the elapsed time // in hundredths of a second. // (100 = 1 second) // // note that this will overflow in approximately 248 days! // // This variable is marked "volatile" because it is modified // by an interrupt handler. Without the "volatile" marking, // the compiler might just assume that it doesn't change in // the flow of any given function (if the compiler doesn't // see any code in that function modifying it -- sounds // reasonable, normally!). // // But with "volatile", it will always read it from memory // instead of making that assumption. volatile int32_t the_time;

SIGNAL(SIG_OUTPUT_COMPARE0A) { // when Timer0 gets to its Output Compare value, // one one-hundredth of a second has elapsed (0.01 seconds). the_time++; }

void speaker_off() { TCCR1A &= ~(1<<COM1A0); }

void speaker_on() { TCCR1A |= (1<<COM1A0); }

int main() { realtimeclock_setup(); uint8_t hours=0; uint8_t minutes=0; uint8_t seconds=0; // init lcd lcd_init(); FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE); lcd_home();

// init serial port uart_init(); FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); stdin = stdout = &uart_stream;

// Set the 6 pins to input mode - Two 3 bit numbers

DDRA &= ~(1<<PA0); // set PA0 as input DDRA &= ~(1<<PA1); // set PA1 as input DDRA &= ~(1<<PA2); // set PA2 as input DDRA &= ~(1<<PA3); // set PA3 as input DDRA &= ~(1<<PA6); // set PA0 as input DDRA &= ~(1<<PA7); // set PA0 as input

// turn on the internal resistors for the pins

PORTA |= (1<<PA0); // turn on internal pull up resistor for PA0 PORTA |= (1<<PA1); // turn on internal pull up resistor for PA1 PORTA |= (1<<PA2); // turn on internal pull up resistor for PA2 PORTA |= (1<<PA3); // turn on internal pull up resistor for PA3 PORTA |= (1<<PA6); // turn on internal pull up resistor for PA6 PORTA |= (1<<PA7); // turn on internal pull up resistor for PA7

// declare the variables to represent each bit, of our two 3 bit numbers uint8_t h1n=0,h1p=0; uint8_t h2n=0,h2p=0; uint8_t m1n=0,m1p=0; uint8_t m2n=0,m2p=0; uint8_t s1n=0,s1p=0; uint8_t s2n=0,s2p=0;

uint8_t buzzer=0; / we set buzzer =1 when buzzer is active and 0 when inactive / /we use it to check when to turn off the buzzer /

// turn on interrupt handler sei();

while(1) {

/* CODE to set the time
When we set one pin on dip switch to ON
we are adding a digit to the position of the clock that it represends
For example h1 is the position of the first digit that represends the hours
For 19:07:01
h1 is 1
h2 is 9
We use h1n and h1p to check what was the previous state of the switch.
If we didn't use it then at every cycle of the while loop we would be increasing the hours
We only want to increase when the switch moves from OFF to ON and only once for this change.
To increase again the switch must go to OFF and then ON again.
*/
h1n = (PINA & (1<<PA0)) >> PA0;
if(h1n == 1 && h1p == 0){ /* switch went from OFF to ON */
    h1p=1;
    hours=hours+10; /* we add 10 instead of 1 because it is the first digit*/
}
if(h1n == 0 && h1p ==1) /* switch went from ON to OFF */
    h1p=0;

h2n = (PINA & (1<<PA1)) >> PA1;
if(h2n == 1 && h2p == 0){
    h2p=1;
    hours=hours+1;
}
if(h2n == 0 && h2p ==1)
    h2p=0;

m1n = (PINA & (1<<PA2)) >> PA2;
if(m1n == 1 && m1p == 0){
    m1p=1;
    minutes=minutes+10;
}
if(m1n == 0 && m1p ==1)
    m1p=0;

m2n = (PINA & (1<<PA3)) >> PA3;
if(m2n == 1 && m2p == 0){
    m2p=1;
    minutes=minutes+1;
}
if(m2n== 0 && m2p ==1)
    m2p=0;

s1n = (PINA & (1<<PA6)) >> PA6;
if(s1n == 1 && s1p == 0){
    s1p=1;
    seconds=seconds+10;
}
if(s1n == 0 && s1p ==1)
    s1p=0;

s2n = (PINA & (1<<PA7)) >> PA7;
if(s2n == 1 && s2p == 0){
    s2p=1;
    seconds=seconds+1;
}
if(s2n== 0 && s2p ==1)
    s2p=0;

/* END of dip switches code section for setting the time */

/* when the_time is equal to 100 then one second has passed */
if(the_time==100){
    seconds++;
    the_time=0; /* we reset in order to count up to 100 again for the next second */
}

if(seconds>=60){ /* we checks if seconds are >= 60 because the dip switch might increase over 60 */
    minutes++;
    seconds=seconds-60; /* we subtrace 60 in case seconds were 54 for example and we set dipswitch for s1 to ON and have added 10 seconds. 54+10=64. So we increase minutes by 1 and 64-60 we are back to 4 seconds*/
}
if(minutes>=60){
    hours++;
    minutes=minutes-60;
}
if(hours>=24){
    hours=hours-24;
}
lcd_home();
fprintf_P(&lcd_stream, PSTR("Time is %2d:%2d:2d\n"), hours,minutes,seconds);
if(!buzzer && hours == FEED_HOURS && minutes == FEED_MINUTES && seconds <= ALERT_SECONDS){
    buzzer=1;
    speaker_on();
    fprintf_P(&lcd_stream, PSTR("Feeding time!!!\n"));
}
else if( buzzer && seconds > ALERT_SECONDS){
    buzzer=0;
    speaker_off();
    fprintf_P(&lcd_stream, PSTR("Timer: %9.2f sec"), (double) the_time / 100.0);
}
else{
    fprintf_P(&lcd_stream, PSTR("Timer: %9.2f sec"), (double) the_time / 100.0);
}
//fprintf_P(&lcd_stream, PSTR("Time is %2d:%2d:2d"), hours,minutes,seconds);
//fprintf_P(&lcd_stream, PSTR("Timer: %9.2f sec"), (double) the_time / 100.0);

}

return 0; } clockWithInputAndBuzzer.c:89: error: 'DDRA' undeclared (first use in this function) clockWithInputAndBuzzer.c:89: error: (Each undeclared identifier is reported only once clockWithInputAndBuzzer.c:89: error: for each function it appears in.) clockWithInputAndBuzzer.c:89: error: 'PA0' undeclared (first use in this function) clockWithInputAndBuzzer.c:90: error: 'PA1' undeclared (first use in this function) clockWithInputAndBuzzer.c:91: error: 'PA2' undeclared (first use in this function) clockWithInputAndBuzzer.c:92: error: 'PA3' undeclared (first use in this function) clockWithInputAndBuzzer.c:93: error: 'PA6' undeclared (first use in this function) clockWithInputAndBuzzer.c:94: error: 'PA7' undeclared (first use in this function) clockWithInputAndBuzzer.c:98: error: 'PORTA' undeclared (first use in this function) clockWithInputAndBuzzer.c:134: error: 'PINA' undeclared (first use in this function) clockWithInputAndBuzzer.c:203: error: 'FEED_HOURS' undeclared (first use in this function) clockWithInputAndBuzzer.c:203: error: 'FEED_MINUTES' undeclared (first use in this function) make.exe: *** [clockWithInputAndBuzzer.hex] Error 1

July 22, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi cinta51,

If you make sure there are at least four spaces at the beginning of all your code lines it will format it as code, and it will be much more readable.

You are getting these errors because you are attempting to use pins that do not exist on your chip. The PINA bank, along with the DDRA register does not exist on the ATmega168. You need to map the pin registers you are using to the actual names of the pins being utilized in your circuit.

The other errors I think are just spelling errors. You make a reference to FEED_HOURS and FEED_MINUTES when you actually defined FEED_HOUR and FEED_MIN in your code.

Humberto

July 24, 2010
by cinta51
cinta51's Avatar

thankyou very much, will work on the code more and see what happens

Post a Reply

Please log in to post a reply.

Did you know that essentially all power supplies' voltages drop when current is drawn from them? Learn more...