Some people are past help!!!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
You are not being stupid. You are missing the point that OP does not know C. He is copying other peoples work and asking us to port the code for his compiler. I think Ian is just too nice (nothing wrong with that, but it does not help anyone in the long run).
Agreed with you.
I've done quite an amount of porting from different compilers and it requires me to read the compiler manual - a lot of it.
And with small programs like that it is better to write the program from scratch. Just learn how the original code works.
He obviously doesn't want to learn C.... The changing of code from compiler to compiler is relatively simple... So it only took a minute..Why would anyone want to learn C?? thats a ridiculous notion, there are more than enough people online that can program C in any compiler!! all you have to do is ask them! Ok sometimes you have to ask the NICELY, but on the whole you just got to ask. None of this learning stuff you talk about, actually the bit I dont understand was he had it working on two compilers! So he isnt learning he is switching compilers until he finds the magic one that codes for you (dont tell him which one it is )
Why would anyone want to learn C?? thats a ridiculous notion, there are more than enough people online that can program C in any compiler!! all you have to do is ask them! Ok sometimes you have to ask the NICELY, but on the whole you just got to ask. None of this learning stuff you talk about, actually the bit I dont understand was he had it working on two compilers! So he isnt learning he is switching compilers until he finds the magic one that codes for you (dont tell him which one it is )
One word: India.I might just be developing cynicism! Its funny how nearly 3 years online does that to you lol
Lets not mock!!! If he truly understands C there won't be a problem.. However if he doesn't ( as I suspect ) the code I have modified for him will serve no purpose... If it doesn't work as expected, he won't be able to fix it... As it stands I don't know how the code will behave... I can't test it... I already think that the union I created hasn't got bit access.. This may need to be in a different part of the memory!!
thank u sir , its work on Real Pic Simulator The display and led's fine..This compiles But I have no idea if it works... The display and led's simulate fine..
C:/* * PIC16F873A Pin Configuration * * O = Output * I = Input * * ________ * MCLR | 1 28 | RB7 --> (O) == DIGIT1 CC * ADC1(I) --> AN0 | 2 27 | RB6 --> (O) == G CA * ADC2(I) --> AN1 | 3 26 | RB5 --> (O) == F CA * DIGIT2 CC == (O) <-- RA2 | 4 25 | RB4 --> (O) == E CA * ADC3(I) --> AN3 | 5 24 | RB3 --> (O) == D CA * Delay_SW (I) --> RA4 | 6 23 | RB2 --> (O) == C CA * DIGIT3 CC == (O) <-- RA5 | 7 22 | RB1 --> (O) == B CA * Vss | 8 21 | RB0 --> (O) == A CA * OSC1 | 9 20 | Vdd * OSC2 |10 19 | Vss * RL4 == (O) <-- RC0 |11 18 | RC7 --> (O) == LED_HILO * RL3 == (O) <-- RC1 |12 17 | RC6 --> (O) == LED_NORMAL * RL2 == (O) <-- RC2 |13 16 | RC5 --> (0) == LED_DELAY * RL1 == (O) <-- RC3 |14 15 | RC4 --> (0) == * --------- * Program description and features: * - Voltage Stabilizer * - Auto calibration save upon first manual calibration * - Saved in internal EEPROM * - Display on 3-digit 7 SEGMENT display */ #include<XC.h> #define _XTAL_FREQ 20000000 #pragma config BOREN = OFF, CPD = OFF, DEBUG = OFF, WRT = OFF, FOSC = HS, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = OFF #define Relay1 RC3 #define Relay2 RC2 #define Relay3 RC1 #define Relay4 RC0 #define LED_DELAY RC5 #define LED_NORMAL RC6 #define LED_HILO RC7 #define C2 RA2 #define C3 RA5 #define C1 RB7 #define Delay_SWITCH RA4 const unsigned char ADC_0 = 0x00; //Vin_CHANNEL const unsigned char ADC_1 = 0x08; //CalibratePot_CHANNEL const unsigned char ADC_3 = 0x18; //CalibrateSw_CHANNEL volatile unsigned char IntCount; typedef union { struct{ unsigned flgStart :1; unsigned normal :1; unsigned flgAC :1; unsigned change :1; }; struct{ unsigned flg2ms :1; }; }flagRegbits; volatile flagRegbits flagReg ; #define PRESSED 0 #define UNPRESSED 1 const unsigned char EE_ADDRESS = 0x06; // EEPROM address for saving calibration const unsigned char CALIBRATED = 123; float Kc; // "Constant" for calibration unsigned int Kc_INT; // To save to EEPROM, = Kc * 10000 unsigned int Kc_INT_HIGH; // High byte for Kc_INT unsigned int Kc_INT_LOW; // Low byte for Kc_INT float Voltage_FLOAT; unsigned int Voltage_INT; unsigned int oldVoltage; unsigned char SEGMENT; const unsigned char COMMON_CATHODE = 0; const unsigned char COMMON_ANODE = 1; const unsigned char COMMON = COMMON_CATHODE; float calibrator; #define INTAP_LOW 0 // 165V #define INTAP_MED 1 // 190V #define INTAP_HI 2 // 240V #define OUTTAP_LOW 0 // 215V #define OUTTAP_HI 1 // 240V unsigned char INPUT_TAPPING; unsigned char OUTPUT_TAPPING; unsigned char old_INPUT_TAPPING; unsigned char old_OUTPUT_TAPPING; unsigned char oldInput; unsigned int V_DIFFERENCE; #define STARTED 1 #define FIRST_TIME 0 #define HYSTERESIS 3 const unsigned char AnodeDriveSegment[10] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152}; /* These are the values that need to be sent to the seven segment [A..G] for * a common anode display. These values have been obtaned using the * Mikroelektronika seven segment editor provided as part of the * mikroC/mikroBASIC/mikroPASCAL compiler. * The corresponding values for the common cathode display are: * CathodeDriveSegment = 255 - AnodeDriveSegment; */ unsigned char DriveSegment[10]; void UpdateDisplay(void){ unsigned int SendVal; unsigned char Digit; if (flagReg.flg2ms){ SEGMENT++; if (SEGMENT > 3) SEGMENT = 1; C1 = 0; C2 = 0; C3 = 0; switch (SEGMENT){ case 1: Digit = (unsigned char) (Voltage_INT / 100); break; case 2: Digit = (unsigned char) ((Voltage_INT / 10) % 10); break; case 3: Digit = (unsigned char) (Voltage_INT % 10); break; } SendVal = DriveSegment[Digit] & 0x7F; // Make bit 7 zero PORTB = SendVal; switch (SEGMENT){ case 1: C1 = 1; break; case 2: C2 = 1; break; case 3: C3 = 1; break; } flagReg.flg2ms = 0; // Clear flag } } void ADC_Init(void){ ADCON1 = 0x84; // RA0, RA1 and RA3 analog, rest digital } int ADC_Get_Sample(unsigned char CH){ int res = 0; ADCON0 = 0x41; ADCON0 |= CH; ADON = 1; __delay_ms(1); GO = 1; while(GO) res = (int)ADRESH <<8; res+= ADRESL; return res; } void InitIO(void){ PORTB = 0; TRISB = 0; PORTC = 0; TRISC = 0; PORTA = 0; TRISA = 0x1B; // Inputs for ADC CMCON = 7; // Disable comparator } void InitTimer2(void){ T2CON = 3; /* Prescale 1:16 * Timer off for now */ PR2 = 124; /* With prescale 1:16 and at 4MHz clock, * this gives 2ms period. */ TMR2IF = 0; TMR2IE = 1; // Turn on TMR2 interrupt GIE = 1; // Turn on global interrupts PEIE = 1; // Turn on peripheral interrupts TMR2ON = 1; // Start TMR2 } void InitDisplay(void){ unsigned char i; if (COMMON == COMMON_ANODE){ for (i = 0; i < 10; i++){ DriveSegment[i] = AnodeDriveSegment[i]; } } else{ for (i = 0; i < 10; i++){ DriveSegment[i] = 255 - AnodeDriveSegment[i]; } } } void interrupt ISR(void){ if (TMR2IF == 1){ // Configure for 2ms interrupt flagReg.flg2ms = 1; IntCount++; if (IntCount > 200){ flagReg.flgAC = 1; IntCount = 0; } TMR2IF = 0; } } void DoTheCalibrationAndSave(void){ unsigned char i; unsigned int CPot; while (ADC_Get_Sample(ADC_3) < 512){ // While Calibrate switch is PRESSED //Kc = (float)((ADC_Get_Sample(ADC_1) * 2.0) / 1023); // Get the calibration "scaling factor" between 0.00 and 2.00 CPot = ADC_Get_Sample(ADC_1)/2 + 128; Kc = (float)(CPot * 2.0)/1023.0; Kc = (float) ((float)Kc * 10000.0); Kc_INT = (unsigned int) Kc; Kc = (float) ((float)Kc_INT / 10000.0); Voltage_FLOAT = (float)((float)ADC_Get_Sample(ADC_0) * 5.0 * 101.0 * Kc) / (1023.0 * 1.3836); UpdateDisplay(); //Voltage_FLOAT = Voltage_FLOAT + 1.5; /* Use 100k and 1k resistors for voltage division * Divide by 1.3836 to convert from DC voltage to AC */ Voltage_INT = (unsigned int) Voltage_FLOAT; } // Button has been released - so now, it's time to save the value of Kc //Kc = (float) ((float)Kc * 10000.0); //Kc_INT = (unsigned int) Kc; Kc_INT_HIGH = (Kc_INT >> 8); Kc_INT_LOW = (Kc_INT & 0xFF); //Kc = (float) ((float)Kc_INT / 10000.0); eeprom_write(EE_ADDRESS, CALIBRATED); // Write value to indicate that calibration has been completed eeprom_write(EE_ADDRESS + 1, (unsigned char) Kc_INT_HIGH); eeprom_write(EE_ADDRESS + 2, (unsigned char) Kc_INT_LOW); /* Data has been saved to EEPROM as: * At address EE_ADDRESS, value CALIBRATED has been saved to indicate that calibration HAS occurred * Kc_INT = (unsigned int) (Kc * 10000); * Kc_INT_HIGH = upper byte of Kc_INT; Kc_INT_LOW = lower byte of Kc_INT; * At address EE_ADDRESS + 1, value of Kc_INT_HIGH is saved. * At address EE_ADDRESS + 2, value of Kc_INT_LOW is saved. */ } void Calibrate(void){ unsigned char EERead; EERead = eeprom_read(EE_ADDRESS); if (EERead == CALIBRATED){ // If Calibration has been done if (ADC_Get_Sample(ADC_3) < 512){ // If Calibrate switch is PRESSED DoTheCalibrationAndSave(); } else{ // Calibration has been done and Calibrate switch is UNPRESSED, meaning no new calibration will be done // So just get existing saved value of Kc from EEPROM and proceed Kc_INT_HIGH = eeprom_read(EE_ADDRESS + 1); Kc_INT_LOW = eeprom_read(EE_ADDRESS + 2); Kc_INT = (unsigned int) ((Kc_INT_HIGH << 8) | (Kc_INT_LOW)); // Put Kc_INT_HIGH and Kc_INT_LOW into a 16-bit variable Kc = (float) ((float)Kc_INT / 10000.0); // Get the float value of Kc Voltage_FLOAT = (float)((float)ADC_Get_Sample(ADC_0) * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0); //Voltage_FLOAT = Voltage_FLOAT + 1.5; Voltage_INT = (unsigned int) Voltage_FLOAT; } } else{ while (ADC_Get_Sample(ADC_3) > 512); // While Calibrate switch is UNPRESSED DoTheCalibrationAndSave(); } } void Delay(void){ /* If Delay Switch is pressed (Delay_SWITCH == 0), long Delay * Otherwise, short Delay */ unsigned char timeKeeper; LED_DELAY = 1; if (Delay_SWITCH == UNPRESSED){ for (timeKeeper = 0; timeKeeper < 10; timeKeeper ++){ __delay_ms(720); // 3 minutes Delay } } else{ __delay_ms(2000); // 2 seconds delay } LED_DELAY = 0; } void StabilizeVoltage(void){ unsigned int ADS; unsigned char V_LOW_CUT; if (flagReg.flgAC){ ADS = ADC_Get_Sample(ADC_0); Voltage_FLOAT = (float)((float)ADS * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0); //Voltage_FLOAT = Voltage_FLOAT + 1.5; Voltage_INT = (unsigned int) Voltage_FLOAT; if (flagReg.normal == 1){ V_LOW_CUT = 125; } else{ V_LOW_CUT = 135; } if (Voltage_INT < V_LOW_CUT){ /* Low Cut Check */ flagReg.normal = 0; LED_HILO = 1; LED_NORMAL = 0; INPUT_TAPPING = INTAP_LOW; OUTPUT_TAPPING = OUTTAP_HI; oldVoltage = V_LOW_CUT; flagReg.flgStart = FIRST_TIME; } else if (Voltage_INT > 270){ /* High Cut Check */ LED_HILO = 1; LED_NORMAL = 0; flagReg.normal = 0; INPUT_TAPPING = INTAP_HI; OUTPUT_TAPPING = OUTTAP_LOW; oldVoltage = 270; } else{ /* Normal --> Stabilize */ flagReg.normal = 1; LED_HILO = 0; LED_NORMAL = 1; if (Voltage_INT > oldVoltage){ V_DIFFERENCE = Voltage_INT - oldVoltage; } else{ V_DIFFERENCE = oldVoltage - Voltage_INT; } if (V_DIFFERENCE >= HYSTERESIS){ if (Voltage_INT < 157){ INPUT_TAPPING = INTAP_LOW; OUTPUT_TAPPING = OUTTAP_HI; oldVoltage = 157; } else if (Voltage_INT < 175){ INPUT_TAPPING = INTAP_LOW; OUTPUT_TAPPING = OUTTAP_LOW; oldVoltage = 175; } else if (Voltage_INT < 183){ INPUT_TAPPING = INTAP_MED; OUTPUT_TAPPING = OUTTAP_HI; oldVoltage = 183; } else if (Voltage_INT < 202){ INPUT_TAPPING = INTAP_MED; OUTPUT_TAPPING = OUTTAP_LOW; oldVoltage = 202; } else if (Voltage_INT < 235){ INPUT_TAPPING = INTAP_HI; OUTPUT_TAPPING = OUTTAP_HI; oldVoltage = 235; } else{ INPUT_TAPPING = INTAP_HI; OUTPUT_TAPPING = OUTTAP_LOW; oldVoltage = 215; } } } if (INPUT_TAPPING == INTAP_LOW){ Relay1 = 0; Relay2 = 0; } else if (INPUT_TAPPING == INTAP_MED){ Relay1 = 1; Relay2 = 0; } else{ Relay1 = 1; Relay2 = 1; } if (OUTPUT_TAPPING == OUTTAP_LOW){ Relay3 = 1; } else{ Relay3 = 0; } if (flagReg.normal == 1){ Relay4 = 1; // Main relay on; so output on } else{ Relay4 = 0; // Main relay off; so output off } flagReg.flgAC = 0; } } void main() { InitIO(); ADC_Init(); InitTimer2(); InitDisplay(); Calibrate(); Delay(); while(1){ UpdateDisplay(); StabilizeVoltage(); } }
void Delay(void){
/* If Delay Switch is pressed (Delay_SWITCH == 0), long Delay
* Otherwise, short Delay
*/
unsigned char timeKeeper;
LED_DELAY = 1;
if (Delay_SWITCH == UNPRESSED){
for (timeKeeper = 0; timeKeeper < 10; timeKeeper ++){
__delay_ms(720); // 3 minutes Delay
}
}
else{
__delay_ms(2000); // 2 seconds delay
}
LED_DELAY = 0;
}
/*****************************************************************************
Delay Defines
****************************************************************************/
#define STARTUP_SEC 15 // Change to 180 for read card
#define STARTUP_SECs 15
#define TIMER1_FREQUENCY (4000000 / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4
/*****************************************************************************
Timer1 Interrupt, executed every 10 ms
****************************************************************************/
#INT_TIMER1
void TIMER1_isr(void)
{
// Increment time_elasped to keep track of ms
time_elasped++;
// Increment seconds variable if 1000 ms have passed
if (time_elasped == 100) //high voltage cut led blink freqency @ 1000 ms
{
seconds++;
BlinkLED1_flag=~BlinkLED1_flag;
time_elasped = 0;
}
// If Quick Start switch is set between start up time, set QuickStart_flag
if ((input(Delay_SWITCH)==0) && (seconds<STARTUP_SEC) )
{
QuickStart_flag = 1;
}
// On start up, blink LED1 for 3 mins
//led1 blink mains normal ,led1 blink & Reads ADC
// both are work in same time. not one by one.
//if led1 blink duration complet then it no blink agen.
//Has three mins passed?
if (seconds > STARTUP_SEC)
{
BlinkMains_flag=1;
//output_high(RL4);
}
//if(seconds=!TEMP_seconds)
if(ErrorConditionExists_flag == 1)
{
if (TEMP_seconds > STARTUP_SECs)
{
BlinkMains_flag=1;
}}
//Reset Timer 1
set_timer1(60545);
clear_interrupt(INT_TIMER1);
}
/*****************************************************************************
Function BlinkLED1
Blink Low LED
****************************************************************************/
void BlinkLED1(void)
{
if (BlinkLED1_flag==0)
{
output_high( LED_DELAY);
}
else
{
output_low( LED_DELAY);
}
}