AVR M16 串口通讯

之前写的,记录下

/*usart.c文件*/
#include <ioavr.h>
#include "delay.h"

#define Crystal 8000000   //晶振8MHZ 
#define Baud 9600         //波特率

//函数声明
void port_init(void);
void usart0_init(void);
void init_devices(void);
void usart_char_send(unsigned char i);
void usart_str_send(char *s);
unsigned char usart_char_receive(void);

/******************端口初始化*******************************/
void port_init(void) 
{
         PORTD = 0xFF;   //设置RXD0和TXD0
         
         DDRD |= (1<<PD1);      //TX   输出口
         DDRD &= ~(1<<PD0);     //RX  输入口
}
/*****************串口初始化*********************************/
void usart_init(void) 
{
        UCSRB = 0x00;                     //禁止发送和接收
        UCSRA = 0x02;                     //倍速异步模式USX=1
        UCSRC = 0x06;                     //0000 0110,UCSZ1=1,UCSZ0=1;8位字符,1位停止位 
        UBRRL = (Crystal/8/(Baud+1))%256;   //若为正常异步模式USX0=0则位(Crystal/16/(Baud+1))%256
        UBRRH = (Crystal/8/(Baud+1))/256;   //参见ATMAGE16使用手册
        UCSRB = 0x18;                     //允许发送和接收
}

/**************************************/
void init(void) 
{
        MCUCSR |= 0x80; //2次 关闭JTAG调试功能       
        MCUCSR |= 0x80;
        
        port_init();
        usart_init();
}

/****************发送一个字符******************************/
void usart_char_send(unsigned char i)
{
        while (!(UCSRA&(1<<UDRE)));
        UDR = i;
}
/******************发送一个字符串*************************/
void usart_str_send(char *s) 
{
        while (*s) {
                usart_char_send(*s); 
                s++;
        }
}

/*****************接收一个字符****************************/
unsigned char usart_char_receive(void) 
{
        while (!(UCSRA&(1<<RXC)));
        
        return UDR;
}

/***************主函数*****************************/
void main(void)
{
        unsigned char usart_temp;
        
        init();
        
        usart_str_send("Hello World");
        
        delay_nms(100);
        
        while(1) {
                /*
                usart_temp = usart_char_receive();   //等待接收数据
                usart_str_send("Send is ");     //发送数据
                usart_char_send(usart_temp); 
                usart_str_send("  ");
                */
                usart_str_send("Demo Demo");
                delay_nms(1000);
        } 
}


/*delay.c 文件*/
#include <ioavr.h>
#include "delay.h"

void delay_1us(void)                 //1us延时函数
{
        asm("nop");
}

void delay_nus(unsigned int n)       //N us延时函数
{
        unsigned int i=0;
        
        for (i=0; i<n; i++)
                delay_1us();
}
  
void delay_1ms(void)                 //1ms延时函数
{
        unsigned int i;
        
        for (i=0; i<1140; i++);
}
  
void delay_nms(unsigned int n)       //N ms延时函数
{
        unsigned int i;
        
        for (i=0; i<n; i++)
                delay_1ms();
}

/*delay.h 文件*/
#ifndef _DELAY_H_
#define _DELAY_H_

void delay_1us(void);
void delay_nus(unsigned int n);
void delay_1ms(void);
void delay_nms(unsigned int n);

#endif




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值