TQ2440裸奔程序:串口UART的PC机按键测试程序
时间:10-02
整理:3721RD
点击:
TQ2440是由广州天嵌计算机科技有限公司精心打造的一款开发板。以稳定的性能,过硬的质量,丰富的扩展接口,优质的售后服务和技术支持,赢得众多企业的青睐和支持。
//=========================================
// NAME: main.c
// DESC: TQ2440串口UART测试程序
//=========================================
/* 头文件包含 */
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include
#include
#include
#include
#include
//======================================================
static volatile int uart_port = 0;
void uart_init(int pclk,int buad,int ch)
{
//UART初始化:端口使能、功能设定、波特率、设置数据格式
rGPHCON = (rGPHCON & ~(0xfff<<4)) | (0xaaa<<4);//端口RX[0:2]、TX[0:2]功能
rGPHUP = rGPHUP | (0x7<<1); //端口GPH[1:3]禁止上拉
rUFCON0 = 0x0; //禁止FIFO
rUFCON1 = 0x0; //禁止FIFO
rUFCON2 = 0x0; //禁止FIFO
rUMCON0 = 0x0; //禁止AFC
rUMCON1 = 0x0; //禁止AFC
//Normal:No parity:One stop:8-bits 中断响应 UART clock: PCLK
rULCON0 = (rULCON0 & ~0xff) | ((0x0<<6)|(0x0<<3)|(0x0<<2)|(0x3));
rUCON0 = (rUCON0 & ~0x3ff) | ((0x1<<9)|(0x1<<6)|(0x1<<2)|(0x1));
rUBRdiv0 = ((int)(pclk/16./buad+0.5)-1);
rULCON1 = (rULCON1 & ~0xff) | ((0x0<<6)|(0x0<<3)|(0x0<<2)|(0x3));
rUCON1 = (rUCON1 & ~0x3ff) | ((0x1<<9)|(0x1<<6)|(0x1<<2)|(0x1));
rUBRdiv1 = ((int)(pclk/16./buad+0.5)-1);
rULCON2 = (rULCON2 & ~0xff) | ((0x0<<6)|(0x0<<3)|(0x0<<2)|(0x3));
rUCON2 = (rUCON2 & ~0x3ff) | ((0x1<<9)|(0x1<<6)|(0x1<<2)|(0x1));
rUBRdiv2 = ((int)(pclk/16./buad+0.5)-1);
uart_port = ch; //设置串口端口号
}
//******************************************************
// 串口发送函数
//******************************************************
//======================================================
void uart_send_byte(int data)
{
if(0 == uart_port)
{
if(data == 'n')
{
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = 'n';
}
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = data;
}
else if(1 == uart_port)
{
if(data == 'n')
{
while(!(rUTRSTAT1 & 0x2));
rUTXH1 = 'n';
}
while(!(rUTRSTAT1 & 0x2));
rUTXH1 = data;
}
else if(2 == uart_port)
{
if(data == 'n')
{
while(!(rUTRSTAT2 & 0x2));
rUTXH2 = 'n';
}
while(!(rUTRSTAT2 & 0x2));
rUTXH2 = data;
}
}
//======================================================
void uart_send_string(char *string)
{
while(*string)
{
uart_send_byte(*string++);
}
}
//======================================================
void uart_printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
uart_send_string(string);
va_end(ap);
}
//******************************************************
// 串口接收函数
//******************************************************
//======================================================
void uart_tx_empty(void)
{
if(0 == uart_port)
{
while(!(rUTRSTAT0 & 0x4));//等待Tx为空
}
if(1 == uart_port)
{
while(!(rUTRSTAT1 & 0x4));//等待Tx为空
}
if(2 == uart_port)
{
while(!(rUTRSTAT2 & 0x4));//等待Tx为空
}
}
//======================================================
char uart_get_ch(void)
{
if(0 == uart_port)
{
while(!(rUTRSTAT0 & 0x1));//等待Rx被读
return rURXH0; //读缓存寄存器
}
else if(1 == uart_port)
{
while(!(rUTRSTAT1 & 0x1));//等待Rx被读
return rURXH1; //读缓存寄存器
}
else if(2 == uart_port)
{
while(!(rUTRSTAT2 & 0x1));//等待Rx被读
return rURXH2; //读缓存寄存器
}
return 0;
}
//======================================================
char uart_get_key(void)
{
if(0 == uart_port)
{
if(rUTRSTAT0 & 0x1) //Rx被读
{
return rURXH0; //读缓存寄存器
}
else
{
return 0;
}
}
if(1 == uart_port)
{
if(rUTRSTAT1 & 0x1) //Rx被读
{
return rURXH1; //读缓存寄存器
}
else
{
return 0;
}
}
if(2 == uart_port)
{
if(rUTRSTAT2 & 0x1) //Rx被读
{
return rURXH2; //读缓存寄存器
}
else
{
return 0;
}
}
return 0;
}
//======================================================
void uart_get_string(char *string)
{
char *string1 = string;
char c = 0;
while((c = uart_get_ch())!='r')
{
if(c == 'b')
{
if((int)string1 < (int)string)
{
uart_printf("bb");
string--;
}
}
else
{
*string++ = c;
uart_send_byte(c);
}
}
*string = '?';
uart_send_byte('n');
}
//======================================================
int uart_get_intnum(void)
{
char str[30];
char *string = str;
int base = 10;
int minus = 0;
int result = 0;
int last_index;
int i;
uart_get_string(string);
if(string[0] == '-')
{
minus = 1;
string++;
}
if(string[0] == '0' && (string[1] == 'x' || string[1] == 'X'))
{
base = 16;
string = string + 2;
}
last_index = strlen(string)-1;
if(last_index < 0)
{
return -1;
}
if(string[last_index] == 'h' || string[last_index] == 'H')
{
base = 16;
string[last_index] = 0;
last_index--;
}
if(base == 10)
{
//atoi将字符串转换为整型值
result = atoi(string);
result = minus ? (-1*result) : result;
}
else
{
for(i=0;i<=last_index;i++)
{
//判断字符是否为英文字母,当为英文字母a-z或A-Z时,返回非零值,否则返回零。
if(isalpha(string))
{
//isupper 判断字符是否为大写英文字母,当c为大写英文字母,返回非零值,否则返回零。
if(isupper(string))
{
result = (result<<4) + string-'A'+10;
}
else
{
result = (result<<4) + string-'a'+10;
}
}
else
{
result = (result<<4) + string-'0'+10;
}
}
result = minus ? (-1*result) : result;
}
return result;
}
//======================================================
int uart_get_intnum_gj(void)
{
char string[16];
char *p_string = string;
char c;
int i = 0;
int data = 0;
while((c = uart_get_ch()) != 'r')
{
if(c == 'b')
{
p_string--;
}
else
{
*p_string++ = c;
}
//uart_send_byte(c);
}
*p_string = '?';
i=0;
while(string != '?')
{
data = data*10;
if(string<'0' || string>'9')
{
return -1;
}
data = data +(string-'0');
i++;
}
return data;
}
//******************************************************
void Main(void)
{
uart_init(50000000,115200,0);
while(1)
{
if('-' == uart_get_ch())
{
uart_printf("+n");
}
if('+' == uart_get_key())
{
uart_printf("-n");
}
}
}
感谢晨旭晚霞的支持!
供货情况:
天嵌科技提供专业嵌入式板卡和行业解决方案。如有需要,可以联系天嵌科技的销售人员。
销售电话:020-38219416 38373101
技术支持:020-38219416转807 820
网址:http://www.embedsky.com
//=========================================
// NAME: main.c
// DESC: TQ2440串口UART测试程序
//=========================================
/* 头文件包含 */
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include
#include
#include
#include
#include
//======================================================
static volatile int uart_port = 0;
void uart_init(int pclk,int buad,int ch)
{
//UART初始化:端口使能、功能设定、波特率、设置数据格式
rGPHCON = (rGPHCON & ~(0xfff<<4)) | (0xaaa<<4);//端口RX[0:2]、TX[0:2]功能
rGPHUP = rGPHUP | (0x7<<1); //端口GPH[1:3]禁止上拉
rUFCON0 = 0x0; //禁止FIFO
rUFCON1 = 0x0; //禁止FIFO
rUFCON2 = 0x0; //禁止FIFO
rUMCON0 = 0x0; //禁止AFC
rUMCON1 = 0x0; //禁止AFC
//Normal:No parity:One stop:8-bits 中断响应 UART clock: PCLK
rULCON0 = (rULCON0 & ~0xff) | ((0x0<<6)|(0x0<<3)|(0x0<<2)|(0x3));
rUCON0 = (rUCON0 & ~0x3ff) | ((0x1<<9)|(0x1<<6)|(0x1<<2)|(0x1));
rUBRdiv0 = ((int)(pclk/16./buad+0.5)-1);
rULCON1 = (rULCON1 & ~0xff) | ((0x0<<6)|(0x0<<3)|(0x0<<2)|(0x3));
rUCON1 = (rUCON1 & ~0x3ff) | ((0x1<<9)|(0x1<<6)|(0x1<<2)|(0x1));
rUBRdiv1 = ((int)(pclk/16./buad+0.5)-1);
rULCON2 = (rULCON2 & ~0xff) | ((0x0<<6)|(0x0<<3)|(0x0<<2)|(0x3));
rUCON2 = (rUCON2 & ~0x3ff) | ((0x1<<9)|(0x1<<6)|(0x1<<2)|(0x1));
rUBRdiv2 = ((int)(pclk/16./buad+0.5)-1);
uart_port = ch; //设置串口端口号
}
//******************************************************
// 串口发送函数
//******************************************************
//======================================================
void uart_send_byte(int data)
{
if(0 == uart_port)
{
if(data == 'n')
{
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = 'n';
}
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = data;
}
else if(1 == uart_port)
{
if(data == 'n')
{
while(!(rUTRSTAT1 & 0x2));
rUTXH1 = 'n';
}
while(!(rUTRSTAT1 & 0x2));
rUTXH1 = data;
}
else if(2 == uart_port)
{
if(data == 'n')
{
while(!(rUTRSTAT2 & 0x2));
rUTXH2 = 'n';
}
while(!(rUTRSTAT2 & 0x2));
rUTXH2 = data;
}
}
//======================================================
void uart_send_string(char *string)
{
while(*string)
{
uart_send_byte(*string++);
}
}
//======================================================
void uart_printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
uart_send_string(string);
va_end(ap);
}
//******************************************************
// 串口接收函数
//******************************************************
//======================================================
void uart_tx_empty(void)
{
if(0 == uart_port)
{
while(!(rUTRSTAT0 & 0x4));//等待Tx为空
}
if(1 == uart_port)
{
while(!(rUTRSTAT1 & 0x4));//等待Tx为空
}
if(2 == uart_port)
{
while(!(rUTRSTAT2 & 0x4));//等待Tx为空
}
}
//======================================================
char uart_get_ch(void)
{
if(0 == uart_port)
{
while(!(rUTRSTAT0 & 0x1));//等待Rx被读
return rURXH0; //读缓存寄存器
}
else if(1 == uart_port)
{
while(!(rUTRSTAT1 & 0x1));//等待Rx被读
return rURXH1; //读缓存寄存器
}
else if(2 == uart_port)
{
while(!(rUTRSTAT2 & 0x1));//等待Rx被读
return rURXH2; //读缓存寄存器
}
return 0;
}
//======================================================
char uart_get_key(void)
{
if(0 == uart_port)
{
if(rUTRSTAT0 & 0x1) //Rx被读
{
return rURXH0; //读缓存寄存器
}
else
{
return 0;
}
}
if(1 == uart_port)
{
if(rUTRSTAT1 & 0x1) //Rx被读
{
return rURXH1; //读缓存寄存器
}
else
{
return 0;
}
}
if(2 == uart_port)
{
if(rUTRSTAT2 & 0x1) //Rx被读
{
return rURXH2; //读缓存寄存器
}
else
{
return 0;
}
}
return 0;
}
//======================================================
void uart_get_string(char *string)
{
char *string1 = string;
char c = 0;
while((c = uart_get_ch())!='r')
{
if(c == 'b')
{
if((int)string1 < (int)string)
{
uart_printf("bb");
string--;
}
}
else
{
*string++ = c;
uart_send_byte(c);
}
}
*string = '?';
uart_send_byte('n');
}
//======================================================
int uart_get_intnum(void)
{
char str[30];
char *string = str;
int base = 10;
int minus = 0;
int result = 0;
int last_index;
int i;
uart_get_string(string);
if(string[0] == '-')
{
minus = 1;
string++;
}
if(string[0] == '0' && (string[1] == 'x' || string[1] == 'X'))
{
base = 16;
string = string + 2;
}
last_index = strlen(string)-1;
if(last_index < 0)
{
return -1;
}
if(string[last_index] == 'h' || string[last_index] == 'H')
{
base = 16;
string[last_index] = 0;
last_index--;
}
if(base == 10)
{
//atoi将字符串转换为整型值
result = atoi(string);
result = minus ? (-1*result) : result;
}
else
{
for(i=0;i<=last_index;i++)
{
//判断字符是否为英文字母,当为英文字母a-z或A-Z时,返回非零值,否则返回零。
if(isalpha(string))
{
//isupper 判断字符是否为大写英文字母,当c为大写英文字母,返回非零值,否则返回零。
if(isupper(string))
{
result = (result<<4) + string-'A'+10;
}
else
{
result = (result<<4) + string-'a'+10;
}
}
else
{
result = (result<<4) + string-'0'+10;
}
}
result = minus ? (-1*result) : result;
}
return result;
}
//======================================================
int uart_get_intnum_gj(void)
{
char string[16];
char *p_string = string;
char c;
int i = 0;
int data = 0;
while((c = uart_get_ch()) != 'r')
{
if(c == 'b')
{
p_string--;
}
else
{
*p_string++ = c;
}
//uart_send_byte(c);
}
*p_string = '?';
i=0;
while(string != '?')
{
data = data*10;
if(string<'0' || string>'9')
{
return -1;
}
data = data +(string-'0');
i++;
}
return data;
}
//******************************************************
void Main(void)
{
uart_init(50000000,115200,0);
while(1)
{
if('-' == uart_get_ch())
{
uart_printf("+n");
}
if('+' == uart_get_key())
{
uart_printf("-n");
}
}
}
感谢晨旭晚霞的支持!
供货情况:
天嵌科技提供专业嵌入式板卡和行业解决方案。如有需要,可以联系天嵌科技的销售人员。
销售电话:020-38219416 38373101
技术支持:020-38219416转807 820
网址:http://www.embedsky.com
已学习,谢谢小编!
有些地方看不懂,我还是联系天嵌的大神。
教程挺多的,适合学习!
是的,我也这么认为,一直都是用天嵌的板子。
感谢小编分享!
看来2440的支持还是蛮多的嘛,可以考虑入手了哦~
小编多多分享点吧~
挺好用的,谢谢小编
可以,多种技术支持方式:电话、邮件、扣扣、旺旺、论坛等等
请继续关注我们吧,不定期更新技术文章,希望对你们有用