GCC Through AVRStudio Problem

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all ,
I've got a problem with GCC project through AVRStudio
When tested with CodeVision , everything was allright
With GCC I can't see anything on HyperTerminal
1)It seems printf doesn't work and
2)Time_Cnt variable ,
is not correctly accepted as a global variable.
I use AVRStudio 4.18 & CodeVision 2.04.4a .
I've shortened the codes and they are attached .

best regards.

Here is The GCC Code

#include 
#include 
#include 
#define  Timer1_INT_Dis     TIMSK  = 0x00;
#define  Timer1_INT_En      TIMSK  = 0x04;

void Init_USART(void);
void Init_Timer1(void);
unsigned char Tim1_Delay(unsigned int delay);


unsigned int Time_Cnt;

int main()
{
	Init_Timer1();
	Init_USART();
	sei();
	printf("start...");

	Tim1_Delay(100);
	UDR='A';
    Tim1_Delay(100);
	UDR='B';
	Timer1_INT_Dis;

	while(1)
	{
		UDR='C';
	}
}

void Init_USART(void){
    // USART initialization
    // Communication Parameters: 8 Data, 1 Stop, No Parity
    // USART Receiver: On
    // USART Transmitter: On
    // USART Mode: Asynchronous
    // USART Baud Rate: 9600
    UCSRA=0x00;
    UCSRB=0x18;
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=0x4D;      
}     

unsigned char Tim1_Delay(unsigned int delay)
{
	Time_Cnt=0;
    Timer1_INT_En;
    
	do{
    }while(Time_Cnt < delay);
    
	Timer1_INT_Dis;
    return 0;
}
//TIMER1 INITIALIZATION
void Init_Timer1(void) 
{
    cli();
	TCCR1A=0x00;
    TCCR1B=0x04;
    //TCNT1 =(65536 - (1*12000000)/ 256000);//1ms
	TCNT1H=0xff;
    TCNT1L=0xef;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    
	Timer1_INT_Dis;

}

ISR(TIMER1_OVF_vect)
{               
	//TCNT1 =(65536 - (1*12000000)/ 256000);//1ms
	TCNT1H=0xff;
	TCNT1L=0xef;
	Time_Cnt++;
}

And Here is The CodeVision Code

#include 
#include 
#define  Timer1_INT_Dis     TIMSK  = 0x00;
#define  Timer1_INT_En      TIMSK  = 0x04;

void Init_USART(void);
void Init_Timer1(void);
unsigned char Tim1_Delay(unsigned int delay);


unsigned int Time_Cnt;

void main(void)
{
    Init_Timer1();
    Init_USART();
    #asm("sei")
    printf("start...");
    Tim1_Delay(100);
    UDR='A';
    Tim1_Delay(100);
    UDR='B';
    Timer1_INT_Dis;

    while(1)
    {
        UDR='C';
    }
}

void Init_USART(void){
    // USART initialization
    // Communication Parameters: 8 Data, 1 Stop, No Parity
    // USART Receiver: On
    // USART Transmitter: On
    // USART Mode: Asynchronous
    // USART Baud Rate: 9600
    UCSRA=0x00;
    UCSRB=0x18;
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=0x4D;      
}     

unsigned char Tim1_Delay(unsigned int delay)
{
    Time_Cnt=0;
    Timer1_INT_En;
    
    do{
    }while(Time_Cnt < delay);
    
    Timer1_INT_Dis;
    return 0;
}
//TIMER1 INITIALIZATION
void Init_Timer1(void) 
{
    #asm("cli");
    TCCR1A=0x00;
    TCCR1B=0x04;
    //TCNT1 =(65536 - (1*12000000)/ 256000);//1ms
    TCNT1H=0xff;
    TCNT1L=0xef;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    
    Timer1_INT_Dis;
}

interrupt[TIM1_OVF] void TIM1_OVF_interupt(void)
{               
    //TCNT1 =(65536 - (1*12000000)/ 256000);//1ms
    TCNT1H=0xff;
    TCNT1L=0xef;
    Time_Cnt++;
}

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I've got a problem with GCC project through AVRStudio
When tested with CodeVision , everything was allright
With GCC I can't see anything on HyperTerminal
1)It seems printf doesn't work and
2)Time_Cnt variable ,
is not correctly accepted as a global variable.

Both programs are seriously wrong.
You should only write to UDR when the USART has space in its buffer (UDRE bit is set)
avr-gcc needs FDEV_xxx() macros before you an use printf()
CodeVision can use prinf() immediately.

Having got your functions 'associated' with the USART, just use putchar(). Never touch UDR at all.

avr-gcc will not know that Time_Cnt ever alters. Declare it volatile.

If you want an accurate timer, use the CTC mode.
In your case you might just as well use delay_ms() or _delay_ms(). i.e. you are just wasting time, and doing nothing else.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Suggest you start by reading the GCC user manual.

Bottom line is that by default Codevision has the putchar() that printf() uses connected to a UART_putchar() routine. In GCC this connection is not made automatically by default. It's up to you, the programmer to make the connection.

As GCC comes from a Linux heritage then all output is done via "file streams". So you need to setup a file stream that connects to a UART output routine and then connect that file stream to "stdout" which is the standard file stream that GCC programs use for output.

So try something like:

#include  
#include  
#include  
#define  Timer1_INT_Dis     TIMSK  = 0x00; 
#define  Timer1_INT_En      TIMSK  = 0x04; 

void Init_USART(void); 
void Init_Timer1(void); 
unsigned char Tim1_Delay(unsigned int delay); 


unsigned int Time_Cnt; 

int uart_putc(char c, FILE *unused) {
#ifdef __AVR_ATmega168__
	while (!(UCSR0A & (1<<UDRE0)));
	UDR0 = c;
#else
	while (!(UCSRA & (1<<UDRE)));
	UDR = c;
#endif
	return 0;
}

FILE uart_str = FDEV_SETUP_STREAM(uart_putc, NULL, _FDEV_SETUP_WRITE);

int main() 
{ 
   Init_Timer1(); 
   Init_USART(); 
   stdout = &uart_str; // printf to UART
   sei(); 
   printf("start..."); 

   Tim1_Delay(100); 
   UDR='A'; 
    Tim1_Delay(100); 
   UDR='B'; 
   Timer1_INT_Dis; 

   while(1) 
   { 
      UDR='C'; 
   } 
} 

void Init_USART(void){ 
    // USART initialization 
    // Communication Parameters: 8 Data, 1 Stop, No Parity 
    // USART Receiver: On 
    // USART Transmitter: On 
    // USART Mode: Asynchronous 
    // USART Baud Rate: 9600 
    UCSRA=0x00; 
    UCSRB=0x18; 
    UCSRC=0x86; 
    UBRRH=0x00; 
    UBRRL=0x4D;      
}      

unsigned char Tim1_Delay(unsigned int delay) 
{ 
   Time_Cnt=0; 
    Timer1_INT_En; 
    
   do{ 
    }while(Time_Cnt < delay); 
    
   Timer1_INT_Dis; 
    return 0; 
} 
//TIMER1 INITIALIZATION 
void Init_Timer1(void) 
{ 
    cli(); 
   TCCR1A=0x00; 
    TCCR1B=0x04; 
    //TCNT1 =(65536 - (1*12000000)/ 256000);//1ms 
   TCNT1H=0xff; 
    TCNT1L=0xef; 
    ICR1H=0x00; 
    ICR1L=0x00; 
    OCR1AH=0x00; 
    OCR1AL=0x00; 
    OCR1BH=0x00; 
    OCR1BL=0x00; 
    
   Timer1_INT_Dis; 

} 

ISR(TIMER1_OVF_vect) 
{                
   //TCNT1 =(65536 - (1*12000000)/ 256000);//1ms 
   TCNT1H=0xff; 
   TCNT1L=0xef; 
   Time_Cnt++; 
} 

To understand how that works read this page in the user manual:

http://www.nongnu.org/avr-libc/u...

BTW change your while(1) to call:

uart_putc('C', NULL);

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks a lot ,
added the word volatile , the timer is accurate now .
And astonishingly printf is working properly .
With your help , both of my problems were solved .
BTW, Sorry , I just used UDR in that way to simply show my problem .

When I see some really useful guys like david.prentice and clawson ,
I get ashamed .
What a nightmare is my life !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When accessing 16 bit registers, let the compiler do the heavy lifting and save yourself some time. Ex:

    TCNT1H=0xff;
    TCNT1L=0xef;

Naw, man !

TCNT1=0xFFEF;

Yea, man !!

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Thu. Dec 16, 2010 - 10:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
CodeVision 2.04.4a .
Did you buy it or got a cracked copy? :roll:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

indianajones11 wrote:
When accessing 16 bit registers, let the compiler do the heavy lifting and save yourself some time. Ex:

    TCNT1H=0xff;
    TCNT1L=0xef;

Naw, man !

TCNT1=0xFFEF;

Yea, man !!


As I said these codes were just for test .
It worked with CodeVision and that was enough .
BTW,I intentionally added the comment below :
"//TCNT1 =(65536 - (1*12000000)/ 256000);//1ms"
Yea, man !!
js wrote:

Did you buy it or got a cracked copy?

As you know , cracked one . [then this goes no further - moderator (CJL)]

Topic locked