⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.h

📁 在基于avr内核的单片机上实现MP3播放器的功能
💻 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 + -