# need help in pic18f4550 programming

#### rathee

Joined Aug 14, 2012
1
hi, i have the following code, the dusk or dawn sensor will sense and give the output to microcontroller and the MC should compare dusk or dawn and switch the relay accordingly... but the program which i wrote is wrong i guess and im very bad in programming could anyone pls help me in this case...

Rich (BB code):
#include "p18f4550.h"
#include "string.h"
#include "stdio.h"
#include "delays.h"
#include "p18cxxx.h"

#define macro
//#define PORTAbits.RB4

void main()
{
int batt=0;
int relay=0;
int i;
char time[10];

/* Compare two strings without regard to case */
if (strcmp("night","day")==0)
printf("day\n");
else
printf("night\n");

b:
if(strcmp(time,"night")==0)
{
a:
do
{
relay=1;
//printf("charging\n");
}
while(batt!=1);
//printf("battery full\n");
relay=0;

i=300;
while(i>0)
{
//delay_ms(1000);
Delay100TCYx(1000);
//printf("battery value\n");
i--;
}
goto a;
}
else if(strcmp(time,"day")==0)
{
do
{
}
while(batt!=0);
//printf("low battery\n");
//while(RA4==1)
//printf("battery value\n");
//goto b;
}

}

#### upand_at_them

Joined May 15, 2010
766
What does "it is wrong" mean? You need to be clear if you want people to help you.

#### jwilk13

Joined Jun 15, 2011
228
Rich (BB code):
#include "p18f4550.h"
#include "string.h"
#include "stdio.h"
#include "delays.h"
#include "p18cxxx.h"

#define macro
//#define PORTAbits.RB4

void main()
{
int batt=0;
int relay=0;
int i;
char time[10];

/* Compare two strings without regard to case */
if (strcmp("night","day")==0)
printf("day\n");
else
printf("night\n");

b:
if(strcmp(time,"night")==0)
{
a:
do
{
relay=1;
//printf("charging\n");
}
while(batt!=1);
//printf("battery full\n");
relay=0;

i=300;
while(i>0)
{
//delay_ms(1000);
Delay100TCYx(1000);
//printf("battery value\n");
i--;
}
goto a;
}

else if(strcmp(time,"day")==0)
{
do
{
}
while(batt!=0);
//printf("low battery\n");
//while(RA4==1)
//printf("battery value\n");
//goto b;
}
}
This might help everyone read it a little bit better. It's good to follow proper indentation practices as it helps to track your code a little bit better.

As for why it's not working, you'll have to give quite a bit more information. I notice you haven't set up any of your configuration registers, data direction registers, etc. Please provide a schematic and some more information and we may be able to help more.

You should really read the datasheet to figure out how everything needs to be set up. When I started with PICs, I remember just thinking I could write my C code and everything should work. In reality, however, the PIC is stupid and needs to be told EXACTLY what to do. Each pin has to know whether it's an input or an output, what type of I/O it is; it has to know how fast it's operating (oscillator configuration) and what it's operating with (internal oscillator, external oscillator). What compiler are you using, and are you using MPLAB?

Last edited: