goo blog サービス終了のお知らせ 

忘備録-備忘録

技術的な備忘録

RX210で矩形波を出力する

2015-12-06 21:14:46 | RX210
RX210のタイマMTU0を使用してをMTIOC0A端子(PORT3-BIT4)から一定周波数の矩形波を出力するプログラムのサンプルです。

  1. #include "iodefine.h"
  2. #include <machine.h>
  3. #define PCLK 125                // 周辺機器のクロック設定 125kHz
  4. /*
  5. タイマの設定
  6. */
  7. void initMTU0cmt(void)
  8. {
  9.     SYSTEM.PRCR.WORD = 0x0A502;
  10.     MSTP(MTU0) = 0;                //MTU0 モジュールスタンバイ解除
  11.     SYSTEM.PRCR.WORD = 0x0A500;
  12. /* 端子の設定 */
  13.     PORT3.PODR.BIT.B4 = 0;        // P34 MTIOC0A 出力初期値
  14.     PORT3.PDR.BIT.B4 = 1;        // P34 出力設定
  15.     PORT3.PMR.BIT.B4 = 0;        // P34ポートとして使用
  16.     MPC.PWPR.BIT.B0WI = 0;        // PFSWE書き込み可
  17.     MPC.PWPR.BIT.PFSWE = 1;        // PFSレジスタへの書き込み可
  18.     MPC.P34PFS.BIT.PSEL = 1;    // P34をMTIOC0Aとして使用
  19.     MPC.PWPR.BIT.PFSWE = 0;        // PFSレジスタへの書き込み禁止
  20.     PORT3.PMR.BIT.B4 = 1;        // P34周辺機器として使用
  21. /* MTUタイマの設定 */
  22.     cycle = PCLK * 1000UL / 1000UL / 2UL;
  23.     MTU.TSTR.BIT.CST0 = 0x00;    //MTUカウント停止
  24.     MTU0.TCR.BIT.TPSC = 0x00;    // CLK PCLK/1 でカウント
  25.     MTU0.TCR.BIT.CCLR = 0x02;    //TCNT0はTGRBのコンペアマッチでクリア
  26.     MTU0.TMDR.BIT.MD = 0x00;    //タイマーノーマルモード
  27.     // 出力波形の設定
  28.     MTU0.TIORH.BIT.IOA = 0x03;    // MTIOC0A初期出力はLow出力コンペアマッチでトグル出力
  29. //    MTU0.TIORH.BIT.IOA = 0x00;    // MTIOC0A 出力禁止
  30.     MTU0.TIORH.BIT.IOB = 0x00;    // MTIOC0B 出力禁止
  31.     MTU0.TGRA = 0;                //出力ON-OFFのタイミング
  32.     MTU0.TGRB = 0;                //周波数の設定
  33.     MTU0.TCNT = 0;                //カウンタクリア
  34.     MTU.TSTR.BIT.CST0 = 0x00;    //タイマ停止
  35. }
  36. /*
  37. 波形の出力
  38.   引数 周波数
  39.  */
  40.  void setFrequency(int Frequency)
  41.  {
  42.      int sycle;
  43.      if(Frequency == 0) {
  44.         MTU.TSTR.BIT.CST0 = 0x00;        //タイマストップ
  45.      } else {
  46.         sycle = PCLK * 1000UL / Frequency / 2UL;
  47.         //MTU0.TCNT = 0;                //カウンタクリア
  48.         MTU0.TGRB = sycle;
  49.         MTU.TSTR.BIT.CST0 = 0x01;    //タイマスタート
  50.      }
  51.  }
  52. void main(void)
  53. {
  54.     unsigned int n=0;
  55. /* クロックは速いほうが正確な周波数になる */
  56.     //change_oscillation_PLL();            // クロックソースPLL
  57.     initMTU0cmt();
  58.     setFrequency(1000);                    // 1kHz出力
  59.     while(1) {
  60. /* ハードウェアで波形出力するのでプログラムでは何もしなくてもよい */
  61.     }
  62. }

RXマイコンでprintfを使う

2015-12-06 20:27:38 | RX210
開発環境はe2 studioでルネサスエレクトロニクス製のCコンパイラを使用しています。やはりprintf()が使えると開発に便利なのでprintf()を動作させる方法を調べてみました。
printf()ないでmalloc()を使用しているらしくヒープ領域が必要です。プロジェクト生成のウィザードでヒープ領域を設定します。どのくらい必要かは調べていません。


次に示すコードlowsrc.cを追加します。このファイルの最後に1文字分の入出力を行う関数charput(),charget()にコードを追加します。lowsrc.cはHEW開発環境では自動的に生成されるらしいです?。
main()関数かreset_program.c()内でlowsrc.cファイル内の入出力初期化用の関数_INIT_IOLIB()を呼び出します。

  1. void _INIT_IOLIB( void );
  2. main()
  3. {
  4.     _INIT_IOLIB();
  5.        .
  6.        .
  7.     printf("Hello world!\n");
  8.        .
  9.        .
  10. }

lowsrc.c


RX210のチップ内臓温度計を使う

2015-10-27 08:40:21 | RX210
ルネサス製のRX210マイコンのチップ内蔵の温度計からデータを取り出すサンプルプログラムです。

  1. #include "iodefine.h"
  2. #include <stdio.h>
  3. #include <machine.h>
  4. #include "rs232c.h"
  5.                                                                            
  6. /*
  7.  * ADコンバータと温度センサーの初期化
  8.  */
  9. void ad_init(void)
  10. {
  11.     SYSTEM.PRCR.WORD = 0xA502;                    /* protect off */
  12.     MSTP(S12AD) = 0;                            /* ADコンバータ */
  13.     MSTP(TEMPS) = 0;                             /* 温度センサー */
  14.     SYSTEM.PRCR.WORD = 0xA500;                    /* protect on */
  15.     //MPC.PWPR.BIT.B0WI = 0;                        /* enable to write PFSWE bit */
  16.     //MPC.PWPR.BIT.PFSWE = 1;                        /* enable to write PFS register */
  17.     //MPC.P40PFS.BIT.ASEL = 1;                    /* AN000 */
  18.     //MPC.P41PFS.BIT.ASEL = 1;                    /* AN001 */
  19.     //MPC.PWPR.BIT.PFSWE = 0;                        /* disable to write PFS register */
  20.     //MPC.PWPR.BIT.B0WI = 1;                        /* disable to write PFSWE bit */
  21.     S12AD.ADEXICR.BIT.TSS = 1;                    /* 温度センサー選択 */
  22.     S12AD.ADCSR.BIT.ADCS = 0;                    /* シングルスキャンモード */
  23.     S12AD.ADSSTRT = 100;                            /* アナログ入力サンプリング時間 */
  24.     S12AD.ADSTRGR.BIT.TRSA = 0x0A;                /* AD開始トリガー設定 温度センサからのトリガ */
  25.     S12AD.ADCSR.BIT.TRGE = 1;                    /* 温度センサーからのトリガーを有効 */
  26.     S12AD.ADCSR.BIT.EXTRG = 0;
  27.     TEMPS.TSCR.BIT.PGAGAIN = 0x01;                /* 電源電圧 3.3V */
  28. }
  29. /*
  30.  * 温度の測定
  31.  */
  32. unsigned int get_temp(void)
  33. {
  34.     volatile int i;
  35.     unsigned int temp;
  36.     TEMPS.TSCR.BIT.TSEN = 1;                    /* 温度センサー起動 */
  37.     for(i=0;i<1000;i++);                        /* 安定するまで待つ */
  38.     TEMPS.TSCR.BIT.PGAEN = 1;                    /* PGA動作 A/D変換スタート */
  39.     while(TEMPS.TSCR.BIT.PGAEN == 1);            /* A/D変換終了まで待つ */
  40.     temp = S12AD.ADTSDR;                        /* 結果の読み出し */
  41.     TEMPS.TSCR.BIT.TSEN = 0;                    /* 温度センサー停止 */
  42.     return temp;
  43. }
  44. /*
  45.  * メイン関数
  46.  */
  47. void main(void)
  48. {
  49.     char buf[256];
  50.     unsigned int t;
  51.     volatile int i;
  52.     change_oscillation_PLL();            //クロックソースPLL 50MHz
  53.     SCI_Init ();
  54.     ad_init();
  55.     setpsw_i();                            // 割込み許可 clrpsw_i()割込み禁止
  56.     printString("chip thermometer test program\n");
  57.     printString("use SCI0\n");
  58.     while(1) {
  59.         t = get_temp();
  60.         sprintf(buf,"temperature %x\n",t);
  61.         printString(buf);
  62.         for(i=0;i<800000;i++);
  63.     }
  64. }

RX210でRS232Cを使う その2

2015-10-27 08:33:09 | RX210
ルネサス製のRX210マイコンのSCI0を使用したシリアル通信プログラムのサンプルです。
リングバッファと割り込みを使用しています。

  1. /*
  2.  * interrupt_handlers.c のSCI0関連の割り込みをコメントアウトすること
  3.  * // SCI0 ERI0
  4.  * //void Excep_SCI0_ERI0(void){ }
  5.  *
  6.  * // SCI0 RXI0
  7.  * //void Excep_SCI0_RXI0(void){ }
  8.  *
  9.  * // SCI0 TXI0
  10.  * //void Excep_SCI0_TXI0(void){ }
  11.  *
  12.  * // SCI0 TEI0
  13.  * //void Excep_SCI0_TEI0(void){ }
  14.  *
  15.  */
  16. #include "iodefine.h"
  17. #include <stdio.h>
  18. #include <machine.h>
  19. #include "vect.h"
  20. #define PCLK 25            //PクロックMHz
  21. #define TX_ACTIVE 1    //送信動作中
  22. #define TX_INACTIVE 0    //送信停止中
  23. #define TX_RING_BUFF_SIZE    32    //リングバッファのサイズ
  24. #define RX_RING_BUFF_SIZE    32    //リングバッファのサイズ
  25. //リングバッファ
  26. unsigned char rx_buff[RX_RING_BUFF_SIZE];    //受信用
  27. unsigned char tx_buff[TX_RING_BUFF_SIZE];    //送信用
  28. //データポインタ
  29. volatile int ptr_rx_top,ptr_rx_bottom;
  30. volatile int ptr_tx_top,ptr_tx_bottom;
  31. //送信フラグ データ送信中かどうか
  32. volatile int tx_flag = TX_INACTIVE;
  33. /*
  34.  * SCI0初期化
  35.  * 57600bps: 8bit: stop bit 1: Parity none
  36.  * PCLK 25MHz
  37.  */
  38. void SCI_Init (void)
  39. {
  40.     /* ---- SCI interrupt request is disabled ---- */
  41.     IR(SCI0,ERI0) = 0;
  42.     IR(SCI0,RXI0) = 0;
  43.     IR(SCI0,TXI0) = 0;
  44.     IR(SCI0,TEI0) = 0;
  45.     /* ---- Initialization of SCI ---- */
  46.     /* PRCR - Protect Register
  47.     b15:b8 PRKEY - PRC Key Code - A5h (The write value should be A5h to permission writing PRCi bit)
  48.     b7:b4 Reserved - The write value should be 0.
  49.     b1 PRC1 - Protect Bit 1 - Write enabled */
  50.     SYSTEM.PRCR.WORD = 0xA502;
  51.     /* The module stop state of SCIn is canceled */
  52.     MSTP(SCI0) = 0;
  53.     /* Enable write protection */
  54.     SYSTEM.PRCR.WORD = 0xA500;
  55.     /* SCR - Serial Control Register
  56.     b7 TIE - Transmit Interrupt Enable - A TXI interrupt request is disabled
  57.     b6 RIE - Receive Interrupt Enable - RXI and ERI interrupt requests are disabled
  58.     b5 TE - Transmit Enable - Serial transmission is disabled
  59.     b4 RE - Receive Enable - Serial reception is disabled
  60.     b2 TEIE - Transmit End Interrupt Enable - A TEI interrupt request is disabled */
  61.     SCI0.SCR.BYTE = 0x00;
  62.     while (0x00 != (SCI0.SCR.BYTE & 0xF0))
  63.     {
  64.         /* Confirm that bit is actually 0 */
  65.     }
  66.     /* ---- Set the I/O port functions ---- */
  67.     /* Set port output data - High level */
  68.     PORT2.PODR.BIT.B0 = 1;
  69.     /* Set port direction - TXDn is output port, RXDn is input port */
  70.     PORT2.PDR.BIT.B0 = 1;
  71.     PORT2.PDR.BIT.B1 = 0;
  72.     /* Set port mode - Use pin as general I/O port */
  73.     PORT2.PMR.BIT.B1 = 0;
  74.     PORT2.PMR.BIT.B0 = 0;
  75.     /* PWPR - Write-Protect Register
  76.     b7 B0WI - PFSWE Bit Write Disable - Writing to the PFSWE bit is enabled
  77.     b6 PFSWE - PFS Register Write Enable - Writing to the PFS register is enabled
  78.     b5:b0 Reserved - These bits are read as 0. The write value should be 0. */
  79.     MPC.PWPR.BIT.B0WI = 0;
  80.     MPC.PWPR.BIT.PFSWE = 1;
  81.     /* PFS - Pin Function Control Register
  82.     b3:b0 PSEL - Pin Function Select - RXDn, TXDn */
  83.     MPC.P20PFS.BYTE = 0x0A;
  84.     MPC.P21PFS.BYTE = 0x0A;
  85.     /* Enable write protection */
  86.     MPC.PWPR.BIT.PFSWE = 0;
  87.     MPC.PWPR.BIT.B0WI = 1;
  88.     /* Use pin as I/O port for peripheral functions */
  89.     PORT2.PMR.BIT.B1 = 1;
  90.     PORT2.PMR.BIT.B0 = 1;
  91.     /* ---- Initialization of SCI ---- */
  92.     /* Select an On-chip baud rate generator to the clock source */
  93.     SCI0.SCR.BIT.CKE = 0;
  94.     /* SMR - Serial Mode Register
  95.     b7 CM - Communications Mode - Asynchronous mode
  96.     b6 CHR - Character Length - Selects 8 bits as the data length
  97.     b5 PE - Parity Enable - When transmitting : Parity bit addition is not performed
  98.                                           When receiving : Parity bit checking is not performed
  99.     b3 STOP - Stop Bit Length - 1 stop bits
  100.     b2 MP - Multi-Processor Mode - Multi-processor communications function is disabled
  101.     b1:b0 CKS - Clock Select - PCLK clock (n = 0) */
  102.     SCI0.SMR.BYTE = 0x00;
  103.     /* SCMR - Smart Card Mode Register
  104.     b6:b4 Reserved - The write value should be 1.
  105.     b3 SDIR - Transmitted/Received Data Transfer Direction - Transfer with LSB-first
  106.     b2 SINV - Transmitted/Received Data Invert - TDR contents are transmitted as they are.
  107.                                                           Receive data is stored as it is in RDR.
  108.     b1 Reserved - The write value should be 1.
  109.     b0 SMIF - Smart Card Interface Mode Select - Serial communications interface mode */
  110.     SCI0.SCMR.BYTE = 0xF2;
  111.     /* SEMR - Serial Extended Mode Register
  112.     b7:b6 Reserved - The write value should be 0.
  113.     b5 NFEN - Digital Noise Filter Function Enable - Noise cancellation function
  114.                                                               for the RXDn input signal is disabled.
  115.     b4 ABCS - Asynchronous Mode Base Clock Select - Selects 16 base clock cycles for 1-bit period
  116.     b3:b1 Reserved - The write value should be 0. */
  117.     SCI0.SEMR.BYTE = 0x00;
  118.     /* BRR - Bit Rate Register
  119.     Bit Rate: (25MHz/(64*2^(-1)*57600bps))-1=12.56 */
  120.     SCI0.BRR = 13;    /* 57600bps */
  121.     //SCI0.BRR = 40;    /* 19200bps */
  122.     /* ---- Initialization of SCI interrupt ---- */
  123.     /* SCI interrupt priority level is 1 */
  124.     IPR(SCI0, ) = 1;
  125.     /* Interrupt request is cleared (Edge interrupt) */
  126.     IR(SCI0,RXI0) = 0;
  127.     IR(SCI0,TXI0) = 0;
  128.     /* 割り込みの許可 */
  129.     IEN(SCI0, RXI0) = 1;
  130.     IEN(SCI0, ERI0) = 1;
  131.     IEN(SCI0, TXI0) = 1;
  132.     IEN(SCI0, TEI0) = 1;
  133.     /* リングバッファの初期化 */
  134.     ptr_rx_top = ptr_rx_bottom = 0;
  135.     ptr_tx_top = ptr_tx_bottom = 0;
  136.     /* 送受信許可 */
  137.     SCI0.SCR.BIT.RIE = 1;    //受信割込み
  138.     SCI0.SCR.BIT.TIE = 1;    //送信割込み
  139.     SCI0.SCR.BIT.RE = 1;    //受信動作開始
  140.     SCI0.SCR.BIT.TE = 0;
  141. }
  142. /*
  143.  * 受信エラー割り込み
  144.  */
  145. /* SSR - Serial Status Register
  146. b7:b6 Reserved - The read value is undefined. The write value should be 1.
  147. b5 ORER - Overrun Error Flag - An overrun error has occurred
  148. b4 FER - Framing Error Flag - A framing error has occurred
  149. b3 PER - Parity Error Flag - A parity error has occurred */
  150. #define SSR_ERROR_FLAGS (0x38)
  151. void Excep_SCI0_ERI0(void)
  152. {
  153.     volatile char c;
  154.     c = SCI0.RDR;    //ダミーリード
  155.     SCI0.SSR.BYTE = (SCI0.SSR.BYTE & ~SSR_ERROR_FLAGS) | 0xC0;    //エラーフラグクリア
  156. }
  157. /*
  158.  * 受信バッファフル割込み
  159.  */
  160. void Excep_SCI0_RXI0(void)
  161. {
  162.     /* Read data */
  163.     rx_buff[ptr_rx_top] = SCI0.RDR;
  164.     ptr_rx_top++;
  165.     ptr_rx_top = ptr_rx_top % RX_RING_BUFF_SIZE;
  166. }
  167. /*
  168.  * 送信バッファエンプティ割込み
  169.  */
  170. void Excep_SCI0_TXI0(void)
  171. {
  172.     if( ptr_tx_bottom == ptr_tx_top ) { //送信するデータがない
  173.         SCI0.SCR.BIT.TEIE = 1;            //送信終了割り込みの発生を待つ
  174.     } else {
  175.     /* Write the character out */
  176.         SCI0.TDR = tx_buff[ptr_tx_bottom];
  177.         ptr_tx_bottom++;
  178.         ptr_tx_bottom = ptr_tx_bottom % TX_RING_BUFF_SIZE;
  179.     }
  180. }
  181. /*
  182.  * 送信終了割り込み
  183.  */
  184. void Excep_SCI0_TEI0 (void)
  185. {
  186.     SCI0.SCR.BIT.TE = 0;    //送信停止
  187.     IR(SCI0,TXI0) = 0;        //割り込みフラグクリア
  188.     SCI0.SCR.BIT.TEIE = 0;    //送信完了割込み停止
  189.     tx_flag = TX_INACTIVE;    //送信回路フラグを停止に
  190. }
  191. /*
  192.  * データの送信
  193.  */
  194. void SCI_put(unsigned char output_char)
  195. {
  196.     int tmp;
  197.     tmp = ptr_tx_top + 1;
  198.     tmp = tmp % TX_RING_BUFF_SIZE;
  199.     while(tmp == ptr_tx_bottom) ;    //バッファに空きができるまで待つ
  200.     tx_buff[ptr_tx_top] = output_char;
  201.     ptr_tx_top++;
  202.     ptr_tx_top = ptr_tx_top % TX_RING_BUFF_SIZE;
  203.     if(tx_flag == TX_INACTIVE) {
  204.         tx_flag = TX_ACTIVE;    //送信回路フラグを動作に
  205.         SCI0.SCR.BIT.TE = 1;    //送信割込み許可
  206.      SCI0.SCR.BIT.TEIE = 0;    //送信完了割込み停止
  207.     }
  208. }
  209. void charput(unsigned char c)
  210. {
  211.     //while(IR(SCI0,TXI0)==0) ;
  212.     if(c=='\r' || c=='\n') {
  213.         SCI_put('\r');
  214.         SCI_put('\n');
  215.     } else {
  216.         SCI_put(c);
  217.     }
  218. }
  219. /* データの受信 バッファに受信したデータがなければ受信するまで待つ */
  220. unsigned int SCI_get(void)
  221. {
  222.     unsigned char c;
  223.     while(ptr_rx_bottom == ptr_rx_top); //データを受信するまで待つ
  224.     c = rx_buff[ptr_rx_bottom];
  225.     ptr_rx_bottom++;
  226.     ptr_rx_bottom = ptr_rx_bottom % RX_RING_BUFF_SIZE;
  227.     return c;
  228. }
  229. unsigned int charget(void)
  230. {
  231.      return SCI_get();
  232. }
  233. /*
  234.  * 文字列の出力
  235.  */
  236. void printString(char *s)
  237. {
  238.     while( *s != 0 ) {
  239.         charput(*s);
  240.         s++;
  241.     }
  242. }
  243. /*
  244.  * printf関数で使用
  245.  */
  246. int _write(int file,char *ptr,int len)
  247. {
  248.     int i;
  249.     for(i=0;i<len;i++) {
  250.         charput(ptr[i]);
  251.     }
  252.     return len;
  253. }
  254. /*
  255.  * scanf関数で使用
  256.  */
  257. int _read (int file, char *ptr, int len)
  258. {
  259.     *ptr = charget();
  260.      return 1;
  261. }
  262. void main(void)
  263. {
  264.     char buf;
  265.     change_oscillation_PLL();            //クロックソースPLL
  266.     SCI_Init ();
  267.     setpsw_i();                            // 割込み許可 clrpsw_i()割込み禁止
  268.     printString("RS232C test program\n");
  269.     printString("use SCI0123\n");
  270.     while(1) {
  271.         buf = charget();
  272.         _write(0,&buf,1);
  273.     }
  274. }


change_oscillation_PLL()関数
/*
 *  動作クロックをPLLで50MHzに設定
*/
void change_oscillation_PLL(void)
{
	unsigned int i;

	/* ---- Enable write protection ---- */
    /* PRCR - Protect Register
    b15:b8  PRKEY - PRC Key Code - A5h
                  (The write value should be A5h to permission writing PRCi bit)
    b7:b4   Reserved - The write value should be 0.
    b3      PRC3 - Protect Bit 3 - Write disabled
    b2      PRC2 - Protect Bit 2 - Write enabled
    b1      PRC1 - Protect Bit 1 - Write enabled
    b0      PRC0 - Protect Bit 0 - Write enabled */
    SYSTEM.PRCR.WORD = 0xA507;

    /* ---- Set the VRCR register ---- */
    SYSTEM.VRCR = 0x00;

    /* ----  Set the main clock oscillator drive capability ---- */
    /* MOFCR - Main Clock Oscillator Forced Oscillation Control Register
    b7       Reserved - The write value should be 0.
    b6       MOSEL    - Main Clock Oscillator Switch - Resonator
    b5:b4    MODRV2   - Main Clock Oscillator Drive Capability Switch 2
                      - 16 MHz to 20 MHz
    b3:b1    MODRV    - Main Clock Oscillator Drive Capability Switch
                      - 16 MHz to 20 MHz non-lead type ceramic resonator
    b0       Reserved - The write value should be 0. */
    SYSTEM.MOFCR.BYTE = (0x30);	/* Drive capability : 20 MHz crystal resonator */

    /* ---- Set wait time until the main clock oscillator stabilizes ---- */
    /* MOSCWTCR - Main Clock Oscillator Wait Control Register
    b7:b5    Reserved - The write value should be 0.
    b4:b0    MSTS     - Main Clock Oscillator Waiting Time
                      - Wait time is 131072 cycles (approx. 6.55 ms). */
    SYSTEM.MOSCWTCR.BYTE = (0x0D);	/* Wait control register : 131072 cycles (approx. 6.55 ms) */

    /* ---- Operate the main clock oscillator ---- */
    /* MOSCCR   - Main Clock Oscillator Control Register
    b7:b1    Reserved - The write value should be 0.
    b0       MOSTP    - Main Clock Oscillator Stop - Main clock oscillator is operating. */
    SYSTEM.MOSCCR.BYTE = 0x00;

    while (0x00 != SYSTEM.MOSCCR.BYTE)
    {
        /* Confirm that the written value can be read correctly. */
    }


    /* ---- Wait processing for the clock oscillation stabilization ---- */
    for(i=0;i<100;i++) nop();

    /* ---- Set the PLL division ratio and multiplication factor ---- */
    /* PLLCR - PLL Control Register
    b15:b13  Reserved - The write value should be 0.
    b12:b8   STC      - Frequency Multiplication Factor Select
                      - Frequency multiplication factor is multiply-by-10.
    b7:b2    Reserved - The write value should be 0.
    b1:b0    PLIDIV   - PLL Input Frequency Division Ratio Select
                      - PLL input division ratio is divide-by-2. */
    SYSTEM.PLLCR.WORD = (0x0901);	/* Division ratio and multiplication factor : divide-by-2, multiply-by-10 */

    /* ---- Set wait time until the PLL clock oscillator stabilizes ---- */
    /* PLLWTCR - PLL Wait Control Register
    b7:b5    Reserved - The write value should be 0.
    b4:b0    PSTS     - PLL Waiting Time
                      - Wait time is 65536 cycles (approx. 655.36 us). */
    SYSTEM.PLLWTCR.BYTE = (0x09);	/* Wait control register : 65536 cycles (approx. 655.36 us) */

    /* ---- Operate the PLL clock oscillator ---- */
    /* PLLCR2 - PLL Control Register 2
    b7:b1    Reserved - The write value should be 0.
    b0       PLLEN    - PLL Stop Control - PLL is operating. */
    SYSTEM.PLLCR2.BYTE = 0x00;

    /* ---- Wait processing for the clock oscillation stabilization ---- */
    for(i=0;i<100;i++) nop();

    /* ---- Set the operating power control mode ---- */
    /* OPCCR - Operating Power Control Register
    b7:b5   Reserved - The write value should be 0.
    b4      OPCMTSF  - Operating Power Control Mode Transition Status Flag
    b3      Reserved - The write value should be 0.
    b2:b0   OPCM     - Operating Power Control Mode Select - High-speed operating mode */
    SYSTEM.OPCCR.BYTE = (0x00);          /* High-speed operating mode */

    while (0 != SYSTEM.OPCCR.BIT.OPCMTSF)
    {
        /* Confirm that the operation power control mode transition completed. */
    }

    /* ---- Set the internal clock division ratio ---- */
    /* SCKCR - System Clock Control Register
    b31:b28 FCK      - FlashIF Clock(FCLK) Select - divide-by-4
    b27:b24 ICK      - System Clock (ICLK) Select - divide-by-2
    b23     PSTOP1   - BCLK Pin Output Control    - disabled. (Fixed high)
    b22:b20 Reserved - The write value should be 0.
    b19:b16 BCK      - External Bus Clock (BCLK) Select - divide-by-4
    b15:b12 Reserved - The write value should be 0001b.
    b10:b8  PCLKB    - Peripheral Module Clock B(PCLKB) Select - divide-by-4
    b7:b4   Reserved - The write value should be 0001b.
    b3:b0   PCLKD    - Peripheral Module Clock D(PCLKD) Select - divide-by-2 */
    SYSTEM.SCKCR.LONG = 0x21821211;	/* ICLK,PCLKD: divide-by-2 PCLKB,BCLK,FCLK: divide-by-4 */

    while (0x21821211 != SYSTEM.SCKCR.LONG)
    {
         /* Confirm that the written value can be read correctly. */
    }

    /* ---- Set the BCLK pin output ---- */
    /* BCKCR - External Bus Clock Control Register
    b7:b1   Reserved - The write value should be 0.
    b0      BCLKDIV  - BCLK Pin Output Select - divide-by-2 */
    SYSTEM.BCKCR.BYTE = 0x01;

    while (0x01 != SYSTEM.BCKCR.BYTE)
    {
        /* Confirm that the written value can be read correctly. */
    }

    /* ---- Set the internal clock source ---- */
    /* SCKCR3 - System Clock Control Register 3
    b15:b11 Reserved - The write value should be 0.
    b10:b8  CKSEL    - Clock Source Select - PLL circuit is selected.
    b7:b1   Reserved - The write value should be 0. */
    SYSTEM.SCKCR3.WORD = (0x0400);	/* PLL */

    while ((0x0400) != SYSTEM.SCKCR3.WORD)
    {
        /* Confirm that the written value can be read correctly. */
    }

    /* ---- Disable write protection ---- */
    /* PRCR - Protect Register
    b15:b8 PRKEY - PRC Key Code - A5h
                  (The write value should be A5h to permission writing PRCi bit)
    b2     PRC2 - Protect Bit 2 - Write disabled
    b1     PRC1 - Protect Bit 1 - Write disabled
    b0     PRC0 - Protect Bit 0 - Write disabled */
    SYSTEM.PRCR.WORD = 0xA500;

}
</pre>


RX210でRS232Cを使う

2015-10-15 17:57:35 | RX210
ルネサス製のRX210マイコンのSCI0を使用したシリアル通信プログラムのサンプルです。
割込みは使用していません。

  1. #include "iodefine.h"
  2. #include <stdio.h>
  3. #include <machine.h>
  4. /*
  5.  * SCI0初期化
  6.  * 57600bps: 8bit: stop bit 1: Parity none
  7.  * PCLK 25MHz
  8.  */
  9. void SCI_Init (void)
  10. {
  11.     /* ---- SCI interrupt request is disabled ---- */
  12.     IR(SCI0,ERI0) = 0;
  13.     IR(SCI0,RXI0) = 0;
  14.     IR(SCI0,TXI0) = 0;
  15.     IR(SCI0,TEI0) = 0;
  16.     /* ---- Initialization of SCI ---- */
  17.     /* PRCR - Protect Register
  18.     b15:b8 PRKEY - PRC Key Code - A5h (The write value should be A5h to permission writing PRCi bit)
  19.     b7:b4 Reserved - The write value should be 0.
  20.     b1 PRC1 - Protect Bit 1 - Write enabled */
  21.     SYSTEM.PRCR.WORD = 0xA502;
  22.     /* The module stop state of SCIn is canceled */
  23.     MSTP(SCI0) = 0;
  24.     /* Enable write protection */
  25.     SYSTEM.PRCR.WORD = 0xA500;
  26.     /* SCR - Serial Control Register
  27.     b7 TIE - Transmit Interrupt Enable - A TXI interrupt request is disabled
  28.     b6 RIE - Receive Interrupt Enable - RXI and ERI interrupt requests are disabled
  29.     b5 TE - Transmit Enable - Serial transmission is disabled
  30.     b4 RE - Receive Enable - Serial reception is disabled
  31.     b2 TEIE - Transmit End Interrupt Enable - A TEI interrupt request is disabled */
  32.     SCI0.SCR.BYTE = 0x00;
  33.     while (0x00 != (SCI0.SCR.BYTE & 0xF0))
  34.     {
  35.         /* Confirm that bit is actually 0 */
  36.     }
  37.     /* ---- Set the I/O port functions ---- */
  38.     /* Set port output data - High level */
  39.     PORT2.PODR.BIT.B0 = 1;
  40.     /* Set port direction - TXDn is output port, RXDn is input port */
  41.     PORT2.PDR.BIT.B0 = 1;
  42.     PORT2.PDR.BIT.B1 = 0;
  43.     /* Set port mode - Use pin as general I/O port */
  44.     PORT2.PMR.BIT.B1 = 0;
  45.     PORT2.PMR.BIT.B0 = 0;
  46.     /* PWPR - Write-Protect Register
  47.     b7 B0WI - PFSWE Bit Write Disable - Writing to the PFSWE bit is enabled
  48.     b6 PFSWE - PFS Register Write Enable - Writing to the PFS register is enabled
  49.     b5:b0 Reserved - These bits are read as 0. The write value should be 0. */
  50.     MPC.PWPR.BIT.B0WI = 0;
  51.     MPC.PWPR.BIT.PFSWE = 1;
  52.     /* PFS - Pin Function Control Register
  53.     b3:b0 PSEL - Pin Function Select - RXDn, TXDn */
  54.     MPC.P20PFS.BYTE = (0x0A);
  55.     MPC.P21PFS.BYTE = (0x0A);
  56.     /* Enable write protection */
  57.     MPC.PWPR.BIT.PFSWE = 0;
  58.     MPC.PWPR.BIT.B0WI = 1;
  59.     /* Use pin as I/O port for peripheral functions */
  60.     PORT2.PMR.BIT.B1 = 1;
  61.     PORT2.PMR.BIT.B0 = 1;
  62.     /* ---- Initialization of SCI ---- */
  63.     /* Select an On-chip baud rate generator to the clock source */
  64.     SCI0.SCR.BIT.CKE = 0;
  65.     /* SMR - Serial Mode Register
  66.     b7 CM - Communications Mode - Asynchronous mode
  67.     b6 CHR - Character Length - Selects 8 bits as the data length
  68.     b5 PE - Parity Enable - When transmitting : Parity bit addition is not performed
  69.                                           When receiving : Parity bit checking is not performed
  70.     b3 STOP - Stop Bit Length - 1 stop bits
  71.     b2 MP - Multi-Processor Mode - Multi-processor communications function is disabled
  72.     b1:b0 CKS - Clock Select - PCLK clock (n = 0) */
  73.     SCI0.SMR.BYTE = 0x00;
  74.     /* SCMR - Smart Card Mode Register
  75.     b6:b4 Reserved - The write value should be 1.
  76.     b3 SDIR - Transmitted/Received Data Transfer Direction - Transfer with LSB-first
  77.     b2 SINV - Transmitted/Received Data Invert - TDR contents are transmitted as they are.
  78.                                                           Receive data is stored as it is in RDR.
  79.     b1 Reserved - The write value should be 1.
  80.     b0 SMIF - Smart Card Interface Mode Select - Serial communications interface mode */
  81.     SCI0.SCMR.BYTE = 0xF2;
  82.     /* SEMR - Serial Extended Mode Register
  83.     b7:b6 Reserved - The write value should be 0.
  84.     b5 NFEN - Digital Noise Filter Function Enable - Noise cancellation function
  85.                                                               for the RXDn input signal is disabled.
  86.     b4 ABCS - Asynchronous Mode Base Clock Select - Selects 16 base clock cycles for 1-bit period
  87.     b3:b1 Reserved - The write value should be 0. */
  88.     SCI0.SEMR.BYTE = 0x00;
  89.     /* BRR - Bit Rate Register
  90.     Bit Rate: (25MHz/(64*2^(-1)*57600bps))-1=12.56 */
  91.     SCI0.BRR = 13;    /* 57600bps */
  92.     //SCI0.BRR = 40;    /* 19200bps */
  93.     /* ---- Initialization of SCI interrupt ---- */
  94.     /* SCI interrupt priority level is 1 */
  95.     IPR(SCI0, ) = 1;
  96.     /* Interrupt request is cleared (Edge interrupt) */
  97.     IR(SCI0,RXI0) = 0;
  98.     IR(SCI0,TXI0) = 0;
  99.     /* 送受信許可 */
  100.     SCI0.SCR.BIT.RIE = 1; //受信割込み
  101.     SCI0.SCR.BIT.TIE = 1; //送信割込み
  102.     SCI0.SCR.BIT.RE = 1;
  103.     SCI0.SCR.BIT.TE = 1;
  104. }
  105. /*
  106.  * データの送信
  107.  */
  108. void SCI_put(unsigned char c)
  109. {
  110.     //while(IR(SCI0,TXI0)==0) ;
  111.     if(c=='\r' || c=='\n') {
  112.         while(IR(SCI0,TXI0)==0) ;
  113.         IR(SCI0,TXI0) = 0;
  114.         SCI0.TDR = '\r';
  115.         while(IR(SCI0,TXI0)==0) ;
  116.         IR(SCI0,TXI0) = 0;
  117.         SCI0.TDR = '\n';
  118.     } else {
  119.         while(IR(SCI0,TXI0)==0) ;
  120.         IR(SCI0,TXI0) = 0;
  121.         SCI0.TDR = c;
  122.     }
  123. }
  124. /*
  125.  * データの受信
  126.  */
  127. /* SSR - Serial Status Register
  128. b7:b6 Reserved - The read value is undefined. The write value should be 1.
  129. b5 ORER - Overrun Error Flag - An overrun error has occurred
  130. b4 FER - Framing Error Flag - A framing error has occurred
  131. b3 PER - Parity Error Flag - A parity error has occurred */
  132. #define SSR_ERROR_FLAGS (0x38)
  133. int SCI_get (void)
  134. {
  135.     int c;
  136.     if((SCI0.SSR.BYTE & SSR_ERROR_FLAGS)!=0) {    // 受信エラー
  137.         c = SCI0.RDR;    //ダミーリード
  138.         SCI0.SSR.BYTE = (SCI0.SSR.BYTE & ~SSR_ERROR_FLAGS) | 0xC0;    //エラーフラグクリア
  139.         IR(SCI0,ERI0) = 0;
  140.         return -1;
  141.     }
  142.     while(IR(SCI0,RXI0) == 0) ;
  143.     c = SCI0.RDR;
  144.     IR(SCI0,RXI0) = 0;
  145.      return c;
  146. }
  147. /*
  148.  * printf関数で使用
  149.  */
  150. int _write(int file,char *ptr,int len)
  151. {
  152.     int i;
  153.     for(i=0;i<len;i++) {
  154.         SCI_put(ptr[i]);
  155.     }
  156.     return len;
  157. }
  158. /*
  159.  * scanf関数で使用
  160.  */
  161. int _read (int file, char *ptr, int len)
  162. {
  163.     *ptr = SCI_get();
  164.      return 1;
  165. }
  166. void change_oscillation_PLL(void)
  167. {
  168.     unsigned int i;
  169.     /* ---- Enable write protection ---- */
  170.     /* PRCR - Protect Register
  171.     b15:b8 PRKEY - PRC Key Code - A5h
  172.                   (The write value should be A5h to permission writing PRCi bit)
  173.     b7:b4 Reserved - The write value should be 0.
  174.     b3 PRC3 - Protect Bit 3 - Write disabled
  175.     b2 PRC2 - Protect Bit 2 - Write enabled
  176.     b1 PRC1 - Protect Bit 1 - Write enabled
  177.     b0 PRC0 - Protect Bit 0 - Write enabled */
  178.     SYSTEM.PRCR.WORD = 0xA507;
  179.     /* ---- Set the VRCR register ---- */
  180.     SYSTEM.VRCR = 0x00;
  181.     /* ---- Set the main clock oscillator drive capability ---- */
  182.     /* MOFCR - Main Clock Oscillator Forced Oscillation Control Register
  183.     b7 Reserved - The write value should be 0.
  184.     b6 MOSEL - Main Clock Oscillator Switch - Resonator
  185.     b5:b4 MODRV2 - Main Clock Oscillator Drive Capability Switch 2
  186.                       - 16 MHz to 20 MHz
  187.     b3:b1 MODRV - Main Clock Oscillator Drive Capability Switch
  188.                       - 16 MHz to 20 MHz non-lead type ceramic resonator
  189.     b0 Reserved - The write value should be 0. */
  190.     SYSTEM.MOFCR.BYTE = (0x30);    /* Drive capability : 20 MHz crystal resonator */
  191.     /* ---- Set wait time until the main clock oscillator stabilizes ---- */
  192.     /* MOSCWTCR - Main Clock Oscillator Wait Control Register
  193.     b7:b5 Reserved - The write value should be 0.
  194.     b4:b0 MSTS - Main Clock Oscillator Waiting Time
  195.                       - Wait time is 131072 cycles (approx. 6.55 ms). */
  196.     SYSTEM.MOSCWTCR.BYTE = (0x0D);    /* Wait control register : 131072 cycles (approx. 6.55 ms) */
  197.     /* ---- Operate the main clock oscillator ---- */
  198.     /* MOSCCR - Main Clock Oscillator Control Register
  199.     b7:b1 Reserved - The write value should be 0.
  200.     b0 MOSTP - Main Clock Oscillator Stop - Main clock oscillator is operating. */
  201.     SYSTEM.MOSCCR.BYTE = 0x00;
  202.     while (0x00 != SYSTEM.MOSCCR.BYTE)
  203.     {
  204.         /* Confirm that the written value can be read correctly. */
  205.     }
  206.     /* ---- Wait processing for the clock oscillation stabilization ---- */
  207.     for(i=0;i<100;i++) nop();
  208.     /* ---- Set the PLL division ratio and multiplication factor ---- */
  209.     /* PLLCR - PLL Control Register
  210.     b15:b13 Reserved - The write value should be 0.
  211.     b12:b8 STC - Frequency Multiplication Factor Select
  212.                       - Frequency multiplication factor is multiply-by-10.
  213.     b7:b2 Reserved - The write value should be 0.
  214.     b1:b0 PLIDIV - PLL Input Frequency Division Ratio Select
  215.                       - PLL input division ratio is divide-by-2. */
  216.     SYSTEM.PLLCR.WORD = (0x0901);    /* Division ratio and multiplication factor : divide-by-2, multiply-by-10 */
  217.     /* ---- Set wait time until the PLL clock oscillator stabilizes ---- */
  218.     /* PLLWTCR - PLL Wait Control Register
  219.     b7:b5 Reserved - The write value should be 0.
  220.     b4:b0 PSTS - PLL Waiting Time
  221.                       - Wait time is 65536 cycles (approx. 655.36 us). */
  222.     SYSTEM.PLLWTCR.BYTE = (0x09);    /* Wait control register : 65536 cycles (approx. 655.36 us) */
  223.     /* ---- Operate the PLL clock oscillator ---- */
  224.     /* PLLCR2 - PLL Control Register 2
  225.     b7:b1 Reserved - The write value should be 0.
  226.     b0 PLLEN - PLL Stop Control - PLL is operating. */
  227.     SYSTEM.PLLCR2.BYTE = 0x00;
  228.     /* ---- Wait processing for the clock oscillation stabilization ---- */
  229.     for(i=0;i<100;i++) nop();
  230.     /* ---- Set the operating power control mode ---- */
  231.     /* OPCCR - Operating Power Control Register
  232.     b7:b5 Reserved - The write value should be 0.
  233.     b4 OPCMTSF - Operating Power Control Mode Transition Status Flag
  234.     b3 Reserved - The write value should be 0.
  235.     b2:b0 OPCM - Operating Power Control Mode Select - High-speed operating mode */
  236.     SYSTEM.OPCCR.BYTE = (0x00); /* High-speed operating mode */
  237.     while (0 != SYSTEM.OPCCR.BIT.OPCMTSF)
  238.     {
  239.         /* Confirm that the operation power control mode transition completed. */
  240.     }
  241.     /* ---- Set the internal clock division ratio ---- */
  242.     /* SCKCR - System Clock Control Register
  243.     b31:b28 FCK - FlashIF Clock(FCLK) Select - divide-by-4
  244.     b27:b24 ICK - System Clock (ICLK) Select - divide-by-2
  245.     b23 PSTOP1 - BCLK Pin Output Control - disabled. (Fixed high)
  246.     b22:b20 Reserved - The write value should be 0.
  247.     b19:b16 BCK - External Bus Clock (BCLK) Select - divide-by-4
  248.     b15:b12 Reserved - The write value should be 0001b.
  249.     b10:b8 PCLKB - Peripheral Module Clock B(PCLKB) Select - divide-by-4
  250.     b7:b4 Reserved - The write value should be 0001b.
  251.     b3:b0 PCLKD - Peripheral Module Clock D(PCLKD) Select - divide-by-2 */
  252.     SYSTEM.SCKCR.LONG = 0x21821211;    /* ICLK,PCLKD: divide-by-2 PCLKB,BCLK,FCLK: divide-by-4 */
  253.     while (0x21821211 != SYSTEM.SCKCR.LONG)
  254.     {
  255.          /* Confirm that the written value can be read correctly. */
  256.     }
  257.     /* ---- Set the BCLK pin output ---- */
  258.     /* BCKCR - External Bus Clock Control Register
  259.     b7:b1 Reserved - The write value should be 0.
  260.     b0 BCLKDIV - BCLK Pin Output Select - divide-by-2 */
  261.     SYSTEM.BCKCR.BYTE = 0x01;
  262.     while (0x01 != SYSTEM.BCKCR.BYTE)
  263.     {
  264.         /* Confirm that the written value can be read correctly. */
  265.     }
  266.     /* ---- Set the internal clock source ---- */
  267.     /* SCKCR3 - System Clock Control Register 3
  268.     b15:b11 Reserved - The write value should be 0.
  269.     b10:b8 CKSEL - Clock Source Select - PLL circuit is selected.
  270.     b7:b1 Reserved - The write value should be 0. */
  271.     SYSTEM.SCKCR3.WORD = (0x0400);    /* PLL */
  272.     while ((0x0400) != SYSTEM.SCKCR3.WORD)
  273.     {
  274.         /* Confirm that the written value can be read correctly. */
  275.     }
  276.     /* ---- Disable write protection ---- */
  277.     /* PRCR - Protect Register
  278.     b15:b8 PRKEY - PRC Key Code - A5h
  279.                   (The write value should be A5h to permission writing PRCi bit)
  280.     b2 PRC2 - Protect Bit 2 - Write disabled
  281.     b1 PRC1 - Protect Bit 1 - Write disabled
  282.     b0 PRC0 - Protect Bit 0 - Write disabled */
  283.     SYSTEM.PRCR.WORD = 0xA500;
  284. }
  285. void main(void)
  286. {
  287.     int buf;
  288.     change_oscillation_PLL();            //クロックソースPLL
  289.     SCI_Init ();
  290.     _write(0,"RS232C test program\n",20);
  291.     _write(0,"use SCI0\n",9);
  292.     while(1) {
  293.         buf = SCI_get();
  294.         SCI_put(buf);
  295.     }
  296. }