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 support@ccsinfo.com

rs232 and putc on 18F452

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







rs232 and putc on 18F452
PostPosted: Wed Aug 11, 2004 5:44 pm     Reply with quote

Hi,

I'm having trouble with putc() not sending stuff. It's supposed to send an initialisation string upon start, but it skips this. After that, it behaves fine. If the board resets from the WDT, though, the initialisation string *is* sent. An example of this is shown below. I'm not the original author of this code, and I'm new to PIC programming, so if there are any other issues, feel free to point those out as well.

Thanks!
-Shaun

The folowing code produces output like
<lots of 'A's>
send 'x' to PIC
receive 'x'
<WDT resets>
"initAAAAAAA..."

=====
#include <18F452.H>

#bit RCIF=0x0F9E.5 // RS232 (serial) info received
#bit OERR=0x0FAB.1 // overrun error
#bit CREN=0x0FAB.4 // continuous receive enable

#list
#include "Math32.c"
#include <stdlib.h>
#include <string.h>

#fuses HS, NOPROTECT, WDT, PUT, BROWNOUT, NOLVP

#use delay (clock=20000000)

#use rs232 (baud=115200, xmit=PIN_C6, rcv=PIN_C7)

#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)

#define TRIS_A 0xFF // 11111111
#define TRIS_B 0x80 // 10000000
#define TRIS_C 0xD9 // 11011001
#define TRIS_C_SLEEP 0x81 // 10000001
#define TRIS_D_IN 0xFF // 11111111
#define TRIS_D_OUT 0x00 // 00000000
#define TRIS_D_RTC_OUT 0xFC
#define TRIS_D_RTC_IN 0xFE
#define TRIS_E 0x07

#define CF_LED0 PIN_C1
#define CF_LED1 PIN_C2

char loadc();
void led_off();
void led_red();
void led_green();

int main()
{
SET_TRIS_A(TRIS_A); // set TRIS values
SET_TRIS_B(TRIS_B);
SET_TRIS_C(TRIS_C);
SET_TRIS_D(TRIS_D_IN);
SET_TRIS_E(TRIS_E);

led_green();
delay_ms(200);
led_off();
restart_wdt();

// enable_interrupts(GLOBAL);
putc('i'); putc('n'); putc('i'); putc('t');

while(!RCIF){
led_red();
putc('A');
delay_ms(20);
led_off();
delay_ms(80);
restart_wdt();
}
putc(loadc());

while(1);
}

char loadc(){
while (!RCIF) {
restart_wdt();
if (OERR){
CREN=0;
CREN=1;
}
}
return getc();
}

// Fun with LEDs
void led_off(){
OUTPUT_LOW(CF_LED0);
OUTPUT_LOW(CF_LED1);
}

void led_green(){
OUTPUT_LOW(CF_LED1);
OUTPUT_HIGH(CF_LED0);
}

void led_red(){
OUTPUT_LOW(CF_LED0);
OUTPUT_HIGH(CF_LED1);
}
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

PostPosted: Wed Aug 11, 2004 9:42 pm     Reply with quote

Hi....

Try this code below.... it uses a putc and an interrupt handler....
You can use a hyperterminal included with windows such that you can type and check the result....

Code:



#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_A1)
#endif


#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;


#int_rda
void serial_isr() {
   int t;

   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}



void main() {

   enable_interrupts(global);
   enable_interrupts(int_rda);

   output_low(PIN_A2);
   printf("\r\n\Running...\r\n");

               // The program will delay for 10 seconds and then display
               // any data that came in during the 10 second delay

   do {
      delay_ms(10000);
      printf("\r\nBuffered data => ");
      while(bkbhit)
        putc( bgetc() );
   } while (TRUE);
}



I hope this helps....

...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 12, 2004 12:33 pm     Reply with quote

I didn't try to trouble-shoot your whole program, but I have these
comments:

1. You're using the Watchdog Timer, and you're also using some
fairly long delay_ms() statements. You should put in the CCS directive
that tells the compiler to reset the WDT within the software delay loops.
See the changes shown in bold, below:

#use delay (clock=20000000, restart_wdt)

2. You have some direct register access code, in order to clear
overrun errors in the USART. You can tell the compiler to insert
this code by adding the ERRORS directive, as shown in bold below:

#use rs232 (baud=115200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
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