Problem with serial.write() implementation using interrupt

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Disk
 
Posts: 1
Joined: Wed Mar 21, 2012 11:05 pm

Problem with serial.write() implementation using interrupt

Post by Disk »

I would like to implement serial.write() command in Arduino UNO by using interrupt command of Atmel Atmega328P.

The data of Atmega328P is: http://www.atmel.com/Images/doc8271.pdf

The following is the code I try to write but it seems like it doesn't work.

Code: Select all

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define BAUD 9600

void serial_init()
{
        uint16_t   ubbr;

        ubbr = 103;
        UBRR0H = ubbr >> 8;
        UBRR0L = ubbr & 0xff;

#if 0
        UBRR0H = UBRRH_VALUE;
        UBRR0L = UBRRL_VALUE;
#endif
        UCSR0C = (3 << UCSZ00);
        UCSR0B = (1 << RXEN0) | (1 << TXEN0);
        UCSR0B = (1<<TXCIE0);

        return;
}

char *uart = "abc";
int i=0;

ISR(USART_TXC_vect){
  if (i<5)
  UDR0=uart[i++]; 
}

int main(void){
  serial_init();
  return 0;}
Thanks for your help :D

User avatar
philba
 
Posts: 387
Joined: Mon Dec 19, 2011 6:59 pm

Re: Problem with serial.write() implementation using interrupt

Post by philba »

Wow. Where to start? Serious or Troll?

Serious: Arduino+bootloader or GCC+atmel programmer?

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: Problem with serial.write() implementation using interrupt

Post by fat16lib »

Here is a working version. Look at the comments at the end of lines.

Code: Select all

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define BAUD 9600
char *uart = "abc";
int i=0;

void serial_init()
{
  uint16_t   ubbr;

  ubbr = 103;
  UBRR0H = ubbr >> 8;
  UBRR0L = ubbr & 0xff;

#if 0
  UBRR0H = UBRRH_VALUE;
  UBRR0L = UBRRL_VALUE;
#endif
  UCSR0C = (3 << UCSZ00);
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);       
  UCSR0B |= (1 << TXCIE0);  //  You must OR the bit
  UDR0 = uart[i++];  // You must send the first byte
  return;
}

ISR(USART_TX_vect){  // vector name may be wrong
  if (i < 3)  // only 3 chars
    UDR0=uart[i++];
  else
    UCSR0B &= ~(1 << TXCIE0);  // disable interrupt when done
}

int main(void){
  serial_init();
  sei();  // enable interrupts
  while(1);  // wait for data to be sent
  return 0;
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”