View previous topic :: View next topic |
Author |
Message |
leesing2k3
Joined: 08 Nov 2005 Posts: 28
|
16F877A port c as output or input and also rs232? output_c() |
Posted: Sun Jun 04, 2006 12:24 pm |
|
|
Something wrong with input at port c also
Code: |
while(1) {
newval=input_c();
if (newval!=oldval) {
oldval=newval;
printf("%03u|",newval);
}
}
|
This one works like a interrupt functions. If any changes occur, the value will be sent to the pc.
But there are always changes, because port c's pin c6 and c7 are used as rs232.
What do i need to do to prevent input_c() from reading the state of c6 and c7?
-----------------------------------------------------------
I use port c as output and c6 and c7 as rs232.
When i call
output_c(0);
the rs232 stops responding.
do i need to use
set_tris_c(0xC0);
or something else to use the function output_c()?
Last edited by leesing2k3 on Sun Jun 04, 2006 6:48 pm; edited 2 times in total |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Sun Jun 04, 2006 1:10 pm |
|
|
There are a couple of things you need to do. The CCS function output_c() will override and overwrite whatever the USART is doing and thereby destroy your RS232 comms.
The first thing you need to do is tell the compiler that you'll handle the tris (direction) of port C yourself. Unless you specify that you're handling the tris registers yourself, the compiler automatically sets the direction of a port based on whether you're reading from it or writing to it. Near the top of your program, you'll need this line:
Then you'll need to set the tris register for port C. C7 is rx (input), C6 is tx (output). If you want the rest of the port's pins to be outputs, you'll need so set the tris as:
To write to port C all at once basically involves some manipulation. Before doing a write to port C, read it first. You'll need to use the #byte directive to associate a variable with the port C data latch register in the processor's memory. For the 877A, port C lies at address 0x07. In your program you'll need:
The basic idea is:
Code: | current_rs232 = portc & 0xc0; // read port c to find out what C6 and C7 are
new_portc = 0; // this is necessary to make sure bits 6 & 7 are 0...
new_portc = my_portc_data | current_rs232; //...otherwise this OR won't work
portc = new_portc; // finally write out what you want, while not disturbing what C6 and C7 are OR:
output_c(new_portc); // this will do it too |
Hope this helps. |
|
|
leesing2k3
Joined: 08 Nov 2005 Posts: 28
|
|
Posted: Sun Jun 04, 2006 1:16 pm |
|
|
Thank you very much. |
|
|
leesing2k3
Joined: 08 Nov 2005 Posts: 28
|
|
Posted: Sun Jun 04, 2006 6:49 pm |
|
|
Something wrong with input at port c also
Code: |
while(1) {
newval=input_c();
if (newval!=oldval) {
oldval=newval;
printf("%03u|",newval);
}
}
|
This one works like a interrupt functions. If any changes occur, the value will be sent to the pc.
But there are always changes, because port c's pin c6 and c7 are used as rs232.
What do i need to do to prevent input_c() from reading the state of c6 and c7? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Sun Jun 04, 2006 7:29 pm |
|
|
Code: |
while(1) {
newval=input_c() & 0x3f; // note change here
if (newval!=oldval) {
oldval=newval;
printf("%03u|",newval);
}
}
|
|
|
|
leesing2k3
Joined: 08 Nov 2005 Posts: 28
|
|
Posted: Sun Jun 04, 2006 7:32 pm |
|
|
Thank you very much again |
|
|
leesing2k3
Joined: 08 Nov 2005 Posts: 28
|
|
Posted: Sun Jun 04, 2006 7:36 pm |
|
|
newguy wrote: |
The first thing you need to do is tell the compiler that you'll handle the tris (direction) of port C yourself. Unless you specify that you're handling the tris registers yourself, the compiler automatically sets the direction of a port based on whether you're reading from it or writing to it. Near the top of your program, you'll need this line:
|
Do you mean
or
? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Sun Jun 04, 2006 7:41 pm |
|
|
fast_io. The manual lists the differences between the different io modes. I always use fast_io - preference/habit really. |
|
|
|