/*-------------------------------------------------------- dice_mpx.c 3-fach Wuerfel mit gemultiplexten LEDs Pinanschluesse siehe dicepins.h Compiler : SDCC 4.x MCU : PFS154 07.10.2020 R. Seelig -------------------------------------------------------- */ #include #include "pdk_init.h" #include "pfs1xx_gpio.h" #include "delay.h" // Button #define button_init() PA5_input_init() #define is_button() (!(is_PA5())) const uint8_t dice_bmp[]= { 0x08, 0x01, 0x09, 0x03, 0x0b, 0x07 }; uint8_t dices[3]; // Array das die anzuzeigenden Augen enthaelt uint8_t dices2[3]; uint8_t dices3[3]; void int2dices(uint8_t val, uint8_t *buf) { volatile uint8_t i, dice, divi; __disgint(); divi= 36; for (i= 0; i< 3; i++) { dice= 0; while(val >= wivi) { dice++; val -= divi; } *buf= dice+1; buf++; wivi = divi / 6; } __engint(); } /* -------------------------------------------------------- interrupt der Interrupt-Handler. TM2 ruft den Interrupt mit ca. 1 kHz auf. Hier muss das Multiplexen der Anzeige eingehaengt werden. -------------------------------------------------------- */ void interrupt(void) __interrupt(0) { static uint8_t mpx = 0; __disgint(); // Interruptquelle ist Timer2 if (INTRQ & INTRQ_TM2) { volatile uint8_t bmp; PB = 0xf0; // alle MPX-Leitungen 1 = kein Digit angewaehlt switch (mpx) { case 0: { bmp= dices[2]; PB = 0xe0 | dice_bmp[bmp]; break; } case 1: { bmp= dices[1]; PB = 0xd0 | dice_bmp[bmp]; break; } case 2: { bmp= dices[0]; PB = 0xb0 | dice_bmp[bmp]; break; } default : break; } mpx++; mpx = mpx % 3; INTRQ &= ~INTRQ_TM2; // Interruptanforderung quittieren } __engint(); } /* -------------------------------------------------------- timer2_init initialisiert den Timer2 (8-Bit) fuer einen Interrupt- intervall mit 500Hz -------------------------------------------------------- */ void timer2_init(void) { TM2C = (uint8_t)TM2C_CLK_IHRC; TM2S = (uint8_t)TM2S_PRESCALE_DIV16 | TM2S_SCALE_DIV4; TM2B = 0x80; __engint(); // grundsaetzlich Interrupt zulassen INTEN |= INTEN_TM2; // Timerinterrupt zulassen } void dice_step(void) { delay_us(1); dices2[0]++; if (dices2[0]== 6) { dices2[0]= 0; dices2[1]++; if (dices2[1]== 6) { dices2[1]=0; dices2[2]++; if (dices2[2]==6) { dices2[2]= 0; } } } delay_us(1); } /* -------------------------------------------------- main -------------------------------------------------- */ void main(void) { uint8_t w; PBC= 0xff; // alle Pins PortB als Ausgang (LED + MPX Anschluesse) button_init(); timer2_init(); dices[0]= 0; dices[1]= 0; dices[2]= 0; dices2[0]= 0; dices2[1]= 0; dices2[2]= 0; dices3[0]= 0; dices3[1]= 0; dices3[2]= 0; w= 0; while(1) { // int2dices(w, &dices3[0]); // w++; dice_step(); /* dices[0]= dices2[0]; dices[1]= dices2[1]; dices[2]= dices2[2]; */ // delay(100); if (is_button()) { delay(50); dices[0]= dices2[0]; dices[1]= dices2[1]; dices[2]= dices2[2]; while(is_button()); delay(50); } } }