CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

assign value register to port

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nguyentuandang



Joined: 06 Oct 2012
Posts: 5

View user's profile Send private message

assign value register to port
PostPosted: Sat Oct 06, 2012 8:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 06, 2012 2:14 pm     Reply with quote

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:
Code:
int8 ca;

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

View user's profile Send private message

PostPosted: Sat Oct 06, 2012 7:52 pm     Reply with quote

Thanks I will try Smile 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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 2:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 3:16 am     Reply with quote

I agree Very Happy I'm beginner. I'm looking code for assign value bit port. You help me thanks so much Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 8:43 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group