![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
for learning
SPI_EEP_ENC/SPI_EEP_ENC.cpp@255:aa424a9ca332, 2021-02-22 (annotated)
- Committer:
- Lightvalve
- Date:
- Mon Feb 22 14:09:13 2021 +0000
- Revision:
- 255:aa424a9ca332
- Parent:
- 253:0246366e0dcb
- Child:
- 256:7c8cc8b56b88
210222 - enc.frequency 5M -> 10M
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 | 170:42c938a40313 | 6 | void spi_eeprom_ready(void){ |
Lightvalve | 170:42c938a40313 | 7 | int temp1, temp2; |
Lightvalve | 170:42c938a40313 | 8 | do{ |
Lightvalve | 170:42c938a40313 | 9 | eeprom_cs=0; |
Lightvalve | 170:42c938a40313 | 10 | eeprom.write(0x06); //write enable |
Lightvalve | 170:42c938a40313 | 11 | eeprom_cs=1; |
Lightvalve | 170:42c938a40313 | 12 | |
Lightvalve | 170:42c938a40313 | 13 | eeprom_cs=0; |
Lightvalve | 170:42c938a40313 | 14 | temp1 = eeprom.write(0x05); |
Lightvalve | 170:42c938a40313 | 15 | temp2 = eeprom.write(0x00); |
Lightvalve | 170:42c938a40313 | 16 | eeprom_cs=1; |
Lightvalve | 170:42c938a40313 | 17 | temp2=(temp2&(0x03))!= 0x02; |
Lightvalve | 170:42c938a40313 | 18 | } while(temp2); // before writing or reading |
Lightvalve | 170:42c938a40313 | 19 | } |
Lightvalve | 170:42c938a40313 | 20 | |
Lightvalve | 253:0246366e0dcb | 21 | // void spi_eeprom_write(unsigned short add, unsigned int data){ |
Lightvalve | 253:0246366e0dcb | 22 | // spi_eeprom_ready(); |
Lightvalve | 253:0246366e0dcb | 23 | // add=add*4; |
Lightvalve | 253:0246366e0dcb | 24 | // eeprom_cs=0; |
Lightvalve | 253:0246366e0dcb | 25 | // eeprom.write(0x02); |
Lightvalve | 253:0246366e0dcb | 26 | // eeprom.write(0xff&(add>>8)); |
Lightvalve | 253:0246366e0dcb | 27 | // eeprom.write(0xff&add); |
Lightvalve | 253:0246366e0dcb | 28 | // eeprom.write(0xff&data); |
Lightvalve | 253:0246366e0dcb | 29 | // eeprom.write(0xff&(data>>8)); |
Lightvalve | 253:0246366e0dcb | 30 | // eeprom.write(0xff&(data>>16)); |
Lightvalve | 253:0246366e0dcb | 31 | // eeprom.write(0xff&(data>>24)); |
Lightvalve | 253:0246366e0dcb | 32 | // eeprom_cs=1; |
Lightvalve | 253:0246366e0dcb | 33 | //} |
Lightvalve | 253:0246366e0dcb | 34 | void spi_eeprom_write(unsigned short add, int data){ |
Lightvalve | 170:42c938a40313 | 35 | spi_eeprom_ready(); |
Lightvalve | 170:42c938a40313 | 36 | add=add*4; |
Lightvalve | 170:42c938a40313 | 37 | eeprom_cs=0; |
Lightvalve | 170:42c938a40313 | 38 | eeprom.write(0x02); |
Lightvalve | 170:42c938a40313 | 39 | eeprom.write(0xff&(add>>8)); |
Lightvalve | 170:42c938a40313 | 40 | eeprom.write(0xff&add); |
Lightvalve | 170:42c938a40313 | 41 | eeprom.write(0xff&data); |
Lightvalve | 170:42c938a40313 | 42 | eeprom.write(0xff&(data>>8)); |
Lightvalve | 170:42c938a40313 | 43 | eeprom.write(0xff&(data>>16)); |
Lightvalve | 170:42c938a40313 | 44 | eeprom.write(0xff&(data>>24)); |
Lightvalve | 170:42c938a40313 | 45 | eeprom_cs=1; |
Lightvalve | 170:42c938a40313 | 46 | } |
Lightvalve | 170:42c938a40313 | 47 | |
Lightvalve | 253:0246366e0dcb | 48 | //unsigned int spi_eeprom_read(unsigned short add){ |
Lightvalve | 253:0246366e0dcb | 49 | // add=add*4; |
Lightvalve | 253:0246366e0dcb | 50 | // eeprom_cs=0; |
Lightvalve | 253:0246366e0dcb | 51 | // eeprom.write(0x03); |
Lightvalve | 253:0246366e0dcb | 52 | // eeprom.write(0xff&(add>>8)); |
Lightvalve | 253:0246366e0dcb | 53 | // eeprom.write(0xff&add); |
Lightvalve | 253:0246366e0dcb | 54 | // |
Lightvalve | 253:0246366e0dcb | 55 | // int a1 = eeprom.write(0x00); |
Lightvalve | 253:0246366e0dcb | 56 | // int a2 = eeprom.write(0x00); |
Lightvalve | 253:0246366e0dcb | 57 | // int a3 = eeprom.write(0x00); |
Lightvalve | 253:0246366e0dcb | 58 | // int a4 = eeprom.write(0x00); |
Lightvalve | 253:0246366e0dcb | 59 | // eeprom_cs=1; |
Lightvalve | 253:0246366e0dcb | 60 | // //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; |
Lightvalve | 253:0246366e0dcb | 61 | // unsigned int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24); |
Lightvalve | 253:0246366e0dcb | 62 | // return final; |
Lightvalve | 253:0246366e0dcb | 63 | // } |
Lightvalve | 253:0246366e0dcb | 64 | int spi_eeprom_read(unsigned short add){ |
Lightvalve | 170:42c938a40313 | 65 | add=add*4; |
Lightvalve | 170:42c938a40313 | 66 | eeprom_cs=0; |
Lightvalve | 170:42c938a40313 | 67 | eeprom.write(0x03); |
Lightvalve | 170:42c938a40313 | 68 | eeprom.write(0xff&(add>>8)); |
Lightvalve | 170:42c938a40313 | 69 | eeprom.write(0xff&add); |
Lightvalve | 170:42c938a40313 | 70 | |
Lightvalve | 170:42c938a40313 | 71 | int a1 = eeprom.write(0x00); |
Lightvalve | 170:42c938a40313 | 72 | int a2 = eeprom.write(0x00); |
Lightvalve | 170:42c938a40313 | 73 | int a3 = eeprom.write(0x00); |
Lightvalve | 170:42c938a40313 | 74 | int a4 = eeprom.write(0x00); |
Lightvalve | 170:42c938a40313 | 75 | eeprom_cs=1; |
Lightvalve | 170:42c938a40313 | 76 | //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; |
Lightvalve | 253:0246366e0dcb | 77 | int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24); |
Lightvalve | 170:42c938a40313 | 78 | return final; |
Lightvalve | 170:42c938a40313 | 79 | } |
GiJeongKim | 0:51c43836c1d7 | 80 | |
GiJeongKim | 0:51c43836c1d7 | 81 | |
GiJeongKim | 0:51c43836c1d7 | 82 | // ENCODER |
GiJeongKim | 0:51c43836c1d7 | 83 | void spi_enc_set_clear(void){ |
GiJeongKim | 0:51c43836c1d7 | 84 | unsigned int temp; |
Lightvalve | 255:aa424a9ca332 | 85 | enc_cs = 1; //sw add |
GiJeongKim | 0:51c43836c1d7 | 86 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 87 | temp = enc.write(0b00100000); |
GiJeongKim | 0:51c43836c1d7 | 88 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 89 | } |
GiJeongKim | 0:51c43836c1d7 | 90 | |
GiJeongKim | 0:51c43836c1d7 | 91 | void spi_enc_set_init(void){ |
GiJeongKim | 0:51c43836c1d7 | 92 | unsigned int temp, i, temp1, temp2; |
GiJeongKim | 0:51c43836c1d7 | 93 | |
GiJeongKim | 0:51c43836c1d7 | 94 | // write MDR0 -> 0b11 -> x4 quadrature count mode |
Lightvalve | 255:aa424a9ca332 | 95 | enc_cs = 1; //sw add |
GiJeongKim | 0:51c43836c1d7 | 96 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 97 | temp = enc.write(0b10001000); // WR + MDR0 |
GiJeongKim | 0:51c43836c1d7 | 98 | temp = enc.write(0b00000011); // quadratue mode |
GiJeongKim | 0:51c43836c1d7 | 99 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 100 | |
GiJeongKim | 0:51c43836c1d7 | 101 | // write MDR1 -> 0b10 -> 2-byte counter mode |
GiJeongKim | 0:51c43836c1d7 | 102 | for(i=0;i<100;i++); |
GiJeongKim | 0:51c43836c1d7 | 103 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 104 | temp = enc.write(0b10010000); // WR + MDR1 |
GiJeongKim | 0:51c43836c1d7 | 105 | //temp = enc.write(0b00000010); // 2 byte mode |
GiJeongKim | 0:51c43836c1d7 | 106 | temp = enc.write(0b00000000); // 4 byte mode |
GiJeongKim | 0:51c43836c1d7 | 107 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 108 | |
GiJeongKim | 0:51c43836c1d7 | 109 | // clear |
GiJeongKim | 0:51c43836c1d7 | 110 | spi_enc_set_clear(); |
GiJeongKim | 0:51c43836c1d7 | 111 | } |
GiJeongKim | 0:51c43836c1d7 | 112 | |
GiJeongKim | 0:51c43836c1d7 | 113 | |
GiJeongKim | 0:51c43836c1d7 | 114 | int spi_enc_read(void){ |
GiJeongKim | 0:51c43836c1d7 | 115 | //for(t_i=0;t_i<100;t_i++); |
GiJeongKim | 0:51c43836c1d7 | 116 | unsigned int t_dummy, t_b1, t_b2, t_b3, t_b4, t_i; |
Lightvalve | 255:aa424a9ca332 | 117 | enc_cs = 1; //sw add |
GiJeongKim | 0:51c43836c1d7 | 118 | enc_cs = 0; |
GiJeongKim | 0:51c43836c1d7 | 119 | t_dummy = enc.write(0b01100000); // Read Commend |
GiJeongKim | 0:51c43836c1d7 | 120 | t_b1 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 121 | t_b2 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 122 | t_b3 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 123 | t_b4 = enc.write(0x00); // Dummy data for clock |
GiJeongKim | 0:51c43836c1d7 | 124 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 125 | |
GiJeongKim | 0:51c43836c1d7 | 126 | return((t_b1<<24) + (t_b2<<16) + (t_b3<<8) + t_b4); |
GiJeongKim | 0:51c43836c1d7 | 127 | } |