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

📄 console.c

📁 演示了如何正确产生ucos2的滴答中断,以便于顺利移植ucos2到44b0上.对于ucos2初学者很有参考价值.
💻 C
字号:
#include <stdarg.h>

#include "inc\44b0x.h"
#include "DataType.h"
#include "console.h"

extern unsigned int MCLK;


 


void UartInit(int ch, int baud)
{
	U8 a;
	
	if( ( ch ) == 0 )
	{
		rUFCON0 = 0x0;     //禁止使用FIFO
    	rUMCON0 = 0x0;     //禁止使用FIFO
    	rULCON0 = 0x3;     //正常无奇偶校验,一个停止位,8个数据位
    	rUCON0 = 0x45;    //TX RX 都用PULSE非LEVEL中断    
   
	    //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
	    rUBRDIV0 = (int)(MCLK/(16.0 * baud)+0.5)-1;	   
	    a = rURXH0;
	}
	else
	{
		rUFCON1 = 0x0;     //禁止使用FIFO
	    rUMCON1 = 0x0;     //禁止使用FIFO
    	rULCON1 = 0x3;     //正常无奇偶校验,一个停止位,8个数据位
	    rUCON1 = 0x45;       
	    //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling	    
	    //baud *= 16;	    
	    rUBRDIV1 = (int)(MCLK/(16.0*baud)+0.5)-1;	    
	    a = rURXH1;
	}	
}

void UartSend(int ch, char data)
{
    int timeout=0;
	if(!ch)
	{
		while(!(rUTRSTAT0&0x2))
		 if (timeout++>300000) return;		//等待知道THR变空
		//WrUTXH0(data);
		rUTXH0 = data;
   	}
	else
	{
		while(!(rUTRSTAT1&0x2))
		  if (timeout++>300000) return;		//等待知道THR变空
		rUTXH1 = data;
    }	
}


/*	带有超时检测
int UartReceive(int ch)
{
    int i;
    i=0;
	if(!ch)
    {	    
		while(!(rUTRSTAT0&0x1)) 
		  if (i++>300000) return -1;
		return rURXH0;
    }
    else
    {
		while(!(rUTRSTAT1&0x1)) 
		  if (i++>300000) return -1;
		return	rURXH1;
    }
}

*/
int UartReceive(int ch)		//没有超时检测
{
	if(!ch)
    {	    
		while(!(rUTRSTAT0&0x1)) ;
		return rURXH0;
    }
    else
    {
		while(!(rUTRSTAT1&0x1)) ;
		return	rURXH1;
    }
}


int UartRxStat(int ch)
{
	if(!ch)    	    
		return (rUTRSTAT0&0x1);    
    else    
		return (rUTRSTAT1&0x1);		    
}

int UartGetch(int ch)
{
	 if(!ch)
    {	    
		if(rUTRSTAT0&0x1)		//如果收到字符就返回该字符
    	    return rURXH0;
		else
			return -1;			//如果没有收到字符就返回0
    }
    else
    {
		if(rUTRSTAT1&0x1)		//如果收到字符就返回该字符
		    return rURXH1;
		else
			return -1;			//如果没有收到字符就返回0
    }
}

int UartGetkey(int ch)
{
	return ch?rURXH1:rURXH0; 
}

void UartSendString(int ch, char *pt)
{
    while(*pt)
		UartSend(ch, *pt++);
}

/************************************************/
void console_init(int baud)
{
	UartInit(CONSOLE_UART, baud);
}

void putch(char data)
{
	UartSend(CONSOLE_UART, data);
}

void puts(char *str)
{
	while(*str)
		UartSend(CONSOLE_UART, *str++);
}

int getch()
{
	return UartReceive(CONSOLE_UART);
}

int getkey()
{
	return UartGetkey(CONSOLE_UART);
}

int kbhit()
{
	return UartRxStat(CONSOLE_UART);
}


typedef int *__va_list[1];       /* keep in step with <stdarg.h> */
extern int vsprintf(char * /*s*/, const char * /*format*/, __va_list /*arg*/);
/*
void printf(char *fmt, ...)
{
    va_list ap;
    char string[256];    
    va_start(ap, fmt);
    vsprintf(string, fmt, ap);
    va_end(ap);
}		
*/
void uartprintf(char *fmt, ...)
{
    va_list ap;
    char string[256];    
    va_start(ap, fmt);
    vsprintf(string, fmt, ap);
    
    UartSendString(CONSOLE_UART, string);
    va_end(ap);
}	

⌨️ 快捷键说明

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