PIC 18F4520 Project

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

Moderator: phalanx

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

PIC 18F4520 Project

Post by rlfrosa » Fri Sep 19, 2014 3:56 am

Hi guys,

Although it is definitely not my specialization area i decided to approach a project involving PIC programing in C language. I started from the bottom and worked my way through in the last months, and i have been pushing myself hard (even the basics are complicated for a person not related to this area as you might know). I have reached this far by myself, and i'm hoping that some of you will be kind enough to help me in this final phase of the project, since it has been very frustrating.

My system is basically composed of LEDs connected to pins C2 and C3 of PIC18F4520. I have a HC-06 Bluetooth Module properly connected to the PIC18F4520, so i am sure it is a C language problem.

I have tried hyperterminals and some apps in the Smartphone and i know i'm sending something to the Bluetooth module and i can receive from it to.

What i want to basically do is to have the LEDs turned off for a while when the HC-06 module receives something. But my main difficulty has always been interrupts. Could any of you guys take a look at the following code and give me some suggestions?

Code: Select all

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

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

#define L0	LATCbits.LATC2									
#define L1	LATCbits.LATC3									
#define BAUDRATE 

void main (void)				

	{

		TXSTAbits.TXEN=1; 			
		RCSTAbits.SPEN=1;			
		TXSTAbits.BRGH=1; 			
		RCSTAbits.CREN=1;			
		SPBRG=25;					
		PIE1bits.RCIE = 1;			
		INTCONbits.GIE = 1;			
		ADCON1 = 0x0F;				
		TRISCbits.TRISC2 = 0;		
		TRISCbits.TRISC3 = 0;		
		L0 = 1; 					
		L1 = 1;						
        while (1);					

	}

void pausa(void)								

	{

		unsigned char cnt1,cnt2;				
			for (cnt1=150;cnt1;cnt1--)			
				for (cnt2=150;cnt2;cnt2--);		

	}

#pragma interrupt EUSART_RX_ISR
void  EUSART_RX_ISR(void)				

	{
		L0 = 0;		
		L1 = 0;		
		pausa();
		pausa();
		pausa();
		_asm GOTO main _endasm		
	}	

#pragma code isr_baixa = 0x0008
void ISR_baixa_prioridade(void)
{
	if(PIR1bits.RCIF) _asm BRA EUSART_RX_ISR _endasm
}

Thank you very much for your attention!
Last edited by rlfrosa on Thu Sep 25, 2014 7:17 am, edited 1 time in total.

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

Re: PIC 18F4520 Project

Post by phalanx » Fri Sep 19, 2014 7:25 pm

Hi rlfrosa,

First things first, what compiler are you using? Interrupts are handled differently with every compiler I've used so I would need to see the manual for your compiler to make sure your ISR syntax is correct.

There are a couple things that catch my eye from a program flow standpoint. The first is your usage of BRA and GOTO assembly instructions. C compilers keep track of function calls automatically. When you call a function, a return address is pushed onto the stack (and several other parameters) so that your program can return to where it was prior to calling the function. Injecting an assembly BRA or GOTO instruction disrupts this process by circumventing the compiler's automatic context saving and restoring. When an interrupt occurs in your program, the return address is pushed on the stack but you exit the ISR by issuing a "BRA" command which doesn't remove all the data that was just pushed to it. After multiple cycles, your stack can fill up completely and cause a stack overflow exception. This is handled differently by different compilers but the results can range from a complete program crash to business as usual.

Another side effect of the way you use the BRA and GOTO commands is your main function never returns to where it was currently executing when the interrupt occurred. Say your main function has 6 steps. It would execute like so:
Main(1, 2, 3, 4, 5, 6, 1, 2, 3, ...)
Say your interrupt has 3 steps and occurs during the 4th step of your main function. Proper execution would look like this:
Main(1, 2, 3, 4) -> ISR(1, 2, 3) -> Main(5, 6, 1, 2, 3, ...)
Main is interrupted, the ISR runs, andp the program returns to Main where it left off. Your program flow looks more like this:
Main(1, 2, 3, 4) -> ISR(1, 2, 3) -> Main(1, 2, 3, 4, ...)
The program jumps from the ISR directly to the start of your Main function which effectively resets it instead of letting it continue where it left off. This may not be a problem with your small program but as you get into more complicated programs, it will be a significant problem.

Unless you REALLY know what you are doing, you should avoid changing program flow using assembly.

The other issue I see is the code within your ISR. ISRs are supposed to be as streamlined as possible to keep the overall latency low. Big no-no's are calling functions and running delay routines inside an ISR. In your case you are doing both. A better way to implement them is to have your ISR alter a variable that your main routine checks for and then immediately return. This allows the Main routine to work on other tasks while waiting for the interrupt to occur. This is critical for the proper operation of more complex programs.

I hope that gets you moving in the right direction!

-Bill

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

Re: PIC 18F4520 Project

Post by rlfrosa » Thu Sep 25, 2014 7:09 am

Hello Bill;

First of all, thank you so much for your insight on the subject it has been of great help!

I am using MPLAB IDE v8.92.

I have changed the code a little to suit what i have understood of your insight (i think i was successful) and i created some more functions to suit the entire code better.

Now i'm still not getting any interruption, and i'm sure i have it all properly connected. I'm just not sure about the language configuration and the enabling of the interruptions.

Could you take another look and give me some more feedback?

Code: Select all

#include <p18f4520.h>										
#include <stdio.h>											
#include <usart.h>											
															
#pragma config OSC = INTIO67, WDT = OFF, MCLRE = ON			
#pragma config DEBUG = ON, LVP = OFF, PWRT = ON				
															
#define L0	LATCbits.LATC2									
#define L1	LATCbits.LATC3									
															
void pausa(void)											
	{														
		unsigned char cnt1,cnt2;							
			for (cnt1=150;cnt1;cnt1--)						
				for (cnt2=150;cnt2;cnt2--);					
	}														
															
void blink (void)											
	{														
		L0 = 1;												
		L1 = 1;												
		pausa();											
		L0 = 0;												
		L1 = 0;												
		pausa(); 											
		i=0;												
	}														
															
void light (void)											
	{														
		L0 = 1;												
		L1 = 1;												
		i=0;												
	}														
															
unsigned int i;												
															
void main (void)											
	{														
		ADCON1 = 0x0F;										
		TRISCbits.TRISC2 = 0;								
		TRISCbits.TRISC3 = 0;								
		TXSTAbits.BRGH = 1;									
		RCSTAbits.SPEN=1;									
		TXSTAbits.SYNC = 0;									
		TXSTAbits.TXEN=1; 									
		RCSTAbits.CREN=1;									
		SPBRG=51;											
		PIE1bits.RCIE = 1;									
		INTCONbits.PEIE = 1;								
		INTCONbits.GIE = 1;									
                i=0;												
		while (1)											
		{													
			if (i=1) blink();								
			else light();									
		}													
	}														
															
#pragma code isr = 0x000008									
#pragma interrupt INT_ISR									
void INT_ISR (void)											
{															
		if (PIR1bits.RCIF);									
		i=1;												
}				
I'm sorry for any inconvenient in advance;
Regards

- Ruben

jremington
Support Volunteer
Posts: 2339
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: PIC 18F4520 Project

Post by jremington » Thu Sep 25, 2014 9:21 am

Code: Select all

      if (PIR1bits.RCIF);   
That won't do anything. Remove the semicolon, if you want the following statement to be part of the "if".

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

Re: PIC 18F4520 Project

Post by rlfrosa » Thu Sep 25, 2014 10:13 am

Hi Guys,

I'm sorry about the semicolon error, i'm new to all of this.

I have been working hard and this is what i have accomplished so far

Code: Select all

#include <p18f4520.h>										
#include <stdio.h>											
#include <usart.h>											
#pragma config OSC = INTIO67, WDT = OFF, MCLRE = ON			
#pragma config DEBUG = ON, LVP = OFF, PWRT = ON				
															
#define L0	LATCbits.LATC2									
#define L1	LATCbits.LATC3									
volatile unsigned int i;									
															
void pausa(void)											
	{														
		unsigned char cnt1,cnt2;							
		for (cnt1=150;cnt1;cnt1--)							
		for (cnt2=150;cnt2;cnt2--);							
	}														
															
void blink (void)											
	{														
		L0 = 1;												
		L1 = 1;												
		pausa();											
		L0 = 0;												
		L1 = 0;												
		pausa(); 											
		i=0;												
	}														
															
void light (void)											
	{														
		L0 = 1;												
		L1 = 1;												
		i=0;												
	}														
																											
void main (void)											
	{														
		ADCON1 = 0x0F;										
		TRISCbits.TRISC2 = 0;								
		TRISCbits.TRISC3 = 0;								
		TXSTAbits.BRGH = 1;									
		RCSTAbits.SPEN=1;									
		TXSTAbits.SYNC = 0;									
		TXSTAbits.TXEN=1; 									
		RCSTAbits.CREN=1;									
		SPBRG=51;											
		PIE1bits.RCIE = 1;									
		INTCONbits.PEIE = 1;								
		INTCONbits.GIE = 1;									
        i=0;											
		while (1)										
			{												
				if (i==1) blink();							
				else light();							
			}												
	}														
															
#pragma code isr = 0x000008									
#pragma interrupt INT_ISR									
void INT_ISR (void)										
	{														
			if (PIR1bits.RCIF)i=1;							
	}												
Now i know my i variable is working and it's going to functions light and blink properly (i have tested it) but i still can't get any interrupts to trigger from Bluetooth.

I have read somewhere i need to read RCREG and reset the flag, the problem is I don't really know how to properly do that. Do you guys have any insight that you can share in this subject?

Also, am i obliged to have an MCU_Init function (with all UART respective fields) for it to work?

Again i apologize for any inconvenience;
And thank you one more time!

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

Re: PIC 18F4520 Project

Post by phalanx » Thu Sep 25, 2014 10:25 am

As a good coding practice to eliminate any doubts of your intent, always surround your conditional instructions with braces like so:

Code: Select all

if(PIR1bits.RCIF)
{
    i = 1;
}
In your ISR, you need to clear the interrupt flag of the UART otherwise you will immediately re-interrupt when you exit the ISR. With the 18F EUSART, you have to read the RCREG (like you said) to clear the flag. All you need to do is access the register in some manner. Most applications would be making use of the received data so moving the contents to a variable would satisfy the "read" to clear the flag: MY_RX_CHARACTER = RCREG. Since you aren't making use of the actual data, just its presence, you don't have to assign it to a variable but you still have to access it in some manner.

-Bill

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

Re: PIC 18F4520 Project

Post by rlfrosa » Fri Sep 26, 2014 1:14 am

Hello there there Bill;
First of all (and one more time) thank you so much for your answer and patient.
I will start using braces to avoid bugs, i'm always re-learning even the basics which is very nice.
Regarding the following:
phalanx wrote: In your ISR, you need to clear the interrupt flag of the UART otherwise you will immediately re-interrupt when you exit the ISR. With the 18F EUSART, you have to read the RCREG (like you said) to clear the flag. All you need to do is access the register in some manner. Most applications would be making use of the received data so moving the contents to a variable would satisfy the "read" to clear the flag: MY_RX_CHARACTER = RCREG. Since you aren't making use of the actual data, just its presence, you don't have to assign it to a variable but you still have to access it in some manner.
-Bill
I will try to figure it out by myself and try a little harder to do these things. Regarding the basics of clearance of the flag of the UART, I have no questions for now.

I thank you again for all your patience Bill and I hope I haven't annoyed you or any other people in this forum that much, because i will probably be back with some doubts.

Best regards;
-Ruben

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

Re: PIC 18F4520 Project

Post by rlfrosa » Fri Sep 26, 2014 6:34 am

Hi Bill;

I'm sorry to bother you so soon, I myself was expecting a little more evolution before stumbling into doubts again, but it seems like I'm really stuck in this interrupt via UART.

So far i have:
1 - Made sure all the {} and semicolons are set properly within the code;
2 - Run the code and watched it loop "endlessly" (sometimes) in the blink after a successful interruption (probably because I wasn't cleaning the flag, and i'm not even sure if it was the interrupt routine that was interrupting it);
3 - As I learned from you that the RCIF bit gets set when new data is available in RCREG and gets cleared when all data is emptied from the FIFO and that reading RCREG in the interrupt routine therefore clears the flag automatically if there is no other data in the FIFO, i added an "RCREG = 0" to the interrupt routine.
4 - I can't still get any proper interruption and i'm stuck. Things i have tried:
- "Cleaning" RCREG 3 times by using "RCREG = 0" (I have read somewhere you need to do it 3 times to clear the flag);
- Using if (PIR1bits.RCIF==1) instead of if (PIR1bits.RCIF=1) inside the interrupt routine;
- Using the following code inside my interruption routine to avoid overrun errors (I think):

Code: Select all


 

 

 

#pragma code isr = 0x0008 
#pragma interrupt INT_ISR 
void INT_ISR (void) 
   { 
           if (PIR1bits.RCIF=1) 
                  { 
                      if(RCSTA&0x06) 
                               {
                                    RCSTAbits.CREN=0; 
                                    RCSTAbits.CREN=1;
                               }
          else
                              {
                                     i=1;
                                     RCREG = 0; 
                               } 
                 }
}

 

 

 

I tried all those combinations to see if anything would work out but i had no success whatsoever.
Could it be because i'm setting my interrupt routine the wrong way? I have #pragma code isr = 0x0008 but i really don't have priorities of any kind.
After many trials here lies the code as I left it for now (I doubt I will be able to do anything else, I seriously ran out of options but i will keep trying):

Code: Select all


 

 

 

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

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

 

 

 


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

void MCU_Init (void) 
{ 
ADCON1 = 0x0F; 
TRISCbits.TRISC2 = 0; 
TRISCbits.TRISC3 = 0; 
TXSTAbits.BRGH = 1;
RCSTAbits.SPEN=1; 
TXSTAbits.SYNC = 0; 
TXSTAbits.TXEN=1; 
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=150;cnt1;cnt1--) 
for (cnt2=150;cnt2;cnt2--); 
} 

void blink (void) 
{ 
L0 = 0; 
L1 = 0; 
pausa(); 
L0 = 1; 
L1 = 1; 
pausa(); 
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 isr = 0x0008 
#pragma interrupt INT_ISR 
void INT_ISR (void)
{ 
if (PIR1bits.RCIF=1) 
{ 
i=1; 
RCREG = 0; 
} 
} 

 

 

 

By the way i'm using Tera Term in PC and Bluetooth SPP in Android to send "something" (anything really) to the module; and has my understanding goes so far, if the language is built correctly I would be able to trigger an interrupt, but I couldn't get it.

If you have any knowledge that you can share regarding the situation where i'm standing now (again) I would be very thankfull.

In the meanwhile i'm going to keep trying to make it work.

Again im sorry for any inconvenience.

Best Regards
-Ruben

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

Re: PIC 18F4520 Project

Post by phalanx » Fri Sep 26, 2014 7:57 am

Hi Ruben,
I don't mind helping people that are putting in the effort to learn!

Until now I hadn't looked at your UART configuration and was wondering what your intended baud rate is and what you expect the oscillator speed of the PIC to be. From looking at your configuration bits (by the way, don't set the DEBUG config bit in your code - let MPLAB handle it), it appears that you want the internal oscillator which runs at 8Mhz. Are you aware that there is a settable postscaler after the oscillator that defaults to 1Mhz after power-up? Look at page 29 and 30 of the datasheet for details. With your current settings, you are running at 2.4kbps. If you were at 8Mhz it would be 19.2kbps and if you were at 4Mhz, it would be 9.6kbps.
rlfrosa wrote: "Cleaning" RCREG 3 times by using "RCREG = 0"
The datasheet doesn't say specifically but I believe RCREG is read-only so setting it to zero may not work. Use a temporary variable and move the contents of RCREG to it: TEMP = RCREG;
rlfrosa wrote:Using if (PIR1bits.RCIF==1) instead of if (PIR1bits.RCIF=1) inside the interrupt routine;
I missed this before. "==" is used for comparisons and "=" is an assignment operator. For comparing one value to another in an IF statement (or any other comparison), you have to use "==."

The latest Microchip compiler for 8-bit PICs is XC8 and your ISR formatting doesn't look like the examples I've seen for it. Can you let me know what compiler you are actually using? MPLAB isn't the compiler; it's the IDE (integrated development environment).

-Bill

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

Re: PIC 18F4520 Project

Post by rlfrosa » Fri Sep 26, 2014 9:55 am

Hello Bill;

Thank you very much for your help.
phalanx wrote: If you were at 8Mhz it would be 19.2kbps and if you were at 4Mhz, it would be 9.6kbps.
-Bill
I have taken my "baud rate" configurations out of (Fosc/(16*(SPBRG+1))=9600) because i have BRGH=1 (I will confirm this better). My desired baud is 9600 so by those calculations (and i'm not sure they are right, taken from a very poor book) my SPBRG needed to be 51. But after reading "On device Resets, the default output frequency of the internal oscillator block is set at 1 MHz(page 29)" I understand that after resetting it the frequency from my internal clock will actually not be 8MHz (I wonder why the book i read about this didn't really state anything about it being specialized in PIC18F4520) but I will work on fixing it.
From what i have in info

SYNC= 0|BRG16 = 0|BRGH = 0| EUSART Asynchronous Mode >> (Fosc/(64*(SPBRG+1))=9600)
SYNC= 0|BRG16 = N/A|BRGH = 1| EUSART Asynchronous Mode >> (Fosc/(16*(SPBRG+1))=9600)--The one I used
SYNC= 0|BRG16 = 1|BRGH = 1| EUSART Asynchronous Mode >> (Fosc/(4*(SPBRG+1))=9600)
SYNC= 1|BRG16 = N/A|BRGH = N/A| EUSART Synchronous Mode >> (Fosc/(64*(SPBRG+1))=9600)

Being that the oscillator INTOSC is defaulted at 8MHz i think i can add "OSCCON = 0x70;" in my main and make sure it is allways set to 8MHz. Please correct me if i'm wrong, because now i don't know from my settings if i'm suppose to use another but i will try to figure it out. Also include the info relative to 1MHz.
phalanx wrote: I missed this before. "==" is used for comparisons and "=" is an assignment operator. For comparing one value to another in an IF statement (or any other comparison), you have to use "==."
-Bill
Maybe it wasn't working properly because of the UART configuration, after working on it a little more i will try to solve this part properly using your following advice:
phalanx wrote: The datasheet doesn't say specifically but I believe RCREG is read-only so setting it to zero may not work. Use a temporary variable and move the contents of RCREG to it: TEMP = RCREG;
-Bill
I also read in a book that you can only use "RCREG;" to read it, but i will try both ways because i'm starting to doubt books.

I will work harder to see if I can solve this properly, and then update the topic.
Thank you very much, and sorry for any inconvenience.

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

Re: PIC 18F4520 Project

Post by phalanx » Fri Sep 26, 2014 11:14 am

rlfrosa wrote:i think i can add "OSCCON = 0x70;" in my main and make sure it is allways set to 8MHz. Please correct me if i'm wrong...
Writing 0x70 to OSCCON would clear bits <1:0> of the register and would switch you back to the external oscillator which I'm guessing you don't have connected. Make use of the Microchip defined structures that allow you to access specific bits of the register directly:

OSCCONbits.IRCF = 0x7 --- This will set OSCCON bits 6-4 to "111" which will enable the 8MHz setting without affecting anything else.

If you change the setting to 8MHz, you will have to change your SPBRG value from 25 to 51 to get a 9.6Kbps baud rate.

The most recent datasheet can always be found on the Microchip website: http://www.microchip.com/wwwproducts/De ... e=en010297

Direct to the datasheet: http://ww1.microchip.com/downloads/en/D ... 39631E.pdf

-Bill

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

Re: PIC 18F4520 Project

Post by rlfrosa » Mon Sep 29, 2014 6:39 am

Hi there guys!

Bill i followed your advice and i think I now have a properly defined baud rate.

phalanx wrote: This will set OSCCON bits 6-4 to "111" which will enable the 8MHz setting without affecting anything else.

If you change the setting to 8MHz, you will have to change your SPBRG value from 25 to 51 to get a 9.6Kbps baud rate.

-Bill
So I have also changed my code quite a bit, and optimized it not only using your help but also going through some threads and articles.
I have set the configurations and all the code properly for the objective. Basically in my inexperienced way of seeing things, by now my circuit should interrupt it's main function to blink the LEDs upon an RX communication. Surprisingly it still doesn't do that and I can't figure out why because from what i have learned until now, it really should work.

I know it is sending and receiving, but I was wondering if any of you guys use some kind of "sample" codes to test if the UART circuit is sending and responding "properly". I have seen samples sending code strings and I was able to execute them, but when I tried for example turn on a LED upon the string sending (by changing a variable that took effect on the "main") I was not succeeded.

Anyway here lies the code that I could put together untill now. If you guys have any suggestions/tips or tricks at any level please share them. (Hope you like the layout). Also I have tried PIR1bits.RCIF=0 instead of reading RCREG and no success.

Again I'm using PIC18F4520, MPLab IDE v8.92, PICkit3 and HC-06 Bluetooth Module.

Sorry for any inconvenience and for the extension of the post, but i'm not giving up on this.

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; 
          ADCON1 = 0x0F; 
          TRISCbits.TRISC2 = 0; 
          TRISCbits.TRISC3 = 0; 
          RCONbits.IPEN = 1; 
          IPR1bits.RCIP = 1; 
          TRISCbits.RC7 = 1; 
          TXSTAbits.BRGH = 1; 
          RCSTAbits.SPEN=1; 
          TXSTAbits.SYNC = 0; 
          TXSTAbits.TXEN=1; 
          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=150;cnt1;cnt1--) 
          for (cnt2=150;cnt2;cnt2--); 
     } 

void blink (void) 
     { 
          L0 = 0; 
          L1 = 0; 
          pausa(); 
          L0 = 1; 
          L1 = 1; 
          pausa(); 
          L0 = 0; 
          L1 = 0; 
          pausa(); 
          L0 = 1; 
          L1 = 1; 
          pausa();
          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 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; 
                                        RCREG = dummy; 
                                        RCREG = dummy1; 
                                        RCREG = dummy2; 
                                        i=1; 
                              } 
                } 
     } 
#pragma code


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

Re: PIC 18F4520 Project

Post by phalanx » Mon Sep 29, 2014 7:00 am

A quick comment because I don't have time at the moment to look at things in depth. When you initialize your SPBRG register, you should do it first before setting any of the other UART related registers.

Also, you have MCLR enabled in your configuration bits. After programming, are you pulling that pin high to start program execution?

-Bill

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

Re: PIC 18F4520 Project

Post by rlfrosa » Mon Sep 29, 2014 8:48 am

Hi there Bill;

Thank you for answering!

I have changed SPBRG from place like you advised me to.

Regarding MCLR I'm using a 10K resistor to do that. I leave a circuit schematic here, not only for you to take a look at but for other users that might need the same help I do and would like to know more about the schematic of what is being approached.

Sorry if it is a poor design. I left two 9K resistors connected to INT1 and INT0 (from a previous circuit) but i don't believe that is what is keeping it from working.

Thank you for all the help,
!!!!!DISCLAIMER!!!!!!
!!To all users following the post, please refer to the next circuit scheme as this one contains graphical errors and was removed to avoid mistake inducing!!!
Last edited by rlfrosa on Tue Sep 30, 2014 2:00 am, edited 3 times in total.

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: PIC 18F4520 Project

Post by waltr » Mon Sep 29, 2014 8:49 am

Another Hint:
Do run/step your code in the MPLAB SIMULATOR. This will help you learn how the code executes and find setup and logic errors.

Post Reply