PIC 18F4520 Project

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

User avatar
phalanx
Non-SFE Guru
Posts: 1991
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC 18F4520 Project

Post by phalanx » Mon Sep 29, 2014 9:27 am

Hi Ruben,

I have several questions and comments based on the schematic you just posted.

You have the Vdd and Vss connections swapped in your schematic. Pin #2 of the ICSP header is Vdd but you show it connected to Vss on the PIC.

Are you using the PICkit-3 to power your circuit? If not, you should add the power supply to it. Is the project only running on 3.3V?

The 2 leftmost LEDs don't have any current limiting on them due to where you have the 99Ω resistors installed. The PIC will probably survive for a long time without damage but they really should be current limited.

What's the purpose of the 9K resistors pulling up (or pulling down?) pins RB0 and RB1?

-Bill

rlfrosa
Posts: 12
Joined: Fri Sep 19, 2014 3:43 am

Re: PIC 18F4520 Project

Post by rlfrosa » Tue Sep 30, 2014 1:28 am

Hello,

Thank you for your feedback. Just an update to all you guys.

I tried only

Code: Select all

RCREG;
RCREG;
RCREG;
Instead of RCREG=dummy; but it still didn't work out.

rlfrosa
Posts: 12
Joined: Fri Sep 19, 2014 3:43 am

Re: PIC 18F4520 Project

Post by rlfrosa » Tue Sep 30, 2014 3:53 am

phalanx wrote:Hi Ruben,

I have several questions and comments based on the schematic you just posted.

You have the Vdd and Vss connections swapped in your schematic. Pin #2 of the ICSP header is Vdd but you show it connected to Vss on the PIC.

Are you using the PICkit-3 to power your circuit? If not, you should add the power supply to it. Is the project only running on 3.3V?

The 2 leftmost LEDs don't have any current limiting on them due to where you have the 99Ω resistors installed. The PIC will probably survive for a long time without damage but they really should be current limited.

What's the purpose of the 9K resistors pulling up (or pulling down?) pins RB0 and RB1?

-Bill
Hi there Bill,

I'm currently trying to turn on a LED just to check if my interrupt is working properly, and i'm really stuck in this so any feedback at all would be really helpfull.

First thank you for your answer;
1. I only noticed now and yes my schematic has VDD and VSS swapped but the circuit does not. It was a graphical mistake i'm sorry.
2. I'm using PICkit 3 to power the circuit (5V) and a pack of batteries (3V) to power the HC-06 module.
3. Those LEDs don't have current limiting because they are super bright LEDs and they can (from my calculations, i can be wrong) handle the PIN output along with the other LEDs (basically parallel connected) and 99 Ohms resistors (from what I have learned it can be put after or before the LED and since i'm only trying to "protect" the right LEDs I think i can use it like this, but my background is weak so if you notice any mistakes please let me know).
4. The resistors were left there from a previous circuit but i removed them.

By the way i switched my data in data out cables to make sure they were well connected, and i got and interrupt out of it. The LEDs wouldn't stop blinking (the program got stuck in my "pause" routine) but at least they interrupted, which means they are communicating at least. Also it only happens in debbuger mode, when i program it stops working.

I'm sorry to all you guys and here is an UPDATE from the circuit scheme. ( I will post a warning in the previous version).
Attachments
circuit.jpg

User avatar
phalanx
Non-SFE Guru
Posts: 1991
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC 18F4520 Project

Post by phalanx » Tue Sep 30, 2014 4:55 am

rlfrosa wrote:Instead of RCREG=dummy; but it still didn't work out.
That instruction would write a value to RCREG when you need to read from it, rearrange it so it is "dummy=RCREG" and that way RCREG will be read instead of written.

-Bill

User avatar
phalanx
Non-SFE Guru
Posts: 1991
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC 18F4520 Project

Post by phalanx » Tue Sep 30, 2014 5:09 am

rlfrosa wrote:2. I'm using PICkit 3 to power the circuit (5V) and a pack of batteries (3V) to power the HC-06 module.
Mixing voltages in your system is not a good idea unless you take specific precautions to limit current flow and ensure proper levels for I/O purposes. It would be better to run both the PIC and the Bluetooth module at 3.3V and configure the PICkit-3 to receive power from the target (instead of supplying it).
3. Those LEDs don't have current limiting because they are super bright LEDs and they can (from my calculations, i can be wrong) handle the PIN output along with the other LEDs (basically parallel connected) and 99 Ohms resistors (from what I have learned it can be put after or before the LED and since i'm only trying to "protect" the right LEDs I think i can use it like this, but my background is weak so if you notice any mistakes please let me know).
PIC pins are not current limited to specific levels. Driving an LED without a current limiting resistor will put stresses on the internal circuitry of the PIC that are outside of its absolute maximum ratings and can lead to latent failures in the future. That aside, PIC I/O is pretty robust (especially with the 5V tolerant parts) and it's likely the PIC will survive much longer than you need it to in your current configuration. It is a proper design practice to have current limiting of some fashion for all LEDs connected directly to the microcontroller.
By the way i switched my data in data out cables to make sure they were well connected, and i got and interrupt out of it. The LEDs wouldn't stop blinking (the program got stuck in my "pause" routine) but at least they interrupted, which means they are communicating at least. Also it only happens in debbuger mode, when i program it stops working.
Make sure that the Data Output pin of the Bluetooth module goes to the RX pin of the PIC and the Data Input pin goes to the TX pin. If your LEDs are blinking, then your program isn't getting stuck in your delay routine. Since the delay routines take up the majority of your processing time, odds are that when you pause execution you will be in the middle of your delay routine. It's quite possible that the Bluetooth module is passing much more data than you realize.

-Bill

rlfrosa
Posts: 12
Joined: Fri Sep 19, 2014 3:43 am

Re: PIC 18F4520 Project

Post by rlfrosa » Tue Sep 30, 2014 7:01 am

phalanx wrote:Make sure that the Data Output pin of the Bluetooth module goes to the RX pin of the PIC and the Data Input pin goes to the TX pin. If your LEDs are blinking, then your program isn't getting stuck in your delay routine. Since the delay routines take up the majority of your processing time, odds are that when you pause execution you will be in the middle of your delay routine. It's quite possible that the Bluetooth module is passing much more data than you realize.
-Bill
Hi guys, big UPDATE thanks to Bill!

--> I had Data IN and Data OUT badly connected the all time.

As you can see from my schematic I had it wrong the whole time. Well i'm fairly new at this what can i say...I'm sorry.

Any help one of you guys need regarding anything to do with mechanical/material engineering please fell free to PM me, I like to learn but I also like to share my knowledge, and I'm effectively a member of this forum so I'll be on the lookout for posts that i can help in.

I will try to figure out the rest myself.

-Ruben

(PS: It might have been fairly simple to all you guys but I want to thank you for the help, specially Bill, it has been a great step for me and surely it would not be possible without all you gentlemans help. Thank you very much. Sincerely.)

rlfrosa
Posts: 12
Joined: Fri Sep 19, 2014 3:43 am

Re: PIC 18F4520 Project

Post by rlfrosa » Tue Sep 30, 2014 7:34 am

Hi there guys!!

Thanks to everyones help I finally got it done.
Below is the code for everyone having similar issues or undergoing a similar project (feel free to use it or change it has you wish).

Code: Select all

#include <p18f4520.h>
#include <stdio.h>
#include <usart.h>

#pragma config OSC = INTIO67, WDT = OFF, MCLRE = ON 
#pragma config LVP = OFF, PWRT = ON 

#define L0 LATCbits.LATC2 
#define L1 LATCbits.LATC3 
volatile unsigned int i;

void MCU_Init (void)
  {
    OSCCONbits.IRCF0 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF2 = 1;
    SPBRG=51;
    TXSTAbits.BRGH = 1;
    ADCON1 = 0x0F;
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
    RCONbits.IPEN = 1;
    IPR1bits.RCIP = 1;
    TRISCbits.RC7 = 1;
    RCSTAbits.SPEN=1;
    TXSTAbits.SYNC = 0;
    TXSTAbits.TXEN=0;
    RCSTAbits.CREN=1;
    SPBRG=51;
    PIE1bits.RCIE = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
    i=0;
  }

void pausa(void)
  {
    unsigned char cnt1,cnt2;
    for (cnt1=200;cnt1;cnt1--)
    for (cnt2=200;cnt2;cnt2--);
  } 

void aux (void)
  {
    L0 = 0;
    L1 = 0;
    pausa();
    pausa();
    pausa();
    L0 = 1;
    L1 = 1;
    pausa();
    pausa();
    pausa();
  }

void blink (void)
  {
    aux();
    aux();
    aux();
    aux();
    aux();
    aux();
    aux();
    aux();
    i=0;
  }

void light (void)
  {
    L0 = 1;
    L1 = 1;
    i=0;
  }

void main (void)
  {
    MCU_Init();
    while (1)
      {
          if (i==1) blink();
          else light();
      }
    }

#pragma code high_vector=0x008
#pragma interrupt high_isr
void high_isr(void)
  {
    if (PIR1bits.RCIF==1)
      {
         if (RCSTAbits.OERR==1)
            {
               RCSTAbits.CREN=0;
               RCSTAbits.CREN=1;
            }
         else
            {
              char dummy, dummy1, dummy2;
              dummy = RCREG;
              dummy1 = RCREG;
              dummy2 = RCREG;
              i=1;
            }
       }
}
#pragma code
If you guys have any constructive critics or optimization suggestions over the code please feel free to do it, obviously you guys have more insight than I and other users might have.

I want to take this moment to thank all of the Sparkfun Electronics *USERS* that helped me untill now.

I'm currently starting the second phase where my main goal is:
- Light up L0 when a certain data is received and light up L1 when a certain data is received;

I'm going to research on it and get it going, but if you guys have any suggestions/code adaptation please feel free to share all the help is very welcome.

Best regards;
Ruben

Post Reply