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 | #include <stdio.h>
#include "boards.h"
#include "app_uart.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#define ARDUINO_I2C_SCL_PIN 27
#define ARDUINO_I2C_SDA_PIN 26
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 1
#define DRV8830_ADDR_Motor1 0x64
#define DRV8830_ADDR_Motor2 0x63
#define CONTROL_ADDR 0x00
#define FALUT_ADR 0x01
#define MOTOR_STANBY 0x00
#define MOTOR_REVERSE 0x01
#define MOTOR_NORMAL 0x02
#define MOTOR_BRAKE 0x03
#define MAX_VOLTAGE 0x3F
#define MIN_VOLTAGE 0x01
/* TWI instance. */
static const nrf_drv_twi_t m_twi_DRV8830 = NRF_DRV_TWI_INSTANCE(0);
static void uart_events_handler(app_uart_evt_t * p_event)
{
switch (p_event->evt_type)
{
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
//UARTの優先度は低い
static void uart_config(void)
{
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_DISABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud38400
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_events_handler,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
}
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
}
//TWIの優先度が高い
void twi_init (void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_DRV8830_config = {
.scl = ARDUINO_SCL_PIN,
.sda = ARDUINO_SDA_PIN,
.frequency = NRF_TWI_FREQ_100K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH
};
err_code = nrf_drv_twi_init(&m_twi_DRV8830, &twi_DRV8830_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi_DRV8830);
}
int main(void)
{
uart_config();
twi_init();
printf("\n\r****************************************\r\n");
printf("*Fabo Shinobi Motor beta Firmware *\r\n");
printf("*Fabo 2017 *\r\n");
printf("*Version 0.0.0 *\r\n");
printf("****************************************\r\n");
nrf_delay_ms(300);
//DVR8830 初期設定
uint8_t reg[2] = {CONTROL_ADDR,0x00+(0x09<<2)};
nrf_drv_twi_tx(&m_twi_DRV8830, DRV8830_ADDR_Motor1, reg, sizeof(reg), false);
nrf_delay_ms(1);
nrf_drv_twi_tx(&m_twi_DRV8830, DRV8830_ADDR_Motor2, reg, sizeof(reg), false);
nrf_delay_ms(1000);
printf("\n\rForward Speed up\r\n");
//前進
for (uint8_t i = MIN_VOLTAGE; i <= MAX_VOLTAGE; i++) {
reg[1] = 0x02+(i<<2);
nrf_drv_twi_tx(&m_twi_DRV8830, DRV8830_ADDR_Motor1, reg, sizeof(reg), false);
nrf_delay_ms(1);
nrf_drv_twi_tx(&m_twi_DRV8830, DRV8830_ADDR_Motor2, reg, sizeof(reg), false);
printf("Speed=%d\n\r",i);
nrf_delay_ms(200);
}
printf("MAX Speed\n\r");
nrf_delay_ms(5000);
printf("\n\rForward Speed down\r\n");
//後進
for (uint8_t i = MAX_VOLTAGE; i >= MIN_VOLTAGE; i--) {
reg[1] = 0x02+(i<<2);
nrf_drv_twi_tx(&m_twi_DRV8830, DRV8830_ADDR_Motor1, reg, sizeof(reg), false);
nrf_delay_ms(1);
nrf_drv_twi_tx(&m_twi_DRV8830, DRV8830_ADDR_Motor2, reg, sizeof(reg), false);
printf("Speed=%d\n\r",i);
nrf_delay_ms(200);
}
printf("END.\n\r");
for(;;){
}
}
|