View previous topic :: View next topic |
Author |
Message |
nguyentuandang
Joined: 06 Oct 2012 Posts: 5
|
assign value register to port |
Posted: Sat Oct 06, 2012 8:52 am |
|
|
I write code for pic16f877a. I use button in pin A0 and pin B0 and try change state pin when push button but can't. Someone please help me.
Code: |
#include <16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use delay(clock=12000000)
#byte ca = 30
#bit ca0 =0x30.0
#bit ca1 =0x30.1
#bit ca2 =0x30.2
#bit ca3 =0x30.3
#bit ca4 =0x30.0
#bit ca5 =0x30.1
#bit ca6 =0x30.2
#bit ca7=0x30.3
main() {
while(1)
{
int8 e;
ca0 = input(pin_a0) ;
ca1 = 1;
ca2 = 1;
ca3 = 1;
ca4 = 1;
ca5 = 1;
ca6 = 1;
ca7 = 1;
e = ca;
output_b(e);
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Oct 06, 2012 2:14 pm |
|
|
To tell you frankly, I have no clue as to what you are doing.
Code: | #byte ca = 30
#bit ca0 =0x30.0 | Here you mix decimal address location 30 and hexadecimal location 0x30. Can't be right and most likely is the reason why your program fails.
Then, why did you choose 0x30? It is an unused address in RAM. Was that your intention? Did you want to reserve a free byte in RAM? If yes, then this will work but is not the best way to achieve your goal because the memory location is not reserved, i.e. the compiler might decide to locate other variables at the same location. This kind of memory location is how other compilers handle memory location in the PIC processors but in CCS you can do it much simpler:
Surprise! This is what you did for variable 'e'.
That's why I don't understand your intentions. You are mixing different techniques.
Code: | #bit ca4 =0x30.0
#bit ca5 =0x30.1
#bit ca6 =0x30.2
#bit ca7=0x30.3 | Here you are mapping the 4-7 variables to the same addresses as the 0-3 variables. Doesn't make sense. Another error?
Here is the more portable way to do it in proper C: Code: | #include <16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use delay(clock=12000000)
struct bit_field{
int b0:1;
int b1:1;
int b2:1;
int b3:1;
int b4:1;
int b5:1;
int b6:1;
int b7:1;
};
union my_port {
int8 port_byte;
struct bit_field bits;
};
void main()
{
int8 e;
union my_port ca;
while(1)
{
ca.bits.b0 = input(PIN_A0) ;
ca.bits.b1 = 1;
ca.bits.b2 = 1;
ca.bits.b3 = 1;
ca.bits.b4 = 1;
ca.bits.b5 = 1;
ca.bits.b6 = 1;
ca.bits.b7 = 1;
e = ca.port_byte;
output_b(e);
}
} |
|
|
|
nguyentuandang
Joined: 06 Oct 2012 Posts: 5
|
|
Posted: Sat Oct 06, 2012 7:52 pm |
|
|
Thanks I will try but 30h is not reserved address. Address ca4,ca5,ca6,ca7 later I repair it but not change. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Oct 07, 2012 2:37 am |
|
|
nguyentuandang wrote: | but 30h is not reserved address. | I know, but that is a potential problem. When your program gets bigger the compiler will need more RAM and could start using your 30h location. To reserve memory for your exclusive use you have to use #locate instead of #byte.
Best is to not use hard coded memory locations at all. The compiler is much better in handling this and results in easier to read code as well. Here is another example program not using unions and structs. Easier to read for a small program like yours. When you have more constructs like this I like my original posted program better. Code: | #include <16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use delay(clock=12000000)
int8 ca;
#bit ca0 =ca.0
#bit ca1 =ca.1
#bit ca2 =ca.2
#bit ca3 =ca.3
#bit ca4 =ca.4
#bit ca5 =ca.5
#bit ca6 =ca.6
#bit ca7 =ca.7
void main()
{
int8 e;
while(1)
{
ca0 = input(PIN_A0) ;
ca1 = 1;
ca2 = 1;
ca3 = 1;
ca4 = 1;
ca5 = 1;
ca6 = 1;
ca7 = 1;
e = ca;
output_b(e);
}
} |
|
|
|
nguyentuandang
Joined: 06 Oct 2012 Posts: 5
|
|
Posted: Sun Oct 07, 2012 3:16 am |
|
|
I agree I'm beginner. I'm looking code for assign value bit port. You help me thanks so much |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Sun Oct 07, 2012 8:43 am |
|
|
tip #1. While your project is open, press F11, and the CCS HELP files come up ! Tons of important information and tips on how to use CCS C.
tip #2. Open and read some of the examples that CCS supplies. Probably 75% of what you want to do ..they've already done ! While the filename may not be what you want, the code inside may prove useful.
hth
jay |
|
|
|