main

Dependencies:   mbed

Committer:
riad
Date:
Mon Jan 13 17:34:32 2020 +0000
Revision:
4:6faa92e1841e
Parent:
3:d9c6df85ff68
main ende

Who changed what in which revision?

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