Help needed for programming PIC16F877A...

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Jan 4, 2013.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    what is wrong in this code showing error in Map lab..??

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
    3.  
    4.  
    5. #define _XTAL_FREQ 4000000
    6.  
    7. void main (void)
    8.  
    9. {
    10. TRISB=0b00000000;
    11. PORTB=0;
    12. int=i;
    13.  
    14. for(i=1,i++,i<5)
    15.  {
    16.   PORTB = 0x1;
    17.   __delay_ms(1000);
    18. PORTB = 0x2;
    19.   __delay_ms(1000);
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  }
    28. }
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    What the heck is Map lab? You mean MPLAB?

    Look at the errors you are getting, it will point you in the right direction, most of the time. Like now, for instance, your for loop requires semi-colons... I see no semi-colons... This is also not the way a for loop works...

    Look at this.

    you should also add a super-loop(a while(1) ), to put your blink section in....
     
  3. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    Ritesh.... We've been here before.

    Nope!!!!

    Yep!!!
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    Ritesh,


    Please study a good C tutorial. Your error is a very basic mistake. You have asked these kinds of questions before. Any first week C programmer should have been able to find it.

    It also helps if you mention the error and where it is occurring. It is very obvious because you only have a few lines but had there been very difficult to find with no information and lots of lines of code.

    First learn to help yourself. When you can no longer help yourself then learn to help others to help you.
     
  5. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Now its working......


    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
    3.  
    4.  
    5. #define _XTAL_FREQ 4000000
    6.  
    7. void main (void)
    8.  
    9. {
    10. TRISB=0b00000000;
    11. PORTB=0;
    12. int i;
    13.  
    14. for(i=1;i<5;i++)
    15.  {
    16.   PORTB = 0x1;
    17.   asm("nop");
    18. PORTB = 0x2;
    19.   asm("nop");
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  }
    28. }
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    Amazing what can happen when you have the right syntax. :)
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Ritesh at this point you should know a thing or two about micro controller programming.
    First your main should always contain an endless loop something like this. (And I hope you agree that it should not be needed to mention this again)
    Code ( (Unknown Language)):
    1.  
    2. main()
    3. {
    4.      while (1)
    5.        {
    6.           // you code here
    7.        }
    8. }
    9.  
    So to simply blink LEDs your first program was not a bad try. But I doubt your second will show any visible blinking. Here is how I would have made your LED blink program
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
    4.  
    5.  
    6. #define _XTAL_FREQ 4000000
    7.  
    8. void main (void)
    9.  
    10. {
    11. TRISB=0b00000000;
    12. PORTB=0;
    13. while(1)
    14. {
    15.  
    16.   PORTB = 0x1;
    17.   __delay_ms(1000);
    18. PORTB = 0x2;
    19.   __delay_ms(1000);
    20.  }
    21. }
    22.  
     
  8. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    I guess this program is a nice way to test out the:

    Debugger -> Select Tool -> MPLAB SIM

    and

    View -> Watch -> Add SFR -> PortB

    and

    Debugger -> Animate

    and see what is happening... :)
     
    RRITESH KAKKAR likes this.
  9. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    nothing happen while adding this it was running only as before..
     
  10. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    This code is working do u think it will work in hardware?

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
    3.  
    4.  
    5. #define _XTAL_FREQ 4000000
    6.  
    7. void main (void)
    8.  
    9. {
    10. TRISB=0b10000001;
    11. PORTB=0b10000001;
    12. if(RB0=1)
    13.  
    14.  
    15.  {
    16.   PORTB = 0x1;
    17.   asm("nop");
    18. }
    19.  
    20. else{
    21. PORTB = 0x2;
    22.   asm("nop");
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  }
    31. }
    32.  
     
  11. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    What are you trying to do? What do you want to happen?
     
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    I was saying if i add switch in hardware connected to portb0 will it work as per programming?
     
  13. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    No.

    Where is the
    Code ( (Unknown Language)):
    1.  
    2. while (1)
    3. {
    4. // some if- statements or case - switch
    5. }
    6.  
    ?

    See earlier post in this thread.
     
  14. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK, If i will not use while then program will run for once,right??

    and in this output of i is not commong in portA why?? portB is working..


    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
    3.  
    4.  
    5. #define _XTAL_FREQ 4000000
    6. #define    i        RA0            
    7. #define    j        RB1
    8.  
    9. void main (void)
    10.  
    11. {
    12. TRISB=0b00000000;
    13. TRISA=0b00000000;
    14.  
    15.  
    16. int i,j;{
    17.  
    18. for(i=0;i<5;i++)
    19. {PORTA = i;
    20.  
    21. {for
    22. (j=0;j<4;j++)
    23.  {
    24.  
    25. PORTB = j;
    26.  
    27.  }
    28. }
    29.  
    30.  
    31.  
    32.  
    33. }
    34. }
    35. }
     
  15. nerdegutta

    Moderator

    Dec 15, 2009
    2,519
    786
    Please explain in words what you want to do, preferably followed with a schematic.

    Now you are defining i as RA0 and j as RB1

    Later you declare them i and j as int.

    What's going on?

    Without a while, the program will run only once, but what is the point with that?
     
  16. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Please forgot the first fo a while..

    Is there any use of it in 877A as i can't find this register in data sheet...
    CMCON = 0x07 ; // Disable analog comparators
     
    Last edited: Jan 5, 2013
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Rithesh are you very rude or just darn stupid:mad: We are using our time to try to help you. But NOTHING seems to sink in. Can you explain why you avoid all our advices??
    NO your program will NOT run only once if you do not put in some endless loop. The controller will execute the assembler NOP instruction until it reach the end of program memory. Then the program counter will roll over and start to execute the instructions at address 0x0000.
    Second, I guess you want to see some LEDs blink in your program. The human eye is quite slow. So in order to see any changes you must slow you program down. Use delays from 100msec and up between each change. inserting a NOP will not help in anyway. Your LEDs will appear to either be full on or full off.

    ADCON0=0b00000000;
    NO that will not work. 0x00 is wrong. READ the datasheet. Since you ignore everything we say. I do not bother to tell you what the correct value should be.
     
  18. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    No. Where is your while loop?
     
  19. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    No.... Like I said previously.... The code from my tutorials was written on the pic16f628a.... We have been through this quite a few times..

    The pic16f877a hasn't got a compatator but HAS adc's

    You need to sort your superloop as spinnaker said... AND read the data sheet to see what peripherals to configure...
     
  20. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    I remember I am just learning but testing in MPlab only after this i will add while() delay when i will come to hardware..
     
Loading...