All pastes #405268 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-21 17:09 UTC
rendered paste body
#include <avr/io.h>
#include <avr/interrupt.h>

//------------------------------------------------------------------------------------------------
// S U B R O U T I N E S
//------------------------------------------------------------------------------------------------

void USART_Init(unsigned int baud)
{
  UBRRH = (unsigned char)(baud>>8); // set baudrate
  UBRRL = (unsigned char)baud;

  UCSRB = (1<<RXEN)|(1<<RXCIE); // enable the receiver

  UCSRC = (1<<URSEL)|(3<<UCSZ0); // set frame format to 8-bit data
}

//------------------------------------------------------------------------------------------------
// I N T E R R U P T   S U B R O U T I N E S
//------------------------------------------------------------------------------------------------

unsigned char a=0;
volatile unsigned char UDRfoo;

//================================================
// incoming USART interrupt routine
//================================================
ISR(UART_RX_vect)
{
  UDRfoo = UDR;

  UCSRB &= ~(1<<RXCIE); // disable USART irqs

  // midi status byte
  if ((UDRfoo&0x80)==0x80)
  {
    unsigned char temp = UDRfoo&0x70;
	/*if (temp==0x00) // key up event
      PORTA=0x00;*/
    if (temp==0x10) // key down event
    {
      static int a=0;
	  PORTA=a++;
    }
  }
  
  UCSRB |= (1<<RXCIE); // enable USART irqs
}

//------------------------------------------------------------------------------------------------
// M A I N
//------------------------------------------------------------------------------------------------

int main(void)
{
  cli();
  
  DDRA = 0xFF; // setup ports A and B
  DDRB = 0xFF;

  PORTD = 0xFC; // setup port D
  DDRD = 0xFE;

  USART_Init(31); // setup USART with 31250 baudrate
  
  sei(); // enable interrupts
  
  unsigned short b=0; // a little loop that flashes port b
  while(1)
  {
    b++;
    PORTB=b>>8;
  }
  
  return 0;
}