/* Start user code for function. Do not edit comment generated here */
#include <stdint.h>
#include <stdbool.h>
extern volatile bool gMILLISECONDS_TIMER_EVENT_OCCURRED_FLAG;
extern volatile int8_t gROTATION_COUNTER;
/* End user code. Do not edit comment generated here */
タイマユニット /src/cg_src/r_cg_tau_user.c
1
2
3
/* Start user code for include. Do not edit comment generated here */
#include <stdbool.h>
/* End user code. Do not edit comment generated here */
/* Start user code for global. Do not edit comment generated here */
volatile bool gMILLISECONDS_TIMER_EVENT_OCCURRED_FLAG = false;
//
#define INITIAL_TIMER_EVENT_FLAGS (0xFFU)
volatile static uint8_t gTIMER_EVENT_FLAGS = INITIAL_TIMER_EVENT_FLAGS;
//
#define MIN_ROTATION_COUNT (-90)
#define MAX_ROTATION_COUNT (90)
volatile int8_t gROTATION_COUNTER = 0;
//
static inline void countup_rotation_counter(void) {
if (gROTATION_COUNTER < MAX_ROTATION_COUNT) {
gROTATION_COUNTER++;
}
}
//
static inline void countdown_rotation_counter(void) {
if (gROTATION_COUNTER > MIN_ROTATION_COUNT) {
gROTATION_COUNTER--;
}
}
#define ENCORDER_B_BIT (P0_bit.no0)
#define ENCORDER_A_BIT (P0_bit.no1)
#define ENCORDER_AB_BIT (P0 & 3)
volatile static uint8_t gCAPTURED_ENCORDER = 0U;
//
static void sample_encoder(void) {
static const int8_t dir[] = { 0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1,
1, 0 };
static int8_t i;
int8_t n;
i = (i << 2) + ENCORDER_AB_BIT;
n = dir[i & 15];
if (n < 0) {
countdown_rotation_counter();
} else if (n > 0) {
countup_rotation_counter();
}
}
static void capture_encorder(void) {
gCAPTURED_ENCORDER = (gCAPTURED_ENCORDER << 2) | ENCORDER_AB_BIT;
gCAPTURED_ENCORDER &= 15;
switch (gCAPTURED_ENCORDER) {
case 0x7:
countup_rotation_counter();
break;
case 0xd:
countdown_rotation_counter();
break;
default:
break;
}
}
/* End user code. Do not edit comment generated here */
1
2
3
4
5
6
7
8
9
10
11
static void __near r_tau0_channel2_interrupt(void) {
/* Start user code. Do not edit comment generated here */
sample_encoder();
// タイマー割り込みを8分周する
gTIMER_EVENT_FLAGS <<= 1;
if (gTIMER_EVENT_FLAGS == 0U) {
gMILLISECONDS_TIMER_EVENT_OCCURRED_FLAG = true;
gTIMER_EVENT_FLAGS = INITIAL_TIMER_EVENT_FLAGS;
}
/* End user code. Do not edit comment generated here */
}
/src/cg_src/r_cg_main.c
1
2
3
4
5
/* Start user code for include. Do not edit comment generated here */
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
/* End user code. Do not edit comment generated here */
void main(void) {
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
// 100ms待つ
delay(100);
// LCDの初期化
AQM1602A_init();
// 1行目
AQM1602A_puts("RL78 R5F10Y47ASP");
//
gROTATION_COUNTER = 0;
while (1U) {
// 2行目
char message[17];
strcpy(&message[0], "Angle = ");
to_string(&message[8], gROTATION_COUNTER);
strncat(message, " deg ", 5);
//
AQM1602A_send_command(0x80 | 0x40); // アドレス設定
AQM1602A_puts(message);
//
set_servo_angle(gROTATION_COUNTER);
delay(100);
}
/* End user code. Do not edit comment generated here */
}
1
2
3
4
5
6
7
static void R_MAIN_UserInit(void) {
/* Start user code. Do not edit comment generated here */
R_TAU0_Channel0_Start();
R_TAU0_Channel2_Start();
EI();
/* End user code. Do not edit comment generated here */
}