Hello
I'm new in PIC programming and I'm trying to modify a program that blinks a LED. Now the program is suposed to:
-Turn on a LED connected to RB1
-Wait 1s
-Turn on another LED connected to RB2 (while the LED connected to RB1 remains turned on)
-Wait 2s
-Turn off the LED connected to RB1 (while the LED connected to RB2 remains turned on)
-Wait 1s
-Turn off the LED connected to RB2
-Wait 1s and restart the program
And the code of my program is this:
The program turns on the first led, waits a second, turns on the second led and then doesn't follow correctly the delays. Simply turns on and off the two leds very fast and waits a second. Where is the problem?
I'm new in PIC programming and I'm trying to modify a program that blinks a LED. Now the program is suposed to:
-Turn on a LED connected to RB1
-Wait 1s
-Turn on another LED connected to RB2 (while the LED connected to RB1 remains turned on)
-Wait 2s
-Turn off the LED connected to RB1 (while the LED connected to RB2 remains turned on)
-Wait 1s
-Turn off the LED connected to RB2
-Wait 1s and restart the program
And the code of my program is this:
Code:
#define _XTAL_FREQ 10000000
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
TRISB = 0x00; //PORTB as output
PORTB = 0x00;
while(1)
{
RB1 = 1; // LED 1 ON
__delay_ms(1000); // 1 Second Delay
RB2 = 1; // LED 2 ON
__delay_ms(2000); // 2 Second Delay
RB1 = 0; // LED 1 OFF
__delay_ms(1000); // 1 Second Delay
RB2 = 0; // LED 2 OFF
__delay_ms(1000); // 1 Second Delay
}
return 0;
}