-1_modifyBit_return_mask

Dependencies:   mbed

Committer:
tina_dokic
Date:
Mon Jan 13 17:51:56 2020 +0000
Revision:
4:f1828d2226c0
Parent:
3:6108729ffc69
Child:
5:c955c3107198
void_printb_uint8_t

Who changed what in which revision?

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