From d59df82aa563bb8f01bc5060e595fa2a23b893cb Mon Sep 17 00:00:00 2001 From: SeungheeOh Date: Thu, 25 Jul 2019 00:03:15 +0900 Subject: [PATCH] update --- d72042.ino | 283 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 209 insertions(+), 74 deletions(-) diff --git a/d72042.ino b/d72042.ino index f87d901..b2275ca 100644 --- a/d72042.ino +++ b/d72042.ino @@ -5,32 +5,13 @@ #define READ 0x08 #define RESET 7 +#define DATASIZE 20 +char data[DATASIZE] = {0x0,}; +void iebus_init(); byte inline bitchange(byte b) { - byte z=0; - if(b&0x8) - { - z|=0x10; - } - - if(b&0x4) - { - z|=0x20; - } - if(b&0x2) - { - z|=0x40; - } - if(b&0x1) - { - z|=0x80; - } - - - z=b<<4; - - return z; + return b << 4; } @@ -55,69 +36,223 @@ byte inline bitchange(byte b) #define REG_READ_RCR (bitchange(0x8)|READ) #define REG_READ_RBF (bitchange(0xe)|READ) - + void setup (void) { - Serial.begin(9600); //set baud rate to 115200 for usart + Serial.begin(9600); //set baud rate to 115200 for usart - SPI.begin (); - SPI.setClockDivider(SPI_CLOCK_DIV128);//divide the clock by 8 + SPI.begin (); + SPI.setClockDivider(SPI_CLOCK_DIV128);//divide the clock by 8 - pinMode(MODE,OUTPUT); - pinMode(CONTROL,OUTPUT); + pinMode(MODE, OUTPUT); + pinMode(CONTROL, OUTPUT); - pinMode(RESET, OUTPUT); - - digitalWrite(MODE, HIGH); - digitalWrite(CONTROL, HIGH); + pinMode(RESET, OUTPUT); - - digitalWrite(RESET, HIGH); -digitalWrite(SS, LOW); + digitalWrite(MODE, HIGH); + digitalWrite(CONTROL, HIGH); - + + digitalWrite(RESET, HIGH); + digitalWrite(SS, LOW); + + iebus_init(); } - + +// avclan.deviceAddress = 0x360; +// avclan.broadcast = AVC_MSG_NORMAL; +// avclan.masterAddress = 0x0360; +// avclan.slaveAddress = 0x0140; + + + +#define LOW_ADDR 0x0 +#define HIGH_ADDR 0x36 + + + +struct _communicationFlag +{ + bool RAWF; + bool TRRQ; + bool TRCF; + byte I; + bool RERQ; + byte RECF; + byte SIZE; + byte PW; + byte PR; + byte J; + bool MCRQ; + bool SDRQ; + bool CORQ; + bool MTRQF; + bool MRRQF; + bool STRQF; + bool SLREF; +}commFlag; + + + + +void iebus_init() +{ + digitalWrite(RESET, LOW); + delay(10); + digitalWrite(RESET, HIGH); + delay(10); + + dataWrite1byte(REG_WRITE_UAR1, LOW_ADDR << 4); + dataWrite1byte(REG_WRITE_UAR2, HIGH_ADDR); + dataWrite1byte(REG_WRITE_CMR, B10000000); //CMR 100000 b1 b0 + + flaginit(); +} +void flaginit() +{ + commFlag.RAWF = 0; + commFlag.TRRQ = 0; + commFlag.RERQ = 0; + commFlag.SIZE = 0; + commFlag.J = 1; + commFlag.PW = 0; + commFlag.PR = 0; + commFlag.MCRQ = 0; + commFlag.SDRQ = 0; + commFlag.CORQ = 0; + commFlag.MTRQF = 0; + commFlag.MRRQF = 0; + commFlag.STRQF = 0; + commFlag.SLREF = 0; + + +} + +void iebus_transmission() +{ + +} + + +void iebus_reception() +{ + +} +void iebus_mastercomm() +{ + +} +void iebus_slavetransmission() +{ + +} +void iebus_command() +{ + +} +void iebus_loop() +{ + if (commFlag.RAWF == 1) + { + iebus_init(); + return; + } + + if (commFlag.TRRQ == 1) + { + iebus_transmission(); + commFlag.TRRQ = 0; + } + if (commFlag.RERQ == 1) + { + iebus_reception(); + commFlag.RERQ = 0; + } + + if (commFlag.MCRQ == 1) + { + iebus_mastercomm(); + commFlag.MCRQ = 0; + } + + if (commFlag.SDRQ == 1) + { + iebus_slavetransmission(); + commFlag.SDRQ = 0; + } + + if (commFlag.CORQ == 1) + { + iebus_command(); + commFlag.CORQ = 0; + } + +} +void dataWrite(char reg_addr, char *data, int datasize) +{ + //data + for (int i = 0; i < datasize; i++) + { + + } +} + +int dataRead(char reg_addr, char *data) +{ + int size = 0; + + return size; +} + +byte dataRead1byte(char addr) +{ + byte data; + // write the LTC CS pin low to initiate ADC sample and data transmit + digitalWrite(CONTROL, HIGH); + delay(5); + SPI.transfer(REG_READ_FLG); // Register Select + + digitalWrite(CONTROL, LOW); + delay(5); + data = SPI.transfer(0x0); // read second 8 bits + // Serial.println(data); //0x2 + + return data; +} + + +void dataWrite1byte(char addr , char data) +{ + digitalWrite(CONTROL, HIGH); + delay(5); + SPI.transfer(addr); // Register Select + + digitalWrite(CONTROL, LOW); + delay(5); + SPI.transfer(data); // read second 8 bits + delay(5); + +} + + void loop (void) { + iebus_loop(); + + + + byte addr = 0x0; byte data; - - SPI.beginTransaction(SPISettings(1000, MSBFIRST, SPI_MODE0)); - // set speed bit format and clock/data polarity while starting SPI transaction + // SPI.beginTransaction(SPISettings(1000, MSBFIRST, SPI_MODE0)); + // set speed bit format and clock/data polarity while starting SPI transaction + Serial.println(dataRead1byte(REG_READ_FLG)); -digitalWrite(CONTROL, HIGH); - delay(5); - SPI.transfer(REG_WRITE_CTR); // Register Select - - digitalWrite(CONTROL, LOW); - delay(5); - SPI.transfer(0x10); // read second 8 bits - delay(5); - - - - - - - - - // write the LTC CS pin low to initiate ADC sample and data transmit - digitalWrite(CONTROL, HIGH); - delay(5); - SPI.transfer(REG_READ_FLG); // Register Select - - digitalWrite(CONTROL, LOW); - delay(5); - data = SPI.transfer(0x0); // read second 8 bits - Serial.println(data); //0x2 - - // wite LTC CS pin high to stop LTC from transmitting zeros. - SPI.endTransaction(); - // close SPI transaction - + // wite LTC CS pin high to stop LTC from transmitting zeros. + // SPI.endTransaction(); + // close SPI transaction delay(250); - // Delay that is fast but easy to read. -// delayMicroseconds(83); - // Delay that matches 12 khz delay time. + // Delay that is fast but easy to read. + // delayMicroseconds(83); + // Delay that matches 12 khz delay time. }