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

📄 uart128.c

📁 AT45DB161D的测试程序.rar
💻 C
字号:
#include<global.h>
//#include<buffer.h>
#include<macros.h>
#include<uart128.h>
//#include<iom128v.h>
/*
#define TRUE 1
#define FALSE 0


// UART global variables
// flag variables
volatile u08   uartReadyTx[2];
volatile u08   uartBufferedTx[2];
 // receive and transmit buffers
cBuffer uartRxBuffer[2];
cBuffer uartTxBuffer[2];
unsigned short uartRxOverflow[2];
#ifndef UART_BUFFERS_EXTERNAL_RAM
     // using internal ram,
     // automatically allocate space in ram for each buffer
     static char uart0RxData[UART0_RX_BUFFER_SIZE];
     static char uart0TxData[UART0_TX_BUFFER_SIZE];
     static char uart1RxData[UART1_RX_BUFFER_SIZE];
     static char uart1TxData[UART1_TX_BUFFER_SIZE];
#endif
 
 typedef void (*voidFuncPtru08)(unsigned char);
 volatile static voidFuncPtru08 UartRxFunc[2];
 
void uartInit(void)
 {
     // initialize both uarts
     uart0Init();
     uart1Init();
 }
 */
 /*
void uart0Init(void)
 {
     // initialize the buffers
     uart0InitBuffers();
     // initialize user receive handlers
     UartRxFunc[0] = 0;
     // enable RxD/TxD and interrupts
//     outb(UCSR0B, (1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN));
		 UCSR0B=//*(1<<RXCIE)|(1<<TXCIE)|
		 (1<<RXEN)|(1<<TXEN);
     // set default baud rate
     uartSetBaudRate(0, UART0_DEFAULT_BAUD_RATE); 
     // initialize states
     uartReadyTx[0] = TRUE;
     uartBufferedTx[0] = FALSE;
     // clear overflow count
     uartRxOverflow[0] = 0;
     // enable interrupts
     SEI();
 }
 
void uart1Init(void)
 {
     // initialize the buffers
     uart1InitBuffers();
     // initialize user receive handlers
     UartRxFunc[1] = 0;
     // enable RxD/TxD and interrupts
   //  outb(UCSR1B, (1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN));
		 UCSR1B=//*(1<<RXCIE)|(1<<TXCIE)|
		 (1<<RXEN)|(1<<TXEN);
     // set default baud rate
     uartSetBaudRate(1, UART1_DEFAULT_BAUD_RATE);
     // initialize states
     uartReadyTx[1] = TRUE;
     uartBufferedTx[1] = FALSE;
     // clear overflow count
     uartRxOverflow[1] = 0;
     // enable interrupts
     SEI();
 }
 
void uart0InitBuffers(void)
 {
     #ifndef UART_BUFFERS_EXTERNAL_RAM
         // initialize the UART0 buffers
         bufferInit(&uartRxBuffer[0], uart0RxData, UART0_RX_BUFFER_SIZE);
         bufferInit(&uartTxBuffer[0], uart0TxData, UART0_TX_BUFFER_SIZE);
     #else
         // initialize the UART0 buffers
         bufferInit(&uartRxBuffer[0], (u08*) UART0_RX_BUFFER_ADDR, UART0_RX_BUFFER_SIZE);
         bufferInit(&uartTxBuffer[0], (u08*) UART0_TX_BUFFER_ADDR, UART0_TX_BUFFER_SIZE);
     #endif
 }
 
void uart1InitBuffers(void)
 {
     #ifndef UART_BUFFERS_EXTERNAL_RAM
         // initialize the UART1 buffers
         bufferInit(&uartRxBuffer[1], uart1RxData, UART1_RX_BUFFER_SIZE);
         bufferInit(&uartTxBuffer[1], uart1TxData, UART1_TX_BUFFER_SIZE);
     #else
         // initialize the UART1 buffers
         bufferInit(&uartRxBuffer[1], (u08*) UART1_RX_BUFFER_ADDR, UART1_RX_BUFFER_SIZE);
         bufferInit(&uartTxBuffer[1], (u08*) UART1_TX_BUFFER_ADDR, UART1_TX_BUFFER_SIZE);
     #endif
 }
 
void uartSetRxHandler(u08 nUart, void (*rx_func)(unsigned char c))
 {
     // make sure the uart number is within bounds
     if(nUart < 2)
     {
         // set the receive interrupt to run the supplied user function
         UartRxFunc[nUart] = rx_func;
     }
 }
 
void uartSetBaudRate(u08 nUart, u32 baudrate)
 {
     // calculate division factor for requested baud rate, and set it
     u08 baudrateDiv;
     baudrateDiv = (u08)((F_CPU+(baudrate*8L))/(baudrate*16L)-1);
     if(nUart)
         outb(UBRR1L, baudrateDiv);
     else
         outb(UBRR0L, baudrateDiv);
 }
 
cBuffer* uartGetRxBuffer(u08 nUart)
 {
     // return rx buffer pointer
     return &uartRxBuffer[nUart];
 }
 
cBuffer* uartGetTxBuffer(u08 nUart)
 {
     // return tx buffer pointer
     return &uartTxBuffer[nUart];
 }
 
void uartSendByte(u08 nUart, u08 txData)
 {
     // wait for the transmitter to be ready
 //  while(!uartReadyTx[nUart]);
     // send byte
     if(nUart)
     {
         while(!(UCSR1A & (1<<UDRE1)));//while(!(UCSR1A & (1<<UDRE)));
         outb(UDR1, txData);
     }
     else
     {
         while(!(UCSR0A & (1<<UDRE0)));//      while(!(UCSR0A & (1<<UDRE)));
         outb(UDR0, txData);
     }
     // set ready state to FALSE
     uartReadyTx[nUart] = FALSE;
 }
 
void uart0SendByte(u08 data)
 {
     // send byte on UART0
     uartSendByte(0, data);
 }
 
void uart1SendByte(u08 data)
 {
     // send byte on UART1
     uartSendByte(1, data);
 }
 
int uart0GetByte(void)
 {
     // get single byte from receive buffer (if available)
     u08 c;
     if(uartReceiveByte(0,&c))
         return c;
     else
         return -1;
 }
 
int uart1GetByte(void)
 {
     // get single byte from receive buffer (if available)
     u08 c;
     if(uartReceiveByte(1,&c))
         return c;
     else
         return -1;
 }
 
 
u08 uartReceiveByte(u08 nUart, u08* rxData)
 {
     // make sure we have a receive buffer
     if(uartRxBuffer[nUart].size)
     {
         // make sure we have data
         if(uartRxBuffer[nUart].datalength)
         {
             // get byte from beginning of buffer
             *rxData = bufferGetFromFront(&uartRxBuffer[nUart]);
             return TRUE;
         }
         else
             return FALSE;           // no data
     }
     else
         return FALSE;               // no buffer
 }
 
 void uartFlushReceiveBuffer(u08 nUart)
 {
     // flush all data from receive buffer
     bufferFlush(&uartRxBuffer[nUart]);
 }
 
 u08 uartReceiveBufferIsEmpty(u08 nUart)
 {
     return (uartRxBuffer[nUart].datalength == 0);
 }
 
 void uartAddToTxBuffer(u08 nUart, u08 data)
 {
     // add data byte to the end of the tx buffer
     bufferAddToEnd(&uartTxBuffer[nUart], data);
 }
 
 void uart0AddToTxBuffer(u08 data)
 {
     uartAddToTxBuffer(0,data);
 }
 
 void uart1AddToTxBuffer(u08 data)
 {
     uartAddToTxBuffer(1,data);
 }
 
 void uartSendTxBuffer(u08 nUart)
 {
     // turn on buffered transmit
     uartBufferedTx[nUart] = TRUE;
     // send the first byte to get things going by interrupts
     uartSendByte(nUart, bufferGetFromFront(&uartTxBuffer[nUart]));
 }
 
 u08 uartSendBuffer(u08 nUart, char *buffer, u16 nBytes)
 {
     register u08 first;
     register u16 i;
 
     // check if there's space (and that we have any bytes to send at all)
     if((uartTxBuffer[nUart].datalength + nBytes < uartTxBuffer[nUart].size) && nBytes)
     {
         // grab first character
         first = *buffer++;
         // copy user buffer to uart transmit buffer
         for(i = 0; i < nBytes-1; i++)
         {
             // put data bytes at end of buffer
             bufferAddToEnd(&uartTxBuffer[nUart], *buffer++);
         }
 
         // send the first byte to get things going by interrupts
         uartBufferedTx[nUart] = TRUE;
         uartSendByte(nUart, first);
         // return success
         return TRUE;
     }
     else
     {
         // return failure
         return FALSE;
     }
 }
 
 // UART Transmit Complete Interrupt Function
 void uartTransmitService(u08 nUart)
 {
     // check if buffered tx is enabled
     if(uartBufferedTx[nUart])
     {
         // check if there's data left in the buffer
         if(uartTxBuffer[nUart].datalength)
         {
             // send byte from top of buffer
             if(nUart)
                 //outb(UDR1,  bufferGetFromFront(&uartTxBuffer[1]) );
					 UDR1=bufferGetFromFront(&uartTxBuffer[1]) ;
             else
                 //outb(UDR0,  bufferGetFromFront(&uartTxBuffer[0]) );
                  UDR0=bufferGetFromFront(&uartTxBuffer[0]) ;
         }
         else
         {
             // no data left
             uartBufferedTx[nUart] = FALSE;
             // return to ready state
             uartReadyTx[nUart] = TRUE;
         }
     }
     else
     {
         // we're using single-byte tx mode
         // indicate transmit complete, back to ready
         uartReadyTx[nUart] = TRUE;
     }
 }
 
 // UART Receive Complete Interrupt Function
 void uartReceiveService(u08 nUart)
 {
     u08 c;
     // get received char
     if(nUart)
         c = UDR1;//inb(UDR1);
     else
         c = UDR0;//inb(UDR0);
 
     // if there's a user function to handle this receive event
     if(UartRxFunc[nUart])
     {
         // call it and pass the received data
         UartRxFunc[nUart](c);
     }
     else
     {
         // otherwise do default processing
         // put received char in buffer
         // check if there's space
         if( !bufferAddToEnd(&uartRxBuffer[nUart], c) )
         {
             // no space in buffer
             // count overflow
             uartRxOverflow[nUart]++;
         }
     }
 }
 /*
//中断定义 
 
UART_INTERRUPT_HANDLER(SIG_UART0_TRANS)      
 {
     // service UART0 transmit interrupt
     uartTransmitService(0);
 }
 
UART_INTERRUPT_HANDLER(SIG_UART1_TRANS)      
 {
     // service UART1 transmit interrupt
     uartTransmitService(1);
 }
 
UART_INTERRUPT_HANDLER(SIG_UART0_RECV)      
 {
     // service UART0 receive interrupt
     uartReceiveService(0);
 }
 
UART_INTERRUPT_HANDLER(SIG_UART1_RECV)      
 {
     // service UART1 receive interrupt
     uartReceiveService(1);
 }

*/


//this function convert char to ascii
//char2hex 把字符型转变成ACSII
char char2hex(char t1)
{
if((t1>=00) &&(t1<=0x09))t1=t1+0x30;//'0'--'9'
else 
{
 
 if((t1>=0x0a)&&(t1<=0x0f))//'A'--'F'
 t1=t1-0x0a+0x61;
}
return t1;
}

void sendinthex0(char c)
{

char t2=0;

t2=(c%256)/16;//templ&0xf0;
//t2=t2>>8;
UDR0 = char2hex(t2);
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

t2=(c%256)%16;//templ&0x0f;
UDR0 = char2hex(t2);
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

}
//sendinthex1把整型转化成4个ASCII输出
void sendinthex1(int c)
{
char temph=0,templ=0;
char t1=0,t2=0;

temph=c/256;
templ=c%256;

t1=(c/256)/16;
//t1=t1>>8;
UDR0 = char2hex(t1);
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

t1=(c/256)%16;
UDR0 = char2hex(t1);
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

t2=(c%256)/16;//templ&0xf0;
//t2=t2>>8;
UDR0 = char2hex(t2);
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

t2=(c%256)%16;//templ&0x0f;
UDR0 = char2hex(t2);
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

}


//发送字符
void sendchar1(char c) // 发送 
{
UDR0 = c;
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;
}



//发送整型常数
void sendint1( int c) // 发送 
{
UDR0 = (c&0xff00)>>8;
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;

UDR0 = c&0xff;
while(!(UCSR0A & 0x40));
UCSR0A |=0x40;
}

//发送字符串函数
void sendstring1(unsigned char * txbuf) // 发送 
{
unsigned int j;
for (j = 0; *txbuf; j++, txbuf++)sendchar1(*txbuf);
//for(;*txbuf!='/0';txbuf++)

}

//UART0 initialisation
// desired baud rate:115200
// actual baud rate:111111 (3.7%)
// char size: 8 bit
// parity: Disabled

void uart0_init(void)
{
 #ifdef MCUBAUD9600
// UBRRL = (fosc / 16 / (baud + 1)) % 256; 
// UBRRH = (fosc / 16 / (baud + 1)) / 256; 
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x67; //set baud rate lo
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0x18;
#else 
//baud115200
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = (F_CPU / 16 / (baud + 1)) % 256;//0x03;//0x08; //set baud rate lo
 UBRR0H = (F_CPU / 16 / (baud + 1)) / 256;//0x00; //set baud rate hi
 UCSR0B = 0x18;//0x98;
#endif 
 /*
//115200

 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;

// UBRRL = (fosc / 16 / (baud + 1)) % 256; 
// UBRRH = (fosc / 16 / (baud + 1)) / 256; 

 UBRR0L = (F_CPU / 16 / (baud + 1)) % 256;//0x03;//0x08; //set baud rate lo
 UBRR0H = (F_CPU / 16 / (baud + 1)) / 256;//0x00; //set baud rate hi
 UCSR0B = 0x18;//0x98;
 */
}

//UART1 initialisation
// desired baud rate:115200
// actual baud rate:111111 (3.7%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
 
#ifdef MCUBAUD9600
// UBRRL = (fosc / 16 / (baud + 1)) % 256; 
// UBRRH = (fosc / 16 / (baud + 1)) / 256; 
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = 0x67; //set baud rate lo
 UBRR1H = 0x00; //set baud rate hi
 UCSR1B = 0x18;
#else 
//baud115200
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = (F_CPU / 16 / (baud + 1)) % 256;//0x03;//0x08; //set baud rate lo
 UBRR1H = (F_CPU / 16 / (baud + 1)) / 256;//0x00; //set baud rate hi
 UCSR1B = 0x18;//0x98;
#endif 
}
signed int debug_check_rx(void)
{
return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -