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.
main.cpp
00001 #include "mbed.h" 00002 00003 Serial pc(USBTX, USBRX); // tx, rx 00004 00005 00006 SPI spi(p5, p6, p7); // mosi, miso, sclk 00007 DigitalOut rst_n(p8); // 00008 DigitalOut cs_n(p9); 00009 00010 // p5 : pin19 MOSI 00011 // p6 : pin20 MISO 00012 // p7 : pin01 SCLK 00013 // p8 : pin04 RESET_N 00014 00015 00016 00017 00018 00019 int read_reg(int addr); 00020 void write_reg(int addr,int data); 00021 00022 int read_reg(int addr) { 00023 int retval; 00024 cs_n=0; 00025 wait_us(4000); 00026 spi.write(0x21); 00027 wait_us(1000); 00028 spi.write(addr & 0xFF); 00029 wait_us(1000); 00030 retval = spi.write(0); 00031 wait_us(1000); 00032 retval = spi.write(0); 00033 wait_us(4000); 00034 cs_n=1; 00035 return retval; 00036 } 00037 00038 void write_reg(int addr,int data){ 00039 cs_n=0; 00040 wait_us(4000); 00041 spi.write(0x20); 00042 wait_us(1000); 00043 spi.write(addr & 0xFF); 00044 wait_us(1000); 00045 spi.write(data); 00046 wait_us(4000); 00047 cs_n=1; 00048 } 00049 00050 int main() { 00051 int rw,addr,data; 00052 // 8-bit, SPI mode 2 00053 spi.format(8,2); 00054 // 10kHz 00055 spi.frequency(10000); 00056 00057 pc.printf("Init device\r\n"); 00058 00059 cs_n = 1; 00060 rst_n = 0; 00061 wait_ms(10); 00062 rst_n = 1; 00063 00064 while(1) { 00065 rw = -1; 00066 addr = 0; 00067 data = 0; 00068 pc.printf("Write and Read Address Data? rw(write=0,read=1) addr(hex) data(hex): \r\n"); 00069 pc.scanf("%d %x %x",&rw,&addr,&data); 00070 // Write 00071 cs_n=0; 00072 if ( rw == 0 ) { 00073 write_reg(addr,data); 00074 pc.printf("Wrote %02x @ %02x\r\n",data,addr); 00075 pc.printf("Read %02x @ %02x\r\n",read_reg(addr),addr); 00076 } 00077 else if (rw == 1 ) { 00078 pc.printf("Read %02x @ %02x\r\n",read_reg(addr),addr); 00079 } 00080 else if (rw == 2) { 00081 for (int i=0; i<10;i++){ 00082 pc.printf("Read data %02x @ %02x\r\n",read_reg(i),i); 00083 } 00084 } 00085 else { 00086 pc.printf("Unknown command\r\n"); 00087 } 00088 } 00089 }
Generated on Fri Oct 14 2022 08:15:06 by
1.7.2