need help in pic18f4550 programming

Discussion in 'Embedded Systems and Microcontrollers' started by rathee, Aug 14, 2012.

  1. rathee

    Thread Starter New Member

    Aug 14, 2012
    1
    0
    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...

    Code ( (Unknown Language)):
    1. #include "p18f4550.h"
    2. #include "string.h"
    3. #include "stdio.h"
    4. #include "delays.h"
    5. #include "p18cxxx.h"
    6.  
    7. #define macro
    8. //#define PORTAbits.RB4
    9.  
    10. void main()
    11. {
    12. int load=0;
    13. int batt=0;
    14. int relay=0;
    15. int i;
    16. char time[10];
    17.  
    18.    /* Compare two strings without regard to case */
    19.    if (strcmp("night","day")==0)
    20.       printf("day\n");
    21.    else
    22.      printf("night\n");
    23.  
    24. b:
    25. if(strcmp(time,"night")==0)
    26. {
    27. a:
    28. do
    29. {
    30. relay=1;
    31. load=1;
    32. //printf("charging\n");
    33. }
    34. while(batt!=1);
    35. //printf("battery full\n");
    36. relay=0;
    37.  
    38. i=300;
    39. while(i>0)
    40. {
    41. //delay_ms(1000);
    42. Delay100TCYx(1000);
    43. //printf("battery value\n");
    44. i--;
    45. }
    46. goto a;
    47. }
    48. else if(strcmp(time,"day")==0)
    49. {
    50. do
    51. {
    52. load=0;
    53. //printf("load off\n");
    54. }
    55. while(batt!=0);
    56. load=1;
    57. //printf("low battery\n");
    58. //while(RA4==1)
    59. //printf("battery value\n");
    60. //goto b;
    61. }
    62.  
    63. }
     
  2. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    What does "it is wrong" mean? You need to be clear if you want people to help you.
     
  3. jwilk13

    Member

    Jun 15, 2011
    228
    12
    Code ( (Unknown Language)):
    1. #include "p18f4550.h"
    2. #include "string.h"
    3. #include "stdio.h"
    4. #include "delays.h"
    5. #include "p18cxxx.h"
    6.  
    7. #define macro
    8. //#define PORTAbits.RB4
    9.  
    10. void main()
    11. {
    12.     int load=0;
    13.     int batt=0;
    14.     int relay=0;
    15.     int i;
    16.     char time[10];
    17.  
    18.    /* Compare two strings without regard to case */
    19.     if (strcmp("night","day")==0)
    20.         printf("day\n");
    21.     else
    22.         printf("night\n");
    23.  
    24.     b:
    25.     if(strcmp(time,"night")==0)
    26.     {
    27.         a:
    28.         do
    29.         {
    30.             relay=1;
    31.             load=1;
    32.             //printf("charging\n");
    33.         }
    34.         while(batt!=1);
    35.         //printf("battery full\n");
    36.         relay=0;
    37.  
    38.         i=300;
    39.         while(i>0)
    40.         {
    41.             //delay_ms(1000);
    42.             Delay100TCYx(1000);
    43.             //printf("battery value\n");
    44.             i--;
    45.         }
    46.         goto a;
    47.     }
    48.  
    49.     else if(strcmp(time,"day")==0)
    50.     {
    51.         do
    52.         {
    53.             load=0;
    54.             //printf("load off\n");
    55.         }
    56.         while(batt!=0);
    57.         load=1;
    58.         //printf("low battery\n");
    59.         //while(RA4==1)
    60.         //printf("battery value\n");
    61.         //goto b;
    62.     }
    63. }
    64.  
    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: Aug 18, 2012
Loading...