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

Serial communication between 16f877a and 18f87k22

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



Joined: 21 Aug 2012
Posts: 47

View user's profile Send private message

Serial communication between 16f877a and 18f87k22
PostPosted: Thu Dec 13, 2012 3:18 pm     Reply with quote

Hi. I want to serial communicate between 16f877a and 18f87k22 but i have a problem. 16f877a is receiver and 18f87k22 is sender. When I want to send a variable, the receiver displays the wrong value. I use internal osc in 18f87k22. But when i use 16f877a for sender and receiver its working properly. I think the problem is the internal clock 18f87k22.

This is my code:

Sender:
Code:

#include <18F87K22.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES VREGSLEEP                //Ultra low-power regulator is disabled
#FUSES INTRC_HP                 //LF-INTOSC in High-Power mode during Sleep
#FUSES SOSC_HIGH                //High-power SOSC circuit is selected
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES INTRC_IO                    //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES PLLEN                    //4X HW PLL enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV18                   //Brownout reset at 1.8V
#FUSES ZPBORM                   //Zero-Power BOR
#FUSES WDT_NOSLEEP              //Watch Dog Timer, disabled during SLEEP
#FUSES WDT1048576               //Watch Dog Timer uses 1:1048576 Postscale
#FUSES RTCOSC_T1                //RTCC uses Secondary Oscillator as reference source
#FUSES EXTADDRSFT               //External address is shifted to start at 0x000000
#FUSES ABW8                     //8-bit Address bus
#FUSES BW16                     //16-bit external bus mode
#FUSES NOWAIT                   //Wait selections unavailable for Table Reads or Table Writes
#FUSES ECCPE                    //Enhanced CCP PWM outpts multiplexed with RE6 thorugh RE3
#FUSES MSSPMSK7                 //MSSP uses 7 bit Masking mode
#FUSES MCLR                     //Master Clear pin enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES BBSIZ2K                  //2K words Boot Block size
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOCPD                    //No EE protection
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES VREGSLEEP                //Ultra low-power regulator is disabled
#FUSES INTRC_HP                 //LF-INTOSC in High-Power mode during Sleep
#FUSES SOSC_HIGH                //High-power SOSC circuit is selected
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES PLLEN                    //4X HW PLL enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV18                   //Brownout reset at 1.8V
#FUSES ZPBORM                   //Zero-Power BOR
#FUSES WDT_NOSLEEP              //Watch Dog Timer, disabled during SLEEP
#FUSES WDT1048576               //Watch Dog Timer uses 1:1048576 Postscale
#FUSES RTCOSC_T1                //RTCC uses Secondary Oscillator as reference source
#FUSES EXTADDRSFT               //External address is shifted to start at 0x000000
#FUSES ABW8                     //8-bit Address bus
#FUSES BW16                     //16-bit external bus mode
#FUSES NOWAIT                   //Wait selections unavailable for Table Reads or Table Writes
#FUSES ECCPE                    //Enhanced CCP PWM outpts multiplexed with RE6 thorugh RE3
#FUSES MSSPMSK7                 //MSSP uses 7 bit Masking mode
#FUSES MCLR                     //Master Clear pin enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES BBSIZ2K                  //2K words Boot Block size
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOCPD                    //No EE protection                    //No EE protection                     
#use delay(clock=4m)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,restart_wdt,errors)

#define use_portb_lcd TRUE
#define LCD_ENABLE_PIN PIN_b2
#define LCD_RS_PIN PIN_b0
#define LCD_RW_PIN PIN_b1
#define LCD_TYPE 2
#include <lcd.c>

void main()
{
int ad1=10,ad3=50,ad4=23,ad2=11;

lcd_init();

while(true)
  {
   putc(ad1);
   putc(ad2);
   putc(ad3);
   putc(ad4);

   lcd_gotoxy(1,1);
   printf(lcd_putc,"ad1=%u",ad1);

   lcd_gotoxy(9,1);
   printf(lcd_putc,"ad2=%u",ad2);

   lcd_gotoxy(1,2);
   printf(lcd_putc,"ad3=%u",ad3);

   lcd_gotoxy(9,2);
   printf(lcd_putc,"ad4=%u",ad4);

   delay_ms(20);
  }

}


REC CODE:
Code:

#include <16F877A.h>

#FUSES NOWDT                   
#FUSES HS                       
#FUSES NOPUT                   
#FUSES NOBROWNOUT             
#FUSES NOLVP                   
#FUSES NOCPD                   
#FUSES NOWRT                   
#FUSES NODEBUG                 
#FUSES NOPROTECT                       
                   
#use delay(clock=4m)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,restart_wdt,errors)             

#define use_portb_lcd TRUE
#define LCD_ENABLE_PIN PIN_B2
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
 
#include <lcd.c>

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,restart_wdt,errors)

void main()
{
int ad1,ad2,ad3,ad4;

lcd_init();

while(true){

  ad1=getc();
  ad2=getc();
  ad3=getc();
  ad4=getc();

  lcd_gotoxy(1,1);
  printf(lcd_putc,"ad1=%u",ad1);

  lcd_gotoxy(9,1);
  printf(lcd_putc,"ad2=%u",ad2);

  lcd_gotoxy(1,2);
  printf(lcd_putc,"ad3=%u",ad3);

  lcd_gotoxy(9,2);
  printf(lcd_putc,"ad4=%u",ad4);
  }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 7:41 pm     Reply with quote

Did you look at your #fuses ? You have two full sets of fuses.
You have several different oscillator fuses.

Get rid of all your fuses. Try it with a simple set of fuses:
Code:

#include <18F87K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,CCP2C1,SOSC_DIG,NOPLLEN
#use delay(clock=4M)
 
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Dec 14, 2012 2:43 am     Reply with quote

What's the tolerance on the internal clock?
What's the frequency error that RS232 can tolerate?
(Both are covered in the microchip data sheets)

Mike
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