?? com.c
字號:
/*******************************************************************************
* 標題: 藍海微芯LJD-SY-5200單片機開發系統演示程序 *
* 文件: COM_INT.C *
* 日期: 2006-8-12 *
* 版本: 1.0 *
* 作者: 藍海微芯 *
* 網站: http://www.ljd-2008.com *
********************************************************************************
* 描述: *
* 串口收發測試程序 *
* *
* *
********************************************************************************
* 【版權】 Copyright(C)微芯科技 http://www.bluemcu.com All Rights Reserved *
* 【聲明】 此程序僅用于學習與參考,引用請注明版權和作者信息! *
*******************************************************************************/
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include <string.h>
#define MAX_RINTL 8 /* 串口接收緩沖區長度 */
typedef unsigned char uchar;
typedef unsigned int uint;
uchar data pint_buf[MAX_RINTL]; /* 串口接收緩沖區 */
uchar data pint_read; /* 串口緩沖區讀指針 */
uchar data pint_write; /* 串口緩沖區寫指針 */
uchar data psend_int; /* 串口發送允許標志 */
char pdata str_test[25] = ":0106001100153D";
char pdata str_hello[14] = " BLUE MCU";
char pdata str_world[30] = " WELCOME TO USE BLUE MCU";
/********************************************************************
函 數 名:com_int_proc()
功 能:串口中斷服務程序
說 明:
調 用:
入口參數:
出口參數:
***********************************************************************/
void com_int_proc(void) interrupt 4 using 3
{
uchar temp;
uchar temp1;
if (TI == 1) /* 是發送中斷 */
{
TI = 0;
psend_int = 1; /* 可以發送 */
}
if (RI == 1) /* 是接收中斷 */
{
RI = 0; /* 清串口接收中斷 */
temp1 = SBUF;
temp = pint_write + 1; /* 判斷是否可以寫入 */
if (temp == MAX_RINTL)
{
temp=0;
}
if (temp != pint_read)
{
pint_buf[pint_write] = temp1; /* 讀取數據 */
pint_write = temp;
}
}
}
/********************************************************************
函 數 名:com_send_command()
功 能:串口發送一個字節
說 明:
調 用:
入口參數:
出口參數:
***********************************************************************/
void com_send_command(char onebyte)
{
psend_int = 0;
SBUF = onebyte;
while (psend_int != 1);
}
/********************************************************************
函 數 名:com_command_receive()
功 能:串口接收數據處理
說 明:若收到數據0x0A,則發送字符串"BLUE MCU",若收到數據0x0D,
則發送字符串"WELCOME TO USE BLUE MCU"。
調 用:
入口參數:
出口參數:
***********************************************************************/
void com_command_receive(void)
{
uchar var1,var2;
uchar i;
var2 = pint_read;
if (var2 != pint_write)
{
var1=pint_buf[var2];
var2=var2+1;
if (var2 >= MAX_RINTL)
var2=0;
pint_read=var2;
if (var1 == 0x0A) /* 收到0x0A */
{
for (i=0; i<strlen(str_hello); i++)
{
com_send_command(str_hello[i]); /* 發送字符串HELLO */
}
com_send_command(0x0D);
return;
}
if (var1 ==0x0D) /* 收到0x0D */
{
for (i=0; i<strlen(str_world); i++)
{
com_send_command(str_world[i]); /* 發送字符串WORLD */
}
com_send_command(0x0D);
return;
}
}
}
/********************************************************************
函 數 名:system_init()
功 能:初始化子程序
說 明:
調 用:
入口參數:
出口參數:
***********************************************************************/
void system_init()
{
uchar loop;
EA = 0; /* CPU關中斷 */
pint_read = 0; /* 串口緩沖讀指針 */
pint_write = 0; /* 串口緩沖寫指針 */
SCON = 0x48;
PCON = 0x80;
TMOD = 0x20;
TCON = 0x50;
TH1 = 0xFD; /* 波特率為19200 */
TL1 = 0xFD;
TR1 = 1; /* 定時器1啟動計數*/
ES = 1; /* 串口開中斷 */
PS = 0; /* 串口低優先級 */
REN = 1; /* 串口接收允許 */
EA = 1; /* 開CPU中斷 */
loop = SBUF; /* 清串口緩沖區 */
for (loop=0; loop<MAX_RINTL; loop++)
{
pint_buf[loop] = 0;
}
}
main()
{
uchar i = 0;
system_init();
for (i=0; i<strlen(str_test); i++)
{
com_send_command(str_test[i]); /* 發送字符串 */
}
com_send_command(0x0D);
while(1)
{
com_command_receive();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -