336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

http://wormfood.net/avrbaudcalc.php

흔히 쓰는

16MHz에 9600bps 라면

UBRR은 103이 된다.

===========================================================

//http://mulgu.kr/entry/Code-Example-ATMEGA848-UART-%EC%98%88%EC%A0%9C%EC%86%8C%EC%8A%A4


▼ATmega8기준 (수정 필요하면 instructables.tistory.com/64 로 의뢰해주세요)


#define F_CPU 16000000UL  // ◀8000000UL == 8 MHz


/* Very Important - change F_CPU to match target clock 

  타겟 클락에 맞춰서 바꿀것

Note: default AVR CLKSEL is 1MHz internal RC

디폴트는 1MHz 내부 RC이다.

  This program transmits continously on USART.

Interrupt is used for 

Receive charactor, which is then transmitted instead.

LEDs are used

as a test. Normal RX routine is included but not used.

  Change USART_BAUDRATE constant to change Baud Rate

*/


#include <avr/io.h>

#include <util/delay.h>

#include <avr/interrupt.h>


// Define baud rate

#define USART_BAUDRATE 9600//◀38400   

#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

// 8000000 / (38400 * 16)

// 8000000 / 614400 = 625/48

// 625/48 - 1 = 577/48 = 12.0208...


volatile unsigned char value;  

/* This variable is volatile so both main and RX interrupt can use it.

It could also be a uint8_t type */


/* Interrupt Service Routine for Receive Complete 

NOTE: vector name changes with different AVRs see AVRStudio -

Help - AVR-Libc reference - Library Reference - <avr/interrupt.h>: Interrupts

for vector names other than USART_RXC_vect for ATmega32 */


ISR(USART_RXC_vect)

   value = UDR;             //read UART register into value

   PORTB = ~value;          // output inverted value on LEDs (0=on)

}


void USART_Init(void)

{

   // Set baud rate

   UBRRL = BAUD_PRESCALE;// Load lower 8-bits into the low byte of the UBRR register

   UBRRH = (BAUD_PRESCALE >> 8); 

/* Load upper 8-bits into the high byte of the UBRR register

    Default frame format is 8 data bits, no parity, 1 stop bit

  to change use UCSRC, see AVR datasheet*/ 


  // Enable receiver and transmitter and receive complete interrupt 

  UCSRB = ((1<<TXEN)|(1<<RXEN) | (1<<RXCIE));

}



void USART_SendByte(uint8_t u8Data)

{

// Wait until last byte has been transmitted

  while((UCSRA &(1<<UDRE)) == 0);

  // Transmit data

  UDR = u8Data;

}



// not being used but here for completeness

// Wait until a byte has been received and return received data 

uint8_t USART_ReceiveByte()

{

while((UCSRA &(1<<RXC)) == 0);

  return UDR;

}


void Led_init(void)

{

//outputs, all off

DDRB =0xFF;       

PORTB = 0xFF;        

}


int main(void)

{

    USART_Init();  // Initialise USART

    sei();         // enable all interrupts

    Led_init();    // init LEDs for testing

    value = 'A'; //0x41;    

    PORTB = ~value; // 0 = LED on

   

    for(;;){    // Repeat indefinitely

             

     USART_SendByte(value);  // send value 

     _delay_ms(250);         

        // delay just to stop Hyperterminal screen cluttering up    

   }

}



Posted by ElectricShock
:
BLOG main image
잡동사니들(지극히 개인취향인...) (다른글에도 댓글 부탁해요♥) You May Leave English Messages on GuestBook. by ElectricShock

공지사항

카테고리

분류 전체보기 (782)
Programming(=프로그래밍) (3)
MiDi (2)
Animation (4)
Blender (3D Graphic Program.. (10)
Blendtuts.com (Series) (1)
Blender 기초 팁들 (2)
Processing (디지털미디어과) (2)
Music (1)
Books in the world (0)
Communication(CAN, UART, et.. (12)
MCU Examples (PIC 기반) (7)
Transistor (1)
Mikro C Pro (11)
Mikro Pascal (1)
Proton IDE (0)
Robot (0)
Swift 3D (1)
Dummies Series (1)
All about Hacking (0)
제2 외국어 (1)
PIC 해외서적들 (3)
AVR (25)
PIC (MikroC) (MPLAB) (4)
Assembly (2)
ARM (3)
Arduino (26)
PSpice (1)
Proteus ISIS (14)
CodeVision (2)
FPGA (15)
MPLAB (24)
PCB (the Procedure) (15)
3D Printer (5)
PICKIT3 (6)
Matlab (11)
RaspBerry PI (15)
BeagleBone (1)
Android Studio (17)
졸업작품 (172)
Korea History (0)
Issue(사회) (73)
Multimeter 리뷰 (1)
Oscilloscope (1)
A (34)
B (19)
J (6)
C (32)
P (12)
T (37)
H (12)
I (12)
M (44)
R (5)
E (5)
F (2)
D (9)
O (2)
L (7)
S (9)
W (2)
V (6)
G (14)
Visual C++ or Visual Studio (2)
Android App Development (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백