# Doubt about microcontroller program that add bits

#### richieeqc

Joined Nov 9, 2012
2
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

#### Attachments

• 82.5 KB Views: 15

#### spinnaker

Joined Oct 29, 2009
7,830
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.

#### nerdegutta

Joined Dec 15, 2009
2,684
How about those code-tags...
Rich (BB code):
#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;
}
}
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.

#### ErnieM

Joined Apr 24, 2011
8,353
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:
Rich (BB code):
  while(true){
w = porta + w;
portb = w;
}
You could do this:
Rich (BB code):
  while(true){
w = porta + 5;
portb = w;
}
You could also do this, it doesn't require the w variable:
Rich (BB code):
  while(true){
portb = porta + 5;
}
And welcome to the forums!

#### MrChips

Joined Oct 2, 2009
28,092
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

#### nerdegutta

Joined Dec 15, 2009
2,684
Is "true" a defined constant?
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.

#### spinnaker

Joined Oct 29, 2009
7,830
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.
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.

#### MrChips

Joined Oct 2, 2009
28,092
Of course you can simply add to your header file:

#define true 1
#define false 0
#define TRUE 1
#define FALSE 0