C program for the PIC18F1220

Thread Starter

omar1990

Joined Sep 7, 2018
12
I need to Flash the LED four times at a rate of one flash per second.and then turn it OFF ,i manage to get to flash but i do not know how to stop it at 4 .
please help me
thanks
this is what i have done so far :
#include <xc.h>

#define TRUE 1
#define FALSE 0
#define FOSC 20000000L /* target device system clock freqency */
#define FCYC (FOSC/4L) /* target device instruction clock freqency */

#define _XTAL_FREQ FOSC /* required for HITECH PICC delay macros */

// Configuration settings
#pragma config OSC=HS,FSCM=OFF,IESO=OFF
#pragma config PWRT=ON,BOR=OFF,BORV=45
#pragma config WDT=OFF
#pragma config MCLRE=ON
#pragma config STVR=OFF,LVP=OFF,DEBUG=OFF
#pragma config CP0=OFF,CP1=OFF
#pragma config CPB=OFF,CPD=OFF
#pragma config WRT0=OFF,WRT1=OFF
#pragma config WRTB=OFF,WRTC=OFF,WRTD=OFF
#pragma config EBTR0=OFF,EBTR1=OFF
#pragma config EBTRB=OFF


//===============================================================
void main(void)
{

OSCCONbits.SCS0=0; //set oscillator to primary (external)
OSCCONbits.SCS1=0;

TRISA = 0b11111111; //Set port input output directions
TRISB = 0b00000000;
ADCON1 = 0b11111100; //All bits digital I/O except AN0 and AN1
RCON=0b00000000; //disable interrupt priorities (compatible with 16F series)
INTCON=0b00000000; //disable interrupts
INTCON2=0b00000000; //pull-ups enabled interrupt are all low priority

PORTA=0xFF; //Turn off seven segment display
PORTB=0xFF; //Note LED’s active low ie turned on with logic 0

while(1)

{


PORTBbits.RB4=1;//LED ON
__delay_ms(500);
PORTBbits.RB4=0;//LED OFF
__delay_ms(500);



}

}
 

danadak

Joined Mar 10, 2018
4,057
Several ways. You need a loop with a test for 4 iterations of the code you
have in the while(1) loop.

You could do that with the existing while() with a test condition in while()
or write a FOR loop inside the while.

Regards, Dana.
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
Several ways. You need a loop with a test for 4 iterations of the code you
have in the while(1) loop.

You could do that with the existing while() with a test condition in while()
or write a FOR loop inside the while.

Regards, Dana.
Hi Dana,
could you please help me with it because this all new to me
thanks
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
Several ways. You need a loop with a test for 4 iterations of the code you
have in the while(1) loop.

You could do that with the existing while() with a test condition in while()
or write a FOR loop inside the while.

Regards, Dana.
Hi danadak,
this is what did :

while(1)

{
for(l=0; l<4; l++)

PORTBbits.RB4=1;//LED ON
__delay_ms(500);
PORTBbits.RB4=0;//LED OFF
__delay_ms(500);

}


}
 

danadak

Joined Mar 10, 2018
4,057
Yourt missing some brackets, eg FOR loop does not
contain the LED code inside it.

Code:
    while(1) {

        for(l=0; l<4; l++) {


            PORTBbits.RB4=1;//LED ON
            __delay_ms(500);
            PORTBbits.RB4=0;//LED OFF
            __delay_ms(500);

        }

    }

}
This will execute the led burst 4 times repeatedly, so how will you
stop it ? A test needed to exit out of the while().

So what do you have that tells you FOR loop in finished that could be
tested for ?

Regards, Dana.

PS : Pay attetion to code indentation, makes it readable and debug-able.
Also use code tags when you post to preserve formatted code, again more
readable.
 
Last edited:

Thread Starter

omar1990

Joined Sep 7, 2018
12
Yourt missing some brackets, eg FOR loop does not
contain the LED code inside it.

Code:
    while(1) {

        for(l=0; l<4; l++) {


            PORTBbits.RB4=1;//LED ON
            __delay_ms(500);
            PORTBbits.RB4=0;//LED OFF
            __delay_ms(500);

        }

    }

}
This will execute the led burst 4 times repeatedly, so how will you
stop it ? A test needed to exit out of the while().

So what do you have that tells you FOR loop in finished that could be
tested for ?

Regards, Dana.

PS : Pay attetion to code indentation, makes it readable and debug-able.
Yourt missing some brackets, eg FOR loop does not
contain the LED code inside it.

Code:
    while(1) {

        for(l=0; l<4; l++) {


            PORTBbits.RB4=1;//LED ON
            __delay_ms(500);
            PORTBbits.RB4=0;//LED OFF
            __delay_ms(500);

        }

    }

}
This will execute the led burst 4 times repeatedly, so how will you
stop it ? A test needed to exit out of the while().

So what do you have that tells you FOR loop in finished that could be
tested for ?

Regards, Dana.

PS : Pay attetion to code indentation, makes it readable and debug-able.
im really confused
please could you just correct for me
thanks
 

danadak

Joined Mar 10, 2018
4,057
The post #5 has the correct bracketing. Just does not stop doing 4
bursts.

So when you exit the FOR loop what variable has taken on a value
that tells you that you exited the code loop ? And what is that variables
final value that you can use as a test ? And where would you use that
test to see that the FOR loop, WHILE loop only happens once ?

Regards, Dana.
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
The post #5 has the correct bracketing. Just does not stop doing 4
bursts.

So when you exit the FOR loop what variable has taken on a value
that tells you that you exited the code loop ? And what is that variables
final value that you can use as a test ? And where would you use that
test to see that the FOR loop, WHILE loop only happens once ?

Regards, Dana.
I really appropriate you help
so i think i should use the if statement for testing
something like this :
while(1){


for(l=0; l<4; l++) {

PORTBbits.RB4=1;//LED ON
__delay_ms(500);
PORTBbits.RB4=0;//LED OFF
__delay_ms(500);

}

if (l < 4) PORTBbits.RB4=0 ;


}


}
 

danadak

Joined Mar 10, 2018
4,057
Look at the link I gave you that tells you how a FOR loop
works.

Then look at link for WHILE loop.

Regards, going to bed, have a good evening, Dana.
 

dl324

Joined Mar 30, 2015
15,115
really i have no idea
You don't need two loops.

You can do it with either a while or for loop.

while:
Code:
i = 0;
while (i++ < 4) {
  PORTBbits.RB4=1;//LED ON
  __delay_ms(500);
  PORTBbits.RB4=0;//LED OFF
  __delay_ms(500);
}
for:
Code:
for (i = 0; i < 4; i++) {
  PORTBbits.RB4=1;//LED ON
  __delay_ms(500);
  PORTBbits.RB4=0;//LED OFF
  __delay_ms(500);
}
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
You don't need two loops.

You can do it with either a while or for loop.

while:
Code:
i = 0;
while (i++ < 4) {
  PORTBbits.RB4=1;//LED ON
  __delay_ms(500);
  PORTBbits.RB4=0;//LED OFF
  __delay_ms(500);
}
for:
Code:
for (i = 0; i < 4; i++) {
  PORTBbits.RB4=1;//LED ON
  __delay_ms(500);
  PORTBbits.RB4=0;//LED OFF
  __delay_ms(500);
}
Hi dl324,
i tried the code and is not working its keep flashing the led
thanks for your help
this is my code :
#include <xc.h>

#define TRUE 1
#define FALSE 0
#define FOSC 20000000L /* target device system clock freqency */
#define FCYC (FOSC/4L) /* target device instruction clock freqency */

#define _XTAL_FREQ FOSC /* required for HITECH PICC delay macros */

// Configuration settings
#pragma config OSC=HS,FSCM=OFF,IESO=OFF
#pragma config PWRT=ON,BOR=OFF,BORV=45
#pragma config WDT=OFF
#pragma config MCLRE=ON
#pragma config STVR=OFF,LVP=OFF,DEBUG=OFF
#pragma config CP0=OFF,CP1=OFF
#pragma config CPB=OFF,CPD=OFF
#pragma config WRT0=OFF,WRT1=OFF
#pragma config WRTB=OFF,WRTC=OFF,WRTD=OFF
#pragma config EBTR0=OFF,EBTR1=OFF
#pragma config EBTRB=OFF


//===============================================================
void main(void)
{

unsigned int i ;

OSCCONbits.SCS0=0; //set oscillator to primary (external)
OSCCONbits.SCS1=0;

TRISA = 0b11111111; //Set port input output directions
TRISB = 0b00000000;
ADCON1 = 0b11111100; //All bits digital I/O except AN0 and AN1
RCON=0b00000000; //disable interrupt priorities (compatible with 16F series)
INTCON=0b00000000; //disable interrupts
INTCON2=0b00000000; //pull-ups enabled interrupt are all low priority

PORTA=0xFF; //Turn off seven segment display
PORTB=0xFF; //Note LED’s active low ie turned on with logic 0

while(1){



for (i = 0; i < 4; i++) {
PORTBbits.RB4=1;//LED ON
__delay_ms(500);
PORTBbits.RB4=0;//LED OFF
__delay_ms(500);
}

}
}
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
Because you still have the for loop inside a while loop. You only need one or the other as I showed in the two code fragments.
i tried it again but its seems im doing something wrong , i did not use the while loop this time but still it does not stop

i = 0;
while (i++ < 4) {
PORTBbits.RB4=1;//LED ON
__delay_ms(500);
PORTBbits.RB4=0;//LED OFF
__delay_ms(500);
}
}
 

dl324

Joined Mar 30, 2015
15,115
i tried it again but its seems im doing something wrong , i did not use the while loop this time but still it does not stop
Are you certain the program is compiling and you're running the new code?

Do you have a terminal device that you can print debug messages to while the program is running?
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
Are you certain the program is compiling and you're running the new code?

Do you have a terminal device that you can print debug messages to while the program is running?
im using the pickit3 to flash the middle segment which is RB 4, and it seems to be not working (it does not stop at 4 flash
this is what i did :
C:
#include    <xc.h>

#define        TRUE    1
#define        FALSE    0
#define FOSC 20000000L      /* target device system clock freqency */
#define FCYC (FOSC/4L)      /* target device instruction clock freqency */

#define _XTAL_FREQ FOSC     /* required for HITECH PICC delay macros */

// Configuration settings
#pragma config OSC=HS,FSCM=OFF,IESO=OFF
#pragma config PWRT=ON,BOR=OFF,BORV=45
#pragma config WDT=OFF
#pragma config MCLRE=ON
#pragma config STVR=OFF,LVP=OFF,DEBUG=OFF
#pragma config CP0=OFF,CP1=OFF
#pragma config CPB=OFF,CPD=OFF
#pragma config WRT0=OFF,WRT1=OFF
#pragma config WRTB=OFF,WRTC=OFF,WRTD=OFF
#pragma config EBTR0=OFF,EBTR1=OFF
#pragma config EBTRB=OFF


//===============================================================
void    main(void)
{

   unsigned int i ;

    OSCCONbits.SCS0=0;         //set oscillator to primary (external)
    OSCCONbits.SCS1=0;

    TRISA = 0b11111111;        //Set port input output directions
    TRISB = 0b00000000;
    ADCON1 = 0b11111100;    //All bits digital I/O except AN0 and AN1
    RCON=0b00000000;        //disable interrupt priorities (compatible with 16F series)
    INTCON=0b00000000;        //disable interrupts
    INTCON2=0b00000000;     //pull-ups enabled interrupt are all low priority

    PORTA=0xFF;            //Turn off seven segment display
    PORTB=0xFF;            //Note LED’s active low ie turned on with logic 0

  
  
        for (i = 0; i < 4; i++) {
        PORTBbits.RB4=1;//LED ON
        __delay_ms(500);
        PORTBbits.RB4=0;//LED OFF
       __delay_ms(500);
}

}
 
Last edited by a moderator:

dl324

Joined Mar 30, 2015
15,115
I'm not familiar with anything PIC. Are you certain the program is compiling? Are you certain that flashing is working? If flashing failed, could it leave any previous code?
 
Top