Xmitting via USART Interrupts

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

The following code works but I would like to use interrupts to Xmit text via the USART. In the last few lines below I don't believe the lines sprintf and str_send() are using interrupts to send the text. Is it being sent via Interrupt?

Is there a better way to do this?

Can the text be formatted and sent to the USART inside the sprintf statement?

struct Time_Date {
  int hour;
  int minute;
  int second;
} time;

void usart_xmit(char Byte)
{
  while ((UCSRA & (1<<UDRE)) == 0) {};
  UDR = Byte;
}

void str_send(char byte[])
{
  char x;
  
  for(x = 0; x < strlen(byte); x++)
    usart_xmit(byte[x]);
}

ISR (TIMER1_COMPA_vect)
{
  PORTB ^= (1 << 0);         // Toggle second LED
  if(++time.second > 59)     // count up seconds
  {
    time.second = 0;         // enough for a minute?
    PORTB ^= (2 << 0);       // Toggle minute LED
    if(++time.minute > 59)   // count up minute
    {
      time.minute = 0;	     // enough for a minute?
      PORTB ^= (3 << 0);      // Toggle hour LED
      if(++time.hour > 23)    // count up seconds
        {
          time.hour = 0;      // enough for a minute?
          PORTB ^= (4 << 0);  // Toggle day LED
        }
      }
    }
  sprintf(usart_time,"%02d:%02d:%02d\n",time.hour,time.minute,time.second);
  str_send(usart_time);
}

ISR (USART_RXC_vect)
{
  char RxByte ;
  RxByte = UDR ;
  UDR = RxByte ;
}

Thanks,
Mike

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

The incoming chars are echoed in the rx isr but since you have no tx isr, sending is not done via interrupts. Rather than sprintf, you want printf. Google 'avrgcc printf files' and see what you get. Basically you set up a file to send your data. Printf sends to this file.

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

Quote:
Rather than sprintf, you want printf.
printf would not use interrupts either. Set up a buffer and a UDRE ISR. When the buffer is full, enable the UDRE interrupt and use the ISR to send one byte. When the ISR sees that it has sent the last byte, it turns the UDRE interrupt off.

Regards,
Steve A.

The Board helps those that help themselves.

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

Printf calls putchar. If your putchar uses interrupts you're golden. To make this happen, you need to make putchar add the char being output to the output array. And, if the tx interrupt is off, you must grab the first char in the output array, stuff it in the UDR, and enable the TX interrupt. This interrupt will hit in 1 usec, because the first char always jumps into the uart shift register and starts shifting out, but since you have already queued up all the chars to be sent, you can grab the next char in the output array and stuff it into the UDR. At 115200 bps, the output interrupt will hit in 83 usec (a long time to a 16 MHz AVR), and after the last char is gone. there are no more chars to send, you have to turn off the tx interrupt. Sort of complicated, but it has the potential to free up about 80% of the cpu that is wasted waiting for the char to shift out, if you are clever enought to do something with 80% of 83usecs.

Imagecraft compiler user