View previous topic :: View next topic |
Author |
Message |
arys
Joined: 09 Mar 2007 Posts: 31
|
Very simple Q about output |
Posted: Thu Jan 17, 2008 7:01 pm |
|
|
Hi.
I read the manual but still do not understand how to use.
the question is, what is the function to set the port as output and how to use it when to give it high at certain pin/bit.
|
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Thu Jan 17, 2008 7:26 pm |
|
|
for information, i'm using 16f873 and max232 to interface to PC. Is it possible to send and receive 2 variables from PC to PIC and run at the same time. if not, how to change 2 variables in PIC from VB synchronizely? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Jan 18, 2008 8:35 am |
|
|
The usual output functions, such as Output_x(), automatically handle the port direction register.
If you need to use #use fast_io or #use fixed_io to eek out a little more speed, then you will have to use something like Set_tris_x() to handle the direction register yourself. But most code just uses the automatic functions. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Fri Jan 18, 2008 9:09 am |
|
|
The output_high(pin_xxx) and output_low(pin_xxx) work with individual pins.
Ex. output_low(PIN_B0)
You will need to understand this basic I/O first and would be advised to try turning on and off LED's before leaping into RS232. For RS232 you might want to repeat to yourself several times over that RS232 is asynchronous. The key word is asynchronous. Asynchronous means from the point of view of your code data can arrive at any time. There are only two ways to go with this.
1) Your PIC must be capable ( speed per instruction multiplied by the number of instructions)of doing its work within the time it takes a character to be transmitted to the PIC ( baud rate). This means a very short loop to get back to the next getc()
2) You use an isr tied to int_rda ..This is the preferred method since the isr can manage a circular buffer. The buffer acts like a shock absorber on a car it smooths out any bursts of incoming RS232 chars.
Receiving and sending can overlap but you have to be aware of overall I/O demands. Ex suppose you receive a char and the do a silly printf(
" My PIC just received char= %c",c) which outputs 20 plus chars for every received char then inbound chars can only be received at a throughput of less than 1/20th of the output ( in this case printf ) to avoid overruns. |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Sat Jan 19, 2008 9:43 pm |
|
|
thanks for the ideas. For beginning I start with very simple code. I try to send byte data from VB (which I use mscomm1.output=chr$(XX)). and received by PIC trough port B (which I set up Port B as output byte. Every bit represents different task.) here I paste the code. Code: | void main(void)
{char z;
set_tris_b(0x00); //set port b as output
output_b (0x00); //init all output pins as low
while(1)
{
z=getc();
if (z==0x128)
{ output_b(0x128); //set only pinB7 high for 5s
delay_ms(5);
output_b(0x00); } //set pinB7 low
if (z==0x02)
{ output_b(0x32); //set only pinB5 high for 5s
delay_ms(5);
output_b(0x00); } //set pinB5 low
}
} | .
what happen is ONLY pinb5 turn ON in 1s and nothing happen in pin B7. I dont know which one problem VB or CCS code. hope you can help me. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 19, 2008 10:00 pm |
|
|
Quote: | char z;
z=getc();
if (z==0x128)
{ output_b(0x128);
|
Download the CCS compiler manual:
http://www.ccsinfo.com/downloads/CReferenceManual.pdf
Look in the section on the getc() function. What does it say
is the size of the returned value ? What size is a value of 0x128 ?
Look in the section on data types, which lists the min and max
values that each data type can hold. What is the max size that a
char can hold ? Can you compare a char to 0x128 ?
Look in this section in the CCS manual.
Quote: | DATA DEFINITIONS
Basic and Special types
|
Also look up the output_b() function, and look at the size of the
parameter that you are allowed to give it. Compare that to the
value that you're giving it (0x128). |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Sun Jan 20, 2008 6:58 pm |
|
|
thanks. thats clear.
since getc() return character but I need the binary (ascii) number. so, i have to convert the 8bit character to 8bit integer. is it correct?
I look into sprintf(), printf() but I dont have idea how to do that. Or may be I choose wrong method. |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Sun Jan 20, 2008 7:50 pm |
|
|
but when i change to this code, only pinB5 ON only for 1s.
Code: | set_tris_b(0x00); //set port b as output
output_b (0x00); //init all output pins as low
while(1)
{
if (getc()=='a')
{ output_b(0x128); //set only pinB7 high for 5s
delay_ms(5);
output_b(0x00); } //set port b low
if (getc()=='b')
{ output_b(0x32); //set only pinB5 high for 5s
delay_ms(5);
output_b(0x00); } //set port b low |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 20, 2008 8:07 pm |
|
|
You can't do this. Port B is 8 bits wide. 0x128 is 9 bits wide. The top
bit will be thrown away. |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Mon Jan 21, 2008 3:40 am |
|
|
thanks PCM. I've change to output_b(0b00000001). then its OK. |
|
|
|