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.

pin 2 of pic16f688 is not giving the output

Status
Not open for further replies.

piyushpandey

Member level 4
Joined
Mar 26, 2012
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
2,051
hello guys

I have connected two LEDS one to RC3 and another to RC2 of the pic16f688 and have written this code to check the blinking of the LED's

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pic.h>

__CONFIG(WDTE_OFF);

#define _XTAL_FREQ 4000000    // Define the frequency of the clock

int main (void) {

    // Defining the pins direction as output
//#define one
//#ifdef one
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
//#endif

   // TRISC = 0x00;
    // Disabling the analog condition of the pins
//#define two
//#ifdef two
    ANSELbits.ANS2 = 0;
    ANSELbits.ANS3 = 0;
//endif

   // ANSEL = 0x00;
   // CMCON0 = 0x07;

    for (int i=0;i<=5;i++)
    //while(1)
    {
        __delay_ms(100);
        PORTCbits.RC2 = 0;
        PORTCbits.RC3 = 1;
        __delay_ms(100);
        PORTCbits.RC2 =1;
        PORTCbits.RC3 =0;

    }

   //while(1);

   return (EXIT_SUCCESS);
}


In this code I have disabled the watchdog timer but I am getting the following result:

1. the LED connected to the RC3 is only blinking and not the LED connected to the RC2 , it is not blinking at all.

2. rather than blinking for 6 times the led connected to RC3 is blinking continuously.


I am not able to get the logic working behind this , SO please help me guys.


Thanks
 

Try to put the for loop in the while loop.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <pic.h>
 
__CONFIG(WDTE_OFF);
 
#define _XTAL_FREQ 4000000    // Define the frequency of the clock
 
int main (void) {
 
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
    
    TRISC = 0x00;
    
    ANSELbits.ANS2 = 0;
    ANSELbits.ANS3 = 0;
 
    ANSEL = 0x00;
    CMCON0 = 0x07;
 
    
    while(1)
    {
       for (int i=0;i<=5;i++)
       {
           __delay_ms(100);
           PORTCbits.RC2 = 0;
           PORTCbits.RC3 = 1;
           __delay_ms(100);
           PORTCbits.RC2 =1;
           PORTCbits.RC3 =0;
       }
   }
 
   return (EXIT_SUCCESS);
}

 

hello baas

it worked with the while loop but fails with the for loop , so I want to know that why is this happening and wht is the cause of this unusual behaviour.

as I have seen in AVR this does not happen but in this PIC micrcontroller this is very unusual.


Thanks
 

I think these things depends from compiler to compiler.

Try this:


Code C - [expand]
1
2
3
4
5
6
7
int i;    // may also be unsigned int or try short
   // other code
   while(1)
   {
      for (i=0;i<=5;i++)
      {
       .....

 

in your original code, after the for loop is ended you are running on "random" code, therefore you can not know the behaviour. the return from the main is meaningless, you should not leave the main function.
 

hi yuvko

can you tell me in detail what you mean by random code.

- - - Updated - - -

baas

the led are blinking continuously with your code .

but the LED connected to the RC2 pin is not blinking at all so why is this happening.

- - - Updated - - -

baas

the led are blinking continuously with your code .

but the LED connected to the RC2 pin is not blinking at all so why is this happening.
 

Random code:
Whatever is in the flash memory of the PIC after your code, old code etc. that is why we loop the "script".

IF you do not want the continuous blink try to use a while-loop or a do-while-loop instead of a for-loop. Or add a test with a if statement.

RC2 and RC3 is identical according to the datasheet... Have you tried any of the other ports, maybe one output is faulty...



TIP: Go wild. The only way to learn PICs is to play with it and read the data-sheets intensively.
 

Hi Baas

I think you are right the pic controller is not stopping after the for loop but is jumping to the main and start executing because I think that as you said it is taking the garbage value from my old programs.

do you have any idea that how to clean the whole flash memory of the pic16f688.

I am using the MPLAB ICD 3 in circuit debugger.


and next thing which I am going to tell might help someone in future if he/she is going to face the same problem.

Here is a short note on that:

The PIC16f688 is consist of the 12 I/O pins and two ports A & C .

to control each port their is a corresponding register of each port e.g. for PORTA it is TRISA and for PORTC it is TRISC.

and to control the analog feature of the port there is the ANSEL register but unlike other PIC micrcontrollers here is not individual ANSEL register for each ports.

Here the lower four bits of the ANSEL register is for controlling the analog status of the PORTA and the upper four bits of the ANSEL register is for the PORTC.

So to control the analog status of the PORTC you have to use ANSEL4-ANSEL7 bits.


SO this is a small research from my side , hope you people will appreciate it.



Thanks

Piyush Pandey



Thanks
 

hello guys

I have connected two LEDS one to RC3 and another to RC2 of the pic16f688 and have written this code to check the blinking of the LED's

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pic.h>

__CONFIG(WDTE_OFF);

#define _XTAL_FREQ 4000000    // Define the frequency of the clock

int main (void) {

    // Defining the pins direction as output
//#define one
//#ifdef one
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
//#endif

   // TRISC = 0x00;
    // Disabling the analog condition of the pins
//#define two
//#ifdef two
    ANSELbits.ANS2 = 0;
    ANSELbits.ANS3 = 0;
//endif

   // ANSEL = 0x00;
   // CMCON0 = 0x07;

    for (int i=0;i<=5;i++)
    //while(1)
    {
        __delay_ms(100);
        PORTCbits.RC2 = 0;
        PORTCbits.RC3 = 1;
        __delay_ms(100);
        PORTCbits.RC2 =1;
        PORTCbits.RC3 =0;

    }

   //while(1);

   return (EXIT_SUCCESS);
}


In this code I have disabled the watchdog timer but I am getting the following result:

1. the LED connected to the RC3 is only blinking and not the LED connected to the RC2 , it is not blinking at all.

2. rather than blinking for 6 times the led connected to RC3 is blinking continuously.


I am not able to get the logic working behind this , SO please help me guys.


Thanks

Mr. piyush,
The program looks like a C program written for PC!
Why you have used the following two lines?
#include <stdio.h>
#include <stdlib.h>

Also remember not to returen a value from main when you are programming a Micro. In case of PC programming, when a program ends the OS is there to take control. But in case of a micro, there is no OS to take control. So if the control leaves the main(), the micro system becomes unstable. So, never let the control escape the main().
Use void main(), not int main() and avoid that return statement.

In your case, the program structure should be like this :-

Code C - [expand]
1
2
3
4
5
6
7
8
9
//include header file for the pic
void main(){
//register initialize codes
for (int i=0;i<=5;i++)
       {
              statements;
       }
while(1){ }
}



There may be other errors. Only the structure of the program is discussed.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top