From 3ee1a69c65589a744b67f770001022a884fe32ba Mon Sep 17 00:00:00 2001 From: Joshua Drake Date: Sat, 4 May 2024 11:11:46 -0500 Subject: Code is fully functional with delay timer at uC level. --- SPI.c | 18 ++-- SPI.h | 4 +- build/default/production/SPI.o | Bin 15592 -> 15592 bytes build/default/production/main.o | Bin 8560 -> 8896 bytes .../production/mcc_generated_files/mssp2_i2c.o | Bin 19196 -> 19500 bytes dist/default/production/memoryfile.xml | 8 +- main.c | 48 ++++++----- mcc_generated_files/mssp2_i2c.c | 15 ++-- mcc_generated_files/mssp2_i2c.h | 3 +- nbproject/Makefile-genesis.properties | 8 +- nbproject/configurations.xml | 93 +++++++++++++++++++-- nbproject/private/configurations.xml | 2 +- 12 files changed, 148 insertions(+), 51 deletions(-) diff --git a/SPI.c b/SPI.c index a6590b4..96a18a8 100644 --- a/SPI.c +++ b/SPI.c @@ -35,17 +35,19 @@ //WriteBuffer[1] = 0xFF >> (15-Ohm_150); //WriteBuffer[3] = 0xFE << (7-(Ohm_150-8)) | (Ohm_68 - 4); //2WriteBuffer[1] = 0x0F << (4-(Ohm_68 - 1)) | (testLED << 7); - WriteBuffer[1] = (0xf0 >> (Ohm_68 - 1)) | (testLED << 7); - if(Ohm_150>7)WriteBuffer[3] = 0x7f | ((Ohm_68 - 4)<<7); - else WriteBuffer[3] = (0x7f >> (7-Ohm_150)) | ((Ohm_68 - 4)<<7); + + if(Ohm_150>7)WriteBuffer[3] = 0x7f; + else WriteBuffer[3] = (0x7f >> (7-Ohm_150));// + ((Ohm_68 - 4)<<7); WriteBuffer[5] = 0xff >> (15-Ohm_150); + if(Ohm_68<5)WriteBuffer[1] = ((0xf0 >> (Ohm_68))&0x0f)| (testLED << 7); + else {WriteBuffer[1] = 0x0f | (testLED << 7); WriteBuffer[3] |= 0x80;} //WriteBuffer[1] = 0xFF; //if (Ohm_68 == 5) WriteBuffer[3]|= 0b10000000; SPI_slaveSelect(); readData = MSSP1_SPI_Exchange8bitBuffer(WriteBuffer, 6, ReadBuffer); SPI_slaveDeselect(); } - void active(uint8_t *ohm150, uint8_t *ohm68) + void active(uint8_t *activeResistors) { WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x40; WriteBuffer[1] = WriteBuffer[3] = WriteBuffer[5] = 0x02; @@ -57,9 +59,11 @@ SPI_slaveSelect(); readData = MSSP1_SPI_Exchange8bitBuffer(WriteBuffer, 6, ReadBuffer); SPI_slaveDeselect(); - ohm150 = bitcount(ReadBuffer[3]&0x7F) + bitcount(ReadBuffer[5]); //+ bitcount(ReadBuffer[3] << 1)); + activeResistors[0] = bitcount(ReadBuffer[3]&0x7F) + bitcount(ReadBuffer[5]); //+ bitcount(ReadBuffer[3] << 1)); //ohm150 = bitcount(ReadBuffer[3]);// + ReadBuffer[5] + ReadBuffer[3] + ReadBuffer[1]; - ohm68 = ((ReadBuffer[3] & 0x80)>>7) + bitcount(ReadBuffer[0] & 0x0f); + activeResistors[1] = /*((ReadBuffer[3] & 0x80)>>7) +*/ bitcount(ReadBuffer[1] & 0x0f); + //activeResistors[0] = 0; + //activeResistors[0] = 3; } @@ -182,7 +186,7 @@ WriteBuffer[5] = outputs[2]; } -int bitcount(uint8_t n) +uint8_t bitcount(uint8_t n) { uint8_t count = 0; while (n > 0) diff --git a/SPI.h b/SPI.h index 4181835..6cdeeea 100644 --- a/SPI.h +++ b/SPI.h @@ -57,8 +57,8 @@ Message_OUT(15, 5, false); //Max Load with testLED turned off. */ - void active(uint8_t *ohm150, uint8_t *ohm68); - int bitcount(uint8_t); + void active(uint8_t *activeResistors); + uint8_t bitcount(uint8_t); void Message_BIM(uint8_t outputs[3]); /** @Summary diff --git a/build/default/production/SPI.o b/build/default/production/SPI.o index a1791f2..4a81a77 100644 Binary files a/build/default/production/SPI.o and b/build/default/production/SPI.o differ diff --git a/build/default/production/main.o b/build/default/production/main.o index 189c9dc..20c5e38 100644 Binary files a/build/default/production/main.o and b/build/default/production/main.o differ diff --git a/build/default/production/mcc_generated_files/mssp2_i2c.o b/build/default/production/mcc_generated_files/mssp2_i2c.o index 4a618a4..1a72c9e 100644 Binary files a/build/default/production/mcc_generated_files/mssp2_i2c.o and b/build/default/production/mcc_generated_files/mssp2_i2c.o differ diff --git a/dist/default/production/memoryfile.xml b/dist/default/production/memoryfile.xml index d64cd8d..48256ad 100644 --- a/dist/default/production/memoryfile.xml +++ b/dist/default/production/memoryfile.xml @@ -5,14 +5,14 @@ bytes 2048 - 100 - 1948 + 98 + 1950 bytes 16128 - 4188 - 11940 + 4359 + 11769 diff --git a/main.c b/main.c index 77d9b11..04c1754 100644 --- a/main.c +++ b/main.c @@ -53,8 +53,10 @@ /* Main application */ -uint8_t desired150, desired68, current150, current68 = 0; +uint8_t activeResistors[2] = {0x00,0x00}; +uint8_t desired150, desired68 = 0; bool relay_delay = false; +static uint8_t* timeoutcounter; void TMR1_Process(void); @@ -77,41 +79,43 @@ int main(void) while (1) { + timeoutcounter = getTimeoutCounter(); desired150 = getI2CArray(1) & 0x0F; - desired68 = (getI2CArray(1) & 0x70) >> 4; + desired68 = (getI2CArray(1) >> 4) & 0x07; - //if((relay_delay)&&(getTimeoutCounter() < 2)) - //{ - //relay_delay = false; - active(¤t150,¤t68); - /* - if(desired150 < current150) + if((relay_delay)&&(*timeoutcounter < 2)) + { + _LATB8 = 1; + relay_delay = false; + active(activeResistors); + + if(desired150 < activeResistors[0]) { - Message_OUT(current150-1, current68, 1); + Message_OUT(activeResistors[0]-1, activeResistors[1], 1); } - else if(desired150 > current150) + else if(desired150 > activeResistors[0]) { - Message_OUT(current150+1, current68, 1); + Message_OUT(activeResistors[0]+1, activeResistors[1], 1); } - else if(desired68 < current68) + else if(desired68 < activeResistors[1]) { - Message_OUT(current150, current68-1, 1); + Message_OUT(activeResistors[0], activeResistors[1]-1, 0); } - else if(desired68 > current68) + else if(desired68 > activeResistors[1]) { - Message_OUT(current150, current68+1, 1); + Message_OUT(activeResistors[0], activeResistors[1]+1, 0); } } - - if(getTimeoutCounter() > 2){ //Open contactors if I2C communication is broken. + + if(*timeoutcounter > 0x02) //Open contactors if I2C communication is broken. + { Message_OUT(0, 0, 0); - _LATB8 = 0; - _LATB7 = 1; - }*/ - Message_OUT(2, 0, relay_delay); + } + //(desired150, desired68, relay_delay); - if(current150>0){_LATB8=0; _LATB7=0;} + //if(activeResistors[1]==3){_LATB8=0;} + //else{_LATB8=1;} } return 1; diff --git a/mcc_generated_files/mssp2_i2c.c b/mcc_generated_files/mssp2_i2c.c index dabd9a9..580366b 100644 --- a/mcc_generated_files/mssp2_i2c.c +++ b/mcc_generated_files/mssp2_i2c.c @@ -128,7 +128,7 @@ uint8_t yeah; #define ARRAY_CNT 6 // Number of bytes in array uint8_t slaveAddress = 0x30; // 7-bit slave address uint8_t index = 0; // Array pointer -uint8_t I2C_timeout = 10; //Number of timer iterations since clear +static uint8_t I2C_timeout = 0; //Number of timer iterations since clear uint8_t temp = 0; // Temp register uint8_t regAdd = 1; // First data byte was reg add uint8_t i2cArray[ARRAY_CNT] = @@ -188,10 +188,10 @@ void __attribute__((interrupt, no_auto_psv)) _MSSP2Interrupt(void) { } if (SSP2STATbits.R_NOT_W == 0) // Master write (slave receive) { - + resetTimeoutCounter(); //Reset our counter when a message is received. if (SSP2STATbits.D_NOT_A == 0) // Last byte was an address { - I2C_timeout = 0; //Reset our counter when a message is received. + //resetTimeoutCounter(); //Reset our counter when a message is received. _LATB7 = !_LATB7; regAdd = 1; // Next byte register address temp = SSP2BUF; // Clear BF @@ -199,6 +199,7 @@ void __attribute__((interrupt, no_auto_psv)) _MSSP2Interrupt(void) { } if (SSP2STATbits.D_NOT_A == 1) // Last byte was data { + //resetTimeoutCounter(); //Reset our counter when a message is received. if (regAdd == 1) // Last byte was register add { index = SSP2BUF; // Load register address @@ -225,15 +226,19 @@ void __attribute__((interrupt, no_auto_psv)) _MSSP2Interrupt(void) { _LATB8 = !SSP2CON1bits.SSPOV; } -uint8_t getTimeoutCounter() +uint8_t* getTimeoutCounter() { - return I2C_timeout; + return &I2C_timeout; } void incrementTimeoutCounter() { I2C_timeout++; } +void resetTimeoutCounter() +{ + I2C_timeout = 0x00; +} uint8_t getI2CArray(int index) { if(index < 5)return i2cArray[index]; diff --git a/mcc_generated_files/mssp2_i2c.h b/mcc_generated_files/mssp2_i2c.h index f76d48c..329983a 100644 --- a/mcc_generated_files/mssp2_i2c.h +++ b/mcc_generated_files/mssp2_i2c.h @@ -149,7 +149,8 @@ typedef enum */ -uint8_t getTimeoutCounter(void); +uint8_t* getTimeoutCounter(void); +void resetTimeoutCounter(void); void incrementTimeoutCounter(void); void MSSP2_I2C_Initialize(void); diff --git a/nbproject/Makefile-genesis.properties b/nbproject/Makefile-genesis.properties index 58a5b7d..e8ecf1c 100644 --- a/nbproject/Makefile-genesis.properties +++ b/nbproject/Makefile-genesis.properties @@ -1,13 +1,13 @@ # -#Thu May 02 18:31:10 CDT 2024 +#Fri May 03 20:19:47 CDT 2024 default.languagetoolchain.version=2.10 default.Pack.dfplocation=/opt/microchip/mplabx/v6.15/packs/Microchip/PIC24F-KA-KL-KM_DFP/1.4.83 +default.com-microchip-mplab-mdbcore-PK5Tool-PK5ToolImpl.md5=723771b957e0f1e0ec1761a42d8bb5e8 conf.ids=default default.languagetoolchain.dir=/opt/microchip/xc16/v2.10/bin -host.id=17y8-g9hi-6g -configurations-xml=548bce5bcce17c7b8a52ea2b767e1662 +host.id=0 +configurations-xml=297850d1f3e30335411a86c54d420620 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=e62346c0c0ecee2637e613b49cb7b7fa proj.dir=/home/josh/MPLABXProjects/resbox -default.com-microchip-mplab-mdbcore-pk4hybrid-Pk4HybridTooImpl.md5=38058ec39ea8224adcc766836376f0d6 host.platform=linux default.com-microchip-mplab-nbide-toolchain-xc16-XC16LanguageToolchain.md5=07f7da95e66d00aa4668de5175f752c5 diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 8b11231..b91c632 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -57,7 +57,7 @@ PIC24FV16KM202 - pk4hybrid + PK5Tool XC16 2.10 2 @@ -222,6 +222,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -705,7 +788,7 @@ - + @@ -731,8 +814,7 @@ - + @@ -742,6 +824,7 @@ + @@ -786,7 +869,7 @@ - + diff --git a/nbproject/private/configurations.xml b/nbproject/private/configurations.xml index 512c97f..4cec2e3 100644 --- a/nbproject/private/configurations.xml +++ b/nbproject/private/configurations.xml @@ -4,7 +4,7 @@ 0 - :=MPLABComm-USB-Microchip:=<vid>04D8:=<pid>9012:=<rev>0100:=<man>Microchip Technology Incorporated:=<prod>MPLAB PICkit 4:=<sn>BUR222271433:=<drv>x:=<xpt>b:=end + :=MPLABComm-USB-Microchip:=<vid>04D8:=<pid>9036:=<rev>0100:=<man>Microchip Technology Incorporated:=<prod>MPLAB PICkit 5:=<sn>020026702RYN018328:=<drv>x:=<xpt>b:=end /opt/microchip/xc16/v2.10/bin place holder 1 -- cgit v1.2.3