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

rs232 need help

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



Joined: 12 Mar 2006
Posts: 20

View user's profile Send private message

rs232 need help
PostPosted: Sat Apr 01, 2006 2:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 01, 2006 3:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 01, 2006 6:53 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 01, 2006 8:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 01, 2006 10:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 01, 2006 10:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 03, 2006 3:40 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Mon Apr 03, 2006 5:52 am     Reply with quote

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.
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