Search This Blog

Tuesday, March 23, 2010

USART send/receive a Byte : ATmega16 Avr studio code

USART send/receive a Byte : ATmega16 Avr studio code

VB.net code is working on other side.
------------------------------------------------
#include inttypes.h
#include avr/interrupt.h
#include avr/io.h
#include avr/signal.h
#include util/delay.h
#include "inc/lcd.h"
#include "inc/START_LCD_SCREEN.h"
#define F_CPU 80000000L              //8Mhz

//#define POINTCHAR 'o' /* 0x6f */
//#define BLANKCHAR ' ' /* 0x20 */
#define F_OSC 8000000                   /* oscillator-frequency in Hz */
#define UART_BAUD_RATE 4800
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC)/((UART_BAUD_RATE)*16l)-1)


unsigned int var=1;
void usart_putc(unsigned char c) {
   // wait until UDR ready
    while(!(UCSRA & (1 << UDRE)));
    UDR = c;    // send character
}

void uart_puts (char *s) {
    //  loop until *s != NULL
    while (*s) {
        usart_putc(*s);
        s++;
    }
}
/*
void init(void) {
    // set baud rate
    UBRRH = (uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8);
    UBRRL = (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC);
    // Enable receiver and transmitter; enable RX interrupt
    UCSRB = (1<<<
    //asynchronous 8N1
    UCSRC = (1<<
 //Enable The receiver and transmitter
   UCSRB=(1<<


}
*/
char USARTReadChar()
{
   //Wait untill a data is available
   while(!(UCSRA & (1<
   {
   }

   return UDR;
}


void USARTInit(uint16_t ubrr_value)
{

   //Set Baud rate

  // UBRRL = ubrr_value;
  // UBRRH = (ubrr_value>>8);
    UBRRH = 0 ; //(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8);
    UBRRL =  103 ; // (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC);

   UCSRC=(1<<

   //Enable The receiver and transmitter
 //  UCSRB=(1<<

 // Enable receiver and transmitter; enable RX interrupt
   UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
   UCSRA &= (11111101);            //SET U2X TO 0
}

void USARTWriteChar(char data)
{

   while(!(UCSRA & (1<
   {
   }
   UDR=data;
}

void serialterminate(void) { // terminate sent string!!!
    while(!(UCSRA & (1 << UDRE)));
    UDR = 0x0d;
    while(!(UCSRA & (1 << UDRE)));
    UDR = 0x0a;   
}



int main(void)
{

    int CHOICE=2;   
    unsigned char ch;
    char data;
   
     // Interrupt flag set
    GICR=1<
    //sbi(MCUCR,1);       // falling edge at INT0 generates interrupt
    //cbi(MCUCR,0);
    MCUCR |= (00000010);
    MCUCR &= (11111110);

    // Port A initialization
    PORTA=0x00;        //ACD
    DDRA=0x00;        //input
    // Port B initialization
    PORTB=0xff;        //PUSH BUTTONS
    DDRB=0x00;        //input
    // Port C initialization
    DDRC=0xFF;        //LCD
    PORTC=0xFF;        //output
    // Port D initialization
    PORTD=0xFF;        //LED's
    DDRD=0x00;        //output

    /* LCD module initialization display, cursor on */   
    lcd_init(LCD_DISP_ON_CURSOR_BLINK);
    lcd_clrscr();

    USARTInit(103);

    lcd_gotoxy(0,0);
    lcd_puts_P("PRESS1 -- READING");
    lcd_gotoxy(0,2);
    lcd_puts_P("PRESS2 -- SEND STH");
    sei();
    while(1)
    {
        if((PINB & 0x01) == 0)
        {
            lcd_clrscr();
            lcd_gotoxy(0,0);
            lcd_puts_P("READING....");
            ch=USARTReadChar();
            lcd_gotoxy(0,1);
            lcd_putc(ch);
            UCSRA &= (01111111);

        }
        if((PINB & 0x02) == 0)
        {
            lcd_clrscr();
            lcd_gotoxy(0,0);
            lcd_puts_P("SENDING BYTE");
            USARTWriteChar('v');
            lcd_gotoxy(0,1);
            lcd_puts_P("SENT BYTE");
            UCSRA &= (11011111);
        }

        if((PINB & 0x10) == 0)
        {
        lcd_gotoxy(0,0);
        lcd_puts_P("PRESS1 FOR READING");
        lcd_gotoxy(0,2);
        lcd_puts_P("PRESS2 TO SEND STH");
        }
        _delay_ms(1000);

}
}

SIGNAL(SIG_INTERRUPT0) // SIGNAL is a macro for interrupts

{
cli();
    // Do some stuff
        lcd_clrscr();
        lcd_gotoxy(0,0);
        lcd_puts_P("INT GEN");
        USARTWriteChar('c');
        lcd_gotoxy(0,1);
        lcd_puts_P("Send BYTE");
        UCSRA &= (11011111);
        _delay_ms(1000);
        sei();

}

// INTERRUPT can be interrupted
// SIGNAL can't be interrupted
SIGNAL (SIG_UART_RECV) { // USART RX interrupt
   unsigned char ch;
   // read received character
  
   // and just send it back
             lcd_clrscr();
            lcd_gotoxy(0,0);
            lcd_puts_P("Rx Intrupt ....");
while(!(UCSRA & (1<
   {
   }
            ch=UDR  ;//USARTReadChar();
            lcd_gotoxy(0,1);
            lcd_putc(ch);
            UCSRA &= (01111111);
        var=0;
        PORTD |= (11000001);   
}

No comments:

Post a Comment