LPC1343 7seg LEDを制御する count up動作&code
7Seg LED制御の動作とコード解説です。
動作
単純なカウントアップを行います。
1秒毎に1→2・・とカウントアップし9までいくと次は0に戻ります。
コード
今回もベースはエレキジャックさんの記事です。
SysTick timerの使い方と
GPIOの使い方が掲載されていました。
下のコードは7seg用にモディファイしたものです。
#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif#include
#include__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
int main(void) {
volatile static uint32_t period; // Assigned to a variable for debug
volatile static int i = 0 ; // dummy counter// Configure PORT PIO2_0 - PIO2_6 as output
LPC_IOCON->PIO2_0 &= 0x00; // Configure PIO2-0 Pullup off
LPC_IOCON->PIO2_1 &= 0x00; // Configure PIO2-1 Pullup off
LPC_IOCON->PIO2_2 &= 0x00; // Configure PIO2-2 Pullup off
LPC_IOCON->PIO2_3 &= 0x00; // Configure PIO2-3 Pullup off
LPC_IOCON->PIO2_4 &= 0x00; // Configure PIO2-4 Pullup off
LPC_IOCON->PIO2_5 &= 0x00; // Configure PIO2-5 Pullup off
LPC_IOCON->PIO2_6 &= 0x00; // Configure PIO2-6 Pullup off
LPC_GPIO2->DIR |= 0x7F; // PIO2_0 - PIO2_6 as output// Configure System tick timer in 10msec period
period = SystemCoreClock / 100; // Period for 10msec SYSTICK
SysTick_Config(period); // Configuration// Enter an infinite loop
while(1) { // Infinite loop
i++; // dummy count.
}
return 0 ;
}void SysTick_Handler(void) {
static uint8_t count = 0; // Software counter
static int sec=0; // second counter
if (++count >= 100) { // wait for 100 system ticks =1s
if(sec>=10){
sec = 0;
}
count = 0; // Reload counter
switch (sec) {
case 0:
LPC_GPIO2->DATA = 0x02;
break;
case 1:
LPC_GPIO2->DATA = 0x2F;
break;
case 2:
LPC_GPIO2->DATA = 0x41;
break;
case 3:
LPC_GPIO2->DATA = 0x05;
break;
case 4:
LPC_GPIO2->DATA = 0x2C;
break;
case 5:
LPC_GPIO2->DATA = 0x14;
break;
case 6:
LPC_GPIO2->DATA = 0x10;
break;
case 7:
LPC_GPIO2->DATA = 0x27;
break;
case 8:
LPC_GPIO2->DATA = 0x00;
break;
case 9:
LPC_GPIO2->DATA = 0x04;
break;
default:
LPC_GPIO2->DATA = 0x02;
break;
}
++sec;}
}
最初はGPIOの設定です。PIO2_0 - PIO2_6までをOUTPUTとして使用します。
まずは以下のコードでPull-upをはずします。
LPC_IOCON->PIO2_0 &= 0x00; // Configure PIO2-0 Pullup off
データシートを見るとリセットバリューでPull up設定になっていることがわかります。
放置しても良いといえばよいですが今回は外しました。
次にoutputの設定を行います。
PIO2_0-PIO2_6まで7本 outputにしますので 111 1111=7Fです。
LPC_GPIO2->DIR |= 0x7F; // PIO2_0 - PIO2_6 as output
SysTick timerは10ms毎に割り込みを入れるようにしているので
100カウント=1秒で secをカウントup sec=10でリセット(=0)するようにしました。
そしてsecの値を7seg LED表示するようにデコード値をcase switch文で割り振りました。
ここまでで、7seg LEDはカウントアップ動作をします。
しかし、今回 LEDの電流を10mA程度にしたにもかかわらず明るすぎでした。
また、1LEDで10mAですから7segの場合8を表示すると70mAも流れることになります。
USB powerは500mAですからその14%です。これではたくさんの7segLEDを使用することが出来ません。
そこで次は高速でLEDをオン-オフさせ電流をセーブしてみようと思います。