Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

programming help nedded

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
899
Helped
24
Reputation
48
Reaction score
26
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Activity points
8,252
unsigned int battery, main, over_load, temp, sw;
void main()

{
int i ;
long int charg, flash, alarm, main_ok;
DDRD = 0xFF;
DDRB = 0xFF;
DDRC= 0b0000010;
while(1)
{
battery = ADC_Read(1); // battery stasus check
main = ADC_Read(4); // main status check
over_load = ADC_Read(0); // o\L check
temp = ADC_Read(2); // temp check
sw = ADC_Read(3); // switch check.

if(sw < 512)
{
main_ok = 0;
charg = 0;
if((main <= 860) &&(main >= 737)) // main status check . main is ok.
{

main_ok++;
if(main_ok >= 10000) // main delay for surge protection.
{
PORTB2_bit = 1; //output triac on
PORTB5_bit = 1; // output indication on, non flashing
if(battery < 223) // check battery under 12V
{
charg++;
if(charg >= 100000)
{
PORTB0_bit = 1; // charging on by relay on.
flash++ ;
if(flash >= 10000)
{
PORTB5_bit = ~PORTB5_bit; // charging indication by toggleing main indicator.
flash = 0;
}
}
}
if(battery >= 275) // battery full charge.
{
PORTB0_bit = 0; // setting relay off.
PORTB5_bit = 1; // setting main indicator on, non flashing.
}
}
}
else
{
PORTB2_bit = 0; // setting output triac off.
PORTB5_bit = 0; // main indicator off.
if((battery < 195)&&(over_load > 150)&&(temp < 350))// check battery if lowr than 10.5V & overload & temperature
{
PORTD = 0x00; // main sinewave oscillator off.
PORTB3_bit = 1; // osc # 1 off.
PORTB4_bit = 0; // backup indicator off.
alarm++;
if(alarm > 10000)
{
PORTC5_bit = ~PORTC5_bit; // generating alarm
alarm = 0;
}


}
else // if all conditions are suitable then run the inverter.
{
PORTB3_bit = 0; // osc # 1 on.
PORTB5_bit = 0; // alarm off
PORTB4_bit = 1; // backup indicator on.
PORTB1_bit = 1; // inverter output switch(Triac) on.

// osc sinewave on
{
for(i=0;i<15;i++)
{
PORTD = (i);
Delay_us(32);
}

for(i=15;i>0;i--)
{
PORTD = (i);
Delay_us(32);
}
for(i=0;i<15;i++)
{
PORTD = (16*i);
Delay_us(32);
}

for(i=15;i>0;i--)
{
PORTD = (16*i);
Delay_us(32);
}
}

}

}

}
else
{
if(main > 737) // main is over 150V
{
PORTB5_bit = 1; // main availability indication on.
if(battery >= 275)
{
PORTB0_bit = 0; // charging off.
}
else // charging on...
{
PORTB0_bit = 1; // charging on.
flash++ ;
if(flash >= 10000)
{
PORTB5_bit = ~PORTB5_bit; // charging indication on by toggling main LED.
flash = 0;
}
}
}
}
}
}




///////////////////////////////////////////////////////////

Errors are shown in line 2.
please help me, I can't find any error... but still can't make it error free.
 
Last edited:

Let me give you some recomendations :

> Explain what compiler are you using ( some ones don´t accept multiple variables declarations at same statements )
> Use
Code:
 formatter provide at this forum to make your code more inteligible.

+++
 

@ btbass: errors are at line 2: 'main function error.' fortunately I've found the error. the main is declared twitch in the first line and 2nd line. I've corrected it.

@ andre_teprom: it is MikroC Pro.

thanks for help.

---------- Post added at 06:19 ---------- Previous post was at 06:13 ----------

error is shown in the colored line.

but not I'm fetching another problem. time is not maintained for each cycle of sine wave I've generated in the program. Can you help me?

simulated image:
 

You need to look at the structure of your program. At the moment you have a single function, 'main()', that does everything. Code written in this way quickly becomes unmanageable and hard to understand.

You should divide the program into functional blocks, with each function doing one specific task.
Then the main function should be a loop that calls the functions as required.

You should also consider using a timer interrupt to generate your sine wave output to achieve a constant time step.
 

ok, i'm trying.

---------- Post added at 11:20 ---------- Previous post was at 11:16 ----------

how can I call blocks? can you give me any example... I'm not so strong in programming, pls help
 

Well you need to write the program as you know what is going on and what you want it to do.
The idea is to divide and conquer, small simple functions that do one specific thing are easier to write and understand than one big complex function that tries to do everything. If you are using Mplab, use the SIM debugger and single step through the code to see what is going on.
If it was me, I would approach it something like this.

Code:
/*--- Function prototype ---*/

void switch_check(void);
void battery_status(void);

/*--- Program entry ---*/

void main(void)
  {

  /* Initialise ports here */

  while(1)  /* Main program loop */
    {
    switch_check();

    battery_status();  /* Similar functions that you write */
    }
  }	


/*--- Check switch function ---*/

void switch_check(void)
  {
  unsigned int sw;

  sw = ADC_Read(3); // switch check.

  if(sw < 512)
     {
     main_ok = 0;
     charg = 0;
     }
  else
    {
    main_ok = 1;
    charg = 1;
    }
  }

/*--- Get battery Status ---*/

void battery_status(void)
  {
  unsigned int battery;

  battery = ADC_Read(1);

  if(battery    /* Stuff you need to write */

  }
 
Hi,
all other functions are responding well but except ADC. I can't be able to use 5ADCs at a time. I don't know why. I'm using atmega8. Any idea?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top