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