DPRG
DPRG List  



[DPRG] ATmega64 - Having problems getting both serial ports to work.

Subject: [DPRG] ATmega64 - Having problems getting both serial ports to work.
From: Kipton Moravec kip at kdream.com
Date: Sun Jun 26 21:29:23 CDT 2005

I have a ATMega64 with 2 serial ports. I am programming in C (WINAVR)
and Assembler. No matter what language I use I can't get USART1 to work.
USART0 seems to be working fine. I am running the processor at 14.7456
MHz, and I think I am doing the exact same thing to both ports. One
works the other does not.

Does anyone know of a problem with USART1?  I do know about not being
able to use OUT on the ports because they are too high for the OUT
assembler command. The compiler seems to be generating the correct
assembler.

I have been chopping down the code to get the essence of what is wrong.

====================================================

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <stdio.h>
#include <avr/boot.h>
#include <string.h> 
#include <stdlib.h>
#include <avr/wdt.h>

#define FULL

#include "uart.h"

/**************************************************************
 *  FLASH CONSTANTS
 **************************************************************/
/*const char yes[]           PROGMEM = "YES";
const char no[]            PROGMEM = "NO ";
const char on[]            PROGMEM = "ON ";
const char off[]           PROGMEM = "OFF";
*/
/**************************************************************
 *  Function Declare
 **************************************************************/

void init_usart(void);
int usart_getc0(void);
int usart_putc0(char c);
int usart_getc1(void);
int usart_putc1(char c);


/**************************************************************
 *  SRAM CONSTANTS
 **************************************************************/
static unsigned int volatile  inputs;
/**************************************************************
 *  UART CONSTANTS
 **************************************************************/

#define TBUFLEN 128
#define RBUFLEN   8

///////////////////////////////////////////////////////////////////////
// init_usart() - initialize on-chip USART
///////////////////////////////////////////////////////////////////////

#define PROCESSOR_CLOCK 14745600L
#define BAUD_RATE0 9600
#define BAUD_RATE_DIVISOR0 (((PROCESSOR_CLOCK / BAUD_RATE0) / 16) - 1)
#define BAUD_RATE1 9600
#define BAUD_RATE_DIVISOR1 (((PROCESSOR_CLOCK / BAUD_RATE1) / 16) - 1)

/*
FILE *usart0;
FILE *usart1; 
*/
void init_usart(void) {
 unsigned int i;
// UBRR0H = (unsigned char) BAUD_RATE_DIVISOR0 >> 8;
// UBRR0L = (unsigned char) BAUD_RATE_DIVISOR0;
 UBRR0H = 0;
 UBRR0L = 95; // 9600 Baud with 14,745,600 crystal
 UCSR0A = 0x00;
             // enable transmitter & receiver
 UCSR0B = (1<<RXEN0) | (1<<TXEN0); 
             // asynchronous, no parity, 1 stop bit, 8 data bits
 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); 


 //usart0 = fdevopen(usart_putc0, usart_getc0, 0);
 
 // UBRR1H = (unsigned char) BAUD_RATE_DIVISOR1 >> 8;
 // UBRR1L = (unsigned char) BAUD_RATE_DIVISOR1;
 UBRR1H = 0;
 UBRR1L = 95; // 9600 Baud with 14,745,600 crystal
 UCSR1A = 0x00;
             // enable transmitter & receiver
 UCSR1B = (1<<RXEN1) | (1<<TXEN1); 
             // asynchronous, no parity, 1 stop bit, 8 data bits
 UCSR1C = (1<<UCSZ11) | (1<<UCSZ10); 


// usart1 = fdevopen(usart_putc1, usart_getc1, 0);
 
 // Delay
 for (i=0; i<60000; i++) inputs = inputs + 123;
/* 
 fputs_P(PSTR("Compiled: "),usart0);
 fputs_P(PSTR(__DATE__),usart0);
// space_out(2,usart0);
 fputs_P(PSTR(__TIME__),usart0);
// CRLF(usart0);
*/

// fputs_P(PSTR("Compiled: "),usart1);
// fputs_P(PSTR(__DATE__),usart1);
// space_out(2,usart1);
// fputs_P(PSTR(__TIME__),usart1);
// CRLF(usart1);


}

///////////////////////////////////////////////////////////////////////
// USART functions
///////////////////////////////////////////////////////////////////////



int usart_putc0(char c) {

 if(c == '\n') usart_putc0('\r');
// while(bit_is_clear(UCSR0A, UDRE0));
 while (!( UCSR0A & (1 <<UDRE0))) ;
 UDR0 = c;

 return 0; // always works
}
/*
int usart_getc0(void) {

 while(bit_is_clear(UCSR0A, RXC0)); // wait for it

 return UDR0;
}
*/
int usart_putc1(char c) {
 if(c == '\n') usart_putc1('\r');
// while(bit_is_clear(UCSR1A, UDRE1));
 while (!(UCSR1A & (1<<UDRE1)));
 UDR1 = c;

 return 0; // always works
}
/*
int usart_getc1(void) {

 while(bit_is_clear(UCSR1A, RXC1)); // wait for it

 return UDR1;
}
*/
///////////////////////////////////////////////////////////////////////
// INTERRUPT functions
///////////////////////////////////////////////////////////////////////


int main(void) {
    unsigned char i;
    unsigned int ii;
    
    wdt_disable();
    
    
    DDRC  = 0;      // all these pins are inputs
    PORTC = 0;      // all of Port C has no pullups (on board)
    
    DDRB  = 0x0F;
    PORTB = 0x10;
        
    DDRD  =  0xF6;
    PORTD =  0;
    
    DDRA = 0xff;     // All of PORTA is OUTPUT
    DDRB = 0xff;     // All of PORTB is OUTPUT
    DDRC = 0xff;     // All of PORTC is OUTPUT
    PORTC = 0;
    
    //  PORTD Pin 0, 1, 2, and 4 are Input rest are OUTPUT
    DDRD = (1<<PD3) | (1<<PD5) | (1<<PD6) | (1<<PD7); 
    PORTD = 3;     //  (Start) Switch and Switch 5 are pulled up       

    DDRE = (1<<PE1) | (1<<PE3); // Outputs rest are input
    PORTE = 0xf0;    // PortE make Switch 1-4 have pullup

    DDRF = 7;        // PORTF Pin  0, 1, and 2 are OUTPUT rest are INPUT
    DDRG = 0X1F;
    
    init_usart();
    
    while (1) {
        for (i=48; i<125; i++)
        usart_putc0(i); // Works
        usart_putc1(i); // Does not work
        inputs = 0;
        // Delay so it does not roll too fast
        for (ii=0; ii<60000; ii++) inputs = inputs + 123; 
        }
    }
-- 
Kipton Moravec <kip at kdream.com>


More information about the DPRG mailing list