|
|
View previous topic :: View next topic |
Author |
Message |
handsprince
Joined: 12 Mar 2006 Posts: 20
|
rs232 need help |
Posted: Sat Apr 01, 2006 2:50 am |
|
|
i had wrote a program that will receive signal from lm35dz then show result on LCD and also it can send the result to VB program i wrote.
but my problem is the PIC program won't run when it not connect to my vb program. can anyone teach me how to adjust my PIC program,make it always run and when i run my VB program it will auto send the result via rs232. below is my code :
Code: |
#include <16f877.h>
#fuses hs, nowdt, nolvp, noprotect, noput, nobrownout,nocpd, nodebug, nowrt
#device adc=10
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=n, bits=8)
#include <lcd.c>
char test;
float temp,volt,temperature;
void main()
{
set_tris_a(0xff);
set_tris_e(0xff);
setup_adc_ports(all_analog);
setup_adc(adc_clock_div_32);
lcd_init();
lcd_putc("\fvolt:");
lcd_putc("\ntemp:");
do
{
set_adc_channel(5);
delay_us(40);
temp = read_adc();
volt = (temp*5)/1023;
temperature = 20*volt;
lcd_gotoxy(7,1);
printf(lcd_putc,"%2.2f",volt);
lcd_gotoxy(7,2);
printf(lcd_putc,"%2.2f",temperature);
delay_ms(1000);
do
{
getch();
printf("%2.2f",volt);
getch();
printf("%2.2f",temperature);
}while(true);
}while (true);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 01, 2006 3:20 am |
|
|
The problem is that "getch()" waits for a character.
You can fix this by checking to see if a character is available
before you call the getch() function. (or before you enter a
loop that contains the getch() function).
Use the kbhit() function to do this. kbhit() will return 1 if a
character is available. If no character is ready, then it will return 0. |
|
|
handsprince
Joined: 12 Mar 2006 Posts: 20
|
|
Posted: Sat Apr 01, 2006 6:53 am |
|
|
thank for your reply PCM programmer, but can you show me some example about kbhit(), because i'm a bit slow to catch up... thank, hope i din't bring trouble for you. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Apr 01, 2006 8:01 am |
|
|
Page 125 in the last couple of versions of the manual shows two examples of how to use kbhit() |
|
|
handsprince
Joined: 12 Mar 2006 Posts: 20
|
|
Posted: Sat Apr 01, 2006 10:02 pm |
|
|
i'm still don't know to use the kbhit(), anyone can teach me how to modified my program by using kbhit(). thank |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 01, 2006 10:58 pm |
|
|
No. You want us to do your homework for you. Mr. Yeatman is
an instructor and spotted this. Instead of saying so, he politely
showed you how to find examples in the CCS manual.
If you want more examples, look at the while() loop at the
very end of the CCS example file, EX_LOGGER.C.
You can find the file in this folder: c:\Program Files\PICC\Examples |
|
|
handsprince
Joined: 12 Mar 2006 Posts: 20
|
|
Posted: Mon Apr 03, 2006 3:40 am |
|
|
i had modified my program, it's correct???
below is the code:
[code]
#include <16f877.h>
#fuses hs, nowdt, nolvp, noprotect, noput, nobrownout,nocpd, nodebug, nowrt
#device adc=10
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=n, bits=8)
#include <lcd.c>
char test;
float temp,volt,temperature;
void main()
{
set_tris_a(0xff);
set_tris_e(0xff);
setup_adc_ports(all_analog);
setup_adc(adc_clock_div_32);
lcd_init();
lcd_putc("\fvolt:");
lcd_putc("\ntemp:");
do{
if(kbhit())
{
set_adc_channel(5);
delay_us(40);
temp = read_adc();
volt = (temp*5)/1023;
temperature = 20*volt;
getch();
printf("%2.2f",volt);
lcd_gotoxy(7,1);
printf(lcd_putc,"%2.2f",volt);
getch();
printf("%2.2f",temperature);
lcd_gotoxy(7,2);
printf(lcd_putc,"%2.2f",temperature);
};
if(!kbhit())
{
set_adc_channel(5);
delay_us(40);
temp = read_adc();
volt = (temp*5)/1023;
temperature = 20*volt;
lcd_gotoxy(7,1);
printf(lcd_putc,"%2.2f",volt);
lcd_gotoxy(7,2);
printf(lcd_putc,"%2.2f",temperature);
delay_ms(2000);
};
}while(true);
} |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Apr 03, 2006 5:52 am |
|
|
No, it is not correct if you want it to do something without the VB program. For every getch() you would need a kbhit() otherwise your program is going to wait for a character as PCM has already pointed out. Also, you should only put code that should wait for the VB program inside the kbhit(), otherwise, the rest of the program will not be executed until a char comes in. |
|
|
|
|
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
|