PIC16F1789 & MPUトレーナー 6 / 7 セグメント LED を使う 2 / 外部スイッチで何かを表示する

  f:id:ti-nspire:20170314072414p:plain


main.c

#include "mcc_generated_files/mcc.h"

#define PUSH_SW0    RA3
#define PUSH_SW1    RB7
#define PUSH_SW2    RE3

#define DIG1    RE0 //千の位
#define DIG2    RE1 //百の位
#define DIG3    RE2 //十の位
#define DIG4    RC1 //一の位

volatile int msCnt = 0;
void delay_ms(int cnt) {
	msCnt = cnt;
	while(msCnt);
}

void SelectDig(int dig) {
	DIG1 = 0; DIG2 = 0; DIG3 = 0; DIG4 = 0;
	if((dig < 0) || (dig > 3)) {return;}
	switch(dig) {
		case 0: DIG4 = 1; break;
		case 1: DIG3 = 1; break;
		case 2: DIG2 = 1; break;
		case 3: DIG1 = 1; break;
	}
}

void ShowSeg(int val) {
	char pat[] = {
		//ABCDEFGP
		0b11111100, //0
		0b01100000, //1
		0b11011010, //2
		0b00000000, //all off
	};
	PORTD = pat[val];
}

void ShowData(int dat) {
	int i;
	int val;
	for(i = 0; i < 4; i++) {
		val = (dat & 0x000f);
		ShowSeg(val);
		SelectDig(i);
		delay_ms(1);
		SelectDig(-1);
		dat >>= 4;
	}
}

void main(void) {
	SYSTEM_Initialize();

	INTERRUPT_GlobalInterruptEnable();
	INTERRUPT_PeripheralInterruptEnable();

	/*
	while(1) {
		if(!PUSH_SW0) {ShowData(0x3330);} //XXX0
		if(!PUSH_SW1) {ShowData(0x3313);} //XX1X
		if(!PUSH_SW2) {ShowData(0x3233);} //X2XX

		if(!PUSH_SW1 && !PUSH_SW0) {ShowData(0x3310);}              //XX10
		if(!PUSH_SW2 && !PUSH_SW0) {ShowData(0x3230);}              //X2X0
		if(!PUSH_SW2 && !PUSH_SW1) {ShowData(0x3213);}              //X21X
		if(!PUSH_SW2 && !PUSH_SW1 && !PUSH_SW0) {ShowData(0x3210);} //X210

		if(PUSH_SW2 && PUSH_SW1 && PUSH_SW0) {ShowData(0x3333);} //XXXXX
	}
	*/
	while(1) {
		int str = 0x3333;
		if(!PUSH_SW0) {str = str - 0x0003;}
		if(!PUSH_SW1) {str = str - 0x0020;}
		if(!PUSH_SW2) {str = str - 0x0100;}
		ShowData(str);
	}
}