-
Notifications
You must be signed in to change notification settings - Fork 0
/
Com.c
179 lines (156 loc) · 2.93 KB
/
Com.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Com.h"
#include "Delay.h"
void ComInit(void) {
//Watch
PA_DDR_DDR2 = 0;
PA_CR1_C12 = 0;
PA_CR2_C22 = 1;
//外部中断初始化
EXTI_CR1 &= ~BIT(2);//开启PA口中断
EXTI_CR1 &= ~BIT(3);
}
#define COM_BIT_OUT PA_ODR_ODR2
#define COM_BIT_IN PA_IDR_IDR2
#define COM_BIT_DR PA_DDR_DDR2
#define COM_BIT_INT PA_CR2_C22
u8 ComSend(u8 data[]) {
u16 wait = 0;
u8 data_t = 0;//保存临时值
u8 i = 0,j = 0;
COM_BIT_INT = 0;//中断
COM_BIT_DR = 1;//设置为输出
COM_BIT_OUT = 0;
DelayUs(100);//拉低20ms说明总线开始
COM_BIT_DR = 0;//设置为输入
DelayUs(1);
while(COM_BIT_IN == 1) {//等待从机拉高
if(wait < 100) {
wait++;
} else {//超时,退出
COM_BIT_INT = 1;//中断
return 0;
}
}
wait = 0;
while(COM_BIT_IN == 0) {
if(wait < 100) {
wait++;
} else {//超时,退出
return 0;
}
}
COM_BIT_DR = 1;//设置为输出
for(j = 0;j < 5;j++) {
data_t = data[j];
for(i=0;i<8;i++) {
COM_BIT_OUT = 0;
if(data_t&0x80) {
DelayUs(80);
} else {
DelayUs(40);
}
COM_BIT_OUT = 1;
DelayUs(20);
data_t<<=1;
}
}
DelayUs(60);
COM_BIT_OUT = 1;
COM_BIT_INT = 1;//中断
COM_BIT_DR = 0;//设置为输入
return 0;
}
u8 ComRead(u8 data_s[]) {
static u16 wait = 0;
u8 i = 0,j = 0;
u8 data = 0;
wait = 0;
COM_BIT_DR = 0;//设置为输入
while(COM_BIT_IN == 0) {
if(wait < 60) {
wait++;
} else {
return 0x44;
}
}
if(wait > 25) {
wait = 0;
COM_BIT_DR = 1;//设置为输出
COM_BIT_OUT = 0;
DelayUs(1);
COM_BIT_DR = 0;//设置为输入
//开始接受数据
for(j = 0;j < 5;j++) {
for(i=0;i<8;i++) {
data<<=1;
while(COM_BIT_IN == 1) {
if(wait < 200) {
wait++;
} else {
return 0x44;
}
}
wait = 0;
while(COM_BIT_IN == 0) {
if(wait < 200) {
wait++;
} else {
return 0x44;
}
}
if(wait > 30) {//为1
data|=0x01;
}
wait = 0;
}
data_s[j] = data;
data = 0;
}
if(data_s[4] == (data_s[0]+data_s[1]+data_s[2]+data_s[3])) {//累加校验
if(data_s[4] != 0) {
return 0x88;
} else {
return 0x44;
}
} else {
return 0x44;
}
} else {//时间不对 推出
return 0x44;
}
}
static u8 com_date[5];
static u8 rs_ok = 0;//whether it has received data
u8 ComGetData(u8 num) {
return com_date[num];
}
u8 ComGetFlag(void) {
return rs_ok;
}
void ComClearFlag(void) {
rs_ok = 0;
}
void ComSendCmdWatch(u8 cmd,u8 par1,u8 par2,u8 par3) {
u8 com_t_data[5] = {0,0,0,0,0};//前拨
u8 com_t_data2[5] = {0,0,0,0,0};//前拨
com_t_data[0] = cmd; //cmd
com_t_data[1] = par1;
com_t_data[2] = par2;
com_t_data[3] = par3;
com_t_data[4] = com_t_data[0]+com_t_data[1]+com_t_data[2]
+com_t_data[3];
INTOFF
ComSend(com_t_data2);
ComSend(com_t_data);
INTEN
}
#pragma vector=12
__interrupt void EXTI2_IRQHandler(void)
{
INTOFF
if(ComRead(com_date) == 0x88) {
rs_ok = 0x80;
}
EXTI_SR1 = 0xff;
INTEN
}