今回はビットシフトを用いて二つの7segLEDの外周を順に点灯させていくプログラムです。
7segRotation.c
//7segLEDの外周を順に点灯させ1の位で一周すると10の位で1つ移動するプログラム
# include <stdio.h>
# define BRR (*((unsigned char *)0xFFA9))
# define TMA (*((unsigned char *)0xFFA6))
# define PMR1 (*((unsigned char *)0xFFE0))
# define PMR5 (*((unsigned char *)0xFFE1))
# define PCR1 (*((unsigned char *)0xFFE4))
# define PCR5 (*((unsigned char *)0xFFE8))
# define PCR7 (*((unsigned char *)0xFFEA))
# define PCR8 (*((unsigned char *)0xFFEB))
# define PDR5 (*((unsigned char *)0xFFD8))
# define PDR7 (*((unsigned char *)0xFFDA))
void wait();
main()
{
int i,cnt=1;
unsigned char Seg1,Seg10;//表示用変数
unsigned char rot;//1の位シフト用
unsigned char rot10;//10の位シフト用
BRR=12; //16MHZ時には12を デフォルトは7 ボーレート変更
PMR1 = 0x00; /*ポート1を汎用IOに設定*/
PMR5 = 0x00; /*ポート1を汎用IOに設定*/
TMA = 0x98 ;
PCR1 = 0xff; //アドレス FFE4
PCR5 = 0xff; //FFE8
PCR7 = 0x00;
PCR8 = 0xff; //FFED //00入力 ff出力
if(cnt==1){//初期化
Seg1=0x01;Seg10=0x81;rot=0x01;rot10=0x81;cnt=0;
}
for (;;) {/* 無限ループ */
wait(2000);
PDR5=Seg1;
wait(2000);
PDR5=Seg10;
wait(2000);
if(rot==0x40){//1の位が1周したときの条件文
rot=0x01;
Seg1=rot;
rot=rot<<1;
//10の位のシフト
rot10=rot10<<1;
rot10=rot10 | 0x80;
Seg10=rot10;
}
else{
Seg1=rot;
rot=rot<<1;
}
if(rot10==0xC0){//10の位が1周したときの条件文
rot10=0x81;
Seg10=rot10;
}
else{}
}
return 0;
}
void wait(int n)
{
int i;
for(i=0;i<n;i++);
}
以前にも述べましたが7segLEDは上図のようなビットとの対応をしているので、順にAから順に左シフトさせていくことで外周を順に点灯させていくことができます。そしてFを点灯後Gを飛ばしてAへ戻るようにします。