All pastes #1467636 Raw Edit

Stuff

public text v1 · immutable
#1467636 ·published 2009-06-20 13:53 UTC
rendered paste body
#include <avr/io.h>
#include <avr/iom8.h>
#include <avr/interrupt.h>

//variables
#define portio      PIND
#define portiow     PORTD
#define rxled       PD7
#define rele        PD6

#define ubrr 51
#define MAX_BUFFSIZE 3

short int ovf = 0;
volatile short int ovf0cnt = 0;
volatile short int timer1 = 0;
volatile short int btnsw_right = 0;

volatile unsigned char usart[MAX_BUFFSIZE];
volatile unsigned char usart1 = 0;

unsigned char rx_count = 0;
volatile unsigned char message = 0;

void sendChar(char data)
{
  while( !(UCSRA&(1<<UDRE)) )
    ;
  UDR = data;
  UCSRA=UCSRA|0x40;
}

int main(void)
{
  cli();
  //ovf0cnt = 0;
  UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(1<<TXCIE)|(0<<UDRIE)|(0<<UCSZ2);
  UCSRC = (1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
  UBRRH = (unsigned char) (ubrr>>8);
  UBRRL = (unsigned char) ubrr;
  TIMSK |= (1 << TOIE0);  
  TCCR0 = (1<<CS02 | 0<<CS01 | 0<<CS00);  
  DDRD = (1 << PD7 | 1 << PD6 | 0<<PD5 | 1<<PD4 | 0<<PD2);
  PORTD |= (1 << PD5 | 1<<PD4);
  sei();

  while(1)
  {

  }
}

ISR(TIMER0_OVF_vect)
{
  if(portio & (1<<rxled)) {
    timer1 = 400;
    portiow &= ~(1 << rxled);
  }
  
  if(timer1) {
    if(--timer1 == 0) {
      sendChar(usart1);
/*      if(usart1 == '3')
        portiow |= (1 << rele);
      if(usart1 == '4')
        portiow &= ~(1 << rele);
*/      portiow ^= (1 << rele);
    }
  }
}

ISR(USART_RXC_vect)
{
  if((rx_count < MAX_BUFFSIZE) && !message)
    usart[rx_count++] = UDR;

  if((rx_count == MAX_BUFFSIZE) && !message)
  {
    sendChar(usart[1]);

    message = 1;
    if(message) {
 
    switch(usart[0])
    {
      case '1': usart1 = usart[1]; timer1 = usart[2]; message = 1;
        switch(usart1) {
	  case '1': portiow |= (1 << rele); break;
	  case '2': portiow &= ~(1 << rele); break;
	  case '3': portiow |= (1 << rxled); break;
	  case '4': portiow |= (1 << rxled); break;
	  default: UDR = 'C';  
	} break;
      default : sendChar('X');
    }
    message = 0;
    rx_count = 0;
   }
 }
}