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.

Please, try programming your PICs with these two codes

Status
Not open for further replies.

Tricka90

Member level 1
Member level 1
Joined
Sep 2, 2013
Messages
40
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
385
I've been testing for days and days my PICs, Programmer, connections ecc in order to understand what is the problem.
Now I've decided to ask your help.
I have these two codes:

FIRST
Code:
void main(void) { 
    unsigned long i; 

    TRISA = 0; 
    PORTA = 0; 
     


    while(i++ <= 50000); 

    while(1) 
        PORTA.RA2 = 1; 
}


SECOND
Code:
long i = 0; 

void main() { 

TRISA = 0; 
PORTA = 0; 

while(1) 
        { 
         i++; 
         if(i >= 50000) 
                       { 
                        PORTA.RA2 = ~PORTA.RA2; 
                        i = 0; 
                        } 
         } 

}


The FIRST should make a LED turn On after some time. But the LED just turns On immediately!
The SECOND should make a LED blink continuously. But the LED, after some time, turns On and just stays On forever.
I'm using PIC 16F628A and PIC 16F876, if you have you can use them, else you can use similar PIC from 16F family.
Please I really need you to tell me if you get the same results I get, so I can finally understand what is the problem, after days and days of work and hundred of tests :(
 

Hi,

The FIRST should make a LED turn On after some time. But the LED just turns On immediately!

Code:
void main()                      //MAIN;
{
 TRISA = 0x00;                   //SET PORTA TO BE OUTPUT;
 PORTA = 0x00;                   //TURN OFF LEDs ON PORTA;
 Delay_ms(1000);        //WAIT 1S; 
 while(1){                       //INFINITE LOOP;
          PORTA = 0xFF;        //SET PORTA HIGH;
          
          }
}                                //END;


The SECOND should make a LED blink continuously. But the LED, after some time, turns On and just stays On forever.

Code:
void main()                      //MAIN;
{
 TRISA = 0x00;                   //SET PORTA TO BE OUTPUT;
 PORTA = 0x00;                   //TURN OFF LEDs ON PORTA;
 while(1){                       //INFINITE LOOP;
          PORTA = ~PORTA;        //INVERT STATE ON PORTA;
          Delay_ms(1000);        //WAIT 1S;
          }
}                                //END;


update me.

Best regards,
 

Of course, a blinking LED can be made in a lot of different ways, with delays, with cyclic timer overflow interrupts and so on.
But I want to make a LED blink using that particular code.
I don't need a blinking LED, I'm just doing some tests to see if it does work in this way, because it SHOULD if all the things are working properly.
So please, if you can, try those codes and tell me if they do work for you :-(
 
Last edited:

Hi,

Code:
void main(void) { 
    unsigned long i; 

    TRISA = 0; 
    PORTA = 0; 
     


    while(i++ <= 50000); 

    while(1) 
        PORTA.RA2 = 1; 
}

unsigned long i;
and
while(i++ <= 50000);
in some compiler i will automatically initialized from zero. but some might not work.. please declare and define i here.

unsigned long i=0;

Best regards,

- - - Updated - - -

and also

Code:
long i = 0; 

void main() { 

TRISA = 0; 
PORTA = 0; 

while(1) 
        { 
         i++; 
         if(i >= 50000) 
                       { 
                        PORTA.RA2 = ~PORTA.RA2; 
                        i = 0; 
                        } 
         } 

}

unsigned long i=0;

best regards,

- - - Updated - - -

As jayanth.devarayanadurga mentioned ,you have to improve your delay routine

Best regards,
 
paul's second point is correct if you didnt define a local variable it will be always a garbage value, a static and global values will be initialized by default....

second point is compiler optimization:
when you are writing

Code C - [expand]
1
while(i++ <= 50000);


May be your compile could optimize the lines and it can just put i = 50001;
to avoid this you can declare i as

Code C - [expand]
1
volatile unsigned int i;



If you keep this two steps in mind they will never miss the delay...
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
By C spezification, "automatic" variables (variables defined inside a function) aren't initialized. In so far the actual delay in code 1 can't be predicted.

You didn't tell about the involved compiler, so some implementation dependent details can be only guessed, e.g. possible optimization as mentioned in the previous post. Also the clock frequency is unknown. For 8 MHz and 16 bit long, I see about 250 ms delay for the loop. That's a bit short to be clearly detected.

The blink code is expected to work. There might be a hardware problem if the LED is connected without a series resistor. (A popular fault according to circuits posted at Edaboard). Read-modify-write operation of PORTA can result in always on in this case, because "1" state is never sensed across the LED.
 
Thanks for the help, I tried with your advices but the codes just doesn't work.
Trust me, I've tried a lot of things in these days, including turning off the analog peripherals, initializing i with lots of different values, near or far from 50000, different oscillators ecc.
I'm asking you to try these codes with your PICs (possibly 16F628A, 16F876 or similar) because I think my programmer is not working properly.
I know it may sound strange but please, I really would be grateful if you can try these.
 

Really the first code works for me it gives 0.5707sec delay on 8Mhz...

I checked with Hitech compiler

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <htc.h>
 
void main(void)
 {
    unsigned long i = 0; 
 
    TRISA = 0; 
    PORTA = 0; 
     
    while(i++ <= 50000); 
 
    while(1) 
    RA2 = 1; 
 }



- - - Updated - - -

Here is the proteus file....
 

Attachments

  • pic.7z
    42.1 KB · Views: 145
If you just simply want to know whether the codes work or not just simulate them in Proteus. If they work there they are fine.
 

jayanth: Thank you, but I use MikroC. If you tell me what kind of 16F PICs you're going to use (possibly 16F628A, 16F876 or similar) I will happily post the two HEX file for your PICs so you can program them immediately
Venkadesh: THANKS A LOT for trying it! Could you please tell me what kind of 16F PIC did you use?

- - - Updated - - -

salmanliaquat: I can tell you, from experience, that it could happen that simulator like Proteus doesn't return the same result of real PICs
 

Perfect, thank you Venkadesh, your help was very precious. Could you also tell me what programmer did you use? Pickit?
If someone else wants to try, maybe with PIC 16F628A or 16F876 I would be very greatful
 
I think you are suffering from Read Modify Write problems - or R-M-W. EDIT: WRONG! - the code below works by avoiding the problem - see the later code that works by fixing the problem.

I will let you google the subject, or better still, read MikroC help files, which explain it well.

For second code try:

Code:
long i = 0; 
char toggle;

void main() { 

TRISA = 0; 
PORTA = 0; 

    while(1){
         i++; 
         if(i >= 50000){
             PORTA.RA2 = toggle;
             toggle++;
             i = 0;
         }
    }

}
This code avoids RMW effect.

It is bad code because it is uncommented, but I followed your example with lack of comments :) Please comment all future code that you write.

It Flashes an LED on RA2 at about 1Hz with 4MHz crystal clock.
 
Last edited:
I think you are suffering from Read Modify Write problems - or R-M-W.
It can only happen if the required LED current limiting resistor has been omitted, also exceeding PIC rated output current, Unfortunately the original poster didn't manage yet to show a circuit or answer the respective question.
 

@FvM - I agree with you - I am not thinking straight.

The OP has posted the same question on many forums, but I suspect that p.erasmus hit the nail on the head in this forum:

https://www.electronicspoint.com/please-try-programming-your-pics-these-two-codes-t263101.html


Adding "CMCON = 7;" seems to solve the problem;

Code:
// simple LED flash code for PIC16F628A with 4MHz oscillator
// mikroC pro for PIC

unsigned long i = 0;        // just a counter for delay

void main() { 

CMCON = 7;       // disable comparator to make ports digital
TRISA = 0;        // PORTA all outputs
PORTA = 0;       // PORTA all low

    while(1){                  // loop forever
         i++; 
         if(i >= 50000){                              // count to cause a delay
             PORTA.RA2 = ~PORTA.RA2;      // toggle LED on PORTA bit 2
             i = 0;                                      // reset count
         }
    }

}

As others have pointed out: the fix for code 1 is to initialise the counter:
Code:
    unsigned long i=0;
Plus "CMCON = 7; // disable comparator" is a good thing to add, but you can get away without it in code 1 if you must
 
Last edited:
I really have to say THANKS A LOT TO EVERYBODY who gave me so much precious help.
Now, with PIC 16F628A the two code works PERFECTLY! 8-O
I just followed your advice.
For the first code I just had to declare i = 0...and it immediately worked!
For the second code I just had to turn off the comparators adding string CMCON = 7
Just for curiosity, could you please explain why the comparator was creating such a mess in the second code?
Thank you again, I really appreciate all your precious help :smile:
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top