-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
220 lines (168 loc) · 4.89 KB
/
Copy pathmain.c
File metadata and controls
220 lines (168 loc) · 4.89 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "gd32vf103.h"
#include "systick.h"
#include "ST7735.h"
#include "DHT11.h"
#include "CAN_settings.h"
#define RED_LED_PORT GPIOC
#define BLUE_LED_PORT GPIOA
#define GREEN_LED_PORT GPIOA
#define RED_LED_PIN GPIO_PIN_13
#define BLUE_LED_PIN GPIO_PIN_2
#define GREEN_LED_PIN GPIO_PIN_1
can_receive_message_struct rx_message;
uint8_t values[5];
void GPIO_setup(void);
void SPI_setup(void);
void CAN0_RX0_IRQHandler(void)
{
if(can_interrupt_flag_get(CAN0, CAN_INT_FLAG_RFL0))
{
can_message_receive(CAN0, CAN_FIFO0, &rx_message);
can_interrupt_flag_clear(CAN0, CAN_INT_FLAG_RFL0);
}
}
void main (void)
{
uint8_t text[40];
uint8_t state = 0;
uint8_t tx_data[2] = {0, 0};
uint8_t i =0;
uint8_t t = 0;
uint8_t rh = 0;
SystemInit();
SystemCoreClockUpdate();
eclic_global_interrupt_enable();
GPIO_setup();
SPI_setup();
DHT11_init();
ST7735_init();
TFT_fill(Black);
i = CAN_setup(DEVICE_ID_DHT11_SENSOR);
if(i == CAN_SUCCESS)
{
print_str(6, 13, 1, Light_Blue_2, Black, "GD32VF103 CAN Transceiver");
}
can_filter_config(DEVICE_ID_GPIO_MONITOR, 0xEF0);
//Mask IDs local device and only filter IDs from(0x100 - 0x10F)
print_str(1, 30, 2, Red, Black, "RH/%:");
print_str(1, 48, 2, Green, Black, "T/'C:");
print_str(1, 68, 2, Grey, Black, "RGB:");
while(1)
{
state = DHT11_get_data();
switch(state)
{
case 1:
{
print_str(120, 30, 2, Cyan, Black, "XX ");
print_str(120, 48, 2, Cyan, Black, "XX ");
tx_data[0] = 254;
tx_data[1] = 254;
break;
}
case 2:
{
print_str(120, 30, 2, Cyan, Black, "-- ");
print_str(120, 48, 2, Cyan, Black, "-- ");
tx_data[0] = 255;
tx_data[1] = 255;
break;
}
default:
{
rh = (values[0] + values[1]);
sprintf(text, "%2d ", rh);
print_str(120, 30, 2, White, Black, text);
t = (values[2] + values[3]);
sprintf(text, "%2d ", t);
print_str(120, 48, 2, White, Black, text);
tx_data[0] = rh;
tx_data[1] = t;
break;
}
}
can_send_message(DEVICE_ID_DHT11_SENSOR, tx_data, 2);
if(rx_message.rx_sfid == DEVICE_ID_GPIO_MONITOR)
{
switch(rx_message.rx_data[0])
{
case 1:
{
print_str(60, 68, 2, Red, Black, "*");
gpio_bit_reset(RED_LED_PORT, RED_LED_PIN);
break;
}
default:
{
print_str(60, 68, 2, Red, Black, "-");
gpio_bit_set(RED_LED_PORT, RED_LED_PIN);
break;
}
}
switch(rx_message.rx_data[1])
{
case 1:
{
print_str(90, 68, 2, Green, Black, "*");
gpio_bit_reset(GREEN_LED_PORT, GREEN_LED_PIN);
break;
}
default:
{
print_str(90, 68, 2, Green, Black, "-");
gpio_bit_set(GREEN_LED_PORT, GREEN_LED_PIN);
break;
}
}
switch(rx_message.rx_data[2])
{
case 1:
{
print_str(120, 68, 2, Light_Blue, Black, "*");
gpio_bit_reset(BLUE_LED_PORT, BLUE_LED_PIN);
break;
}
default:
{
print_str(120, 68, 2, Light_Blue, Black, "-");
gpio_bit_set(BLUE_LED_PORT, BLUE_LED_PIN);
break;
}
}
}
delay_1ms(1000);
};
}
void GPIO_setup(void)
{
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_GPIOC);
gpio_afio_deinit();
gpio_deinit(GPIOA);
gpio_deinit(GPIOB);
gpio_deinit(GPIOC);
gpio_init(RED_LED_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_10MHZ, RED_LED_PIN);
gpio_init(BLUE_LED_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_10MHZ, BLUE_LED_PIN);
gpio_init(GREEN_LED_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_10MHZ, GREEN_LED_PIN);
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, (GPIO_PIN_5 | GPIO_PIN_7));
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2));
}
void SPI_setup(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(RCU_SPI0);
spi_i2s_deinit(SPI0);
spi_struct_para_init(&spi_init_struct);
spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_2;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_crc_off(SPI0);
spi_init(SPI0, &spi_init_struct);
spi_enable(SPI0);
}