Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Programming PIC 18F4550 in C

Status
Not open for further replies.
A SUBROUTINE is a callable code module. Its code is included exactly one time in the assembled HEX file, when the SUBROUTINE is called the current PC register value is pushed on the stack, the program execution then jumps to the location of the SUBROUTINE, executes it and then the previous PC value is popped from the stack and loaded into the PC register and program execution resumes where it originally left off.

A Macro is a set of instructions and directives referenced by a given name, during assembly when a MACRO is invoked, its name is replaced by the set of instructions or expanded as it is often referred. Multiple references to a MACRO results in multiple and identical sets of instructions occurring in the assembled HEX file. In other words a MACRO is like an alias for a particular set of instructions and directives, which the set replaces the invoked MACRO name during assembly.
 

I was reading C programs in mazidi's book. Now i want to write a program, RB0 is input pin and RC0 is for output. When RB0 is high , program should toggle RC0. Program will stay in loop forever and keep checking RB0 status. I wrote following program :

#include <pic18f4550.h>
void main(void)
{
TRISBbits.TRISB0 = 1;
TRISCbits.TRISC0 = 0;

while (PORTBbits.RB0 == 1)
{
PORTCbits.RC0 = ~PORTCbits.RC0;
}
while (1);
}


Above program may not be the exact one , but it just defines the logic to be used.
Now if someone make RB0 high using a touch switch , then quite possible that loop can be executed for maore than one time while switch is pressed and RC0 will toggle more than one time and will be undetermined. How to handle this situation. I feel that a delay can be implemented in loop or some condition can be put to check first 1 and then 0 on RB0 , only then RC0 should toggle. What u suggest.
 

Now if someone make RB0 high using a touch switch , then quite possible that loop can be executed for maore than one time while switch is pressed and RC0 will toggle more than one time and will be undetermined. How to handle this situation. I feel that a delay can be implemented in loop or some condition can be put to check first 1 and then 0 on RB0 , only then RC0 should toggle. What u suggest.

The answer to that particular problem is solved by using "Hardware Interrupts", which you will find out about in a later lesson. The method you are using in the above program is referred to as "Polling."

BigDog
 

Don't you think the code is a bit problematic for a switching application i.e.. if the mcu's pin RB0 is not high at the time of start up then the while loop
Code:
while (PORTBbits.RB0 == 1)
will never execute..it will only execute the last while loop forever...
It should be something like this...with adding the code to stop the loop to be executed more than once.
Code:
#include <pic18f4550.h>
void main(void)
{
TRISBbits.TRISB0 = 1;
TRISCbits.TRISC0 = 0;

while(1)
{
while (PORTBbits.RB0 == 1)
{
PORTCbits.RC0 = ~PORTCbits.RC0;
 while(PORTBbits.RB0 == 1){}; //This is to stop the loop to be executed more than once
}
}
}
Using the above code,if the user press a switch and make RB0 high,it will toggle RC0 and waits till the user release the switch and repeats this action forever.

Yes interrupts will also solve this problem,but when you are having this kind of problem with many inputs then the software approach comes into play as hardware interrupts are very costly resource and less in number,you will hardly find 2 or 3 external hardware interrupts in a mcu.

Good Luck
 
Last edited:
Hi,
How about something like this?

Code:
#include <pic18f4550.h>
void main(void){
   TRISBbits.TRISB0 = 1;
   TRISCbits.TRISC0 = 0;

   while(1){
      if (PORTBbits.RB0 == 1){
         PORTCbits.RC0 = ~PORTCbits.RC0;
      }
      while (PORTBbits.RB0 == 1);
   }
}

It checks if RB0 is 1. If it is, it toggles RC0 and waits for RB0 to become 0. Then it starts checking again.

Hope this helps.
Tahmid.

---------- Post added at 01:23 ---------- Previous post was at 01:16 ----------

you will hardly find 2 or 3 external hardware interrupts in a mcu.

The common ATMEGA16 has 3 external interrupt sources that trigger upon rising/falling edge or low level. The common ATMEGA88 has 2 external interrupt sources that trigger upon rising/falling edge or low level as well as hardware interrupt for change on pin on every input pin (3 interrupt vectors).
 
@debjit625 : You give thorough thought to the problem at hand , i mean even considering the situation if RB0 is not not high at the time of while loop. Thanks for the solution. I will handle RB0 in software only rather than using interrupt.

@Tahmid : I don't see any significant difference between your code and debjit's code. Only difference is using the conditional statement.You are using "if" and debjit is using "when" statement. Correct me if i am wrong. I think both codes handle the problem in same way.
 

Yes, it handles it the same way. I hadn't noticed debjit's code before when posting mine.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top