avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] Atmega2560


From: david
Subject: Re: [avr-libc-dev] Atmega2560
Date: Mon, 14 Nov 2005 19:57:35 -0800 (PST)

Duh - the atmega2560 has a fuse bit for "Divide clock
by 8".  This was set, I unset it, and I managed with
some mods to get the uart to work.


Thanks to all that wrote me with suggestions.

Next step is interrupts - I believe I have to modify
the file gcrt1.S
in avr-libc and maybe something else there to get a
interrupt vector table big enough for the atmega2560. 


dave


--- david <address@hidden> wrote:

> I have a atmega2560 with the stk500 w/ 503 adapter.
> 
> I'm trying to get something to work.
> 
> I've produced a simple program - one that reads the
> switches and lights the opposite LED (i.e pressing
> switch 7 lights LED 0, pressing switch 7 lights LED
> 1,
> etc) .  It also prints out the switch number that
> was
> pressed.
> 
> I developed the original program on an atmega128. 
> It
> appears to work fine on that target.  I realize that
> just because a program runs - doesn't mean it is
> correct - I could have errors in my code.
> 
> For the atmega2560, I compiled the program for the
> atmega128, but replaced the iom128.h file with one
> for
> the atmega1280.  I avoided passing parameters on the
> stack - in fact I only call a single procedure - the
> one to transmit a byte to the uart.  My transmit
> routine polls - I use no interrupts - so I should
> not
> have to worry about having an incorrect interrupt
> vector table.
> 
> My code appears to run.  The LEDs light when the
> switches are pressed.  I get output from the UART -
> but it is garbage characters (periods as viewed from
> a
> minicom window).  I don't think I have a problem
> with
> the baudrate calculation verses the fuse bits
> settings.  I have fusebits set up use an external
> clock and use the clock on the stk500 (same as I do
> with the atmega128).
> 
> Can anyone point out what I am doing wrong?
> 
> Any suggestions and wisdom will be appreciated.  My
> goal is to eventually get a working setup with an
> atmega2560 using gcc and avrlibc.  I am taking baby
> steps.
> 
> dave
> 
> 
> #ifndef UCSZ0
> #define UCSZ0 UCSZ00
> #define RXC RXC0
> #define UDRE UDRE0
> #endif
> 
> #include <avr/io.h>        // I/O definitions (port
> names, pin names, etc)
> 
> #ifndef BV
>         #define BV(bit)                 (1<<(bit))
> #endif
> 
> #ifndef cbi
>         #define cbi(reg,bit)    reg &= ~(BV(bit))
> #endif
> #ifndef sbi
>         #define sbi(reg,bit)    reg |= (BV(bit))
> #endif
> 
> typedef unsigned long  u32;
> typedef unsigned char  u08;
> 
> // CPU clock speed
> //#define F_CPU        16000000                               //
> 16MHz processor
> //#define F_CPU        14745000                               //
> 14.745MHz processor
> //#define F_CPU        8000000                        //
> 8MHz
> processor
> //#define F_CPU        7372800                        //
> 7.37MHz processor
> //#define F_CPU        4000000                        //
> 4MHz
> processor
> #define F_CPU        3686400                          //
> 3.69MHz processor
> #define CYCLES_PER_US ((F_CPU+500000)/1000000)        //
> cpu
> cycles per microsecond
> 
> #define SW0()               ((PINA >> 0) & 1)
> #define SW1()               ((PINA >> 1) & 1)
> #define SW2()               ((PINA >> 2) & 1)
> #define SW3()               ((PINA >> 3) & 1)
> #define SW4()               ((PINA >> 4) & 1)
> #define SW5()               ((PINA >> 5) & 1)
> #define SW6()               ((PINA >> 6) & 1)
> #define SW7()               ((PINA >> 7) & 1)
> 
> #define SET_LED0()          cbi(PORTB,0)
> #define SET_LED1()          cbi(PORTB,1)
> #define SET_LED2()          cbi(PORTB,2)
> #define SET_LED3()          cbi(PORTB,3)
> #define SET_LED4()          cbi(PORTB,4)
> #define SET_LED5()          cbi(PORTB,5)
> #define SET_LED6()          cbi(PORTB,6)
> #define SET_LED7()          cbi(PORTB,7)
> 
> #define CLR_LED0()          sbi(PORTB,0)
> #define CLR_LED1()          sbi(PORTB,1)
> #define CLR_LED2()          sbi(PORTB,2)
> #define CLR_LED3()          sbi(PORTB,3)
> #define CLR_LED4()          sbi(PORTB,4)
> #define CLR_LED5()          sbi(PORTB,5)
> #define CLR_LED6()          sbi(PORTB,6)
> #define CLR_LED7()          sbi(PORTB,7)
> 
> #define SWITCH_ON 0
> #define SWITCH_OFF  1
> 
> 
> 
> /* Initialize UART */
> u32 baudrate;
> u08 baudrateDiv;
> void InitUART( )
> {
> 
>      baudrate = 9600;
>      baudrateDiv =
> (u08)((F_CPU+(baudrate*8L))/(baudrate*16L)-1);
>   
>      UBRR0H = baudrateDiv >> 8;
>      UBRR0L = baudrateDiv;
> 
>      UCSR0B = (1 << RXEN0) | (1<<TXEN0);
> 
>      UCSR0C = (3<<UCSZ0);
> }
> 
> 
> unsigned char data;
> void transmitByte( )
> {
> 
>    while (!(UCSR0A & (1<<UDRE)))
>     ;
>    UDR0 = data;
> 
>    data = 'A';
>    while (!(UCSR0A & (1<<UDRE)))
>     ;
>    UDR0 = 0xa;
> 
>    data = 'A';
>    while (!(UCSR0A & (1<<UDRE)))
>     ;
>    UDR0 = 0xd;
> }
> 
> int main(void)
> {
> 
>    /* Setup Digital IO */
>    DDRA = 0x00;  /* Port a (all pins) is input  */
>    DDRB = 0xFF;  /* Port c (all pins) is output */
>    PORTB = 0xFF;
> 
>    InitUART();
>    data = 'A';
>    transmitByte();
> 
> 
> while(1)
>    {
> 
>       if (SW0() == SWITCH_ON)
>       {
>          data = '0';
>          transmitByte();
>          SET_LED7();
>       }
>       else
>          CLR_LED7();
> 
>       if (SW1() == SWITCH_ON)
>       {
>          data = '1';
>          transmitByte();
>          SET_LED6();
>       }
>       else
>          CLR_LED6();
> 
>       if (SW2() == SWITCH_ON)
>       {
>          data = '2';
>          transmitByte();
>          SET_LED5();
> 
=== message truncated ===



                
__________________________________ 
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]