View previous topic :: View next topic |
Author |
Message |
johnpoo
Joined: 23 Mar 2010 Posts: 9
|
help on programming |
Posted: Mon Mar 29, 2010 10:02 pm |
|
|
Code: |
#include <16F877.h>
#use delay(clock=20000000)
#pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,PUT
unsigned char c;
unsigned char data;
void uart_sent()
{
char c;
output_high(PIN_C6);
c= putc(data);
output_low(PIN_C6);
}
void main ()
{
unsigned char data;
while(1)
{
data = 0b00010000;
uart_sent();
data = 0b10010000;
uart_sent();
data = 0b01010000;
uart_sent();
data = 0b11010000;
uart_sent();
data = 0b00110000;
uart_sent();
data = 0b10110000;
uart_sent();
data = 0b01110000;
uart_sent();
data = 0b11110000;
uart_sent();
data = 0b01110000;
uart_sent();
data = 0b10110000;
uart_sent();
data = 0b00110000;
uart_sent();
data = 0b11010000;
uart_sent();
data = 0b01010000;
uart_sent();
data = 0b10010000;
uart_sent();
data = 0b00010000;
uart_sent();
data = 0b11100000;
uart_sent();
data = 0b01100000;
uart_sent();
data = 0b10100000;
uart_sent();;
data = 0b00100000;
uart_sent();
data = 0b11000000;
uart_sent();
data = 0b01000000;
uart_sent();
data = 0b10000000;
uart_sent();
data = 0b00000000;
uart_sent();
data = 0b10000000;
uart_sent();
data = 0b01000000;
uart_sent();
data = 0b11000000;
uart_sent();
data = 0b00100000;
uart_sent();
data = 0b10100000;
uart_sent();
data = 0b01100000;
uart_sent();
data = 0b11100000;
uart_sent();
data = 0b00010000;
uart_sent();
}
}
|
HI, i just expose to this programing. i trying to sent some 8bit value out to PC through the rs233. the program i write seem cannot sent the data out. help pleased ?
[/b] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 29, 2010 10:22 pm |
|
|
Don't write code like that. Write a simple "Hello World" program like this:
Code: |
#include <16F877.H>
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
printf("Hello World\n\r");
while(1);
}
|
This program will send "Hello World" to the PC. |
|
|
johnpoo
Joined: 23 Mar 2010 Posts: 9
|
|
Posted: Tue Mar 30, 2010 12:49 am |
|
|
oh.. thanks a lot . how about i want to do serial communication ? same program apply ? |
|
|
mbge5amw
Joined: 13 Dec 2004 Posts: 15 Location: Yorkshire, UK
|
|
Posted: Tue Mar 30, 2010 4:37 am |
|
|
I notice that you are manually altering PIN_C6 before and after you send data, this is incorrect. You should leave the UART to control this pin.
You may see a similar bit of code in programs that use RS485, instead of RS232, as this has a 3rd control pin that dictates if the transceiver is talking or listening, but for RS232 you just leave tx and rx idle high, then devices will pull them low to indicate certain bits in the transmitted byte, then will leave them high again.
I wonder if the line #pragma use rs232... should also read #use rs232...
I'm not sure, but I think the pragma command will promote that line, so that it is applied before the other pre processor directives. However the use rs232 does need to know what crystal speed you are using.
I'm not sure what you mean by Quote: | how about i want to do serial communication ? |
The printf example given does do serial communication, in that the data is sent out over the serial port. Effectively it will call the putc() routine for each character in teh transmit string.
It will be good to stick to ASCII characters when testing your code and hardware initially as programs like Hyperterminal only show ASCII characters.
Then, once you have proven your connection you can return to using the putc() command on any 8bit value. |
|
|
johnpoo
Joined: 23 Mar 2010 Posts: 9
|
|
Posted: Tue Mar 30, 2010 10:08 pm |
|
|
Thanks, I think I understand from your explanation.
Another question here, How can I show the result on my PC console or the terminal window? |
|
|
mbge5amw
Joined: 13 Dec 2004 Posts: 15 Location: Yorkshire, UK
|
|
Posted: Wed Mar 31, 2010 1:58 am |
|
|
I think the link below should explain how to use hyperterminal to talk to your PIC.
http://www.ccsinfo.com/forum/viewtopic.php?t=9253&start=1
Here we see the problem with 'search':
If you know what keywords to put in your search then you can easily find the answer, however that probably means you already know the answer to know what to search for!
You also need to be sure that you are altering the signal voltage levels, your PIC uses TTL (0V to vcc) while your PC uses RS232 (+xV to -xV where x is between 5 and 20)
You will need a TTL to 232 converter such as MAX202. The wiring instructions for this will be on it's data sheet.
Hope that helps
Andy |
|
|
|