-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
100 lines (72 loc) · 3.09 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* --- QQ: 800003751 ----------------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "config.h"
#include "USART1.h"
#include "delay.h"
#define RELAY_ON P00=0;
#define RELAY_OFF P00=1;
/************* 功能说明 **************
双串口全双工中断方式收发通讯程序。
通过PC向MCU发送数据, MCU收到后通过串口把收到的数据原样返回.
******************************************/
/************* 本地常量声明 **************/
/************* 本地变量声明 **************/
/************* 本地函数声明 **************/
/************* 外部函数和变量声明 *****************/
/************* 串口1初始化函数 *****************/
void UART_config(void)
{
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //使用波特率, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate = 9600ul; //波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
COMx_InitStructure.UART_Polity = PolityLow; //中断优先级, PolityLow,PolityHigh
COMx_InitStructure.UART_P_SW = UART1_SW_P30_P31; //切换端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17(必须使用内部时钟)
COMx_InitStructure.UART_RXD_TXD_Short = DISABLE; //内部短路RXD与TXD, 做中继, ENABLE,DISABLE
USART_Configuration(USART1, &COMx_InitStructure); //初始化串口1 USART1,USART2
PrintString1("STC15F2K60S2 UART1 Test Prgramme!\r\n"); //SUART1发送一个字符串
}
/**********************************************/
void main(void)
{
u8 i;
UART_config();
EA = 1;
//初始化IO口,用P00 P01,分别控制两个继电器
P0M0=0;
P0M1=0;
//
while (1)
{
delay_ms(1);
if(COM1.RX_TimeOut > 0) //超时计数
{
if(--COM1.RX_TimeOut == 0)
{
if(COM1.RX_Cnt > 0)
{
for(i=0; i<COM1.RX_Cnt; i++) TX1_write2buff(RX1_Buffer[i]); //收到的数据原样返回
if(RX1_Buffer[0]=='U'){
RELAY_ON;
}
if(RX1_Buffer[0]=='D'){
RELAY_OFF;
}
}
COM1.RX_Cnt = 0;
}
}
}
}