Prototype

Dependencies:   mbed ButtonEventDemo

Committer:
Kelrath1984
Date:
Mon Jan 13 18:12:09 2020 +0000
Revision:
17:080847c64d0b
Parent:
16:acf89f757b1b
Noch zu debuggen aber alles drinnen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kelrath1984 12:e4ee1507e722 1 #include "mbed.h"
Kelrath1984 12:e4ee1507e722 2
Kelrath1984 15:b7025faf0571 3 //#define BUTTON1 p14
Kelrath1984 12:e4ee1507e722 4 #define INIT 0x03;
Kelrath1984 12:e4ee1507e722 5
Kelrath1984 12:e4ee1507e722 6
Kelrath1984 12:e4ee1507e722 7 int modifyBit(int x, unsigned char position, bool state);
Kelrath1984 12:e4ee1507e722 8 int lauflicht(bool richtung, int time, int &anz);
Kelrath1984 13:024d65ed9c26 9 void nibbleLeds(int value);
Kelrath1984 16:acf89f757b1b 10 void printb(uint8_t x);
Kelrath1984 12:e4ee1507e722 11
Kelrath1984 12:e4ee1507e722 12 BusOut myleds(LED1, LED2, LED3, LED4);
Kelrath1984 12:e4ee1507e722 13 DigitalIn button(BUTTON1);
Kelrath1984 12:e4ee1507e722 14
Kelrath1984 12:e4ee1507e722 15 int main(){
Kelrath1984 13:024d65ed9c26 16 uint8_t value=INIT;
Kelrath1984 16:acf89f757b1b 17 int anzahl, anz;
Kelrath1984 13:024d65ed9c26 18
Kelrath1984 17:080847c64d0b 19 anzahl=lauflicht(0,400,anz=0);
Kelrath1984 17:080847c64d0b 20 printf("anzahl= %d\n", anzahl);
Kelrath1984 16:acf89f757b1b 21 nibbleLeds(value);
Kelrath1984 16:acf89f757b1b 22 printb(value);
Kelrath1984 17:080847c64d0b 23 value=modifyBit(value,2,1);
Kelrath1984 16:acf89f757b1b 24 printb(myleds);
Kelrath1984 17:080847c64d0b 25 printb(modifyBit(INIT,3,1));
Kelrath1984 15:b7025faf0571 26 lauflicht(0,400,anz=0);
Kelrath1984 16:acf89f757b1b 27 wait(0.1);
Kelrath1984 14:0ccc3d5ca8ce 28 }
Kelrath1984 14:0ccc3d5ca8ce 29
Kelrath1984 14:0ccc3d5ca8ce 30 int lauflicht(bool richtung, int time, int &anz){
Kelrath1984 14:0ccc3d5ca8ce 31 int i=0;
Kelrath1984 14:0ccc3d5ca8ce 32 uint8_t lauf=0x01;
Kelrath1984 15:b7025faf0571 33
Kelrath1984 15:b7025faf0571 34
Kelrath1984 14:0ccc3d5ca8ce 35 if(!richtung){
Kelrath1984 14:0ccc3d5ca8ce 36 lauf= 0x08;
Kelrath1984 14:0ccc3d5ca8ce 37 }
Kelrath1984 12:e4ee1507e722 38 while(1){
Kelrath1984 14:0ccc3d5ca8ce 39 nibbleLeds(lauf&0x0F);
Kelrath1984 14:0ccc3d5ca8ce 40 if(richtung){
Kelrath1984 14:0ccc3d5ca8ce 41 lauf=lauf<<1;
Kelrath1984 14:0ccc3d5ca8ce 42 if(lauf>8){
Kelrath1984 14:0ccc3d5ca8ce 43 lauf=0x01;
Kelrath1984 14:0ccc3d5ca8ce 44 }
Kelrath1984 12:e4ee1507e722 45 }
Kelrath1984 14:0ccc3d5ca8ce 46 else {
Kelrath1984 14:0ccc3d5ca8ce 47 lauf=lauf>>1;
Kelrath1984 14:0ccc3d5ca8ce 48 if(lauf==0){
Kelrath1984 14:0ccc3d5ca8ce 49 lauf=0x08;
Kelrath1984 14:0ccc3d5ca8ce 50 }
Kelrath1984 14:0ccc3d5ca8ce 51 }
Kelrath1984 14:0ccc3d5ca8ce 52 if(button){
Kelrath1984 14:0ccc3d5ca8ce 53 break;
Kelrath1984 14:0ccc3d5ca8ce 54 }
Kelrath1984 14:0ccc3d5ca8ce 55 wait_ms(time);
Kelrath1984 14:0ccc3d5ca8ce 56 anz++;
Kelrath1984 14:0ccc3d5ca8ce 57 }
Kelrath1984 14:0ccc3d5ca8ce 58 return anz;
Kelrath1984 13:024d65ed9c26 59 }
Kelrath1984 13:024d65ed9c26 60
Kelrath1984 17:080847c64d0b 61 int modifyBit(int x, uint8_t position, bool State){
Kelrath1984 17:080847c64d0b 62 int mask=1<<position;
Kelrath1984 17:080847c64d0b 63 int state =int(State);
Kelrath1984 17:080847c64d0b 64 return (x&~mask)|((state<<position)&mask);
Kelrath1984 17:080847c64d0b 65 }
Kelrath1984 17:080847c64d0b 66
Kelrath1984 16:acf89f757b1b 67 void printb(uint8_t x){
Kelrath1984 16:acf89f757b1b 68 for(int i=sizeof(x)<<3; i; i--){
Kelrath1984 16:acf89f757b1b 69 putchar('0'+((x>>(i-1))&1));
Kelrath1984 16:acf89f757b1b 70 }
Kelrath1984 16:acf89f757b1b 71 printf("\n");
Kelrath1984 16:acf89f757b1b 72 }
Kelrath1984 16:acf89f757b1b 73
Kelrath1984 13:024d65ed9c26 74 void nibbleLeds(int value){
Kelrath1984 13:024d65ed9c26 75 myleds=value%16;
Kelrath1984 12:e4ee1507e722 76 }