8 years, 5 months ago.  This question has been closed. Reason: Unclear question

test

// Calculator: Addition and Subtraction
// Joystick Up: +  ; Joystick Down - (Operator selection)
// Joystick Left: count value up (+1); Joystick Right: count value down (-1)
// Joystick Center: Calculate
// Joystick Left or Joystick Right: start at input of value 1 again

#include "mbed.h"
#include "C12832_lcd.h"

InterruptIn iiUp(p15);
DigitalIn diUp(p15);
InterruptIn iiDown(p12);
DigitalIn diDown(p12);
InterruptIn iiCenter(p14);
DigitalIn diCenter(p14);
InterruptIn iiLeft(p13);
DigitalIn diLeft(p13);
InterruptIn iiRight(p16);
DigitalIn diRight(p16);
uint8_t step = 0;
char calcOp = '+';
bool updateLcd = true;
int value1, value2, result;

// prototypes

// functions
uint8_t debounce(DigitalIn myIn)  
{
    #define LEVEL_CHECKS 8
    #define MAX_LOOPS 30                // stoppt das Überprüfen des Prellen nach max. MAX_LOOPS Durchläufen
    unsigned char port_buffer;
    unsigned char debounceCounter = 0;
    uint8_t loopCounter = 0;
    
    do
    {
        port_buffer = myIn;
        wait_us(100);
        loopCounter++;
        if(myIn == port_buffer)
          debounceCounter++;    // mindestens 'LEVEL_CHECKS' Abtastungen in Folge: gleicher Pegel
        else
          debounceCounter = 0;      
    }
    while ((debounceCounter <= LEVEL_CHECKS) && (loopCounter <= MAX_LOOPS));
    return loopCounter;
}

// ISR



// main program
int main() {
    C12832_LCD lcd;
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("5ABELI: Calulator");

    
    while(1) { 
        if (updateLcd) {
            updateLcd = false;
            switch (step) {
                case 0: 
                    lcd.locate(0, 20);
                    lcd.printf("%d                ", value1);
                    break;
								case 1:
				
									break;
				

                default: break;
            }
        }
    }
}

Question relating to:

When posting code please use <<code>> tags so that the formatting is correct e.g.

<<code>>
#include "mbed.h"
#include "C12832_lcd.h"
...
<</code>>

See the editing tips link for more details.

posted by Andy A 15 Apr 2016

1 Answer

8 years, 5 months ago.

  1. include "mbed.h"
  2. include "C12832_lcd.h"

InterruptIn iiUp(p15); DigitalIn diUp(p15); InterruptIn iiDown(p12); DigitalIn diDown(p12); InterruptIn iiCenter(p14); DigitalIn diCenter(p14); InterruptIn iiLeft(p13); DigitalIn diLeft(p13); InterruptIn iiRight(p16); DigitalIn diRight(p16); unint8_t step = 0; char calcOp = '+'; bool updateLcd = true; int value1, value2, result;

uint8_t debounce(DigitalIn myIn) {

  1. define LEVEL_CHECKS 8
  2. define MAX_LOOPS 30 unsigned char port_buffer; unsigned char debounceCounter = 0; uint8_t loopCounter = 0;

do { port_buffer = myIn; wait_us(100); loopCounter++; if(myIn == port_buffer) debounceCounter++; mindestens 'LEVEL_CHECKS' Abtastungen in Folge: gleicher Pegel else debounceCounter = 0; } while ((debounceCounter <= LEVEL_CHECKS) && (loopCounter <= MAX_LOOPS)); return loopCounter; }

void Plus() { debounce(dsUp); calcOp = '+'; step = 1; updateLcd = true; } void Minus() { debounce(diDown); calcOp = '-'; step = 1; updateLcd = true; } void countUp() { debounce(diLeft); if (step == 0) value1++; if (step == 1) { value2++; step = 2; } updateLcd = true; } void countDown() { debounce(diRight); if (step == 0) value1; if (step == 1) { value2; step = 2; } updateLcd = true; } void Calculate() { debounce(diCenter); if (calcOp = '+') result = value1 + value2; if (calcOp = '-') result = value1 - value2; step = 3; updateLcd = true; }

int main() { C12832_LCD lcd; lcd.cls(); lcd.locate(0, 0); lcd.printf("4CHEL Calulator");

iiJsUp.rise(&Plus); iiJsDown.rise(&Minus); iiJsLeft.rise(&countUp); iiJsRight.rise(&countDown); iiJsCenter.rise(&Calculate); while(1) { if (updateLcd) { updateLcd = false; switch (step) { case 0: lcd.locate(0, 20); lcd.printf("%d ", value1); break; case 1: lcd.locate(0, 20); lcd.printf("%d %c ", value1, calcOp); break; case 2: lcd.locate(0, 20); lcd.printf("%d %c %d", value1, calcOp, value2); break; case 3: lcd.locate(0, 20); lcd.printf("%d %c %d = %d", value1, calcOp, value2, result); step = 0; value1 = 0; value2 = 0; result = 0; break; default: break; } } } }