There is currently 1 post.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1388 0
【串口通信】AVR单片机串口发送到PC
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 10/13/13 1:56
#include <iom16v.h>
#include <macros.h>

#define FOSC 8000000
#define BAUD 9600

void delay(unsigned int n)
{
    unsigned int i;
    while (n--)
        for (i=0;i<1140;i++);
}

void UART_Init(void)
{
    UCSRB=0x00; //禁止发送和接收
    UCSRA=0x02; //倍速异步模式USX=1
    UCSRC=0x06;

    UBRRL=(FOSC/8/(BAUD+1))%256;
    UBRRH=(FOSC/8/(BAUD+1))/256;

    UCSRB=0xd8; //允许发送和接收,接收和发送结束中断使能
}

void UART_Send(unsigned char Data)
{
    while (!(UCSRA&BIT(UDRE))); //等待数据寄存器为空
    UDR=Data;
}

void UART_SendString(char* pStr)
{
    while (*pStr!='\0')
    {
        UART_Send(*pStr);
        pStr++;
    }
}

void main(void)
{
    DDRD=0xf3; //两个外中断口设为输入
    PORTD=0xff;
    UART_Init();
    
    while (1)
    {
        UART_SendString("This is a string sent by ATMega16.\n");
        delay(1000);
    }
}
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1388 Replies: 0
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 10/13/13 1:56
Bar Good Posts
Announcements