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

📄 uart.c

📁 自己根据2410的串口修改的s3c2440的串口代码
💻 C
字号:
#include "def.h"    
#include "option.h"    
#include "2440addr.h"    
 

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>  


#define UART_BPS 115200
 
 
void  Delay(int  dly)
{  
	int  i;

   	for(; dly>0; dly--) 
       for(i=0; i<50000; i++);
}
 
    

static U8   g_uart_sel = 0;



int  UART_Select(U8  no)
{	
	int  ret;
	
	ret = g_uart_sel;
	g_uart_sel = no;
	return(ret);
}



void  UART_Init(void)
{  	
	int  i;

	if(g_uart_sel)			// 判断是否为串口1
	{	
		// I/O口设置 (GPH5,GPH4)
		rGPHUP = rGPHUP | (0x03<<4);
		rGPHCON = (rGPHCON & (~0x00000F00)) | (0x00000A00);	
	
		// 串口模式设置
		rUFCON1 = 0x00;   	// 禁止FIFO功能
		rUMCON1 = 0x00;   	// AFC(流控制)禁能
		rULCON1 = 0x03; 	// 禁止IRDA,无奇偶校验,1位停止位,8位数据位
		rUCON1  = 0x245; 	// 使用PCLK来生成波特率,发送中断为电平触发模式,接收中断为边沿触发模式,
	                    	// 禁止接收超时中断,使能接收错误中断,正常工作模式,中断或查询方式(非DMA)	
		// 串口波特率设置
		rUBRDIV1=(int)(PCLK/16.0/UART_BPS + 0.5) -1; 
	}
	else
	{	
		// I/O口设置 (GPH3,GPH2)
		rGPHUP = rGPHUP | (0x03<<2);
		rGPHCON = (rGPHCON & (~0x000000F0)) | (0x000000A0);	
	
		// 串口模式设置
		rUFCON0 = 0x00;   	// 禁止FIFO功能
		rUMCON0 = 0x00;   	// AFC(流控制)禁能
		rULCON0 = 0x03; 	// 禁止IRDA,无奇偶校验,1位停止位,8位数据位
		rUCON0  = 0x245; 	// 使用PCLK来生成波特率,发送中断为电平触发模式,接收中断为边沿触发模式,
	                    	// 禁止接收超时中断,使能接收错误中断,正常工作模式,中断或查询方式(非DMA)	
		// 串口波特率设置
		rUBRDIV0=(int)(PCLK/16.0/UART_BPS+0.5) -1; 
	}
	
   	for(i=0;i<100;i++);
}



void  UART_SendByte(U8 data)
{  	
	int  i;
   
    if(g_uart_sel)
    {	
    	while(!(rUTRSTAT1 & 0x02));	// 等待发送器为空
   		for(i=0; i<10; i++);
   		rUTXH1 = data;				// 发送数据
    }
    else
   	{	
   		while(!(rUTRSTAT0 & 0x02));	// 等待发送器为空
   		for(i=0; i<10; i++);
   		rUTXH0 = data;				// 发送数据
   	}   	
}



void  UART_SendStr(char const *str)
{ 	
	while(*str != '\0')
   	{ 	
   		if(*str == '\n') UART_SendByte('\r');   	  
     	  UART_SendByte(*str++);	    // 发送数据
   	}
}



int  UART_GetKey(void)
{  	
	int	 i;

	if(g_uart_sel)
	{	
		while(!(rUTRSTAT1 & 0x1));
		for(i=0; i<10; i++);
		return(rURXH1);
	}
	else	
	{	
		while(!(rUTRSTAT0 & 0x1));
		for(i=0; i<10; i++);
		return(rURXH0);
	}
}


  
void Main(void)  
{  
     int i,g_getch;
   
     rMPLLCON = (92<<12) | (1<<4) |(1);//FCLK=400M
     
     UART_Select(0);
     
     UART_Init(); 
     
    for(i=0; i<10; i++)

    {
        UART_SendStr("Hello World!\n");
    }
    
    while(1)
    {
        g_getch = UART_GetKey();   
        
        if(g_getch == 0x0D)         
        {
            UART_SendByte('\r');    
            UART_SendByte('\n');
        }
        else
        {
            UART_SendByte(g_getch);
        }
    }

}  
  
  

⌨️ 快捷键说明

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