asdgas
Dependencies: mbed Eigen FastPWM
SPI_EEP_ENC/SPI_EEP_ENC.cpp@16:903b5a4433b4, 2019-09-02 (annotated)
- Committer:
- Lightvalve
- Date:
- Mon Sep 02 13:32:33 2019 +0000
- Revision:
- 16:903b5a4433b4
- Parent:
- 0:51c43836c1d7
190902
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GiJeongKim | 0:51c43836c1d7 | 1 | #include "mbed.h" |
GiJeongKim | 0:51c43836c1d7 | 2 | #include "setting.h" |
GiJeongKim | 0:51c43836c1d7 | 3 | #include "SPI_EEP_ENC.h" |
GiJeongKim | 0:51c43836c1d7 | 4 | |
GiJeongKim | 0:51c43836c1d7 | 5 | // EEPROM |
Lightvalve | 16:903b5a4433b4 | 6 | //void spi_eeprom_ready(void){ |
Lightvalve | 16:903b5a4433b4 | 7 | // int temp1, temp2; |
Lightvalve | 16:903b5a4433b4 | 8 | // do{ |
Lightvalve | 16:903b5a4433b4 | 9 | // eeprom_cs=0; |
Lightvalve | 16:903b5a4433b4 | 10 | // eeprom.write(0x06); //write enable |
Lightvalve | 16:903b5a4433b4 | 11 | // eeprom_cs=1; |
Lightvalve | 16:903b5a4433b4 | 12 | // |
Lightvalve | 16:903b5a4433b4 | 13 | // eeprom_cs=0; |
Lightvalve | 16:903b5a4433b4 | 14 | // temp1 = eeprom.write(0x05); |
Lightvalve | 16:903b5a4433b4 | 15 | // temp2 = eeprom.write(0x00); |
Lightvalve | 16:903b5a4433b4 | 16 | // eeprom_cs=1; |
Lightvalve | 16:903b5a4433b4 | 17 | // temp2=(temp2&(0x03))!= 0x02; |
Lightvalve | 16:903b5a4433b4 | 18 | // } while(temp2); // before writing or reading |
Lightvalve | 16:903b5a4433b4 | 19 | // } |
Lightvalve | 16:903b5a4433b4 | 20 | // |
Lightvalve | 16:903b5a4433b4 | 21 | // void spi_eeprom_write(unsigned short add, unsigned int data){ |
Lightvalve | 16:903b5a4433b4 | 22 | // eeprom_cs=0; |
Lightvalve | 16:903b5a4433b4 | 23 | // eeprom.write(0x02); |
Lightvalve | 16:903b5a4433b4 | 24 | // eeprom.write(0xff&(add>>8)); |
Lightvalve | 16:903b5a4433b4 | 25 | // eeprom.write(0xff&add); |
Lightvalve | 16:903b5a4433b4 | 26 | // eeprom.write(0xff&data); |
Lightvalve | 16:903b5a4433b4 | 27 | // eeprom.write(0xff&(data>>8)); |
Lightvalve | 16:903b5a4433b4 | 28 | // eeprom.write(0xff&(data>>16)); |
Lightvalve | 16:903b5a4433b4 | 29 | // eeprom.write(0xff&(data>>24)); |
Lightvalve | 16:903b5a4433b4 | 30 | // eeprom_cs=1; |
Lightvalve | 16:903b5a4433b4 | 31 | //} |
Lightvalve | 16:903b5a4433b4 | 32 | // |
Lightvalve | 16:903b5a4433b4 | 33 | //unsigned int spi_eeprom_read(unsigned short add){ |
Lightvalve | 16:903b5a4433b4 | 34 | // eeprom_cs=0; |
Lightvalve | 16:903b5a4433b4 | 35 | // eeprom.write(0x03); |
Lightvalve | 16:903b5a4433b4 | 36 | // eeprom.write(0xff&(add>>8)); |
Lightvalve | 16:903b5a4433b4 | 37 | // eeprom.write(0xff&add); |
Lightvalve | 16:903b5a4433b4 | 38 | // |
Lightvalve | 16:903b5a4433b4 | 39 | // int a1 = eeprom.write(0x00); |
Lightvalve | 16:903b5a4433b4 | 40 | // int a2 = eeprom.write(0x00); |
Lightvalve | 16:903b5a4433b4 | 41 | // int a3 = eeprom.write(0x00); |
Lightvalve | 16:903b5a4433b4 | 42 | // int a4 = eeprom.write(0x00); |
Lightvalve | 16:903b5a4433b4 | 43 | // eeprom_cs=1; |
Lightvalve | 16:903b5a4433b4 | 44 | // //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; |
Lightvalve | 16:903b5a4433b4 | 45 | // unsigned int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24); |
Lightvalve | 16:903b5a4433b4 | 46 | // return final; |
Lightvalve | 16:903b5a4433b4 | 47 | // } |
GiJeongKim | 0:51c43836c1d7 | 48 | |
GiJeongKim | 0:51c43836c1d7 | 49 | |
GiJeongKim | 0:51c43836c1d7 | 50 | // ENCODER |
GiJeongKim | 0:51c43836c1d7 | 51 | void spi_enc_set_clear(void){ |
GiJeongKim | 0:51c43836c1d7 | 52 | unsigned int temp; |
GiJeongKim | 0:51c43836c1d7 | 53 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 54 | temp = enc.write(0b00100000); |
GiJeongKim | 0:51c43836c1d7 | 55 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 56 | } |
GiJeongKim | 0:51c43836c1d7 | 57 | |
GiJeongKim | 0:51c43836c1d7 | 58 | void spi_enc_set_init(void){ |
GiJeongKim | 0:51c43836c1d7 | 59 | unsigned int temp, i, temp1, temp2; |
GiJeongKim | 0:51c43836c1d7 | 60 | |
GiJeongKim | 0:51c43836c1d7 | 61 | // write MDR0 -> 0b11 -> x4 quadrature count mode |
GiJeongKim | 0:51c43836c1d7 | 62 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 63 | temp = enc.write(0b10001000); // WR + MDR0 |
GiJeongKim | 0:51c43836c1d7 | 64 | temp = enc.write(0b00000011); // quadratue mode |
GiJeongKim | 0:51c43836c1d7 | 65 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 66 | |
GiJeongKim | 0:51c43836c1d7 | 67 | // write MDR1 -> 0b10 -> 2-byte counter mode |
GiJeongKim | 0:51c43836c1d7 | 68 | for(i=0;i<100;i++); |
GiJeongKim | 0:51c43836c1d7 | 69 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 70 | temp = enc.write(0b10010000); // WR + MDR1 |
GiJeongKim | 0:51c43836c1d7 | 71 | //temp = enc.write(0b00000010); // 2 byte mode |
GiJeongKim | 0:51c43836c1d7 | 72 | temp = enc.write(0b00000000); // 4 byte mode |
GiJeongKim | 0:51c43836c1d7 | 73 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 74 | |
GiJeongKim | 0:51c43836c1d7 | 75 | // clear |
GiJeongKim | 0:51c43836c1d7 | 76 | spi_enc_set_clear(); |
GiJeongKim | 0:51c43836c1d7 | 77 | } |
GiJeongKim | 0:51c43836c1d7 | 78 | |
GiJeongKim | 0:51c43836c1d7 | 79 | |
GiJeongKim | 0:51c43836c1d7 | 80 | int spi_enc_read(void){ |
GiJeongKim | 0:51c43836c1d7 | 81 | //for(t_i=0;t_i<100;t_i++); |
GiJeongKim | 0:51c43836c1d7 | 82 | unsigned int t_dummy, t_b1, t_b2, t_b3, t_b4, t_i; |
GiJeongKim | 0:51c43836c1d7 | 83 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 84 | t_dummy = enc.write(0b01100000); // Read Commend |
GiJeongKim | 0:51c43836c1d7 | 85 | t_b1 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 86 | t_b2 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 87 | t_b3 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 88 | t_b4 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 89 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 90 | |
GiJeongKim | 0:51c43836c1d7 | 91 | return((t_b1<<24) + (t_b2<<16) + (t_b3<<8) + t_b4); |
GiJeongKim | 0:51c43836c1d7 | 92 | } |