1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PIC32にmruby/cでプログラムする(Lチカ)

Last updated at Posted at 2019-08-08

はじめに

とりあえず今回はタイマーの実装をしない

使用部品・回路

pickit3
PIC32MX170F256B-I/SP
・抵抗330,10k
・コンデンサ1u,10u
LED

電源ラインが1uF
左のソケットがPickit3 左から1ピン

pic_ブレッドボード.png

pic24と同じに見えますが、、
同じです。基本的に同じDIPの28pinなので似ています。
気になる方はマイコンのドキュメントを参照してくださいm(_ _"m)

手順

基本的にPIC24と同じ手順なので異なるところだけ書いておく。(私は不親切で大雑把)
https://www.s-itoc.jp/activity/research/mrubyc/mrubyc_news/842

・3.開発環境でコンパイラはXC16ではなくXC32を使用する
・4.1の4は[pickit3]を選択
・4.1の5はXC32コンパイラを選択
・MCCは無操作で「Generate」ボタンのみ押す(4.1の8,9はスキップ)
・4.1の12

XC32(GrovalOptions)=>Common include dirs
→src,src\hal
xc32-ld=>Heap size
→1024
xc32-ld=>Minimum stack size
→1024

・main.cとsample1.rbとhal.hは↓を使用
・4.1の17は無修正

プログラム

Lチカのみ

sample1.rb
while true
  led1_write( 1 )
  sleep 1
  led1_write( 0 )
  sleep 1
end
main.c
# include "mcc_generated_files/mcc.h"
# include "mrubyc.h"
# include <stdio.h>
# include <stdlib.h>

extern const uint8_t sample1[];
# define MEMORY_SIZE (1024*20)
static uint8_t memory_pool[MEMORY_SIZE];

static void c_led1_write(mrb_vm *vm, mrb_value *v, int argc) {
    int set_value = GET_INT_ARG(1);
    PORTBbits.RB5 = set_value;
}

int hal_write(int fd, const void *buf, int nbytes) {
    return 0;
}
int hal_flush(int fd) {
    return 0;
}

int main(void)
{
    SYSTEM_Initialize();
    TRISB = 0x0;

    mrbc_init(memory_pool, MEMORY_SIZE);
    mrbc_define_method(0, mrbc_class_object, "led1_write", c_led1_write);
    mrbc_create_task(sample1, 0);
    mrbc_run();
    return 1;
}
hal.h
# define MRBC_NO_TIMER

# ifndef MRBC_SRC_HAL_H_
# define MRBC_SRC_HAL_H_

# ifdef __cplusplus
extern "C" {
# endif

/***** Feature test switches ************************************************/
/***** System headers *******************************************************/
# include "../mcc_generated_files/mcc.h"
# include "../delay.h"


/***** Local headers ********************************************************/
/***** Constant values ******************************************************/
/***** Macros ***************************************************************/

# ifndef MRBC_NO_TIMER
# define hal_init()        ((void)0)
# define hal_enable_irq()  __builtin_disi(0x0000)
# define hal_disable_irq() __builtin_disi(0x3FFF)
# define hal_idle_cpu()    Idle()

# else // MRBC_NO_TIMER
# define hal_init()        ((void)0)
# define hal_enable_irq()  ((void)0)
# define hal_disable_irq() ((void)0)
# define hal_idle_cpu()    ((delay(1)), mrbc_tick())

# endif



/***** Typedefs *************************************************************/
/***** Global variables *****************************************************/
/***** Function prototypes **************************************************/
int hal_write(int fd, const void *buf, int nbytes);
int hal_flush(int fd);


/***** Inline functions *****************************************************/


# ifdef __cplusplus
}
# endif
# endif // ifndef MRBC_HAL_H_

xc32にはdelayがないので作る

xc16はdelayがありましたが、今回はないので作ります。
ただループをするだけです。
delay(1)でおよそ1ms

それぞれファイルを作ってプロジェクトの
Header Files
Source Files へ Add Existing item…してください

delay.h
# ifndef _EXAMPLE_FILE_NAME_H    /* Guard against multiple inclusion */
# define _EXAMPLE_FILE_NAME_H


/* Provide C++ Compatibility */
# ifdef __cplusplus
extern "C" {
# endif

void delay(int);

# ifdef __cplusplus
}
# endif

# endif /* _EXAMPLE_FILE_NAME_H */
delay.c
# include <stdio.h>
# include <stdlib.h>
# include "delay.h"

/*
 * 
 */
void delay(int n){ 
    n = n * 1000;
    while(n>0) {n--;}
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?