📄 serial.c
字号:
#include <stdarg.h>
#include <string.h>
#include "mylib.h"
void BT232_init(void)
{
BTIER=0x40; // Enable UART
BTLCR=0x83; // DLAB=1
BTDLL=8; BTDLH=0; // Baud rate=115200
//BTDLL=128; BTDLH=1; // Baud rate=2400
BTLCR=0x03; // No parity, Stop bit=1
GPDR1|=0x800; // Set GPIO[43]=BTTXD(output)
GPDR1&=~0x400; // Set GPIO[42]=BTRXD(input)
GAFR1_L|=0x900000; // bit23=bit20=1
GAFR1_L&=~0x600000; // bit22=bit21=0
BTIIR=0xC1; // Enable FIFO
GPDR0&=~0xF000000;///Set GPIO[24-27](INPUT)
GPDR0|=0x60; //Set GPIO[5-6](output)
//GPSR0=0xF000000;
}
void HW232_init(U32 k)
{
U8 BAUD[3];
BAUD[0]=8;BAUD[1]=24,BAUD[2]=96;//分别115200,38400,9600;
HWIER=0x40; // Enable UART
HWLCR=0x83; // DLAB=1
HWDLL=BAUD[k]; HWDLH=0; // Baud rate=115200
HWLCR=0x03; // No parity, Stop bit=1
GPDR1|=0x10000; // Set GPIO[48]=BTTXD(output)
GPDR1&=~0x20000; // Set GPIO[49]=BTRXD(input)
GAFR1_U|=0x05; // bit2=bit0=1
GAFR1_U&=~0x0A; // bit3=bit1=0
HWIIR=0xC1; // Enable FIFO
}
void Serial_init(void)
{
GPDR0|=0x10;
GPSR0=0x10;
FFIER=0x40; // Enable UART
FFLCR=0x83; // DLAB=1
FFDLL= 8 ; FFDLH=0; // Baud rate=115200
FFLCR=0x03; // No parity, Stop bit=1
GPDR1|=0x80; // Set GPIO[39]=FFTXD(output)
GPDR1&=~4; // Set GPIO[34]=FFRXD(input)
GAFR1_L|=0x8010; // bit15=bit4=1
GAFR1_L&=~0x4020; // bit14=bit5=0
FFIIR=0xC1; // Enable FIFO
BT232_init();
HW232_init(0);
}
void putc(char ch)
{
while((FFLSR&0x20)== 0); // Wait for TX FIFO free
FFTHR=ch;
}
int getc(void)
{
if(FFLSR&1)
return(FFRBR&0xFF);
else
return -1;
}
char get_key(void)
{
int ch;
while(1) {
ch = getc();
if(ch >0)
return ch;
}
return ch;
}
void putstr(char * buf)
{
while (*buf)
{
putc(*buf++);
}
}
typedef int *__va_list[1];
int vsprintf(char * /*s*/, const char * /*format*/, __va_list /*arg*/);
void printf(char *fmt, ...)
{
va_list ap;
char string[1024];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
putstr(string);
va_end(ap);
}
void myprint(char *pbuf, int len)
{
int i;
for(i = 0; i < len; i++) {
if((i & 0x0f) == 0)
printf("\r\n0X%.6x --->", i);
printf(" %.2x", pbuf[i]);
}
printf("\r\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -