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

Problems with the 16F628 USART

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dlc@frii.com



Joined: 05 Nov 2003
Posts: 23

View user's profile Send private message

Problems with the 16F628 USART
PostPosted: Tue Feb 21, 2006 1:15 pm     Reply with quote

Hi all,

I've set up the USART of my program with a 4MHz internal OSC to run 9600 bps which is supported according to the PIC data sheet. However, the data sent is being corrupted. I use TeraTerm on my box to read the data. I get the first four or five characters correct, the rest is then just garbage. Can anyone tell me what I'm missing?

Much appreciated. Here is the <short> code, the same code I've posted in another query, but this is a different question so I thought it deserved another thread.

thanks,
DLC

Code:



#include <16F628.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,NOCPD,NOBROWNOUT,MCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)
#use fast_io(A)
#use fast_io(B)

#define MAXBIT 13         //5 address, 7 control and one junk timing
int rise[MAXBIT];
int rW=0;
int gotOne = 0;

//save_w and save_status are at 7f and 7e which are available in ALL banks
int save_w;
#locate save_w=0x7f
int save_status;
#locate save_status=0x7e
#byte status = 3
#byte TMR1L = 0x0E
#byte TMR1H = 0x0F
#byte PIR1 = 0x0C
#byte CCPR1H = 0x16

#int_global
void isr(void)
{
   #asm
      //store current state of processor
      MOVWF save_w
      SWAPF status,W
      MOVWF save_status
      BCF   status,5                              //Set to page 0 for SFR's
      BCF   status,6
   #endasm
   
      rise[rW++] = CCPR1H;                        //save the pulse time
      if (rW == MAXBIT)
      {
         rW = 0;
   }
   gotOne = 1;
      TMR1L = 0;
      TMR1H = 0;
   
      #asm
      // restore processor and return from interrupt
      SWAPF save_status,W
      MOVWF status
      SWAPF save_w,F
      SWAPF save_w,W
      BCF PIR1,2
      #endasm 
}                               

void main()
{
   int x;
   int n=0;
   
   set_tris_B(0x03);
   set_tris_A(0x00);

   //Turn off comparator
   setup_comparator(NC_NC_NC_NC);

      setup_ccp1(CCP_CAPTURE_FE);    // Configure CCP1 to capture falling edge detection
      setup_timer_1(T1_INTERNAL);    // Start timer 1

   for (x=0;x<MAXBIT;x++)
      rise[x]=0;
      
      enable_interrupts(INT_CCP1);   // Setup interrupt on falling edge
      enable_interrupts(GLOBAL);

   printf("SONY IR decoder\n\r");
   
      while(TRUE)
   {
         delay_ms(1000);
         if (n)
         {
            output_low(PIN_A1);
            n=0;
       }
       else
       {
          output_high(PIN_A1);
          n=1;
      }
         if (gotOne)
         {
            for (x=0;x<MAXBIT;x++)
               printf("\n\r%u %u counts ",x, rise[x]);
         gotOne = 0;
      }
      }
}

_________________
--
------------------------------------
Dennis Clark dlc@frii.com
http://www.techtoystoday.com
------------------------------------
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 21, 2006 1:35 pm     Reply with quote

According to the 16F628 data sheet, in Table 17-4, Parameter 4,
it says the nominal 4 MHz internal RC oscillator can vary from
3.65 to 4.28 MHz. That's at least 7 or 8% off. That's beyond
the 3% maximum allowed. To be safe, you should limit the
error to 1%.
Guest








PostPosted: Tue Feb 21, 2006 3:02 pm     Reply with quote

PCM programmer wrote:
According to the 16F628 data sheet, in Table 17-4, Parameter 4,
it says the nominal 4 MHz internal RC oscillator can vary from
3.65 to 4.28 MHz. That's at least 7 or 8% off. That's beyond
the 3% maximum allowed. To be safe, you should limit the
error to 1%.


I noted that, but I've never had a PIC be THAT far off that it corrupted the serial data stream. However, I tend to write my own bit-bang code so perhaps my code is more forgiving than the USART is.

thanks,
DLC
dlc@frii.com



Joined: 05 Nov 2003
Posts: 23

View user's profile Send private message

PostPosted: Fri Feb 24, 2006 10:14 am     Reply with quote

PCM programmer wrote:
According to the 16F628 data sheet, in Table 17-4, Parameter 4,
it says the nominal 4 MHz internal RC oscillator can vary from
3.65 to 4.28 MHz. That's at least 7 or 8% off. That's beyond
the 3% maximum allowed. To be safe, you should limit the
error to 1%.


You nailed that one. I assumed that the F628 had a calibration byte like the 12F and 12C devices to tweak it in. I find no mention of a calibration byte nor a calibration register in the F628. A 10MHz ceramic resonator later and my serial ops work fine.

thanks,
DLC
_________________
--
------------------------------------
Dennis Clark dlc@frii.com
http://www.techtoystoday.com
------------------------------------
yes
Guest







PostPosted: Fri Feb 24, 2006 2:47 pm     Reply with quote

I had same problem with 4 Mhz....i didnt tested at 10MHz...
I had changed registers value to control tha operation, but nothing good..

628A does not that problem...INT_RC works very well...

Bye
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