본문 바로가기
삽질기초/HW

avr serial register - 2560

by @가을바람 2009. 4. 1.

//atmega 2560 serial register function

 

#include "serial_func.h"

 

#define BAUD_115200 8  //  BAUD RATE : 115200bps 
#define BAUD_57600 16  //  BAUD RATE : 57600bps 
#define BAUD_38400  25  //  BAUD RATE : 38400bps
#define BAUD_9600  103  //  BAUD RATE : 9600bps

 

void main()
{

 init_USART(BAUD_9600,BAUD_9600,BAUD_9600); 


}

 

 

 

/*************************************************************************/

 

 

 


//**********************************************
// USART Initialization
//**********************************************

void init_USART(unsigned int baud0,unsigned int baud1,unsigned int baud2)
{
 UBRR0H = (unsigned char)(baud0 >> 8); // set baud rate 
 UBRR0L = (unsigned char)baud0;  
 UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(0<<TXCIE0);  // Enable RX,TX
 UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
 // Asynchronous,No parity, 1 stop, 8bit data, Rising Xck Edge
  
 UBRR1H = (unsigned char)(baud1 >> 8); // set baud rate 
 UBRR1L = (unsigned char)baud1;  
 UCSR1B = (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1)|(0<<TXCIE1);  // Enable RX,TX
 UCSR1C = (1<<UCSZ11)|(1<<UCSZ10);
 // Asynchronous,No parity, 1 stop, 8bit data, Rising Xck Edge
 
 UBRR2H = (unsigned char)(baud2 >> 8); // set baud rate 
 UBRR2L = (unsigned char)baud2;  
 UCSR2B = (1<<RXEN2)|(1<<TXEN2)|(1<<RXCIE2)|(0<<TXCIE2);  // Enable RX,TX
 UCSR2C = (1<<UCSZ21)|(1<<UCSZ20);
 // Asynchronous,No parity, 1 stop, 8bit data, Rising Xck Edge
}


//***************************
// Serial function
//***************************
//0
void Txd0Byte(unsigned char datum)
{
      
 while(!(UCSR0A & (1 << UDRE0))); 
 UDR0 = datum;   
 
}

void Txd0String(char *str)
{
 int i;

 for( i=0; str[i]!=0; i++)
 {
  Txd0Byte( str[i]);
 }
}

void Txd0Dec(int dec)
{
 char String[5];
 int  loop;

 for (loop = 0 ; loop <5 ; loop++)
 {
  String[loop] = 0x30 + (dec % 10);
  dec = dec / 10;
 }

 for(loop = 4; loop >= 0; loop --)
  Txd0Byte(String[loop]);
 
}
void Txd0StringIndex(char *str, int index)
{
 int i;
 for( i=0; i < index; i++)
 {
  Txd0Byte( str[i]);
 }
}
//1
void Txd1Byte(unsigned char datum)
{
      
 while(!(UCSR1A & (1 << UDRE1))); 
 UDR1 = datum;   
 
}

void Txd1String(char *str)
{
 int i;

 for( i=0; str[i]!=0; i++)
 {
  Txd1Byte( str[i]);
 }
}

void Txd1Dec(int dec)
{
 char String[5];
 int  loop;

 for (loop = 0 ; loop <5 ; loop++)
 {
  String[loop] = 0x30 + (dec % 10);
  dec = dec / 10;
 }

 for(loop = 4; loop >= 0; loop --)
  Txd1Byte(String[loop]);
}
void Txd1StringIndex(char *str, int index)
{
 int i;
 for( i=0; i < index; i++)
 {
  Txd0Byte( str[i]);
 }
}
//2
void Txd2Byte(unsigned char datum)
{
      
 while(!(UCSR2A & (1 << UDRE2))); 
 UDR2 = datum;   
 
}

void Txd2String(char *str)
{
 int i;

 for( i=0; str[i]!=0; i++)
 {
  Txd2Byte( str[i]);
 }
}

void Txd2Dec(int dec)
{
 char String[5];
 int  loop;

 for (loop = 0 ; loop <5 ; loop++)
 {
  String[loop] = 0x30 + (dec % 10);
  dec = dec / 10;
 }

 for(loop = 4; loop >= 0; loop --)
  Txd2Byte(String[loop]);
}
void Txd2StringIndex(char *str, int index)
{
 int i;
 for( i=0; i < index; i++)
 {
  Txd0Byte( str[i]);
 }
}

 

 

 

 

 

 

'삽질기초 > HW' 카테고리의 다른 글

dsPIC33FJ256GP710  (0) 2009.09.25
dspic33FJ256GP710 UART2 interrupt test.  (0) 2009.09.25
연산증폭기.  (0) 2009.09.09
DSP281x_Gpio.c  (0) 2009.08.21
atmega128 iar+pony+초기설정  (0) 2009.04.01