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

18f452 Usart question

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



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

18f452 Usart question
PostPosted: Wed Jul 13, 2011 1:40 am     Reply with quote

hello guys,

I'm run into a problem:
I want to get a function: PC send the 00--ff, when the PIC get them, then put them on the LED.

My program as follow:
Code:

#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,rcv=PIN_C7)

#define  PORTA 0xF80
#define  PORTB 0xF81
#define  PORTC 0xF82
#define  PORTD 0XF83

#bit col8=PORTC.0
#byte RD=PORTD   //LED display

#use fast_io(C)
#use fast_io(D)


#define uchar unsigned char

               
void main()
{
   uchar i;
   col8=1;
   RD=0;
   set_tris_c(0x00);
   set_tris_d(0x00);
  while(1)
  {
   if(getc())
     i=getc();
     RD=putc(i);
  }

}


What wrong with my program?
How to modify it ?
Thank you very much!
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 2:10 am     Reply with quote

Obvious thing is that 'putc', _does not return a value_. It is an output function, not an input function, return is 'undefined' (pure luck).....
Also bracketting...
getc, gets the character - once got it is gone for ever - you need to use 'kbhit' to see if a character is waiting.
Code:

  while(1) {
     if(kbhit()) {
        i=getc(); //character is waiting, so read it.
        RD=i; //Copy to the output port
        putc(i); //and to the serial
     }
  }


Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 2:17 am     Reply with quote

One other problem. TRIS. You are selecting fast_io (why?.....), and setting the tris incorrectly for the serial port - read the data sheet and set the serial port bits correctly.
Simpler, let the compiler do it's job:
Code:

#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,rcv=PIN_C7)

#define COL8=PIN_C0
typedef unsigned char uchar
               
void main(void) {
   uchar i;
   output_high(COL8);
   output_d(0);
   do {
     if(kbhit()) {
        i=getc(); //character is waiting, so read it.
        output_d(i); //Copy to the output port
        putc(i); //and to the serial
     } while(TRUE); //avoids compiler error message
}


Best Wishes
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 2:18 am     Reply with quote

Ttelmah wrote:
Obvious thing is that 'putc', _does not return a value_. It is an output function, not an input function, return is 'undefined' (pure luck).....
Also bracketting...
getc, gets the character - once got it is gone for ever - you need to use 'kbhit' to see if a character is waiting.
Code:

  while(1) {
     if(kbhit()) {
        i=getc(); //character is waiting, so read it.
        RD=i; //Copy to the output port
        putc(i); //and to the serial
     }
  }


Best Wishes


Thank you for your advice!


but ,when i use the
RD=putc(0x01); //means LED(RD port) will light

The first LED light,that why ?
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 2:40 am     Reply with quote

Ttelmah wrote:
One other problem. TRIS. You are selecting fast_io (why?.....), and setting the tris incorrectly for the serial port - read the data sheet and set the serial port bits correctly.
Simpler, let the compiler do it's job:
Code:

#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,rcv=PIN_C7)

#define COL8=PIN_C0
typedef unsigned char uchar
               
void main(void) {
   uchar i;
   output_high(COL8);
   output_d(0);
   do {
     if(kbhit()) {
        i=getc(); //character is waiting, so read it.
        output_d(i); //Copy to the output port
        putc(i); //and to the serial
     } while(TRUE); //avoids compiler error message
}


Best Wishes



when I compile this code, and download the target, but I send the f --->PIC by usart PC terminal soft, the target's LED is not light.

I don't know why ? so pls help me !!!!

thank you and appreciate
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 3:13 am     Reply with quote

leevise wrote:
Ttelmah wrote:
One other problem. TRIS. You are selecting fast_io (why?.....), and setting the tris incorrectly for the serial port - read the data sheet and set the serial port bits correctly.
Simpler, let the compiler do it's job:
Code:

#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,rcv=PIN_C7)

#define COL8=PIN_C0
typedef unsigned char uchar
               
void main(void) {
   uchar i;
   output_high(COL8);
   output_d(0);
   do {
     if(kbhit()) {
        i=getc(); //character is waiting, so read it.
        output_d(i); //Copy to the output port
        putc(i); //and to the serial
     } while(TRUE); //avoids compiler error message
}


Best Wishes



when I compile this code ,and download the target ,but I send the f --->PIC by usart PC terminal soft ,the target's LED is not light.

I don't know why ? so pls help me !!!!

thank you and appreciate



new code

while(1)
{

if(kbhit())
{
i=getc(); //character is waiting, so read it.
RD=i; //Copy to the output port

}
else
putc(0x0d); //and to the serial
delay_ms(500);
}

}

the PIC target doesn't work !!!!

why ????

help me ! thank you !!
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 7:30 am     Reply with quote

pls help me ! appreciate
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 7:45 am     Reply with quote

Check the updated code as follows, pls help me to analyse it. Because it doesn't work! Thank you for your help!!
Code:

#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)

#define  PORTA 0xF80
#define  PORTB 0xF81
#define  PORTC 0xF82
#define  PORTD 0XF83

#define col8 PIN_C0

#define uchar unsigned char

void main()
{
    uchar i;
   output_high(col8);
   output_d(0x00);

   do {
     if(kbhit())
     {
        i=getc(); //character is waiting, so read it.
        output_d(i);
       }//Copy to the output port
        putc(i); //and to the serial
     } while(1); //avoids compiler error message

 
 
}
 
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 9:44 am     Reply with quote

Have you actually proved:
1) Your PIC is running, and running at the required frequency - LED flash program, verify flash rate is correct.
2) You can getc/putc correctly - print a line to the PC, then receive a character, and do something to it (add 1 for example), and return this. Do you then see the printed line, and when you send 'a', get back 'b'. Prove your hardware is working, before trying to code.

Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 10:39 am     Reply with quote

Also, you have moved the putc(i) outside the kbhit test section of the code. It needs to be _inside_. Otherwise you will be continuously transmitting the same character again and again.

Best Wishes
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 6:42 pm     Reply with quote

Ttelmah wrote:
Also, you have moved the putc(i) outside the kbhit test section of the code. It needs to be _inside_. Otherwise you will be continuously transmitting the same character again and again.

Best Wishes


Thank you for your this advice ! I got successful!

The ok code is as follows:
Code:

#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)
#define col8 PIN_C0

#define uchar unsigned char

void main()
{
   uchar i;
   output_high(col8);
   output_d(0x00);

   do {
     if(kbhit())
      {
        i=getc(); //character is waiting, so read it.
        output_d(i);
        putc(i); //and to the serial
       }
     } while(1); //avoids compiler error message
 
}
 
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Jul 13, 2011 8:33 pm     Reply with quote

Ttelmah wrote:
Have you actually proved:
1) Your PIC is running, and running at the required frequency - LED flash program, verify flash rate is correct.
2) You can getc/putc correctly - print a line to the PC, then receive a character, and do something to it (add 1 for example), and return this. Do you then see the printed line, and when you send 'a', get back 'b'. Prove your hardware is working, before trying to code.

Best Wishes


I have an another question, why it is un-works, when I used the code as follow:


#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)
//#use i2c(MASTER,SDA=PIN_C4,SCL=PIN_C3,ADDRESS=0X9A,FORCE_HW)//I2C
#define PORTA 0xF80
#define PORTB 0xF81
#define PORTC 0xF82
#define PORTD 0XF83

#bit col8=PORTC.0
#byte RD=PORTD //equal the syntax "#byte RD=0xf83"

#define uchar unsigned char

void main()
{
uchar i;

col8=1; //equal the "output_high(col8);"
RD=0; // equal the"output_d(0x00);",but this code need decimal
set_tris_d(0x00);


do {
if(kbhit())
{
i=getc(); //character is waiting, so read it.
RD=i;
putc(i); //and to the serial
}

} while(1); //avoids compiler error message



}



PC terminal soft can receive the data,but the LED doesn't light.

why ?
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Thu Jul 14, 2011 3:17 am     Reply with quote

I succeed, the code is as follow:
Code:

// Example 2
#include <18f452.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)

#define  PORTA 0xF80
#define  PORTB 0xF81
#define  PORTC 0xF82
#define  PORTD 0XF83

#bit col8=PORTC.0
#byte RD=PORTD  //相当于 #byte RD=0xf83
#define uchar unsigned char

void main()
{
   uchar i;
   
   col8=1;       //output_high(col8);
   RD=0;       //output_d(0x00);
   set_tris_d(0x00);    //RDdirection
  set_tris_c(0x80);   //set PIN_C7 input   
   do {
     if(kbhit())
      {
        i=getc(); //character is waiting, so read it.
        putc(i); //and to the serial
        RD=i;
       
      }
       
     } while(TRUE); //avoids compiler error message
}             
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Thu Jul 14, 2011 6:33 am     Reply with quote

output_d, doesn't care what numeric format you use.
output_d(23);

decimal number. Works just as well as

output_d(0x17);

Read some basic C manuals on numeric representations. You can use binary, decimal, hex, octal etc.. The point about using hex for port I/O, is it makes it easier to see what bits are going where on the port.

Port C0, and RD, are set as an input, if you use the #bit format as shown. Hence the outputs does nothing.

_Use_ the compiler functions. You are like a person with a motor car, trying to steer it, by sticking his foot out the door. Though it probably can be made to work (within the limitations of shoes, and leg strength), it rather misses the point of having a steering wheel.....

Best Wishes
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