test program

Dependencies:   mbed

Committer:
shogu
Date:
Mon Jan 13 17:53:34 2020 +0000
Revision:
5:d65703f7a282
Parent:
4:cfc02c02dd97
ende

Who changed what in which revision?

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