?? uart.h
字號:
#define uart_debug 0
/*========================
在沒有設置UCPU_F 即用戶晶振頻率時,串口的
默認UCPU_F=11059200 hz
void init_uart(uint16 baudrat);
=========================*/
//==========================uart fifo
#define uart_fifo_size 32
//不得大于256個
unsigned char uart_fifo_buf[uart_fifo_size];
uchar uart_tx_cnt=0;
uchar uart_dat_cnt=0;//uart fifo dat 數
unsigned char uart_headp=0;
unsigned char uart_lastp=0;
//==============================
uchar w_uart_fifo(uchar dat)
{
if(uart_dat_cnt<uart_fifo_size) {uart_dat_cnt++;uart_fifo_buf[uart_lastp]=dat;
if(uart_lastp<(uart_fifo_size-1)) uart_lastp++;
else {uart_lastp=0; }
return ok;}
return error;
}
uchar r_uart_fifo(void)
{ uchar i;
if(uart_dat_cnt) { i=uart_fifo_buf[uart_headp];
if(uart_headp<(uart_fifo_size-1)) uart_headp++;
else {uart_headp=0;}
uart_dat_cnt--;return i;
}
else {return error; }
}
//==================================================
uint8 CmdBuf[32];
uint8 CmdList[][4]={
{"dir"},
{"blk"},
};
void uartSendBlk(uint32 id)
{ReadBlock(id);
CopyBytes(&sd_buf[0],&diruart[0],128);//FB FC 00 00
uart_send2(&diruart[0],128);
CopyBytes(&sd_buf[128],&diruart[0],128);//FB FC 00 00
uart_send2(&diruart[0],128);
CopyBytes(&sd_buf[256],&diruart[0],128);//FB FC 00 00
uart_send2(&diruart[0],128);
CopyBytes(&sd_buf[384],&diruart[0],128);//FB FC 00 00
uart_send2(&diruart[0],128);
}
void uartCmdSev(void)
{
uchar dat;
uchar rd_sw;
uchar i;
//=========================
rd_sw=1;
i=0;
do {dat=r_uart_fifo();
if(dat>='#') CmdBuf[i++]=dat;
if('#'==dat ) rd_sw=0;
}
while(rd_sw);
//=========================取出第一個$
for(i=0;i<sizeof(CmdList)/4;i++)
{if(IsEqual(&CmdBuf, &CmdList[i][0], 3)==ok) break;}
if(i==0) uartSendBlk((CmdBuf[3]-0x30)*1000+(CmdBuf[4]-0x30)*100+(CmdBuf[5]-0x30)*10+(CmdBuf[6]-0x30));
}
SIGNAL(SIG_USART_RECV)
{uchar dat;
//volatile static uchar gps_m_sw=0;
//volatile static uchar gps_b_sw=0;
dat=UDR;
w_uart_fifo(dat);
if(dat=='#') uartCmdSev();//0x23 #
}
/*void Boot_uart_tx(uint8 i)
{
UDR=i;
}
void uart_send(uint8 * p,uint8 size)
{
if(uart_tx_cnt<size)
{UDR=*(p+uart_tx_cnt);uart_tx_cnt++;}
else
uart_tx_cnt=0;
}*/
void uart_send1(uint8 p)
{
UDR=p;
while(!(UCSRA & 0x40));
UCSRA |= 0x40;
_delay_ms(5);
}
void uart_send2(uint8 * p,uint8 size)
{
for(uart_tx_cnt=0;uart_tx_cnt<size;uart_tx_cnt++)
{UDR=*(p+uart_tx_cnt);
while(!(UCSRA & 0x40));
UCSRA |= 0x40;
_delay_ms(10);
}
}/**/
SIGNAL(SIG_USART_TRANS )
{
cli();
//uart_send( &bcd[0] ,3);//sizeof(gps_dat));
sei();
}
#ifndef UCPU_F
#define UCPU_F 11059200
#endif
void init_uart(uint16 baudrat)
{
uint16 j;
UCSRC&=(~(1<<URSEL)); //fosc/(16*baud)-1
j=(UCPU_F>>4)/baudrat-1;
UBRRH=j>>8;
UBRRL=j;//143; //11.0592==4800bps |(1<<TXCIE)
UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
//接收中斷使能,發送中斷使能,接收器與發送器使能
UCSRC=(1<<URSEL)|(3<<UCSZ0); //設置幀格式: 8 個數據位, 1 個停止位*/
uart_send1('o');uart_send1('k');
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -