I funktionen safety() ska jag stanna kvar så länge knappen är hög men den skiter fullständgit i nivån. Provade att bara läsa knappen (annat bortkommenterat) i while loopen under main och toggla EN4 beroende på BTN men den känner inte av det heller. Känner mig helt skelögd av att kolla på samma grejer hela tiden.
Gäller som vanligt mikroC och 16F18346
Kod: Markera allt
#include "SSD1306driver_lite.h" // include SSD1306 OLED display driver source code
#define BTN LATC.F5
#define OUT LATC.F4
#define EN4 LATC.F7
#define EN3 LATB.F4
#define EN2 LATC.F1
#define EN1 LATA.F2
#define test LATC.F4
#define SSD1306_SOFT_I2C
#define SSD1306_128_64
char PrintOut[5];
float Ptot, Temp, TempF, TempOut, Vbat, Adc1, Adc2, Adc3, Adc4;
// Software I2C connections
sbit Soft_I2C_Scl at RB7_bit;
sbit Soft_I2C_Sda at RB5_bit;
sbit Soft_I2C_Scl_Direction at TRISB7_bit;
sbit Soft_I2C_Sda_Direction at TRISB5_bit;
// End Software I2C connections
void NTC_Lookup(){
TempOut=20;
if(511<Temp)
TempOut=25;
if(559<Temp)
TempOut=30;
if(604<Temp)
TempOut=35;
if(646<Temp)
TempOut=40;
if(646<Temp)
TempOut=45;
if(686<Temp)
TempOut=50;
if(722<Temp)
TempOut=55;
if(755<Temp)
TempOut=60;
if(785<Temp)
TempOut=65;
if(812<Temp)
TempOut=70;
if(836<Temp)
TempOut=75;
if(858<Temp)
TempOut=80;
if(875<Temp)
TempOut=85;
// if(893<Temp)
// TempOut=90;
// if(908<Temp)
// TempOut=95;
/* if(922<Temp)
TempOut=100;
if(934<Temp)
TempOut=105;
if(944<Temp)
TempOut=110;
if(953<Temp)
TempOut=115;
if(961<Temp)
TempOut=120;
if(968<Temp)
TempOut=125;
*/
TempF=(TempOut*1.8)+32;
}
void Safety(){
if(Temp>908){ //95grader
SSD1306_ClearDisplay();
SSD1306_GotoXY(1, 1);
SSD1306_Print("SYSTEM OVERHEATED!!");
SSD1306_GotoXY(1, 16);
SSD1306_Print("Cooling, press Btn! ");
SSD1306_Display();
EN1=0;
EN2=0;
EN3=0;
EN4=0;
while((Temp>908)||(BTN==1)){
Temp=ADC_Read(4);
//wait to cool down
}
EN1=1;
EN2=1;
EN3=1;
EN4=1;
SSD1306_ClearDisplay();
}
Vbat=ADC_Read(5);
if((Vbat>647)||(Vbat<540)){//4,75V-5,8V
EN1=0;
EN2=0;
EN3=0;
EN4=0;
SSD1306_ClearDisplay();
Delay_ms(100);
if(Vbat>647){
SSD1306_TextSize(1);
SSD1306_GotoXY(1, 1);
SSD1306_Print("HIGH VOLTAGE, press Btn!");
SSD1306_Display();
while((Vbat>647)||(BTN==0)){
//Wait
Vbat=ADC_Read(5);
}
}
if(Vbat<540){
SSD1306_ClearDisplay();
SSD1306_TextSize(1);
SSD1306_GotoXY(1, 1);
SSD1306_Print("LOW VOLTAGE, press Btn!");
SSD1306_Display();
while((Vbat<540)||(BTN==1)){
//Wait
Vbat=ADC_Read(5);
}
}
EN1=1;
EN2=1;
EN3=1;
EN4=1;
SSD1306_ClearDisplay();
}
if((ADC1>3200)||(ADC1>3200)||(ADC1>3200)||(ADC1>3200)){
Delay_ms(1000);
SSD1306_ClearDisplay();
Adc1=ADC_Read(16);
Adc1=Adc1*0.006;
Adc2=ADC_Read(18);
Adc2=Adc2*0.0049;
Adc3=ADC_Read(14);
Adc3=Adc3*0.0039;
Adc4=ADC_Read(22);
Adc4=Adc4*0.0056;
if(Adc1>3200){
EN1=0;
SSD1306_TextSize(2);
SSD1306_GotoXY(1, 1);
SSD1306_Print("CH1 OVERLOAD, press Btn!");
while(BTN!=0){
//wait
}
}
if(Adc2>3200){
EN2=1;
SSD1306_TextSize(2);
SSD1306_GotoXY(1, 1);
SSD1306_Print("CH2 OVERLOAD, press Btn!");
while(BTN!=0){
//wait
}
}
if(Adc3>3200){
EN3=0;
SSD1306_TextSize(2);
SSD1306_GotoXY(1, 1);
SSD1306_Print("CH3 OVERLOAD, press Btn!");
while(BTN!=0){
//wait
}
}
if(Adc4>3200){
EN4=0;
SSD1306_TextSize(2);
SSD1306_GotoXY(1, 1);
SSD1306_Print("CH4 OVERLOAD, press Btn!");
while(BTN!=0){
//wait
}
}
EN1=1;
EN2=1;
EN3=1;
EN4=1;
SSD1306_ClearDisplay();
}
}
void Adc_Read_All(){
Temp=ADC_Read(4); //ANA4
NTC_Lookup();
Vbat=ADC_Read(5); //ANA5
Vbat=Vbat*0.0088;
Adc1=ADC_Read(16); //CH1
if(Adc1<20) //filter
Adc1=0;
Adc1=Adc1*0.0063;
Adc2=ADC_Read(18); //CH2
if(Adc2<20)
Adc2=0;
Adc2=Adc2*0.0052;
Adc3=ADC_Read(14); //CH3
if(Adc3<20)
Adc3=0;
Adc3=Adc3*0.0041;
Adc4=ADC_Read(22); //CH4
if(Adc4<20)
Adc4=0;
Adc4=Adc4*0.0059;
Ptot=Vbat*(Adc1+Adc2+Adc3+Adc4);
}
Battery_Bar(){
if(Ptot<=0.2){
SSD1306_FillRoundRect(94, 2, 7, 6, 1);
SSD1306_DrawRoundRect(90, 7, 15, 29, 2);
SSD1306_FillRect(92, 9, 11, 7);
SSD1306_FillRect(92, 18, 11, 7);
SSD1306_FillRect(92, 27, 11, 7);
SSD1306_Display();
}
if(Ptot>0.2){
SSD1306_Color = false;
SSD1306_FillRect(92, 9, 11, 7);
SSD1306_Display();
SSD1306_Color = true;
SSD1306_FillRect(92, 9, 11, 7);
}
}
void Display_Out(){
SSD1306_Color = 1;
SSD1306_TextSize(1);
Battery_Bar();
SSD1306_GotoXY(1, 1);
SSD1306_Print("SYSTEM");
SSD1306_GotoXY(1, 36);
SSD1306_Print("OUTPUT");
SSD1306_GotoXY(25, 11);
SSD1306_Print("V");
sprintf(PrintOut, "%.2f", Vbat);
SSD1306_GotoXY(1, 11);
SSD1306_Print(PrintOut);
SSD1306_GotoXY(57, 11);
SSD1306_Print("C");
sprintf(PrintOut, "%.0f", TempOut);
SSD1306_GotoXY(45, 11);
SSD1306_Print(PrintOut);
if(TempF>=100){
SSD1306_GotoXY(64, 20);
SSD1306_Print("F");
}
if(TempF<100){
SSD1306_GotoXY(64, 20);
SSD1306_Print(" ");
}
SSD1306_GotoXY(57, 20);
SSD1306_Print("F");
sprintf(PrintOut, "%.0f", TempF);
SSD1306_GotoXY(45, 20);
SSD1306_Print(PrintOut);
if(Ptot<0.1){
SSD1306_GotoXY(19, 20);
SSD1306_Print("0");
}
SSD1306_GotoXY(25, 20);
SSD1306_Print("W");
sprintf(PrintOut, "%.1f", Ptot);
SSD1306_GotoXY(1, 20);
SSD1306_Print(PrintOut);
SSD1306_GotoXY(1, 46);
SSD1306_Print("Ch1");
if(ADC1>0.1){
SSD1306_GotoXY(53, 46);
SSD1306_Print("<");
}
if(ADC1<0.1){
SSD1306_GotoXY(53, 46);
SSD1306_Print(" ");
}
SSD1306_GotoXY(48, 46);
SSD1306_Print("A");
sprintf(PrintOut, "%.3f", Adc1);
SSD1306_GotoXY(23, 46);
PrintOut[4]=0;
SSD1306_Print(PrintOut);
SSD1306_GotoXY(1, 56);
SSD1306_Print("Ch2");
if(ADC2>0.1){
SSD1306_GotoXY(53, 56);
SSD1306_Print("<");
}
if(ADC2<0.1){
SSD1306_GotoXY(53, 56);
SSD1306_Print(" ");
}
SSD1306_GotoXY(48, 56);
SSD1306_Print("A");
sprintf(PrintOut, "%.3f", Adc2);
SSD1306_GotoXY(23, 56);
PrintOut[4]=0;
SSD1306_Print(PrintOut);
SSD1306_GotoXY(76, 56);
SSD1306_Print("Ch3");
if(ADC3>0.1){
SSD1306_GotoXY(69, 56);
SSD1306_Print(">");
}
if(ADC3<0.1){
SSD1306_GotoXY(69, 56);
SSD1306_Print(" ");
}
SSD1306_GotoXY(123, 56);
SSD1306_Print("A");
sprintf(PrintOut, "%.3f", Adc3);
SSD1306_GotoXY(98, 56);
PrintOut[4]=0;
SSD1306_Print(PrintOut);
SSD1306_GotoXY(76, 46);
SSD1306_Print("Ch4");
if(ADC4>0.1){
SSD1306_GotoXY(69, 46);
SSD1306_Print(">");
}
if(ADC4<0.1){
SSD1306_GotoXY(69, 46);
SSD1306_Print(" ");
}
SSD1306_GotoXY(123, 46);
SSD1306_Print("A");
sprintf(PrintOut, "%.3f", Adc4);
SSD1306_GotoXY(98, 46);
PrintOut[4]=0;
SSD1306_Print(PrintOut);
SSD1306_Display();
}
Init_main(){
OSCCON1=0B00000001;
OSCFRQ=0b00000110; // set internal oscillator to 16MHz
OSCTUNE=0b00000000;
ANSELA=0b00110000; // configure all PORTS
ANSELB=0b01000000;
ANSELC=0b00000101;
TRISA=0b00110000;
TRISB=0b01000000;
TRISC=0b00100101;
// delay_ms(1000); // wait a second
Soft_I2C_Init(); // initialize I2C communication
SSD1306_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);
SSD1306_Display();
delay_ms(2000);
SSD1306_ClearDisplay(); // clear the buffer
// ADC_Init();
EN1=1;
EN2=1;
EN3=1;
EN4=1;
Ptot=0.1;
Battery_Bar();
}
// main function
void main(){
Init_main();
while(1){
ADC_Read_All();
Display_Out();
Safety();
}
}