Delay in C for PIC16F88

Discussion in 'Programmer's Corner' started by Ben_C, Oct 25, 2014.

  1. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Hi everyone,
    I was adamant in getting this to work by myself I've been working on it over the past 2 days but really cant figure it out! This is what I want my code to do:
    Do nothing until RB1 is high with momentary tactile switch and then when it is, to jump into the loop to blink the LED connected to RB0. I want the loop to go around 5 times (Therefore the LED to turn on 5 times) then exit the loop back to the start and again wait for RB1 to go high.

    What it does is go into the loop but is not responsive to my "i <= N". For this code it loops through 9 times where as I only want it to loop 5 times! When I adjust delay settings it will blink for longer/shorter. It just seems i<= 5 has no effect..

    I believe it has something to do with Config or Delay ..


    Code (Text):
    1. #include <htc.h>
    2. #include <pic.h>
    3. #include <pic16f88.h>
    4. #define _XTAL_FREQ 4000000
    5.  
    6.  
    7. int main(void)
    8.  
    9. {
    10.     OSCCON = 0x60;
    11.     TRISB = 0b00000010;
    12.  
    13.     int i;
    14.  
    15.     if (RB1 = 1)
    16.     {
    17.         if (i <= 5)
    18.         {
    19.         RB0 = 1;
    20.         __delay_ms (500);
    21.         RB0 = 0;
    22.         __delay_ms (500);
    23.         i++;
    24.         }
    25.     }
    26. }
     
  2. adam555

    Active Member

    Aug 17, 2013
    858
    39
    You forgot to assign '0' to 'i' when declaring it:

    Code (Text):
    1. int i=0;
    And you are also assigning '1' to 'RB1' with '=' in the first if, when you should have used the comparator '==':

    Code (Text):
    1. if (RB1 == 1)
     
  3. adam555

    Active Member

    Aug 17, 2013
    858
    39
    I think it still won't work; you should have use a for loop instead of the second if:

    Code (Text):
    1. int main(void)
    2.  
    3. {
    4.     OSCCON = 0x60;
    5.     TRISB = 0b00000010;
    6.  
    7.     if (RB1 == 1)
    8.    {
    9.         for( int i=1; i<=5; i++)
    10.         {
    11.         RB0 = 1;
    12.         __delay_ms (500);
    13.         RB0 = 0;
    14.         __delay_ms (500);
    15.        }
    16.     }
    17. }
     
  4. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Sorry, yes I've edited it so many times I just simply forgot to give a value to it before posting here :p I have tried this and it doesn't work, still blinking 9 times.. :(
     
  5. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Also tried for loop too, and while (RB1 = 1) loop.. I'm missing something with Timer set up, or __delay_ms. __delay_ms is showing as Exclamation mark on MPLABX, but when I put _delay it seems to like it... :s
     
    Last edited: Oct 25, 2014
  6. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Screenshot of code, When I debug program and step into it does loop the 5 times, gotta be clock malfunction/incorrect set-up.. Im going bald figuring this out.
     
  7. adam555

    Active Member

    Aug 17, 2013
    858
    39

    If you use "while (RB1 = 1)" you are assigning 1 to RB1; you are not comparing it.

    '=' is to assign,
    '==' is to compare.
     
  8. adam555

    Active Member

    Aug 17, 2013
    858
    39
    Just tested this code and works perfectly:

    Code (Text):
    1. void main(void)
    2. {
    3.     TRISBbits.TRISB0 = 1;
    4.     ANSELH = 0;
    5.  
    6.     TRISD = 0;
    7.     PORTD=0;
    8.  
    9.     if (RB0 == 0)
    10.    {
    11.         for( int i=1; i<=5; i++)
    12.         {
    13.         RD0 = 1;
    14.         __delay_ms (500);
    15.         RD0 = 0;
    16.         __delay_ms (500);
    17.        }
    18.     }
    19. }
    20.  
    Maybe you need to configure the ports before using them (e.g. turning off the analog input as I did for my PIC16F887), or maybe your switch is normally 1, and 0 when pressed; like my configuration.
     
    Last edited: Oct 25, 2014
  9. nerdegutta

    Moderator

    Dec 15, 2009
    2,513
    784
    This code will run only one time in a uC.

    You'll need to put your if - code in a while(1) - loop.

    AFAIK.
     
  10. adam555

    Active Member

    Aug 17, 2013
    858
    39
    Careful with copying my code and trying it; I changed the ports.
     
  11. adam555

    Active Member

    Aug 17, 2013
    858
    39
    Your right; I missed that one. Though it works the same in MPLAB with XC8.

    The code I posted above should be:

    Code (Text):
    1. int main(void)
    2.  
    3. {
    4.     OSCCON = 0x60;
    5.     TRISB = 0b00000010;
    6.  
    7.     while(1)
    8. {
    9.     if (RB1 == 1)
    10.    {
    11.         for( int i=1; i<=5; i++)
    12.         {
    13.         RB0 = 1;
    14.         __delay_ms (500);
    15.         RB0 = 0;
    16.         __delay_ms (500);
    17.        }
    18.     }
    19. }
    20. }
     
  12. adam555

    Active Member

    Aug 17, 2013
    858
    39
    And the main function should also be void; since it doesn't return anything:

    Code (Text):
    1. void main(void)
     
  13. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Hmm, analogue isn't associated to these 2 pins, I did try it though to no avail. I've tried this code:


    Code (Text):
    1. #include <htc.h>
    2. #include <pic.h>
    3. #include <pic16f88.h>
    4. #define _XTAL_FREQ 4000000
    5.  
    6.  
    7. void main(void)
    8.  
    9. {
    10.     OSCCON = 0x60;
    11.     TRISB = 0b00000010;
    12.     PORTB = 0b00000000;
    13.    
    14.     while(1)
    15.     {
    16.     if (RB1 == 1)
    17.         {
    18.         for( int i=1; i<=5; i++)
    19.         {
    20.         RB0 = 1;
    21.         __delay_ms (500);
    22.         RB0 = 0;
    23.         __delay_ms (500);
    24.         }
    25.         }
    26.     }
    27. }
    and nothing. Is it something to do with Timer0, or Config settings? Maybe WDT isnt set correctly.. ? Im running off INTRC btw at 4Mhz
     
  14. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    For the above code, it blinks 9 times for this code (notice delay @ 200ms) it blinks 24 times!


    Code (Text):
    1. #include <htc.h>
    2. #include <pic.h>
    3. #include <pic16f88.h>
    4. #define _XTAL_FREQ 4000000
    5.  
    6.  
    7. void main(void)
    8.  
    9. {
    10.     OSCCON = 0x60;
    11.     TRISB = 0b00000010;
    12.     PORTB = 0b00000000;
    13.    
    14.     while(1)
    15.     {
    16.     if (RB1 == 1)
    17.         {
    18.         for( int i=1; i<=5; i++)
    19.         {
    20.         RB0 = 1;
    21.         __delay_ms (200);
    22.         RB0 = 0;
    23.         __delay_ms (200);
    24.         }
    25.         }
    26.     }
    27. }
     
  15. nerdegutta

    Moderator

    Dec 15, 2009
    2,513
    784
    It should blink 4 times. I think you have your config bits setting wrong.
     
  16. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    I must have, do you have any idea how to configure? I tried myself and couldnt figure it out xD
     
  17. adam555

    Active Member

    Aug 17, 2013
    858
    39
    Try disabling WDT, BOREN, MCLR, PWRTEN and using INTRC with I/O port function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin. This way you will make sure none of those are interfering.

    It would also be good to test the switch on its own, and see if it's working properly. For example:

    Code (Text):
    1. if(RB1 == 1)
    2.     RB0 = 1;
    3. else
    4.     RB0 = 0;
    5.  
     
  18. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    With a little bit of patience, I got it to work! Thanks guys, it was config settings and CLK output. Adam I believe it was because I was using Internal clock I had to use RA7 as output, I couldnt use RB0 as I wasnt using external crystal :D

    If anyone else stumbles upon this it's :

    Code (Text):
    1. #include <htc.h>
    2. #include <pic16f88.h>
    3. #include <xc.h>
    4.  
    5. #define _XTAL_FREQ 4000000
    6.  
    7. #pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
    8. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    9. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    10. #pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital I/O, MCLR internally tied to VDD)
    11. #pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
    12. #pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    13. #pragma config CPD = OFF        // Data EE Memory Code Protection bit (Code protection off)
    14. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
    15. #pragma config CCPMX = RB0      // CCP1 Pin Selection bit (CCP1 function on RB0)
    16. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    17.  
    18. // CONFIG2
    19. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    20. #pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode enabled)
    21.  
    22.  
    23.  
    24. void main(void)
    25.  
    26. {
    27.     OSCCON = 0x60;
    28.     TRISB = 0b00000010;
    29.     TRISA = 0b00000000;
    30.     PORTA = 0b00000000;
    31.  
    32.     while(1)
    33.     {
    34.     if (RB1 == 1)
    35.         {
    36.         for( int i=1; i<=8; i++)
    37.         {
    38.         RA7 = 1;
    39.         __delay_ms (500);
    40.         RA7 = 0;
    41.         __delay_ms (500);
    42.         }
    43.         }
    44.     }
    45. }
     
    killivolt likes this.
  19. nerdegutta

    Moderator

    Dec 15, 2009
    2,513
    784
    I would be nice if you threw in the schematic as well...
     
    killivolt likes this.
  20. vishal12345

    New Member

    Dec 16, 2014
    8
    1
Loading...