Markus Grönholm
/
GMR-sensor-example
Example for TLE5012B GMR sensor
main.cpp@0:1f873630ed4f, 2019-02-07 (annotated)
- Committer:
- mgronhol
- Date:
- Thu Feb 07 12:18:49 2019 +0000
- Revision:
- 0:1f873630ed4f
Initial setup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgronhol | 0:1f873630ed4f | 1 | #include "mbed.h" |
mgronhol | 0:1f873630ed4f | 2 | |
mgronhol | 0:1f873630ed4f | 3 | Serial pc(SERIAL_TX, SERIAL_RX); |
mgronhol | 0:1f873630ed4f | 4 | |
mgronhol | 0:1f873630ed4f | 5 | |
mgronhol | 0:1f873630ed4f | 6 | /* This example uses pins A0, A1 and A2 as SSC interface pins */ |
mgronhol | 0:1f873630ed4f | 7 | DigitalOut SCK( A0 ); |
mgronhol | 0:1f873630ed4f | 8 | DigitalInOut DATA( A2 ); |
mgronhol | 0:1f873630ed4f | 9 | DigitalOut CSQ( A1 ); |
mgronhol | 0:1f873630ed4f | 10 | |
mgronhol | 0:1f873630ed4f | 11 | |
mgronhol | 0:1f873630ed4f | 12 | |
mgronhol | 0:1f873630ed4f | 13 | /*Functions for bit level access to SSC bus */ |
mgronhol | 0:1f873630ed4f | 14 | |
mgronhol | 0:1f873630ed4f | 15 | inline void ssc_write_bit( int value ){ |
mgronhol | 0:1f873630ed4f | 16 | SCK = 0; |
mgronhol | 0:1f873630ed4f | 17 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 18 | SCK = 1; |
mgronhol | 0:1f873630ed4f | 19 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 20 | if( value != 0 ){ DATA = 1; } else { DATA = 0; } |
mgronhol | 0:1f873630ed4f | 21 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 22 | SCK = 0; |
mgronhol | 0:1f873630ed4f | 23 | } |
mgronhol | 0:1f873630ed4f | 24 | |
mgronhol | 0:1f873630ed4f | 25 | inline int ssc_read_bit(){ |
mgronhol | 0:1f873630ed4f | 26 | int out = 0; |
mgronhol | 0:1f873630ed4f | 27 | SCK = 0; |
mgronhol | 0:1f873630ed4f | 28 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 29 | SCK = 1; |
mgronhol | 0:1f873630ed4f | 30 | |
mgronhol | 0:1f873630ed4f | 31 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 32 | SCK = 0; |
mgronhol | 0:1f873630ed4f | 33 | if( DATA.read() != 0 ){ out = 1; } |
mgronhol | 0:1f873630ed4f | 34 | |
mgronhol | 0:1f873630ed4f | 35 | |
mgronhol | 0:1f873630ed4f | 36 | return out; |
mgronhol | 0:1f873630ed4f | 37 | } |
mgronhol | 0:1f873630ed4f | 38 | |
mgronhol | 0:1f873630ed4f | 39 | /* Functions to read & write 16bit words via SSC */ |
mgronhol | 0:1f873630ed4f | 40 | |
mgronhol | 0:1f873630ed4f | 41 | void ssc_write_word( uint16_t word ){ |
mgronhol | 0:1f873630ed4f | 42 | DATA.output(); |
mgronhol | 0:1f873630ed4f | 43 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 44 | for( int i = 0; i < 16 ; ++i ){ |
mgronhol | 0:1f873630ed4f | 45 | ssc_write_bit( word & 0x8000 ); |
mgronhol | 0:1f873630ed4f | 46 | word <<= 1; |
mgronhol | 0:1f873630ed4f | 47 | } |
mgronhol | 0:1f873630ed4f | 48 | } |
mgronhol | 0:1f873630ed4f | 49 | |
mgronhol | 0:1f873630ed4f | 50 | uint16_t ssc_read_word(){ |
mgronhol | 0:1f873630ed4f | 51 | uint16_t word = 0; |
mgronhol | 0:1f873630ed4f | 52 | DATA.input(); |
mgronhol | 0:1f873630ed4f | 53 | wait_us(1); |
mgronhol | 0:1f873630ed4f | 54 | for( int i = 0; i < 16; ++i ){ |
mgronhol | 0:1f873630ed4f | 55 | word <<= 1; |
mgronhol | 0:1f873630ed4f | 56 | word |= ssc_read_bit(); |
mgronhol | 0:1f873630ed4f | 57 | } |
mgronhol | 0:1f873630ed4f | 58 | |
mgronhol | 0:1f873630ed4f | 59 | return word; |
mgronhol | 0:1f873630ed4f | 60 | } |
mgronhol | 0:1f873630ed4f | 61 | |
mgronhol | 0:1f873630ed4f | 62 | /* And function to read angle from GMR sensor */ |
mgronhol | 0:1f873630ed4f | 63 | float encoder_read_angle(){ |
mgronhol | 0:1f873630ed4f | 64 | uint16_t data[2] = {10,10}; |
mgronhol | 0:1f873630ed4f | 65 | uint16_t cmd = 0x8000 | (2<<4) | 1; |
mgronhol | 0:1f873630ed4f | 66 | float angle; |
mgronhol | 0:1f873630ed4f | 67 | |
mgronhol | 0:1f873630ed4f | 68 | /* Request new measurement */ |
mgronhol | 0:1f873630ed4f | 69 | SCK = 0; |
mgronhol | 0:1f873630ed4f | 70 | CSQ = 0; |
mgronhol | 0:1f873630ed4f | 71 | wait_us(2); |
mgronhol | 0:1f873630ed4f | 72 | CSQ = 1; |
mgronhol | 0:1f873630ed4f | 73 | wait_us(2); |
mgronhol | 0:1f873630ed4f | 74 | |
mgronhol | 0:1f873630ed4f | 75 | /* Read angle register */ |
mgronhol | 0:1f873630ed4f | 76 | SCK = 0; |
mgronhol | 0:1f873630ed4f | 77 | CSQ = 0; |
mgronhol | 0:1f873630ed4f | 78 | ssc_write_word( cmd ); |
mgronhol | 0:1f873630ed4f | 79 | wait_us(2); |
mgronhol | 0:1f873630ed4f | 80 | data[0] = ssc_read_word(); /* angle */ |
mgronhol | 0:1f873630ed4f | 81 | data[1] = ssc_read_word(); /* checksum */ |
mgronhol | 0:1f873630ed4f | 82 | CSQ = 1; |
mgronhol | 0:1f873630ed4f | 83 | |
mgronhol | 0:1f873630ed4f | 84 | /* compute angle from measurement */ |
mgronhol | 0:1f873630ed4f | 85 | angle = (0x7fff & data[0]) * 360.0f/(32768.0f); |
mgronhol | 0:1f873630ed4f | 86 | |
mgronhol | 0:1f873630ed4f | 87 | return angle; |
mgronhol | 0:1f873630ed4f | 88 | } |
mgronhol | 0:1f873630ed4f | 89 | |
mgronhol | 0:1f873630ed4f | 90 | int main() { |
mgronhol | 0:1f873630ed4f | 91 | pc.baud( 115200 ); |
mgronhol | 0:1f873630ed4f | 92 | |
mgronhol | 0:1f873630ed4f | 93 | while(1) { |
mgronhol | 0:1f873630ed4f | 94 | float angle = encoder_read_angle(); |
mgronhol | 0:1f873630ed4f | 95 | pc.printf( "Angle: %.2f deg\r\n", angle ); |
mgronhol | 0:1f873630ed4f | 96 | wait( 0.25 ); |
mgronhol | 0:1f873630ed4f | 97 | } |
mgronhol | 0:1f873630ed4f | 98 | } |