?? uart.c
字號:
//-----------------------------------------------------------
//程序由AVR輔助開發工具V2.1.1自動生成
//MCU系統的處理器為: ATMega128
//MCU系統的晶振頻率: 8.0000 Mhz
//-----------------------------------------------------------
#include <iom128v.h>
#include "uart.h"
#include "delay.h"
//-----------------------------------------------------------
//串口0初始化子程序
//字符長度:8位
//奇偶校驗:禁止
//通訊模式:異步
//-----------------------------------------------------------
void Uart0_Init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00; //Bit1為1則倍速發送
UCSR0C = 0x06;
UBRR0L = 0x22; //波特率:14400 Bps
UBRR0H = 0x00; //誤差率:-0.799%
UCSR0B = 0x18;
//初始化RS485通訊的流控制引腳
RS485_RD0_PORT|= RS485_RD0_BIT;
RS485_RD0_DDR|= RS485_RD0_BIT;
RS485_RD0_L;
}
//-----------------------------------------------------------
//串口0接收字節子程序
//-----------------------------------------------------------
unsigned char Getchar0(void)
{
while(!(UCSR0A& (1<<RXC0)));
return UDR0;
}
//-----------------------------------------------------------
//串口0發送字節子程序
//-----------------------------------------------------------
void Putchar0(unsigned char c)
{
RS485_RD0_H;
while (!(UCSR0A&(1<<UDRE0)));
UDR0=c;
while (!(UCSR0A&(1<<UDRE0)));
delay_nus(500);
RS485_RD0_L;
}
//-----------------------------------------------------------
//串口0發送字符串子程序
//-----------------------------------------------------------
void Putstr0(char *s)
{
while (*s)
{
Putchar0(*s);
s++;
}
}
//-----------------------------------------------------------
//串口0發送字符串子程序(帶有換行符)
//-----------------------------------------------------------
void Puts0(char *s)
{
while (*s)
{
Putchar0(*s);
s++;
}
Putchar0(0x0d); //回車換行
Putchar0(0x0a);
}
//-----------------------------------------------------------
//數組轉換成可發送的字符串
//-----------------------------------------------------------
char *buffer2str(char *hhh)
{
unsigned char i=0;
while (i<40)
{
hhh[i]=hhh[i]+48;
i++;
}
return hhh;
}
//-----------------------------------------------------------
//數字轉換成可發送的字符串
//-----------------------------------------------------------
char *long2str(unsigned long hh)
{
char *ss=" ";
ss[0]=48+hh/1000;
ss[1]=48+(hh%1000)/100;
ss[2]=48+(hh%100)/10;
ss[3]=48+(hh%10);
return ss;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -