Starting Programming PIC16f877 with C

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Sep 9, 2012.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    HI guys,

    I want to clear and want to know how to write simple LED blink Code for PIC16F877A ..
    so, searching and asking give me idea i ahve installed Hi tech C compiler MAP lab.

    so,

    i have learn this but how to write further code for blinking LEd i want your help.




    Code ( (Unknown Language)):
    1.  This is for 4MHz xtal
    2. #include <htc.h>
    3.  
    4. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDT_OFF & FOSC_XT);
    5.  
    6. */
    7. Low Voltage Programming Disabled (LVP_OFF)
    8. Brown Out Reset Disabled (BOREN_OFF)
    9. Power Up Timer Enabled (PWRTE_ON)
    10. Watchdog Timer Disabled (WDT_OFF)
    11. Standard Crystal Oscillator (FOSC_XT)
    12. /*
    13.  
    14. #define _XTAL_FREQ 4000000
    15.  
    16. //start code here
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23. #include <htc.h>
    24.  
    25. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDT_OFF & FOSC_HS);
    26.  
    27. */
    28. Low Voltage Programming Disabled (LVP_OFF)
    29. Brown Out Reset Disabled (BOREN_OFF)
    30. Power Up Timer Enabled (PWRTE_ON)
    31. Watchdog Timer Disabled (WDT_OFF)
    32. High Speed Crystal Oscillator (FOSC_HS)
    33. /*
    34.  
    35. #define _XTAL_FREQ 6000000
    36.  
    37. //start code here
     
  2. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    This program has 2 LED and 2 switches. The comments would self-explain itself. It was taken from my PIC development board and I removed the LCD part. Try it and it may required minor modifications if it doesnt work straight away....

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2.  
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDT_OFF & FOSC_HS);
    4.  
    5. */
    6. Low Voltage Programming Disabled (LVP_OFF)
    7. Brown Out Reset Disabled (BOREN_OFF)
    8. Power Up Timer Enabled (PWRTE_ON)
    9. Watchdog Timer Disabled (WDT_OFF)
    10. High Speed Crystal Oscillator (FOSC_HS)
    11. /*
    12.  
    13. #define _XTAL_FREQ 4000000
    14.  
    15. //start code here
    16.  
    17. //configuration
    18. //==========================================================================
    19. //__CONFIG ( 0x3F32 );      //configuration for the  microcontroller
    20.  
    21. //  define
    22. //==========================================================================
    23.  
    24. #define SW1     RB0        
    25. #define SW2     RB1        
    26.  
    27. #define LED1        RB6        
    28. #define LED2        RB7            
    29.  
    30. //  function prototype      (every function must have a function prototype)
    31. //==========================================================================
    32. void delay(unsigned long data);        
    33.  
    34. //  main function                   (main fucntion of the program)
    35. //==========================================================================
    36. void main()
    37. {
    38.     unsigned long delay_time=5000;
    39.  
    40.     //set I/O input output
    41.     TRISB = 0b00000011;             //configure PORTB I/O direction
    42.    
    43.     LED1=0;                             //OFF LED1
    44.     LED2=0;                             //OFF LED2
    45.  
    46.     while(1)                            //Infinite loop
    47.     {
    48.         if(!SW1)                        //check if SW1 is pressed
    49.         {
    50.             while (!SW1);               //wait SW1 pressed
    51.             {
    52.                 delay_time+=1000;       //delay
    53.             }
    54.         }
    55.         else if(!SW2)                   //check if SW2 is pressed
    56.         {
    57.             while (!SW2);               //wait SW2 pressed
    58.             {
    59.                 delay_time-=1000;       //delay
    60.             }
    61.         }
    62.        
    63.         LED1^ = 1;                  //toggle LED1
    64.         delay(delay_time);             
    65.         LED2^ = 1;                  //toggle LED2          
    66.         delay(delay_time);         
    67.     }
    68. }
    69.  
    70. //  functions
    71. //==========================================================================
    72. void delay(unsigned long data)          //delay function, the delay time
    73. {                                       //depend on the given value
    74.     for( ;data>0;data--);
    75. }
    76.  
    77.  
    78.  
    Sw1 would make the LEDs blink slower and Sw2 make them blink faster. The program also works without the switches installed.

    Allen
     
    Last edited: Sep 9, 2012
    RRITESH KAKKAR likes this.
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you programmed your chip with assembler so you know the basics about configuration bits? And also please state what kind of crystal you are using. It has been a lot of changes in Hi-Tech C. So you must also tell us which version you are using of the C-compiler
     
    RRITESH KAKKAR likes this.
  4. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    I am using Hi tech ANSI C compiler with MAP lab 8.33V IDE...
    and if i want to check like simulate code what to do??
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    But which version of C-compiler? That is most important
     
  6. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
  7. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    RRITESH KAKKAR likes this.
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Kind of awckward program. You do know that Hi-Tech C have native delay functions?
    Have you looked in the C:\Program Files\HI-TECH Software\PICC\9.83\docs folder. Recommended for reading is the quickstart.pdf and of course the manual.pdf. Every time you do some work in MPLAB. ALWAYS build a project. That will make things more easy for you. Also in post #3 I asked some questions that you have not answered. Please do so
     
    RRITESH KAKKAR likes this.
  9. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Yes, in PIc16F676 with ASM code PWM the code was not of mine but i have litle knowledge of bits and programming in Asm now i want to start in C ..

    here is linkl of my video..

    http://www.youtube.com/watch?v=W0ZoAzAbi_o
    http://www.youtube.com/watch?v=uQ7nw9mTevI
     
  10. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    Yes, I know <htc.h> does have a built-in __delay_ms() function.

    But I guess the programmer who wrote the program wants to demonstrate how to use SW1 and SW2 to increase and decrease the blinking rate of the 2 LED, so that's why he didnt use the built-in function.

    And it was originally using pic.h and I changed it to htc.h because the OP was using it.

    Allen
     
    RRITESH KAKKAR likes this.
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    A good help is also to open your PIC include file in folder "...\HI-TECH Software\PICC\9.83\include" Here you will find the naming for the registers both at byte and bit level. At the top of the file. You also have the config-bits names. It looks like you have most of them correct. But looking at the header file is WDTE_OFF and not WDT_OFF
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3.  
    Remember to always put this line in any program you write.
    I can not learn you how to write C. That job you have to do by coding and learning on the way. But I can recommend some good books.
    This is a program that should blink the bits on PORTB
    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. /*
    4. Low Voltage Programming Disabled (LVP_OFF)
    5. Brown Out Reset Disabled (BOREN_OFF)
    6. Power Up Timer Enabled (PWRTE_ON)
    7. Watchdog Timer Disabled (WDT_OFF)
    8. High Speed Crystal Oscillator (FOSC_HS)
    9. */
    10.  
    11. #define _XTAL_FREQ 6000000
    12. //start code here
    13. void main (void)
    14. {
    15. TRISB=0b00000000;//all pins set as ouput
    16. PORTB=0;
    17. while (1)
    18.  {//endless loop
    19.   PORTB = !PORTB;
    20.   __delay_ms(1000);
    21.  }
    22. }
    23.  
     
    RRITESH KAKKAR likes this.
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Please explain this as u have written portb=0 outside the loop, why??
    and what does this mean PORTB=!PORTB; as i have done prog in spin it mean logical not gate...
    please explain bit more....


    thanks

    PORTB=0;
    WHILE(1); /* FOR LOOP ENDLESS*/
    {
    PORTB=!PORTB;
    __delay_ms(1000);
    }
     
  13. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    I got it!!
    OK, i have found 4Mhz crystal so, now i will change ...
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I can not learn you C as I said. But maybe this link can be of some help http://www.google.com/search?q=free+books+c
     
    RRITESH KAKKAR likes this.
  15. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    I said ...
     
  16. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Can we see output in Map Lab if yes how??
     
  17. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    HI,

    while building project it show error...why?
    step i have done...
    1. create new project PIc16F877A next
    2.save a file with testpic next
    4.next ( I have not add any file)next
    5.finish
    6.open new file paste my code
    7.save code by testpic.c
    8. then right click i add to project
    9.after that i build it show this error, why??
     
  18. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Using driver C:\Program Files (x86)\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe


    You have sevral versions of Hi-Tech C installed. Remove all but version 9.83
     
    RRITESH KAKKAR likes this.
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Last edited: Sep 10, 2012
    RRITESH KAKKAR likes this.
  20. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Where, i can't find??
     
Loading...