Doubt about microcontroller program that add bits

Discussion in 'Embedded Systems and Microcontrollers' started by richieeqc, Nov 11, 2012.

  1. richieeqc

    Thread Starter New Member

    Nov 9, 2012
    2
    0
    I'm studying now microcontrollers , and i have to do a program in pic 16f84a in c(pcw ccs compiler). This program must add a bit from PORTA with 05h data and the result must show in PORTB. PORTB = PORTA + 05H.

    SO this is the program i have done, it's compile without errors, but in the circuit it doens't works well. I don't know what are the errors, If someone could help me, i would greatly apreciate.

    #include <16f84a.h>
    #fuses xt, nowdt
    #use delay(clock= 4000000)
    #byte portb = 6
    #byte porta = 5
    int main(){
    int w = 5 ;

    set_tris_b(0x00);
    set_tris_a(0x1F);

    while(true){
    w = porta + w;
    portb = w;
    }
    }

    This is the circuit i made
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    What does "doesn't works well" mean?????

    When you go to the doctor with a problem, do all you tell him is "I am not doing well" and that is it? How is he/she suppose to know what your problem is?

    And what have you done to troubleshoot the problem you are having?


    What language is this? Looks line Pascal?? What compiler?


    One issue I see is it looks like your main function would end at some time. The main function should always have an infinite loop of some kind. It should never end.
     
  3. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    How about those code-tags... :)
    Code ( (Unknown Language)):
    1.  
    2. #include <16f84a.h>
    3. #fuses xt, nowdt
    4. #use delay(clock= 4000000)
    5. #byte portb = 6
    6. #byte porta = 5
    7. int main(){
    8. int w = 5 ;
    9.  
    10. set_tris_b(0x00);
    11. set_tris_a(0x1F);
    12.  
    13. while(true){
    14. w = porta + w;
    15. portb = w;
    16. }
    17. }
    In the schema there is no crystal, and in the code line #2, you tell the compiler it's and XT-clock source. Where did you hide it?

    Try to explain exactly what you have done and what you want.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    richieeqc:

    The first beginner problem (meaning I havn't had it in 2 weeks) people have is neglecting to turn off analog functions on port pins, but this chip doesn't have any. So that's not your problem.

    Do you have a crystal or an RC on the PIC? If not, look at section 6.2 "Oscillator Configurations" in the data sheet. As nerdegutta points out, without a clock source the code can't run.

    Now if you have all that, there is a problem in your loop code. I don't use the CCS C compiler myself but C is C. You initialize w with the value 5 so the test loops works correctly the first time, but after that it starts using garbage (ie, the last value if w) for the increment instead of 5.

    So instead of this:
    Code ( (Unknown Language)):
    1.   while(true){
    2.     w = porta + w;
    3.     portb = w;
    4.   }
    5.  
    You could do this:
    Code ( (Unknown Language)):
    1.   while(true){
    2.     w = porta + 5;
    3.     portb = w;
    4.   }
    5.  
    You could also do this, it doesn't require the w variable:
    Code ( (Unknown Language)):
    1.   while(true){
    2.     portb = porta + 5;
    3.   }
    4.  
    And welcome to the forums!
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Is "true" a defined constant? I don't do PICs.

    You can use while(1)

    And

    #byte porta = 5

    shouldn't this be

    byte porta = 5
     
  6. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    When I was doing Turbo Pascal and Delphi, I used true / false as boolean statements. But after I started to learn and use C, I use 1 for true and 0 for false.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Yes it looks like Pascal to me but I don't know if we will ever know for sure, as it looks like the OP is not responding. We are wasting our time till we get a response from the OP.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Of course you can simply add to your header file:

    #define true 1
    #define false 0
    #define TRUE 1
    #define FALSE 0
     
  9. richieeqc

    Thread Starter New Member

    Nov 9, 2012
    2
    0
    Thanks, the problem was in the loop while. I'm using isis proteus, so i don't need to put a crystal, but obviously i gonna use it, when i gonna make it in breadboard.
    So, thanks for the help, i hope further progress in this fascinating area.
     
Loading...