avr-gcc-list
[Top][All Lists]
Advanced

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

[avr-gcc-list] Interrupt Driven UART


From: Asim
Subject: [avr-gcc-list] Interrupt Driven UART
Date: Sun, 29 Aug 2004 15:50:44 +0500

Hi there.

I tried the following code to test the uart interrupts on Mega8535,
but there is no response either from TX and RX, while if I use the
polling mode then there is no problem, any help.

Thanks


/*----------------------------------------------------------*
  Interrupt Driven Uart Test

  MEGA8535 Fuses Setting
  ----------------------
  S8535C  =1     BODLEVEL =0
  WDTON   =1     BODEN    =0
  SPIEN   =1     SUT1     =0
  CKOPT   =0     SUT0     =1
  EESAVE  =1     CKSEL3   =1
  BOOTSZ1 =1     CKSEL2   =0
  BOOTSZ0 =1     CKSEL1   =1
  BOOTRST =1     CKSEL0   =0

  0=Programmed
 *----------------------------------------------------------*/

#include    <avr/io.h>
#include    <avr/interrupt.h>
#include    <avr/signal.h>
#include    <avr/pgmspace.h>
#include    <avr/delay.h>
#include    <stdio.h>

#define uc              unsigned char
#define ui              unsigned int

#define XTAL            11059200UL
#define BAUD         115200UL

#define OffLED()        PORTD |= _BV(PD3)    // status LED control
#define OnLED()         PORTD &= ~_BV(PD3)

#define RX_BUF_LEN      8
#define TX_BUF_LEN      8

                /*----- Function Prototypes -----*/

void     delay_ms(ui msec);
int   com_getchar(void);
int      com_putchar(char c);
void     UpdateSR(void);

                /*----- Global Variables -----*/

static   uc   cRxBuf[RX_BUF_LEN];     // uart rx-isr var
static   uc   cRxWrNdx, cRxRdNdx;
volatile uc   cRxCtr;

static   uc   cTxBuf[TX_BUF_LEN];     // uart tx-isr var
static   uc   cTxWrNdx, cTxRdNdx;
volatile uc   cTxCtr;

uc            cRxData;

/*--------------------------------*/
int     main(void)
{
 DDRA  = 0xFF;     // 1-output, 0-input
 DDRB  = 0xFF;
 DDRC  = 0xFF;
 DDRD  = 0xFE;
 PORTD = 0xFF;

 UBRRH = 0x00;     // baudrate 115200
 UBRRL = 0x05;      // (XTAL/(16 * BAUD)) - 1
 UCSRB = 0xD8;     // Rx-Tx On, Rx-Tx inter. enable
 UCSRC = 0x86;     // 8 data bits, no parity, 1 stop bit

 ACSR  = 0x80;     // comparator off
 sei();             // enable interrupts

 OnLED();
 delay_ms(250);
 OffLED();

 fdevopen(com_putchar, com_getchar, 0);
 puts_P(PSTR("Hello from MEGA8535\n\r"));

 for (;;)
  {
   cRxData = com_getchar();

   if (cRxData == '1')
     OnLED();
   else
     OffLED();

   com_putchar(cRxData);
  }
}
/*--------------------------------*/
void    delay_ms(ui msec)
{
 while (msec)
  {
   --msec;
   _delay_loop_2((ui)(XTAL / 4000));   // delay 1 msec
  }
}
/*--------------------------------*/
int     com_getchar(void)
{
 ui  c;

 while (cRxCtr == 0);   // wait for char
 c = cRxBuf[cRxRdNdx++];

 if (cRxRdNdx >= RX_BUF_LEN)
   cRxRdNdx = 0;

 cli();
 cRxCtr--;
 sei();
 return(c);
}
/*--------------------------------*/
int     com_putchar(char c)
{
 while (cTxCtr >= TX_BUF_LEN);

 if ((cTxCtr == 0) && (bit_is_clear(UCSRA, UDRE)))
   UDR = c;
 else
   {
    cTxRdNdx++;

    if (cTxRdNdx >= TX_BUF_LEN)
      cTxRdNdx = 0;

    cTxBuf[cTxRdNdx] = c;
    cli();
    cTxCtr++;
    sei();
   }

 return(0);
}
/*--------------------------------*/
SIGNAL (SIG_UART_RECV)
{
 cRxBuf[cRxWrNdx++] = UDR;

 if (cRxWrNdx >= RX_BUF_LEN)
   cRxWrNdx = 0;

 if (cRxCtr++ >= (RX_BUF_LEN + 1))
   cRxCtr = RX_BUF_LEN;
}
/*--------------------------------*/
SIGNAL (SIG_UART_DATA)
{
 if (cTxCtr != 0)
   {
    cTxCtr--;
    cTxWrNdx++;

    if (cTxWrNdx > TX_BUF_LEN)
      cTxWrNdx = 0;

    UDR = cTxBuf[cTxWrNdx];
   }
}





reply via email to

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