[ dakinet @ 24.03.2012. 11:31 ] @
Pokušavam da sročim IO modul sa atmelovim mikrokontrolerom koji treba da prima i izvršava naredbe.

Ulazi:
switch4, switch3, switch2, taster1

Izlazi:
LED1, LED2, LED3

Timer:
timer1=30min

1.
kada se aktivira prekidač SW4 (ON) --> pali se LED3
kada se aktivira prekidač SW3 (ON) --> pali se LED2

2.
kada se aktivira prekidač SW2 (ON) --> pali se LED1
Onemogućiti izlaze za LED3 i LED2 (moraju da budu isključene)

3.
Kada se stisne taster 1 , aktivira se izlaz na 30min
if pushbutton1 go (ON) --> light LED1 (30min)
i onemogućiti izlaze za LED3 i LED2 (moraju da budu isključene)



Large schematic

Code:
#define BUT1() (PORTB & ~0x01) == 0x00 
#define SW2()  (PORTB & ~0x02) == 0x00 
#define SW3()  (PORTB & ~0x04) == 0x00 
#define SW4()  (PORTB & ~0x08) == 0x00 

#define LED1ON()  PORTD |=  0x01 
#define LED1OFF() PORTD &= ~0x01 

#define LED2ON()  PORTD |=  0x02 
#define LED2OFF() PORTD &= ~0x02 

#define LED3ON()  PORTD |=  0x04 
#define LED3OFF() PORTD &= ~0x04 

unsigned char but1; 
unsigned char but1os; 
unsigned char but1l; 
unsigned char but1on; 

unsigned char sw2; 
unsigned char sw2os; 
unsigned char sw2l; 
unsigned char sw2on; 

unsigned char sw3; 
unsigned char sw3os; 
unsigned char sw3l; 
unsigned char sw3on; 

unsigned char sw4; 
unsigned char sw4os; 
unsigned char sw4l; 
unsigned char sw4on; 

unsigned int timer1800sec; 

//------------------------- 
void runloop(void){ 
char c; 

  cprintf("running   q=quit\n"); 
   c=0; 
   while(c != 'q'){ 
     if(kbhit()){ 
        c=getchar(); 
      } 
       
      but1= BUT1();          //read input 
      but1os=but1 && !but1l; //look for falling edge 
      but1l=but1;            //remember last pass 
       
      sw2= SW2(); 
      sw2os=sw2 && !sw2l; 
      sw2l=sw2; 
       
      sw3= SW3(); 
      sw3os=sw3 && !sw3l; 
      sw3l=sw3; 
       
      sw4= SW4(); 
      sw4os=sw4 && !sw4l; 
      sw4l=sw4; 
       
      if(sw4os) sw4on=1; 
      if(sw3os) sw3on=1; 
      if(but1os){ 
        but1on=1; 
         sw4on=0; 
         sw3on=0; 
      } 
       
      if(sw4on) LED3ON(); else LED3OFF(); 
      if(sw3on) LED2ON(); else LED2OFF(); 
       
      if(but1on){ 
        LED1ON(); 
      }else{ 
        LED1OFF(); 
      } 
       
      udtime(); //generate timing oneshots 
      if(but1on){ 
        if(os1sec){ 
           timer1800sec++; 
         } 
      }else{ 
        timer1800sec=0; 
      } 
       
      if(timer1800sec==1800){ 
        but1on=0; 
      } 
   } 

 


Nešto nije uredu kad kliknem na build u avr studiu izbaci mi tri greške:
../IOmodulBor.c:118: error: 'os1sec' undeclared (first use in this function)
../IOmodulBor.c:118: error: (Each undeclared identifier is reported only once
../IOmodulBor.c:118: error: for each function it appears in.)

Svaka pomoć dobrodošla
[ Nedeljko @ 24.03.2012. 14:03 ] @
Ja ne znam šta ti je os1sec, ali ako je bibliotečka promenljiva, onda fali odgovarajuće zaglavlje, a ako je bibliotečka funkcija, fale zagrade, a ako nije bibliotečki, morao si negde da ga definišeš.
[ dakinet @ 24.03.2012. 14:29 ] @
Ček evo ceo kod:
Code:
//compiled with iccv7avr
//runs on arduino uno 16mhz 38400bps

//Mar 23 12 Bob G 

//todo:

#include <iccioavr.h>
#include <avrdef.h> //stackcheck
#include <stdio.h>  //cprintf
#include <stdlib.h> //rand, abs
#include <string.h> //memset
#include <ctype.h>  //toupper

#define INTR_OFF() asm("CLI")
#define INTR_ON()  asm("SEI")

//------vars in bss-------
char junk;
unsigned char dispon;
//unsigned char passcnt;
unsigned char os100ms;
unsigned char os1sec;
unsigned char hr,min,sec;
//unsigned char rate;
unsigned int tics,ticsl;
int os100msaccum;
int os1secaccum;
int deltatms;
int deltatmsoff;

//------rom---------
__flash unsigned char banner[]={"freakstimer Mar 23 12\n"};

//--------------------
void port_init(void){

//  PORTA = 0x00; //pullups off
//  DDRA  = 0x00; //inputs
 
  PORTB = 0xFF;
  DDRB  = 0xFE;
 
  PORTC = 0xFF; 
  DDRC  = 0xFF;
 
  PORTD = 0xFF;
  DDRD  = 0xFF;
}

//--------------------
//UART0 initialize
// desired baud rate: 38400
// actual: baud rate:38462 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void){
  UCSR0B = 0x00; //disable while setting baud rate
  UCSR0A = 0x00;
//  UCSR0C = BIT(URSEL) | 0x06;
  UBRR0L = 0x19; //set baud rate lo 103dec=9600  0x19=38400@16
  UBRR0H = 0x00; //set baud rate hi
  UCSR0B = 0x18;
}

#if 0
//---------------------
//SPI initialisation
// clock rate: 115200hz
void spi_init(void){
  SPCR = 0x53; //setup SPI (slowest clock rate! 0x50 fastest)
  SPSR = 0x00; //setup SPI
}
#endif

//--------------------
//TIMER0 initialisation - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value:  1.000mSec (0.0%)
void timer0_init(void){
  TCCR0B = 0x00; //stop
  TCNT0 = 0x06; //set count
  OCR0A  = 0xFA;  //set compare
  TCCR0B = 0x03; //start timer
}

#if 0
//--------------------
//TIMER1 initialisation - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 31372Hz
// actual value: 31372.549Hz (0.0%)
void timer1_init(void){
  TCCR1B = 0x00; //stop
  TCNT1H = 0xFE; //setup
  TCNT1L = 0x02;
  OCR1AH = 0x01;
  OCR1AL = 0xFE;
  OCR1BH = 0x01;
  OCR1BL = 0xFE;
  ICR1H  = 0x01;
  ICR1L  = 0xFE;
  TCCR1A = 0xA0;
  TCCR1B = 0x01; //start Timer
}
#endif

#if 0
//--------------------
void adc_init(void){
//ADC initialize

  ADCSRA = 0x87;
}
#endif

//--------------------
void init_devices(void){

  CLI(); //disable all interrupts
  port_init();
//  spi_init();
  uart0_init();
  timer0_init();

  MCUCR = 0x00;
  TIMSK2 = 0x02; //timer interrupt sources t0 oc
  SEI(); //re-enable interrupts
}

//-------------------
void delnms(int n){
//delay n ms
int x;

  while(n--){
//    x=2400;       //empirically determined fudge factor  14 mhz
    x=2600;       //empirically determined fudge factor  16 mhz
//    x=2800;       //empirically determined fudge factor  18 mhz
//    x=3000;       //empirically determined fudge factor  20 mhz
    while(x--);
  }
}

//-------------------------
void initvars(void){
//init vars

}

//------RS232-------------------
unsigned char kbhit(void){
//return nonzero if char waiting  polled version
unsigned char b;

  b=0;
  if(UCSR0A & (1<<RXC0)) b=1;
  return b;
}

//-----------------------
int getchar(void){
//polled version... 
unsigned char c;

  while(!kbhit()){}; //wait for char
  c=UDR0;             //get char from usart
  return c;  
}

#if 0
//----------------------------
#define RXBUFSIZ 200
unsigned char rxbuf[RXBUFSIZ]; 
unsigned char rxindx,rxondx;   

//-------------------------
unsigned char kbhit(void){
//return nonzero if char waiting  intr version
unsigned char b;

  b=rxindx != rxondx;
  return b;
}

//-----------------------
int getchar(void){
//intr version... overrides version in library
char c;

  while(!kbhit()){}; //wait for char
  c=rxbuf[rxondx++]; //get char from rxbuf
  if(rxondx >= RXBUFSIZ) rxondx=0;
  return c;  
}

//------------------------------
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RX
void uart0_rx_isr(void){
//uart has received a character in UDR
char c;

  c=UDR;            //get char from usart
  rxbuf[rxindx++]=c; //put char in rxbuf
  if(rxindx >= RXBUFSIZ) rxindx=0;  
}
#endif

//-----------------------
int putc(char c){
//put char to usart0... dont add lf after cr

  while((UCSR0A & 0x20)==0){};    //0x20 is UDRE0. ==0 means data register full
  UDR0=c; //send char
  return c;
}

//----------------------  
int putchar(char c)    {
//adds lf after cr

  if(c=='\n'){
    putc('\r');
  }  
  putc(c);
  return c;
}

//--------------------
void crlf(void){
//send cr lf

  putchar('\n');
}

//------------------
void space(void){
//send space

  putc(' ');
}
  
//------------------
void spaces(char n){
//send n spaces

  while(n--)
    space();
}
  
#define ESC 0x1b
//----------------------  
void gotoxy(int x, int y){
//ansi cursor positioning sequence E[y;xH

  putc(ESC);
  putc('[');
  cprintf("%d",y);
  putc(';');
  cprintf("%d",x);
  putc('H');
}

//----------------------
void clrscr(void){
//clear ansi terminal screen

  putc(ESC);
  putc('[');
  putc('2');
  putc('J');
}

//----------------------
void homecrsr(void){
//home cursor

  putc(ESC);
  putc('[');
  putc('f');
}

//------------------
void initscreen(void){
//clear screen and print banner

  clrscr();
    homecrsr();
  cprintf(banner);
}

//-------------------------
unsigned char getche(void){
//get and echo a char from rs232
char c;

  c=getchar();
  putchar(c);
  return(c);
}

//-------------------------
unsigned char gethex(void){
//return a hex char from rs232
unsigned char b,c;

  b=0xff; //error return value
  c=toupper(getche());
  if(isxdigit(c)) b=c-0x30; //if c ok, cvt ascii digit to binary
  if((c>='A') && (c<='F')) b-=7;     //if c hexcvt ascii A to binary 10
  return(b);
}

//---------------------------
unsigned char getbyte(void){
//get 2 nibbles, return a binary byte from rs232
unsigned char n1,n2;

  n1=gethex();
  n2=gethex();
  return((n1 << 4) + n2);
}

//----------------------------
unsigned int getaddr(void){
//return addr from rs232
unsigned int th,tl;

  th=getbyte();
  tl=getbyte();
  return((th << 8) + tl);
}

//------debug subs-----------
void dump256ram(unsigned char *n){
//dump 16 rows of 16 bytes
unsigned char r,c;
unsigned char *p, *pa, ch;

  p=n;
  crlf();
  spaces(6);
  for(c=0; c < 16; c++){
    cprintf("%02x ",c);      //header
    if(c==7) space();
  }
  crlf();
  crlf();
  for(r=0; r < 16; r++){
    cprintf("%04x  ",p);     //print addr  at beg of line
    pa=p;                   //remember p for ascii
    for(c=0; c < 16; c++){
      cprintf("%02x ",*p++); //print hex
      if(c==7) space();
    }
    for(c=0; c < 16; c++){
      ch=*pa++;
      if((ch > 0x20) && (ch !=0x0a) && (ch != 0x8a)) //if printing char
        putc(ch);   //print ascii
      else  
        putc('.'); 
      if(c==7) space();
    }
    crlf();
  }
}

//---------------------------------
void dump256rom(__flash unsigned char *n){
//dump 16 rows of 16 bytes
unsigned char r,c,ch;
__flash unsigned char *p, *pa;

  p=n;
  crlf();
  spaces(6);
  for(c=0; c < 16; c++){
    cprintf("%02x ",c);      //header
    if(c==7) space();
  }
  crlf();
  crlf();
  for(r=0; r < 16; r++){
    cprintf("%04x  ",p);     //print addr  at beg of line
    pa=p;                   //remember p for ascii
    for(c=0; c < 16; c++){
      cprintf("%02x ",*p++); //print hex
      if(c==7) space();
    }
    for(c=0; c < 16; c++){
      ch=*pa++;
      if((ch > 0x20) && (ch !=0x0a) && (ch != 0x8a)) //if printing char
        putc(ch);   //print ascii
      else  
        putc('.'); 
      if(c==7) space();
    }
    crlf();
  }
}

//------------------
void examineram(void){
//ask for mem range and dump
unsigned char *from;
unsigned char c;

  cprintf("from:");
  from=(unsigned char *)getaddr();
  while(c!='q'){
    dump256ram(from);
    cprintf("np or q...");
    c=getchar();
    if(c=='n') from+=0x100;
    if(c=='p') from-=0x100;
  }  
}

//------------------
void examinerom(void){
//ask for mem range and dump
__flash unsigned char *from;
unsigned char c;

  cprintf("from:");
  from=(__flash unsigned char *)getaddr();
  while(c!='q'){
    dump256rom(from);
    cprintf("np or q...");
    c=getchar();
    if(c=='n') from+=0x100;
    if(c=='p') from-=0x100;
  }  
}

//------------------
void deposit(void){
//ask for addr and data
unsigned char *at;
unsigned char c;
unsigned char nh,nl;

  cprintf("at:");
  at=(unsigned char *)getaddr();
  while(1){
    cprintf(" %02x ",*at);
    nh=gethex();
    if(nh==0xff) return;
    nl=gethex();
    if(nl==0xff) return;
    c=((nh << 4) | nl);
    *at++=c;
  }
}

//------------------
void fill(void){
//ask for mem range and fill char and fill
unsigned char *from, *to, with;

  cprintf("from:");
  from=(unsigned char *)getaddr();
  cprintf(" to:");
  to=(unsigned char *)getaddr();
  cprintf(" with:");
  with=getbyte();
  memset(from,with,to-from);
}

//-----------------------------
void (* fn)(void); //fn prototype

void dojsr(void){
//ask for addr, jsr to it
__flash unsigned char *to;

  cprintf(" to:");
  to=(__flash unsigned char *)getaddr();
  fn=(void *)to;
  (*fn)();  //call function fn and returns
}

//------------------
void debugmenu(void){
//monitor type cmds

  cprintf("cmds:\n"
          " e examine\n"
          " d deposit\n"
          " f fill\n"
          " j jsr\n"
          " q quit\n"
                );
}

//------------------
void debugloop(void){
//examine and deposit mem regs
char c;

  debugmenu();
  while(c != 'q'){
    crlf();
    cprintf("cmd:");
    c=getche();
    crlf();
    switch(c){
      case 'e': examineram(); break;
      case 'c': examinerom(); break;
      case 'd': deposit(); break;
      case 'f': fill();    break;
      case 'j': dojsr();   break;
      default: debugmenu();
    }
  }
}
//----end of debug subs----------

//--------------------
void _StackOverflowed(char n){
//called from _Stackcheck()   must link this file first??

  INTR_OFF();
    if(n) putc('H'); else putc('S');
  while(1){};
}

__flash char spindat[4]={'-','\\','|','/'};
char spindex;
//------------------
void spin(void){

  spindex++;
  if(spindex > 3) spindex=0;
  cprintf("%c\r",spindat[spindex]);
}

#if 0
//----spi---------------------------
char readspi(void){
//return char from spi
char cnt,stat;

  cnt=0;
  do{
    stat=SPSR;
    cnt++;
    if(cnt==255){
      cprintf("spi rcv timeout ");
      break;
    }
  }while((stat & 0x80)==0);  //keep checking until done bit
  return(SPDR); //clears done flag
}

//--------------------------
void spiout8(char c){
//send c out spi
char cnt,stat;

  SPDR=c; //send data
  cnt=0;
  do{
    stat=SPSR;
      cnt++;    
    if(cnt==255){
      cprintf("spi xmt timeout! ");
      break;
    }
  }while((stat & 0x80)==0);
}
#endif

//---------------------
#pragma interrupt_handler timer0_compa_isr:iv_TIMER0_COMPA
void timer0_compa_isr(void){
//compare occured TCNT0=OCR0   1ms

  tics++;
}

//----------------------
void udtime(void){
//read tics, generate oneshots, update hr,min,sec
unsigned int tmptics;

  tmptics=tics;           //read copy in case int hits
  deltatms=tmptics-ticsl; //ms since last pass
  ticsl=tmptics;          //remember last pass

  os100msaccum+=deltatms;
  os100ms=0;
  if(os100msaccum >= 100){
    os100ms=1;
    os100msaccum -= 100;
  }      
  os1secaccum+=deltatms; //accumulate milliseconds
  os1sec=0;
  if(os1secaccum >= 1000){
    os1sec=1;
      os1secaccum -= 1000;
  }
  if(os1sec){
    sec++;
      if(sec > 59){
        min++;
        sec=0;
        if(min > 59){
          hr++;
            min=0;
            if(hr > 23){
              hr=0;
            }//hr
        }//min
      }//sec
  }//os1sec            
}

/*
Ulazi:
switch4, switch3, switch2, taster1

Izlazi:
LED1, LED2, LED3

Timer:
timer1=30min

1. 
kada se aktivira prekidač SW4 (ON) --> pali se LED3
kada se aktivira prekidač SW3 (ON) --> pali se LED2

2.
kada se aktivira prekidač SW2 (ON) --> pali se LED1
Onemogućiti izlaze za LED3 i LED2 (moraju da budu isključene)

3.
Kada se stisne taster 1 , aktivira se izlaz na 30min
if pushbutton1 go (ON) --> light LED1 (30min) 
i onemogućiti izlaze za LED3 i LED2 (moraju da budu isključene)
*/

#define BUT1() (PORTB & ~0x01) == 0x00
#define SW2()  (PORTB & ~0x02) == 0x00
#define SW3()  (PORTB & ~0x04) == 0x00
#define SW4()  (PORTB & ~0x08) == 0x00

#define LED1ON()  PORTD |=  0x01
#define LED1OFF() PORTD &= ~0x01

#define LED2ON()  PORTD |=  0x02
#define LED2OFF() PORTD &= ~0x02

#define LED3ON()  PORTD |=  0x04
#define LED3OFF() PORTD &= ~0x04

unsigned char but1;
unsigned char but1os;
unsigned char but1l;
unsigned char but1on;

unsigned char sw2;
unsigned char sw2os;
unsigned char sw2l;
unsigned char sw2on;

unsigned char sw3;
unsigned char sw3os;
unsigned char sw3l;
unsigned char sw3on;

unsigned char sw4;
unsigned char sw4os;
unsigned char sw4l;
unsigned char sw4on;

unsigned int timer1800sec;

//-------------------------
void runloop(void){
char c;

  cprintf("running   q=quit\n");
    c=0;
    while(c != 'q'){
      if(kbhit()){
          c=getchar();
        }
        
        but1= BUT1();          //read input
        but1os=but1 && !but1l; //look for falling edge
        but1l=but1;            //remember last pass
        
        sw2= SW2();
        sw2os=sw2 && !sw2l;
        sw2l=sw2;
        
        sw3= SW3();
        sw3os=sw3 && !sw3l;
        sw3l=sw3;
        
        sw4= SW4();
        sw4os=sw4 && !sw4l;
        sw4l=sw4;
        
        if(sw4os) sw4on=1;
        if(sw3os) sw3on=1;
        if(but1os){
          but1on=1;
            sw4on=0;
            sw3on=0;
        }
        
        if(sw4on) LED3ON(); else LED3OFF();
        if(sw3on) LED2ON(); else LED2OFF();
        
        if(but1on){
          LED1ON();
        }else{
          LED1OFF();
        }
        
        udtime(); //generate timing oneshots
        if(but1on){
          if(os1sec){
              timer1800sec++;
            }
        }else{
          timer1800sec=0;
        }
        
        if(timer1800sec==1800){
          but1on=0;
        }
    }
}

//--------------------------
void menu(void){
//cmds

  cprintf("cmds:\n"
          " r runloop\n"
          " m monitor cmds\n"
                );
}

//-----------------
void main(void){
//main program
char c;

  init_devices();
  initvars();
  initscreen(); //banner
  while(1){     //main loop
    menu();
    crlf();
    putc('>'); //prompt for input
    c=getche();
    crlf();
    switch(c){
      case 'r': runloop();  break;
      case 'm': debugloop();   break;
      default: menu();
    }
    _StackCheck();
  }
}
[ Nedeljko @ 25.03.2012. 22:00 ] @
I koje greške sad izbacuje?
[ dakinet @ 26.03.2012. 19:04 ] @
Izbacuje mi grešku da "cprintf" nešto brlja.
Pogledaj na slici molim te.


Velika slika