Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SPI_EEP_ENC.cpp
00001 #include "mbed.h" 00002 #include "setting.h" 00003 #include "SPI_EEP_ENC.h" 00004 00005 // EEPROM 00006 void spi_eeprom_ready(void){ 00007 int temp1, temp2; 00008 do{ 00009 eeprom_cs=0; 00010 eeprom.write(0x06); //write enable 00011 eeprom_cs=1; 00012 00013 eeprom_cs=0; 00014 temp1 = eeprom.write(0x05); 00015 temp2 = eeprom.write(0x00); 00016 eeprom_cs=1; 00017 temp2=(temp2&(0x03))!= 0x02; 00018 } while(temp2); // before writing or reading 00019 } 00020 00021 void spi_eeprom_write(unsigned short add, unsigned int data){ 00022 spi_eeprom_ready(); 00023 add=add*4; 00024 eeprom_cs=0; 00025 eeprom.write(0x02); 00026 eeprom.write(0xff&(add>>8)); 00027 eeprom.write(0xff&add); 00028 eeprom.write(0xff&data); 00029 eeprom.write(0xff&(data>>8)); 00030 eeprom.write(0xff&(data>>16)); 00031 eeprom.write(0xff&(data>>24)); 00032 eeprom_cs=1; 00033 } 00034 00035 unsigned int spi_eeprom_read(unsigned short add){ 00036 add=add*4; 00037 eeprom_cs=0; 00038 eeprom.write(0x03); 00039 eeprom.write(0xff&(add>>8)); 00040 eeprom.write(0xff&add); 00041 00042 int a1 = eeprom.write(0x00); 00043 int a2 = eeprom.write(0x00); 00044 int a3 = eeprom.write(0x00); 00045 int a4 = eeprom.write(0x00); 00046 eeprom_cs=1; 00047 //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; 00048 unsigned int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24); 00049 return final; 00050 } 00051 00052 00053 // ENCODER 00054 void spi_enc_set_clear(void){ 00055 unsigned int temp; 00056 enc_cs = 0; 00057 temp = enc.write(0b00100000); 00058 enc_cs = 1; 00059 } 00060 00061 void spi_enc_set_init(void){ 00062 unsigned int temp, i, temp1, temp2; 00063 00064 // write MDR0 -> 0b11 -> x4 quadrature count mode 00065 enc_cs = 0; 00066 temp = enc.write(0b10001000); // WR + MDR0 00067 temp = enc.write(0b00000011); // quadratue mode 00068 enc_cs = 1; 00069 00070 // write MDR1 -> 0b10 -> 2-byte counter mode 00071 for(i=0;i<10000;i++); 00072 enc_cs = 0; 00073 temp = enc.write(0b10010000); // WR + MDR1 00074 //temp = enc.write(0b00000010); // 2 byte mode 00075 temp = enc.write(0b00000000); // 4 byte mode 00076 enc_cs = 1; 00077 00078 // clear 00079 spi_enc_set_clear(); 00080 } 00081 00082 00083 int spi_enc_read(void){ 00084 //for(t_i=0;t_i<100;t_i++); 00085 unsigned int t_dummy, t_b1, t_b2, t_b3, t_b4, t_i; 00086 enc_cs = 0; 00087 t_dummy = enc.write(0b01100000); // Read Commend 00088 t_b1 = enc.write(0x00); // Dummy data for clock 00089 t_b2 = enc.write(0x00); // Dummy data for clock 00090 t_b3 = enc.write(0x00); // Dummy data for clock 00091 t_b4 = enc.write(0x00); // Dummy data for clock 00092 enc_cs = 1; 00093 00094 return((t_b1<<24) + (t_b2<<16) + (t_b3<<8) + t_b4); 00095 }
Generated on Mon Aug 1 2022 09:51:13 by
1.7.2