[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Interrupt Driven UART
From: |
Asim |
Subject: |
Re: [avr-gcc-list] Interrupt Driven UART |
Date: |
Mon, 30 Aug 2004 10:48:28 +0500 |
Thanks for the reply.
I tried enabling and disabling the UDRIE, but still no response, the list
file shows the correct interrupt vectors and the interrupts are enabled
properly. I tried disabling the transmitter completely, there is no
response from the receiver side.
Thanks
Larry Barello wrote:
> If nothing is happening then you must not have (the correct) interrupts
> enabled or the system is crashed. Have you tried running this in the
> simulator?
>
> Some obvious problems: You must have UDRIE off until there is data in the
> output queue. So, 1) Enable UDRIE every time you put a character in the
> output queue and 2) disable UDRIE whenever the UDRE interrupt handled has
> nothing to do (ie. in the handler if the queue is empty, turn it off and
> then return). Otherwise your code will endlessly loop servicing the UDRIE
> interrupt, since the UDR is *always* ready.
>
> Don't use TXCIE. Leave RCXIE on all the time.
>
> -----Original Message-----
> From: address@hidden
> [mailto:address@hidden Behalf Of Asim
> Sent: Sunday, August 29, 2004 3:51 AM
> To: address@hidden
> Subject: [avr-gcc-list] Interrupt Driven UART
>
> 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];
> }
> }
>
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list