LED制御 ― KPIT GNU Eclipseの操作手順例/Controlling the LED – Example step by step for Kpit GNU Eclipse

Posted on November 20 2009 by admin

For this example we have created a test project called PE.

この例では、PEというテストプロジェクトを作成しました。

We will see how to set the proper registers in order to simply control the LEDs on board of the SH2 Tiny Controller.

This tutorial can be applied also to users making use of HEW Renesas. The hardware settings of the micro-controller are the same.

User can refer also to the sample programs available on HiBot page under the download/link section.

Also the hardware manual of the SH2 7046 microprocessor should be ready for consultation for this tutorial.

TITech SH2 Tiny ControllerのオンボードLEDを簡単に制御するのに適したレジスタの設定方法を見ていきましょう。
このチュートリアルは、ルネサス統合開発環境を使用している場合にも適用できます。マイコンのハードウェア設定は同じです。
ハイボットのWebページの「DOWNLOAD & LINKS」セクションから入手可能なサンプルプログラムも参照してください。
また、このチュートリアルの参考としてSH2 7046マイクロプロセッサのハードウェアマニュアルをご参照ください。

1. From SH2 Tiny Controller manual Appendix A, we see that LEDs are connected as in the following picture / TITech SH2 Tiny Controllerのマニュアルの付録Aから、LEDは下図のように接続されていることを確認できます。

fig0

Therefore by setting the PE 20 and PE21 as Digital Output we can control the both the LEDs. Supplying in fact 0 volt with the output (digital level LOW 0) we would allow the differential potential on the LEDs with the consequent switching on. On the other hand if we supply 5volt (digital level HIGH 1) we would create a zero differential potential and the LEDs are going to be switched off.

このため、PE20とPE21をデジタル出力として設定すると、両方のLEDを制御できます。実際に、出力で0 Vを供給すると(デジタルレベルLOW 0)、LED上で差動電位が許容され、結果としてスイッチがオンになります。これに対して、5 Vを供給すると(デジタルレベルHIGH 1)、差動電位がゼロとなり、LEDのスイッチがオフになります。

2. On the SH2 7046 hardware manual in the section 16.1.6 (Port E Control Registers L1, L2, and H (PECRL1, PECRL2, and PECRH)) Pin Function Controller we check the possible functions settable for pins PE20 and PE21.

SH2 7046のハードウェアマニュアルの「16.1.6 (Port E Control Registers L1, L2, and H (PECRL1, PECRL2, and PECRH)) Pin Function Controller」の項で、ピンPE20およびPE21に設定可能な機能を確認します。

fig1

For PE20 as marked in red, the possible modes are: I/O port or PVOB output (MMT). The first mode I/O shows the 0 and 0 that are related to respectively the bit 8th and to the 9th bit of PCRH register. So by setting those two bits to 0 we will select the first mode I/O Port.

PE20で使用できるモードは、赤のマーカが引かれているI/OポートまたはPVOB出力(MMT)です。1つ目のモードI/Oは、それぞれPCRHレジスタの8番目と9番目のビットに関連する0を示します。したがって、この2つのビットに0を設定することにより、1つ目のモード「I/Oポート」を選択できます。

3. To find the right register name we have to take a look at the file iodefine.h. Open it by double clicking on its name of the Project Explorer window on the left of the Eclipse Environment.

正しいレジスタ名を見つけるには、ファイルiodefine.hを参照する必要があります。Eclipse Environmentの左にあるProject Explorerウィンドウで名前をダブルクリックして、このファイルを開きます。

fig1a

All registers and the ways of accessing them are defined inside this file. In particular the Port Function Control register (PECRH that we have to set) is defined as the following structure:

このファイルには、すべてのレジスタおよびレジスタのアクセス方法が定義されています。特に、ポート機能制御レジスタ(設定する必要があるPECRH)は、以下の構造体として定義されています。

fig2b

inside the structure struct PFC, we can utilize among the others the union PFC. Inside it, we can access to each single bit PExxMD by assigning both the bits, or by a single WORD of 16bits that would set all the bits for the PECRH in this way:

構造体struct PFCの内部では、特に共用体PFCを使用できます。この構造体の内部では、両ビットを割り当てるか、PECRHのすべてのビットを以下のように設定する16ビットの1 WORDによって、各単一ビットPExxMDにアクセスできます。

PFC.PECRH.WORD    = 0×0000;  /* 0000 0000 0000 0000 */

Starting counting the bits from the Right most bit, the two 0 in red are respectively Bit 8th and 9th of the register PECRH. The green bits are set to zero as restricted by the hardware manual in the same section. Other bits are in this example set to 0 as example. Let’s write then that line inside hwint.c as we can see in the following figure, when we press the dot, a menu will automatically appear allowing us to select the register we want to utilize:

右端のビットから数えると、赤色の2つの0がそれぞれレジスタPECRHの8番目と9番目のビットです。緑色のビットには、ハードウェアマニュアルの同じ項で制限されているように、0が設定されています。この例の他のビットには、例として0が設定されています。下図に示すように、この行をhwint.cの内部に記述します。ドットを押すと、使用するレジスタを選択できるメニューが自動的に表示されます。

fig3

4. We have now to decide if the bits PE20 and PE21 are going to be used as Input or Output. To do this let take a look at the section (16.1.5 Port E I/O Registers L and H (PEIORL and PEIORH))

“A given pin on port E will be an output pin if the corresponding PEIORL or PEIORH bit is set to
1, and an input pin if the bit is cleared to 0.”

Let’s then set to 1 the correspondent bits for 20 and 21.PE20th and 21th are in the higher register (HIGH PIORH) and we set in this case all bits until 21 as outputs:

Once again we look inside the iodefine.h for the register PEIORH and we access to its structure. In this case we have two unions inside the struct PFC, called PEIORL and PEIORH.

ビットPE20およびPE21を入力または出力のどちらとして使用するかを決定します。手順については、「16.1.5 Port E I/O Registers L and H (PEIORL and PEIORH)」の項を参照してください。
「ポートE上の特定のピンは、対応するPEIORLまたはPEIORHビットに1を設定すると出力ピンになり、対応するビットを0にクリアすると入力ピンになります。」
次に、20と21に対応するビットに1を設定してみましょう。20番目と21番目のPEは上位レジスタ(HIGH PIORH)内にあるので、この場合、21までのすべてのビットが出力として設定されます。
ここでも、レジスタPEIORHについてiodefine.hを参照して、その構造体にアクセスします。この場合、struct PFC内部にはPEIORLとPEIORHという共用体があります。

PFC.PEIORH.WORD = 0×003F; /* 0000 0000 0011 1111 */

In red are the bits correspondent to 20th and 21th (starting counting from 0 to 21). Let’s add the line in the file hwint.c.

20番目と21番目に対応するビットは赤色で示されています(0から21をカウントした場合)。ファイルhwint.cにこの行を追加します。

5. We should be now able to change the color of the LEDs by controlling them in the main while loop. Section 17.3 Port E of the hardware manual explains how to access the bits for the PORT E. We have to use the register DRL for the bits between 0 and 15 (the first 16 bits) and the register DRH for the bits between 16 and 31. Once again let’s take a look in the iodefine.h how to call the register.

mainのwhileループでLEDの色を制御して変更できるようになりました。ハードウェアマニュアルの「17.3 Port E」の項では、ポートEのビットのアクセス方法が説明されています。ビット0~15(最初の16ビット)にはレジスタDRLを、ビット16~31にはレジスタDRHを使用してください。ここでもiodefine.hを参照して、レジスタの呼び出し方法を確認します。

We find that inside the structure pe defined as PE we have the unions DRL and DRH. We decide then to access the single bits B20 and B21. Let’s write before the while loop the following lines:

PEとして定義されている構造体peの内部に共用体DRLおよびDRHがあることを確認します。次に、単一ビットB20およびB21にアクセスする方法を決定します。whileループの前に以下の行を記述します。

PE.DRH.BIT.B20=0; //this will turn on the LED connected to the bit 20 /ビット20に接続されているLEDがオンになります。

PE.DRH.BIT.B21=1; //this will turn off the LED connected to the bit 21 /ビット21に接続されているLEDがオフになります。

fig4

We can now compile after setting the build configuration as explained in the previous post on Eclipse.

User can try to change the frequency of the LEDs blinking by writing a for function as a wait function and changing the the illumination of the LEDs by simply negating the actual digital status. This code should be set inside the while loop (do not forget to define in the main the variable int u!!!):

Eclipseに関する以前の投稿で説明したように、ビルド設定を行った後でコンパイルを実行できるようになります。
for関数をwait関数として記述し、実際のデジタルステータスを単にネゲートすることでLEDの照明を変更することにより、LEDの点滅間隔を変更できます。このコードは、whileループ内に設定します(必ずmainで変数int uを定義してください)。

for(u=0;u<100000;u++);  //wait for a while

PE.DRH.BIT.B20=~PE.DRH.BIT.B20;

PE.DRH.BIT.B21=~PE.DRH.BIT.B21;

One Response to “LED制御 ― KPIT GNU Eclipseの操作手順例/Controlling the LED – Example step by step for Kpit GNU Eclipse”

  1. [...] Once again please refer to our sample codes for a clear example or to other previous posts on Eclipse that will give you an [...]

Leave a Reply

RSS FeedStay tuned with us...subscribe to our RSS