bertl.cpp@0:09cac0d76c5e, 2016-03-07 (annotated)
- Committer:
- DomiSukic
- Date:
- Mon Mar 07 11:51:28 2016 +0000
- Revision:
- 0:09cac0d76c5e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DomiSukic | 0:09cac0d76c5e | 1 | /**************************** |
DomiSukic | 0:09cac0d76c5e | 2 | Bertl engine&I2C Lib. rework |
DomiSukic | 0:09cac0d76c5e | 3 | |
DomiSukic | 0:09cac0d76c5e | 4 | 2.2.2016 |
DomiSukic | 0:09cac0d76c5e | 5 | |
DomiSukic | 0:09cac0d76c5e | 6 | by Dominik Sukic |
DomiSukic | 0:09cac0d76c5e | 7 | with help from Kevin Heinrich |
DomiSukic | 0:09cac0d76c5e | 8 | *****************************/ |
DomiSukic | 0:09cac0d76c5e | 9 | |
DomiSukic | 0:09cac0d76c5e | 10 | #include <mbed.h> |
DomiSukic | 0:09cac0d76c5e | 11 | #include "bertl.h" |
DomiSukic | 0:09cac0d76c5e | 12 | // Define Motor Routines |
DomiSukic | 0:09cac0d76c5e | 13 | DigitalOut engineLR(P1_0); //IN1, EP10, MG1 engine-Pin 2 |
DomiSukic | 0:09cac0d76c5e | 14 | DigitalOut engineLF(P1_1); //IN2, EP11, MG1 engine-Pin 1 |
DomiSukic | 0:09cac0d76c5e | 15 | PwmOut engineLEN(p34); //EN1, P34 |
DomiSukic | 0:09cac0d76c5e | 16 | DigitalOut engineRR(P1_4); //IN4, EP13, MG2 engine-Pin 2 |
DomiSukic | 0:09cac0d76c5e | 17 | DigitalOut engineRF(P1_3); //IN3, EP14, MG2 engine-Pin 1 |
DomiSukic | 0:09cac0d76c5e | 18 | PwmOut engineREN(p36); //EN2, P36 |
DomiSukic | 0:09cac0d76c5e | 19 | |
DomiSukic | 0:09cac0d76c5e | 20 | // Define PC9555 Routines |
DomiSukic | 0:09cac0d76c5e | 21 | I2C pc9555(P0_5, P0_4);// SDA, SCL |
DomiSukic | 0:09cac0d76c5e | 22 | |
DomiSukic | 0:09cac0d76c5e | 23 | //Start PC9555 Routines |
DomiSukic | 0:09cac0d76c5e | 24 | void bertl_PC9555_init() |
DomiSukic | 0:09cac0d76c5e | 25 | { |
DomiSukic | 0:09cac0d76c5e | 26 | char data[2]; |
DomiSukic | 0:09cac0d76c5e | 27 | // I2C-Initialisierung |
DomiSukic | 0:09cac0d76c5e | 28 | pc9555.frequency(PC9555_FREQUENCY); |
DomiSukic | 0:09cac0d76c5e | 29 | // Port 0 = Leds, Port 1 = Switches |
DomiSukic | 0:09cac0d76c5e | 30 | // Adresse, R/W# = 0, Config PORT 0 (6) = 0x00(= Output), Stop |
DomiSukic | 0:09cac0d76c5e | 31 | data[0] = PC9555_PORT0_DIRCONFIG; |
DomiSukic | 0:09cac0d76c5e | 32 | data[1] = 0x00; |
DomiSukic | 0:09cac0d76c5e | 33 | pc9555.write(PC9555_ADDR_W, data, 2); |
DomiSukic | 0:09cac0d76c5e | 34 | // Adresse, R/W# = 0, Config PORT 1 (7) = 0xFF(= Input), Stop |
DomiSukic | 0:09cac0d76c5e | 35 | data[0] = PC9555_PORT1_DIRCONFIG; |
DomiSukic | 0:09cac0d76c5e | 36 | data[1] = 0xFF; |
DomiSukic | 0:09cac0d76c5e | 37 | pc9555.write(PC9555_ADDR_W, data, 2); |
DomiSukic | 0:09cac0d76c5e | 38 | } |
DomiSukic | 0:09cac0d76c5e | 39 | |
DomiSukic | 0:09cac0d76c5e | 40 | void bertl_PC9555_leds(unsigned char leds) |
DomiSukic | 0:09cac0d76c5e | 41 | { |
DomiSukic | 0:09cac0d76c5e | 42 | char data[2]; //Two Bytes for Transmitt via I2C |
DomiSukic | 0:09cac0d76c5e | 43 | // Send leds to PORT0 of PC9555 via I2C: |
DomiSukic | 0:09cac0d76c5e | 44 | // Start, Address, RW# = 0, CMD PC9555_PORT0_OUT, leds, STOP |
DomiSukic | 0:09cac0d76c5e | 45 | data[0] = PC9555_PORT0_OUT; |
DomiSukic | 0:09cac0d76c5e | 46 | data[1] = ~leds; //bitwise inversion since Hardware is switched on with 0 (inverse logic) |
DomiSukic | 0:09cac0d76c5e | 47 | pc9555.write(PC9555_ADDR_W, data, 2); |
DomiSukic | 0:09cac0d76c5e | 48 | } |
DomiSukic | 0:09cac0d76c5e | 49 | |
DomiSukic | 0:09cac0d76c5e | 50 | unsigned char bertl_PC9555_switches() |
DomiSukic | 0:09cac0d76c5e | 51 | { |
DomiSukic | 0:09cac0d76c5e | 52 | char taster; |
DomiSukic | 0:09cac0d76c5e | 53 | |
DomiSukic | 0:09cac0d76c5e | 54 | pc9555.start(); // Start PC9555 to write the Adress |
DomiSukic | 0:09cac0d76c5e | 55 | pc9555.write(PC9555_ADDR_W); // Write the WRITE-Adress |
DomiSukic | 0:09cac0d76c5e | 56 | pc9555.write(0x01); // To define the Switches |
DomiSukic | 0:09cac0d76c5e | 57 | pc9555.start(); // Start PC9555 again to read the Values |
DomiSukic | 0:09cac0d76c5e | 58 | pc9555.write(PC9555_ADDR_R); // Set the READ_bit |
DomiSukic | 0:09cac0d76c5e | 59 | taster = pc9555.read(0); // Read the Value of the Switches |
DomiSukic | 0:09cac0d76c5e | 60 | pc9555.stop(); // Stop the I2C |
DomiSukic | 0:09cac0d76c5e | 61 | |
DomiSukic | 0:09cac0d76c5e | 62 | return taster; // Return the value |
DomiSukic | 0:09cac0d76c5e | 63 | |
DomiSukic | 0:09cac0d76c5e | 64 | } |
DomiSukic | 0:09cac0d76c5e | 65 | // END PC9555 Routines |
DomiSukic | 0:09cac0d76c5e | 66 | |
DomiSukic | 0:09cac0d76c5e | 67 | //Experimental |
DomiSukic | 0:09cac0d76c5e | 68 | bool checkbitFront() |
DomiSukic | 0:09cac0d76c5e | 69 | { |
DomiSukic | 0:09cac0d76c5e | 70 | bool Frontbuttons = CHECK_BIT(bertl_PC9555_switches(), 7) | CHECK_BIT(bertl_PC9555_switches(), 6) | CHECK_BIT(bertl_PC9555_switches(), 2) | CHECK_BIT(bertl_PC9555_switches(), 0); //Checks if a bit of the front buttons is set |
DomiSukic | 0:09cac0d76c5e | 71 | |
DomiSukic | 0:09cac0d76c5e | 72 | return Frontbuttons; //returns true if set |
DomiSukic | 0:09cac0d76c5e | 73 | } |
DomiSukic | 0:09cac0d76c5e | 74 | bool checkbitBack() |
DomiSukic | 0:09cac0d76c5e | 75 | { |
DomiSukic | 0:09cac0d76c5e | 76 | bool Backbuttons = CHECK_BIT(bertl_PC9555_switches(), 5) | CHECK_BIT(bertl_PC9555_switches(), 4) | CHECK_BIT(bertl_PC9555_switches(), 1); //Checks if a bit of the back buttons is set |
DomiSukic | 0:09cac0d76c5e | 77 | |
DomiSukic | 0:09cac0d76c5e | 78 | return Backbuttons; //returns true if set |
DomiSukic | 0:09cac0d76c5e | 79 | } |
DomiSukic | 0:09cac0d76c5e | 80 | |
DomiSukic | 0:09cac0d76c5e | 81 | // Begin Motor Routines |
DomiSukic | 0:09cac0d76c5e | 82 | void bertl_engine(int left, int right) |
DomiSukic | 0:09cac0d76c5e | 83 | { |
DomiSukic | 0:09cac0d76c5e | 84 | // If the left engines-value is greater than 0, the left engine turn FORWARD |
DomiSukic | 0:09cac0d76c5e | 85 | if(left > 0) { |
DomiSukic | 0:09cac0d76c5e | 86 | engineLF=1; |
DomiSukic | 0:09cac0d76c5e | 87 | engineLR=0; |
DomiSukic | 0:09cac0d76c5e | 88 | } |
DomiSukic | 0:09cac0d76c5e | 89 | // Or if the left engines-value is less than 0, the left engine turn REVERSE |
DomiSukic | 0:09cac0d76c5e | 90 | else if(left < 0) { |
DomiSukic | 0:09cac0d76c5e | 91 | engineLF=0; |
DomiSukic | 0:09cac0d76c5e | 92 | engineLR=1; |
DomiSukic | 0:09cac0d76c5e | 93 | left = left * (-1); // For PWM the Value have to be positive |
DomiSukic | 0:09cac0d76c5e | 94 | } |
DomiSukic | 0:09cac0d76c5e | 95 | // If the right engines-value is greater than 0, the right engine turn FORWARD |
DomiSukic | 0:09cac0d76c5e | 96 | if(right > 0) { |
DomiSukic | 0:09cac0d76c5e | 97 | engineRF=1; |
DomiSukic | 0:09cac0d76c5e | 98 | engineRR=0; |
DomiSukic | 0:09cac0d76c5e | 99 | } |
DomiSukic | 0:09cac0d76c5e | 100 | // Or if the right engines-value is less than 0, the right engine turn REVERSE |
DomiSukic | 0:09cac0d76c5e | 101 | else if(right < 0) { |
DomiSukic | 0:09cac0d76c5e | 102 | engineRF=0; |
DomiSukic | 0:09cac0d76c5e | 103 | engineRR=1; |
DomiSukic | 0:09cac0d76c5e | 104 | right = right * (-1); // For PWM the Value have to be positive |
DomiSukic | 0:09cac0d76c5e | 105 | } |
DomiSukic | 0:09cac0d76c5e | 106 | // Or if the right- or/and the left engines value equals 0, the ENABLE-Pin turn off |
DomiSukic | 0:09cac0d76c5e | 107 | engineLEN=(left/255.0); |
DomiSukic | 0:09cac0d76c5e | 108 | engineREN=(right/255.0); |
DomiSukic | 0:09cac0d76c5e | 109 | } |