Help needed for programming PIC16F877A...

tshuck

Joined Oct 18, 2012
3,534
It makes me sad that so many people are trying to help you and you are saying that you'll do it when you want to.

Not including the while loop may(and probably will) cause your program to not function the way you'd like.

Now, that being said, I think most PIC programs should be in the following form:
Rich (BB code):
(includes)

void Initialize();

void main()
{
    Initialize(); 
    while(1)
    {
        //this is repeated endlessly
    }
}
void Initialize()
{
    //Initialize your device
}
Now, then... Read through the section on the ADC module in the 16F877A datasheet. You'll find a register called ADCON1 on page 130. Read about it.

As a bit of a preemptive answer to another thread on the subject, one thing to note about an actual 16F877A, there is no internal oscillator, you'll need to provide a clock/crystal.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
As per this program when i=5 the pic should working but in MPlab it is running cont., why??
how to interrupt this?

#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);


#define _XTAL_FREQ 4000000

void main (void)

while(1){
{
TRISA=0b00000000;
TRISB=0b00000000;
ADCON1 =0b11110110; // Changes PORTA to digital
//CMCON = 0x07 ; // Disable analog comparators
PORTA=0;
PORTB=0;
OPTION_REG =0x0;

int i,j;

for(i=1;i<6;i++){



for(j=1;j<5;j++){
PORTA=j;
}
PORTB=i;
}
if(i=5){
INTCON =0xf;
}






}}
 

spinnaker

Joined Oct 29, 2009
7,830
As per this program when i=5 the pic should working but in MPlab it is running cont., why??
how to interrupt this?
What does running "cont. mean"? What does how to interrupt this mean? What do you want to do? Please make yourself clear when posting.

Format your code so that is indented so you can easily see the blocks and matching brackets. That is the whole purpose of the code tag.

What happens when you trace through the code using your debugger? What lines are causing you the problem?

The line if(i=5){ is an assignment. It is not a logical comparison.

You want if(i==5){

Learn C please!
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
when i reaches 5 the program in uC should stop due to interrupt occur...but in MPLAB it is running while interrupt is used..,why?

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);


#define _XTAL_FREQ 4000000

void main (void)

{
TRISA=0b00000000;
TRISB=0b00000000;
ADCON1 =0b11110110; // Changes PORTA to digital
//CMCON = 0x07 ; // Disable analog comparators
PORTA=0;
PORTB=0;
OPTION_REG =0xf;
while(1){

int i,j;

for(i=1;i<6;i++){


 
for(j=1;j<5;j++){
PORTA=j;
}
PORTB=i;
}  
if(i==5) // when i reaches 5 the program in uC should stop due to interrupt occur...
{
INTCON =0xf;
}


    
  


}}
 

absf

Joined Dec 29, 2010
1,968
The interrupt mechanism in PIC is much more complicated than just using a

{
INTCON=0xf;
}

There is a whole section in the datasheet talking about interrupt. Did you read and understand it? May be "interrupt" is better described in the Mid-range PIC datasheet...

Allen
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
I am trying to learn interrupt when switch is closed For an example the program execution let say LED were running it should at that period...i think now are getting me...
 

Attachments

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
I want to ask one in this code when SW=1 the program goes to else, why??



Rich (BB code):
while(1){
    
    int i;
 SW1=1;

    
    if(SW1==1)
    {
    for(i=1;i<2;i++){
    LED1=1;
    __delay_ms(1);
    LED1=0;
     __delay_ms(1);
     }

}
    
    else 
    {
        for(i=1;i<2;i++){
    LED1=1;
    __delay_ms(2);
    LED1=0;
    __delay_ms(2);
        }
}}
 

Ian Rogers

Joined Dec 12, 2012
1,136
If SW1 is defined as a port bit, you will be suffering the "Read Modify Write" syndrome..#

It's well covered in the datasheet....
 

t06afre

Joined May 11, 2009
5,934
If SW1 is defined as a port bit, you will be suffering the "Read Modify Write" syndrome..#

It's well covered in the datasheet....
No I think the problem is that Ritesh. Use the MPLAB simulator. In order to simulate signals from the outside world. He must use the stimulus editor. Ritesh take a look at the links in the end at the first post here http://forum.allaboutcircuits.com/showthread.php?t=44852 I will also give you this advice Ritesh. Then you assume something the probability for being wrong is 99.999%
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Here is the full code:
i want to control the LED from SW1 switch..
and i am using MPLAB Sim debugger...

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);


#define _XTAL_FREQ 4000000
#define    SW1    RB5
#define    LED1 RB7


void main (void)

{

TRISB=0b00100000;
PORTB=0;




while(1){
    
    int i;
 SW1=1;

    
    if(SW1==1)

    
    for(i=1;i<2;i++)
{
    LED1=1;
    __delay_ms(1);
    LED1=0;
     __delay_ms(1);
     }


    
    else 
    
        for(i=1;i<2;i++)
{

    LED1=1;
    __delay_ms(2);
    LED1=0;
    __delay_ms(2);
        }

}
}
 

t06afre

Joined May 11, 2009
5,934
Here is the full code:
i want to control the LED from SW1 switch..
and i am using MPLAB Sim debugger...

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
 
 
#define _XTAL_FREQ 4000000
#define    SW1    RB5
#define    LED1 RB7
 
 
void main (void)
 
{
 
TRISB=0b00100000;
PORTB=0;
 
 
 
 
while(1){
 
    int i;
 SW1=1;
 
 
    if(SW1==1)
 
 
    for(i=1;i<2;i++)
{
    LED1=1;
    __delay_ms(1);
    LED1=0;
     __delay_ms(1);
     }
 
 
 
    else 
 
        for(i=1;i<2;i++)
{
 
    LED1=1;
    __delay_ms(2);
    LED1=0;
    __delay_ms(2);
        }
 
}
}
RRITESH! can you just for one time. At least pretend you are able to absorb information given to you. In post #34 I told you that you have to use the stimulus editor. And gave you several links telling how to use the stimulus editor. Did you even bother to look at those links?? I guess not. Instead you are just sitting there like a nestling waiting to be spoonfed. What is wrong with you!
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

I am working with 4017 connected through 877 for controling LED as per schematic the problem is that i am not getting how to control the Reset pin 15 of 4017 to avoid counting upto 10 i.e. stop at 8 only..??
 

Attachments

Top