Balan Gheorghe
/
rtos
rtos test
Fork of rtos-test by
bldc/i2c_helper.cpp@1:ae213d6a0adf, 2016-05-14 (annotated)
- Committer:
- machines94
- Date:
- Sat May 14 21:15:12 2016 +0000
- Revision:
- 1:ae213d6a0adf
Electromobility 2016
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
machines94 | 1:ae213d6a0adf | 1 | #include "i2c_helper.h" |
machines94 | 1:ae213d6a0adf | 2 | |
machines94 | 1:ae213d6a0adf | 3 | unsigned short duty = 15; // 0 - 150 |
machines94 | 1:ae213d6a0adf | 4 | unsigned short freq = 11000; // 11 kHz - pwm frequecny |
machines94 | 1:ae213d6a0adf | 5 | char direction = 0; // motor direction |
machines94 | 1:ae213d6a0adf | 6 | unsigned short speed = 20; // bldc phase comutation freq |
machines94 | 1:ae213d6a0adf | 7 | |
machines94 | 1:ae213d6a0adf | 8 | i2c_helper::i2c_helper(I2C* i2c, int addr){ |
machines94 | 1:ae213d6a0adf | 9 | this->addr = addr; |
machines94 | 1:ae213d6a0adf | 10 | this->i2c = i2c; |
machines94 | 1:ae213d6a0adf | 11 | } |
machines94 | 1:ae213d6a0adf | 12 | |
machines94 | 1:ae213d6a0adf | 13 | int i2c_helper::write_message(char code, char byte1, char byte2, char cmd[]) { |
machines94 | 1:ae213d6a0adf | 14 | if( code == 1) { |
machines94 | 1:ae213d6a0adf | 15 | duty = (unsigned short)(byte1 << 8) + byte2; |
machines94 | 1:ae213d6a0adf | 16 | cmd[2] = (char) duty >> 8; |
machines94 | 1:ae213d6a0adf | 17 | cmd[3] = (char) duty; |
machines94 | 1:ae213d6a0adf | 18 | } else if ( code == 0 ) { |
machines94 | 1:ae213d6a0adf | 19 | speed = (unsigned short)(byte1 << 8) + byte2; |
machines94 | 1:ae213d6a0adf | 20 | } |
machines94 | 1:ae213d6a0adf | 21 | |
machines94 | 1:ae213d6a0adf | 22 | cmd[0] = speed >> 8; |
machines94 | 1:ae213d6a0adf | 23 | cmd[1] = speed; |
machines94 | 1:ae213d6a0adf | 24 | |
machines94 | 1:ae213d6a0adf | 25 | |
machines94 | 1:ae213d6a0adf | 26 | cmd[4] = freq >>8; |
machines94 | 1:ae213d6a0adf | 27 | cmd[5] = freq; |
machines94 | 1:ae213d6a0adf | 28 | |
machines94 | 1:ae213d6a0adf | 29 | cmd[6] = direction; |
machines94 | 1:ae213d6a0adf | 30 | return this->i2c->write(this->addr, cmd, 8); |
machines94 | 1:ae213d6a0adf | 31 | } |