/* * main3.txt 27/06/2024 11:26:23 * * MAIN Generated Driver File * * @file main.c * * @defgroup main MAIN * * @brief This is the generated driver implementation file for the MAIN driver. * * @version MAIN Driver Version 1.0.0 */ /* © [2023] Microchip Technology Inc. and its subsidiaries. Subject to your compliance with these terms, you may use Microchip software and any derivatives exclusively with Microchip products. You are responsible for complying with 3rd party license terms applicable to your use of 3rd party software (including open source software) that may accompany Microchip software. SOFTWARE IS ?AS IS.? NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. */ #include "mcc_generated_files/system/system.h" #include /* Main application */ // Set the corresponding bit in var #define setbit(var, bit) ((var) |= (1 << (bit))) // Clear the corresponding bit in var #define clrbit(var, bit) ((var) &= ~(1 << (bit))) // Check to see if bit number ?bit?, in the integer ?var? is set #define testbit(var, bit) ((var) & (1 <<(bit))) int down_arrow=0; //RB0 input int probe_temperature=0; //RB2 analog input long pan_temperature=0; //RB3 analog input int motor_current=0; //RB2 analog input int buzzer=0; //RB4 output int spin_reset=0;//RB5 output int up_arrow=0; //RA5 input int time_temp=0; //RA6 input int pwm_motor_out=0; //RA7 pwm out int timer1=0; int timer2=0; char digit[4]; //RC0-RC3 each led display digit int bcd[4]; //RA0-RA3 binary coded output to CD4511 char colon=0; //RA4 colon on the LED display int i,patj; int tick_count=0; int bcdResult = 0; int binaryInput = 0; int shift = 0; char units=0; int time_up=0; int pat1; int pat2; int pat3; int pat4; int pat5; int seconds=0; int minutes=0; int hours=0; int tempSetpoint=150; int timeSetpoint=0; int buttonSelect=0; int oneTime=0; int debounce; int PanTempSetpoint = 150; int ProbeTempSetpoint=150; int TimeSetpoint = 0; int UpArrow=0; int DownArrow=0; int oneTime2=0; int oneTime1=0; int SetpointSelect = 0; int ThreeSecondTimer = 0; int StartThreeSecondTimer=0; int SetFlag=0; int StartCook = 0; int newOne = 0; int CookTime = 0; int RawPanTemperature = 0; int FiltPanTemperature =0; int RawProbeTemperature = 0; int FiltProbeTemperature =0; int myindex = 0; char sPanTempSetpoint[8]; char sProbeTempSetpoint[8]; // NTC thermistor resistance table // Temperature (degrees F) Resistance (kOhms) int ntc_table[][2] = { {59, 14}, {122, 61}, {167, 134}, {212, 252}, {230,309}, {257, 399}, {266,430}, {302, 549}, {320,604}, {347, 679}, {356,700}, {392, 777}, {401,822}, {437,848}, {446,859}, {482, 897}, }; int pat5; int pat6; int pat7; int pat8; //declaration to allow individual access to bits within a variable union both { unsigned char byte; struct { unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1,b9:1,b10:1; } bitv; } var; #include void timeBinary2bcd(int minutes, int seconds) { IO_RA4_SetHigh();//turn on Colon bcdResult = 0; shift = 0; while (seconds > 0){ bcdResult |= (seconds % 10) << (shift++ << 2); seconds /= 10; } bcd[0]=(bcdResult & 0x000F); //units bcd[1]=(bcdResult & 0x00F0) >> 4; //tens bcdResult = 0; shift = 0; while (minutes > 0) { bcdResult |= (minutes % 10) << (shift++ << 2); minutes /= 10; } bcd[2]=(bcdResult & 0x000F); //hundreds bcd[3]=(bcdResult & 0x00F0) >> 4; //thousands IO_RA4_SetHigh(); } //Input a binary number, and it will convert into 4 digits of BCD for use with 7 segment display driver. void binary2bcd( int binaryInput) { IO_RA4_SetLow(); bcdResult = 0; shift = 0; while (binaryInput > 0) { bcdResult |= (binaryInput % 10) << (shift++ << 2); binaryInput /= 10; } bcd[0]=bcdResult & 0x000F; //units bcd[1]=(bcdResult & 0x00F0) >> 4; //tens bcd[2]=(bcdResult & 0x0F00) >> 8; //hundreds bcd[3]=(bcdResult & 0xF000) >> 12; //thousands } //Used to write BCD to the CD4511 and ultimately to the TDGC 1060M void write2display() { IO_RA4_SetLow(); //Display digits Port Pins //Port C3 Units //Port C2 Tens //Port C1 Hundreds //Port C0 thousands // Bin to BDC function output //BCD[0] Units //BCD[1] Tens //BCD[2] Hundreds //BCD[3] Thousands //set all display digit select bits low IO_RC0_SetHigh(); IO_RC1_SetHigh(); IO_RC2_SetHigh(); IO_RC3_SetHigh(); //set all CD4511 inputs low IO_RC0_SetHigh(); IO_RA0_SetLow(); IO_RA1_SetLow(); IO_RA2_SetLow(); IO_RA3_SetLow(); //select thousands on display Associated with BCD[3] IO_RC0_SetLow(); if(testbit(bcd[3],0)!=0)PORTAbits.RA3=1; if(testbit(bcd[3],1)!=0)PORTAbits.RA2=1; if(testbit(bcd[3],2)!=0)PORTAbits.RA1=1; if(testbit(bcd[3],3)!=0)PORTAbits.RA0=1; __delay_ms(2); IO_RC0_SetHigh(); IO_RA0_SetLow(); IO_RA1_SetLow(); IO_RA2_SetLow(); IO_RA3_SetLow(); //select hundreds on display Associated with BCD[2] IO_RC1_SetLow(); if(testbit(bcd[2],0)!=0)IO_RA3_SetHigh(); if(testbit(bcd[2],1)!=0)IO_RA2_SetHigh(); if(testbit(bcd[2],2)!=0)IO_RA1_SetHigh(); if(testbit(bcd[2],3)!=0)IO_RA0_SetHigh(); __delay_ms(2); IO_RC1_SetHigh(); IO_RA0_SetLow(); IO_RA1_SetLow(); IO_RA2_SetLow(); IO_RA3_SetLow(); //select Tens on display Associated with BCD[1] IO_RC2_SetLow(); if(testbit(bcd[1],0)!=0)IO_RA3_SetHigh(); if(testbit(bcd[1],1)!=0)IO_RA2_SetHigh(); if(testbit(bcd[1],2)!=0)IO_RA1_SetHigh(); if(testbit(bcd[1],3)!=0)IO_RA0_SetHigh(); __delay_ms(2); IO_RC2_SetHigh(); IO_RA0_SetLow(); IO_RA1_SetLow(); IO_RA2_SetLow(); IO_RA3_SetLow(); //select units on display Associated with BCD[0] IO_RC3_SetLow(); if(testbit(bcd[0],0)!=0)IO_RA3_SetHigh(); if(testbit(bcd[0],1)!=0)IO_RA2_SetHigh(); if(testbit(bcd[0],2)!=0)IO_RA1_SetHigh(); if(testbit(bcd[0],3)!=0)IO_RA0_SetHigh(); __delay_ms(2); IO_RC3_SetHigh(); } void SelectButton() { if(IO_RB0_GetValue()==0 && oneTime) { debounce++; if (debounce>=1000)debounce=0; if(IO_RB0_GetValue()==0) { buttonSelect++; if(buttonSelect > 2)buttonSelect=0; oneTime=0; } } switch(buttonSelect) { case 0x00: IO_RA4_SetHigh();//set colon for clock if(UpArrow || DownArrow) { SetFlag=1; StartThreeSecondTimer=1; SetpointSelect=0; } if(SetFlag & (ThreeSecondTimer==0)) { if(UpArrow)TimeSetpoint++; UpArrow=0; if(DownArrow)TimeSetpoint--; DownArrow=0; if(TimeSetpoint>=3600)TimeSetpoint=3600; if(TimeSetpoint<=0)TimeSetpoint=0; seconds=0; DownArrow=0; CookTime=TimeSetpoint*60; binaryInput = TimeSetpoint; timeBinary2bcd(TimeSetpoint,seconds); write2display(); pat1=1; } else { StartCook=1; minutes=CookTime/60; seconds=CookTime%60; binaryInput = CookTime; timeBinary2bcd(minutes,seconds); StartThreeSecondTimer=0; SetFlag=0; ThreeSecondTimer=0; SetpointSelect =0; } pat1=1; break; case 0x01: IO_RA4_SetLow();//remove colon for clock if(UpArrow || DownArrow) { SetFlag=1; StartThreeSecondTimer=1; SetpointSelect=0; } if(SetFlag & (ThreeSecondTimer==0)) { if(UpArrow)PanTempSetpoint++; UpArrow=0; if(DownArrow)PanTempSetpoint--; if(PanTempSetpoint>=450)PanTempSetpoint=450; DownArrow=0; if(PanTempSetpoint>=450)PanTempSetpoint=450; binaryInput = PanTempSetpoint; binary2bcd(binaryInput); write2display(); } else { binaryInput = pan_temperature; binary2bcd(binaryInput); StartThreeSecondTimer=0; SetFlag=0; ThreeSecondTimer=0; SetpointSelect =0; } pat1=2; break; case 0x02: IO_RA4_SetLow();//remove colon for clock if(UpArrow || DownArrow) { SetFlag=1; StartThreeSecondTimer=1; SetpointSelect=0; } if(SetFlag & (ThreeSecondTimer==0)) { if(UpArrow)ProbeTempSetpoint++; UpArrow=0; if(DownArrow)ProbeTempSetpoint--; if(ProbeTempSetpoint>=450)ProbeTempSetpoint=450; DownArrow=0; if(ProbeTempSetpoint>=450)ProbeTempSetpoint=450; binaryInput = ProbeTempSetpoint; binary2bcd(binaryInput); write2display(); } else { binaryInput = probe_temperature; binary2bcd(binaryInput); StartThreeSecondTimer=0; SetFlag=0; ThreeSecondTimer=0; SetpointSelect =0; } pat1=3; break; } if(IO_RB0_GetValue()==1)oneTime=1; } void IncDecButton() { if(IO_RA6_GetValue()==0 && oneTime1)//up arrow { debounce++; if (debounce>=1000)debounce=0; if(IO_RA6_GetValue()==0) { UpArrow=1; oneTime1=0; } } if(IO_RA6_GetValue()==1) { oneTime1=1; UpArrow=0; } //------------------------------------------------------------------ if(IO_RA5_GetValue()==0 && oneTime2)//Down arrow { debounce++; if (debounce>=1000)debounce=0; if(IO_RA5_GetValue()==0) { DownArrow=1; oneTime2=0; } } if(IO_RA5_GetValue()==1) { oneTime2=1; DownArrow=0; } } long pan_ntc_temperature(int resistance) { // Find the two table entries that bracket the resistance int index = 0; myindex=0; while(ntc_table[myindex][1] < resistance) { myindex++; // if(myindex >= 13)myindex=13; pat1=1; } // Calculate the temperature using linear interpolation if(resistance <= ntc_table[14][1]){ pan_temperature = ntc_table[myindex][0] + (resistance - ntc_table[myindex][1]) * (ntc_table[myindex + 1][0] - ntc_table[myindex][0]) / (ntc_table[myindex + 1][1] - ntc_table[myindex][1]); pat1=1; } } long probe_ntc_temperature(int resistance) { // Find the two table entries that bracket the resistance // int index = 0; myindex=0; while(ntc_table[myindex][1] < resistance) { myindex++; // if(myindex >= 13)myindex=13; pat1=1; } // Calculate the temperature using linear interpolation if(resistance <= ntc_table[14][1]){ probe_temperature = ntc_table[myindex][0] + (resistance - ntc_table[myindex][1]) * (ntc_table[myindex + 1][0] - ntc_table[myindex][0]) / (ntc_table[myindex + 1][1] - ntc_table[myindex][1]); pat1=1; } } int main(void) { SYSTEM_Initialize(); // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts // Use the following macros to: //Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); //Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); Timer0_Start(); TMR1_Start(); while(1) { //pat3=266; //ntc_temperature(pat3); // pan_ntc_temperature(FiltPanTemperature); // probe_ntc_temperature(FiltProbeTemperature); IO_RA4_SetHigh();//colon write2display(); LATB4=0x01;//buzzer LATB5=0x01;//reset led wheel // SelectButton(); // IncDecButton(); time_up++; if(time_up >= 2) { time_up=0; patj++; SelectButton(); IncDecButton(); // pan_temperature = ADCC_GetSingleConversion(channel_ANB3); // probe_temperature = ADCC_GetSingleConversion(channel_ANB2); RawPanTemperature = ADCC_GetSingleConversion(channel_ANB3); FiltPanTemperature = RawPanTemperature/8 +FiltPanTemperature*7/8; pan_ntc_temperature(FiltPanTemperature); //pan_ntc_temperature(RawPanTemperature); // pan_temperature = pan_ntc_temperature(FiltPanTemperature); RawProbeTemperature = ADCC_GetSingleConversion(channel_ANB2); FiltProbeTemperature = RawProbeTemperature/8 +FiltProbeTemperature*7/8; probe_ntc_temperature(FiltProbeTemperature); // probe_temperature = probe_ntc_temperature(FiltPanTemperature); motor_current = ADCC_GetSingleConversion(channel_ANB1); PWM7_LoadDutyValue(10); // To App as String sprintf(sPanTempSetpoint,"%d\0",PanTempSetpoint); sprintf(sProbeTempSetpoint,"%d\0",ProbeTempSetpoint); printf(sPanTempSetpoint); printf(sProbeTempSetpoint); pat1=1; } } }