📄 uart0.c
字号:
#include <iom128v.h>
#include <macros.h>
//------------
#define TRE0 5
#define SET_485_TX0() PORTD &= ~(1<<TRE0)
#define SET_485_RX0() PORTD |= (1<<TRE0)
//-----------------
#pragma interrupt_handler uart0_rx_isr:19
#pragma interrupt_handler uart0_udre_isr:20
#pragma interrupt_handler uart0_tx_isr:21
extern void command_handle0(void);
unsigned char rx0_step;
unsigned char tx0_step;
//unsigned char rx0_buf[15];
//unsigned char tx0_buf[15];
unsigned char rx0_buf[14];
unsigned char tx0_buf[14];
unsigned char temp_rx;
unsigned char rx_ok;
unsigned char tx_en;
unsigned char uart0_reset_count;
/*************/
// 接收中断
/*********/
void uart0_rx_isr(void)
{
if(rx_ok)return;
uart0_reset_count=2;
rx0_buf[rx0_step]=UDR0;
// if(rx0_step<14)
// temp_rx=temp_rx+rx0_buf[rx0_step];
rx0_step++;
// if(rx0_step>14)
if(rx0_step>13)
{
rx0_step=0;
// if(temp_rx==rx0_buf[14])
rx_ok=1;
// temp_rx=0x00;
};
}
/*******/
//寄存器空中断
/***************/
void uart0_udre_isr(void)
{
// if(tx0_step<14)
// {
UDR0=tx0_buf[tx0_step];
// tx0_buf[14]+=tx0_buf[tx0_step];
tx0_step++;
// }
// else if(tx0_step==14)
if(tx0_step>13)
{
// UDR0=tx0_buf[14];
tx0_step=0;
UCSR0B&=~(1<<UDRIE0);
UCSR0B|=1<<TXCIE0;
}
}
/******************/
//发送完成中断
/****************/
void uart0_tx_isr(void)
{
// tx0_buf[14]=0x00;
SET_485_RX0();
UCSR0B&=~(1<<TXCIE0);
UCSR0B|=(1<<RXEN0)|(1<<RXCIE0);//允许接收,接收结束中断
tx_en=1;
}
//-------------------
void uart0_init(void)
{
unsigned char i;
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
//UBRR0L = 0x33; //set baud rate lo
// UBRR0H = 0x00; //set baud rate hi
UBRR0L = 0x0C; //
UBRR0H = 0x00; //38400
UCSR0B = 0x98;
uart0_reset_count=0;
rx_ok=0;
tx_en=1;
for(i=0;i<15;i++)
{
tx0_buf[i]=0x00;
rx0_buf[i]=0x00;
}
SET_485_RX0();
}
//-------------
void uart0_tx (void)
{
do{
}while(!tx_en);
tx_en=0;
SET_485_TX0();
UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
}
//--------------------
void uart0_handle (void)
{
if(rx_ok)
{
command_handle0();
rx_ok=0;
};
if(!uart0_reset_count)rx0_step=0;//接收超时处理
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -