![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Original Version of STM Board
SPI_EEP_ENC/SPI_EEP_ENC.cpp@0:51c43836c1d7, 2019-08-14 (annotated)
- Committer:
- GiJeongKim
- Date:
- Wed Aug 14 08:13:36 2019 +0000
- Revision:
- 0:51c43836c1d7
- Child:
- 16:903b5a4433b4
cocoa;
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 |
GiJeongKim | 0:51c43836c1d7 | 6 | void spi_eeprom_ready(void){ |
GiJeongKim | 0:51c43836c1d7 | 7 | int temp1, temp2; |
GiJeongKim | 0:51c43836c1d7 | 8 | do{ |
GiJeongKim | 0:51c43836c1d7 | 9 | eeprom_cs=0; |
GiJeongKim | 0:51c43836c1d7 | 10 | eeprom.write(0x06); //write enable |
GiJeongKim | 0:51c43836c1d7 | 11 | eeprom_cs=1; |
GiJeongKim | 0:51c43836c1d7 | 12 | |
GiJeongKim | 0:51c43836c1d7 | 13 | eeprom_cs=0; |
GiJeongKim | 0:51c43836c1d7 | 14 | temp1 = eeprom.write(0x05); |
GiJeongKim | 0:51c43836c1d7 | 15 | temp2 = eeprom.write(0x00); |
GiJeongKim | 0:51c43836c1d7 | 16 | eeprom_cs=1; |
GiJeongKim | 0:51c43836c1d7 | 17 | temp2=(temp2&(0x03))!= 0x02; |
GiJeongKim | 0:51c43836c1d7 | 18 | } while(temp2); // before writing or reading |
GiJeongKim | 0:51c43836c1d7 | 19 | } |
GiJeongKim | 0:51c43836c1d7 | 20 | |
GiJeongKim | 0:51c43836c1d7 | 21 | void spi_eeprom_write(unsigned short int add, unsigned int data){ |
GiJeongKim | 0:51c43836c1d7 | 22 | eeprom_cs=0; |
GiJeongKim | 0:51c43836c1d7 | 23 | eeprom.write(0x02); |
GiJeongKim | 0:51c43836c1d7 | 24 | eeprom.write(0xff&(add>>8)); |
GiJeongKim | 0:51c43836c1d7 | 25 | eeprom.write(0xff&add); |
GiJeongKim | 0:51c43836c1d7 | 26 | eeprom.write(0xff&data); |
GiJeongKim | 0:51c43836c1d7 | 27 | eeprom.write(0xff&(data>>8)); |
GiJeongKim | 0:51c43836c1d7 | 28 | eeprom.write(0xff&(data>>16)); |
GiJeongKim | 0:51c43836c1d7 | 29 | eeprom.write(0xff&(data>>24)); |
GiJeongKim | 0:51c43836c1d7 | 30 | eeprom_cs=1; |
GiJeongKim | 0:51c43836c1d7 | 31 | } |
GiJeongKim | 0:51c43836c1d7 | 32 | |
GiJeongKim | 0:51c43836c1d7 | 33 | unsigned int spi_eeprom_read(unsigned short int add){ |
GiJeongKim | 0:51c43836c1d7 | 34 | eeprom_cs=0; |
GiJeongKim | 0:51c43836c1d7 | 35 | eeprom.write(0x03); |
GiJeongKim | 0:51c43836c1d7 | 36 | eeprom.write(0xff&(add>>8)); |
GiJeongKim | 0:51c43836c1d7 | 37 | eeprom.write(0xff&add); |
GiJeongKim | 0:51c43836c1d7 | 38 | int a1 = eeprom.write(0x00); |
GiJeongKim | 0:51c43836c1d7 | 39 | int a2 = eeprom.write(0x00); |
GiJeongKim | 0:51c43836c1d7 | 40 | int a3 = eeprom.write(0x00); |
GiJeongKim | 0:51c43836c1d7 | 41 | int a4 = eeprom.write(0x00); |
GiJeongKim | 0:51c43836c1d7 | 42 | eeprom_cs=1; |
GiJeongKim | 0:51c43836c1d7 | 43 | unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; |
GiJeongKim | 0:51c43836c1d7 | 44 | return final; |
GiJeongKim | 0:51c43836c1d7 | 45 | } |
GiJeongKim | 0:51c43836c1d7 | 46 | |
GiJeongKim | 0:51c43836c1d7 | 47 | |
GiJeongKim | 0:51c43836c1d7 | 48 | // ENCODER |
GiJeongKim | 0:51c43836c1d7 | 49 | void spi_enc_set_clear(void){ |
GiJeongKim | 0:51c43836c1d7 | 50 | unsigned int temp; |
GiJeongKim | 0:51c43836c1d7 | 51 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 52 | temp = enc.write(0b00100000); |
GiJeongKim | 0:51c43836c1d7 | 53 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 54 | } |
GiJeongKim | 0:51c43836c1d7 | 55 | |
GiJeongKim | 0:51c43836c1d7 | 56 | void spi_enc_set_init(void){ |
GiJeongKim | 0:51c43836c1d7 | 57 | unsigned int temp, i, temp1, temp2; |
GiJeongKim | 0:51c43836c1d7 | 58 | |
GiJeongKim | 0:51c43836c1d7 | 59 | // write MDR0 -> 0b11 -> x4 quadrature count mode |
GiJeongKim | 0:51c43836c1d7 | 60 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 61 | temp = enc.write(0b10001000); // WR + MDR0 |
GiJeongKim | 0:51c43836c1d7 | 62 | temp = enc.write(0b00000011); // quadratue mode |
GiJeongKim | 0:51c43836c1d7 | 63 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 64 | |
GiJeongKim | 0:51c43836c1d7 | 65 | // write MDR1 -> 0b10 -> 2-byte counter mode |
GiJeongKim | 0:51c43836c1d7 | 66 | for(i=0;i<100;i++); |
GiJeongKim | 0:51c43836c1d7 | 67 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 68 | temp = enc.write(0b10010000); // WR + MDR1 |
GiJeongKim | 0:51c43836c1d7 | 69 | //temp = enc.write(0b00000010); // 2 byte mode |
GiJeongKim | 0:51c43836c1d7 | 70 | temp = enc.write(0b00000000); // 4 byte mode |
GiJeongKim | 0:51c43836c1d7 | 71 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 72 | |
GiJeongKim | 0:51c43836c1d7 | 73 | // clear |
GiJeongKim | 0:51c43836c1d7 | 74 | spi_enc_set_clear(); |
GiJeongKim | 0:51c43836c1d7 | 75 | } |
GiJeongKim | 0:51c43836c1d7 | 76 | |
GiJeongKim | 0:51c43836c1d7 | 77 | |
GiJeongKim | 0:51c43836c1d7 | 78 | int spi_enc_read(void){ |
GiJeongKim | 0:51c43836c1d7 | 79 | //for(t_i=0;t_i<100;t_i++); |
GiJeongKim | 0:51c43836c1d7 | 80 | unsigned int t_dummy, t_b1, t_b2, t_b3, t_b4, t_i; |
GiJeongKim | 0:51c43836c1d7 | 81 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 82 | t_dummy = enc.write(0b01100000); // Read Commend |
GiJeongKim | 0:51c43836c1d7 | 83 | t_b1 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 84 | t_b2 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 85 | t_b3 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 86 | t_b4 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 87 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 88 | |
GiJeongKim | 0:51c43836c1d7 | 89 | return((t_b1<<24) + (t_b2<<16) + (t_b3<<8) + t_b4); |
GiJeongKim | 0:51c43836c1d7 | 90 | } |