Erniem,I just pasted your code into MPLAB (with the additions noted above) . I saw exactly what you describe, after soon after RA2 turns on the watchdog timer expires. The watchdog will reset your PIC so the code starts all over so the observed sequence is RA0,RA1, RA2, RA0,RA1, RA2, and so on.
The fix for this was noted above, either turn the watchdog timer off, or reset it after each of the long delays using the "CLRWDT();" command you will find in the XC8 User's Guide.
Once you fix the watchdog you should see all 4 LEDs light.
Note the last state for all off has no delay so you will never see that happen.
Also note that "ADCON1=7;" is completely correct code. The default radix in C is base 10, so 7 is a binary 00000111. That is a bit different than the 00001111 posted.
I think I should use ADCON1 = 0b00000111;That makes sense, look at what
ADCON1 = 0b00001111;
does by looking, again, on page 128 in the datasheet. It commandeers the I/O function.
That is correct, however, 0b00000111 = 7. Since the compiler will default to a decimal notation for just "7", either will work.I think I should use ADCON1 = 0b00000111;
I read that I can also use ADCON1= 7;
Thats what I understood from page 128.. Correct me if I am wrong..
Thanks for clearing that up Ernie. I'm not used to seeing registers defined in decimal form, so that bit caused me some confusion.ErnieM said:Also note that "ADCON1=7;" is completely correct code. The default radix in C is base 10, so 7 is a binary 00000111. That is a bit different than the 00001111 posted.
You are correct. Lower nibble should be 0111, not 1111 as I originally stated. The initial "ADCON1 = 7;" will in fact work. My apologies for stating that wrong the first go through.tshuck said:That makes sense, look at what
ADCON1 = 0b00001111;
does by looking, again, on page 128 in the datasheet. It commandeers the I/O function.
Thanks all..Let me work through it...I have not tested it yet..I will update u allThanks for clearing that up Ernie. I'm not used to seeing registers defined in decimal form, so that bit caused me some confusion.
You are correct. Lower nibble should be 0111, not 1111 as I originally stated. The initial "ADCON1 = 7;" will in fact work. My apologies for stating that wrong the first go through.
Aamil: The others are correct. Your watchdog is disabled now, correcting the mistake on the ADCON1 register should fix the issue you're having. Sorry for pointing you in the wrong direction initially.
/*
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 2000000
#include <xc.h>
// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG
void main(void)
{
TRISA = 0b00000000; //RA0 as Output PIN
TRISB = 0b00000000; //RB0 as Output PIN
PORTA= 0b00000000;
PORTB= 0b00000000;
ADCON1 = 0b00000111; // ADC disabled
while(1)
{
// Lane 1: RA0-Red, RA1-Green
// Lane 2: RA2-Red, RA3-Green
// Lane 3: RB1-Red, RB2-Green
// Lane 4: RB4-Red, RB5-Green
// Step 1: Green-Red-Red-Red
RA0=0; //Red
RA1=1; //Green ON
RA2=1; //Red ON
RA3=0; //Green
RB1=1; //Red ON
RB2=0; //Green
RB4=1; //Red ON
RB5=0; //Green
__delay_ms(10000); // 1 Second Delay
CLRWDT();
// Step 2: Red-Green-Red-Red
RA0=1; //Red ON
RA1=0; //Green
RA2=0; //Red
RA3=1; //Green ON
RB1=1; //Red ON
RB2=0; //Green
RB4=1; //Red ON
RB5=0; //Green
__delay_ms(10000); // 1 Second Delay
CLRWDT();
//Step 3: Red-Red-Green-Red
RA0=1; //Red ON
RA1=0; //Green
RA2=1; //Red ON
RA3=0; //Green
RB1=0; //Red
RB2=1; //Green ON
RB4=1; //Red ON
RB5=0; //Green
__delay_ms(10000); // 1 Second Delay
CLRWDT();
//Step 4:Red-Red-Red-Green
RA0=0; //Red ON
RA1=1; //Green
RA2=1; //Red
RA3=0; //Green ON
RB1=1; //Red
RB2=0; //Green
RB4=1; //Red ON
RB5=0; //Green
__delay_ms(10000); // 1 Second Delay
CLRWDT();
}
}
//Step 1
PORTA = 0b 0000 0110; // spaces to show port connections only - remove to compile..
PORTB = 0b 00 01 0 01 0;
_delay_ms(10000);
//Step 2
PORTA = 0b00001001;
PORTB = 0b00010010;
_delay_ms(10000);
//Step 3
PORTA = 0b00000101;
PORTB = 0b00010101;
_delay_ms(10000);
//Step 4
PORTA = 0b00000110;
PORTB = 0b00010010;
_delay_ms(10000);
Do you mean one resistor per LED, or are you meaning one resistor for multiple LEDs?I am using 5V from DC supply... LEDs are connected to ground using 1K resistor (considering 4 LEDs will be ON at a time ).
So the connection is ... PIC port -> LEDs -> Resistor -> Gnd
I used one resistor for multiple LEDs. Does that matter?Do you mean one resistor per LED, or are you meaning one resistor for multiple LEDs?
Thanks for your support The problem was only resistorsConsider writing your ports as a complete byte instead of flipping individual bits (disconnect any other outputs on PORTA/B for now):
If that works, read the previous posts in this thread about shadowing your output ports to avoid a possible r-m-w problem.Code://Step 1 PORTA = 0b 0000 0110; // spaces to show port connections only - remove to compile.. PORTB = 0b 00 01 0 01 0; _delay_ms(10000); //Step 2 PORTA = 0b00001001; PORTB = 0b00010010; _delay_ms(10000); //Step 3 PORTA = 0b00000101; PORTB = 0b00010101; _delay_ms(10000); //Step 4 PORTA = 0b00000110; PORTB = 0b00010010; _delay_ms(10000);
Good luck!
Yes, the green LEDs have a higher forward voltage (Vf), which, when in parallel with a red LED of a lower Vf, will never light up, as you've seen.I used one resistor for multiple LEDs. Does that matter?
What are you going to control it with? A computer? Will you write the program to interface with it? A handheld remote? Will you build it?[...]But my project is a remote controlled traffic System[...]
I am planning with RF remote (4 button). How do I write the interrupt routine I have not written any interrupt codes yetYes, the green LEDs have a higher forward voltage (Vf), which, when in parallel with a red LED of a lower Vf, will never light up, as you've seen.
Glad you figured it out, though!
What are you going to control it with? A computer? Will you write the program to interface with it? A handheld remote? Will you build it?
There are many RF modules out there and many can work with your system.
See if you really need to use an interrupt. Your controller has nothing else to do until it is told a new traffic pattern, so why not just check the inputs over and over? Using an interrupt just means you can spend more time in a loop that does nothing.I am planning with RF remote (4 button). How do I write the interrupt routine I have not written any interrupt codes yet
I was planning to check inputs over and over. But i felt it complicated. As you mentioned, interrupt was prefered to spend more time in loop and for immediate response.See if you really need to use an interrupt. Your controller has nothing else to do until it is told a new traffic pattern, so why not just check the inputs over and over? Using an interrupt just means you can spend more time in a loop that does nothing.
If you still insist on using an interrupt I would start with reading the compiler's manual on how it expects interrupts to work, thinking it over, then asking questions here.
Those questions should get their own thread, turning on LEDs and interrupt service routines don't fit in the same box.
You are getting ahead of yourself. You need to find a transmitter module, then write the method of communicating with it. Have you found one?Thanks for the explanation on green and Red LED paralleling
I am planning with RF remote (4 button). How do I write the interrupt routine I have not written any interrupt codes yet
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
PROGRAMING TRAFFIC LIGHT - PIC 16f877a Using Mplab XC8 | Homework Help | 1 | ||
pic 16f877a code problem,"freq2write"query. | Microcontrollers | 2 | ||
Pic 16f877a | Microcontrollers | 9 | ||
D | spwm using pic 16f877A | Power Electronics | 2 | |
The Canakit 2 and Pic 16F877a (?) | Programming & Languages | 6 |
by Aaron Carman
by Jake Hertz
by Jake Hertz
by Duane Benson