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

USART PIC16F873

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



Joined: 28 Jul 2013
Posts: 3

View user's profile Send private message

USART PIC16F873
PostPosted: Sun Jul 28, 2013 6:29 pm     Reply with quote

I want to communicate two pics using USART. The code is quite simple but it doesn't work. I think the problem is in the configuration.
The receiver PIC should show in a LCD.
The simulation works perfect but in the real life it doesn't happen.
please help. (Sorry for my english)

The transmitter code is:

Code:
#include <16F873.h>
#include "C:\GSM\EjemploLibro\LCDTx.c"
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   

   int valor;
   lcd_init();
   
   while(1){
      for (valor=0;valor<=10;valor++) {
         putc(valor);
         printf(lcd_putc,"\fEnviando=%1d",valor);
         delay_ms(500);
         }
      }
}


The receiver code is:
Code:
#include <16F873.h>
#include "C:\GSM\EjemploLibro\LCDRx.c"
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#BYTE TRISA=0X85
#BYTE PORTA=0X05

int valor;

#int_RDA
void  RDA_isr(void)
{
valor=getc();
}



void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   bit_clear(TRISA,0);   
   lcd_init();
   
      enable_interrupts(int_RDA);
      enable_interrupts(GLOBAL);
   for(;;) {
      lcd_gotoxy(1,1);
      printf(lcd_putc,"Recibiendo=%1d",valor);
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9240
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jul 28, 2013 6:49 pm     Reply with quote

Some comments
1) LCDs take time to 'initialize'. Normally I delay 500ms before and after doing the LCD_init() function. The first delay allows the LCD module to stabilize and init to 8 bit mode, the second,allows it time to reconfigure to 4bit mode before you send any data to display. Some LCD modules may react faster but ,heck, I can wait 1 second to be sure it's OK.

2) Why do you do this ?

bit_clear(TRISA,0);

What is the purpose? It's not necessary in normal CCS C.

3) Do you have MAX232 type devices between the PICs?

4) The 'numbers' 0-9 in your transmit program are really 'control characters' and not displayable numbers.'7' is aka 'bell','8' is 'backspace'.
Try using 48-57 ( 1-9) or 65-74(A-J).
hth
jay
ocnarf



Joined: 28 Jul 2013
Posts: 3

View user's profile Send private message

PostPosted: Sun Jul 28, 2013 8:17 pm     Reply with quote

Hi Temtronic,

1) The LCD works well, I can read:
"Recibiendo=0"
But the number doesnt increase. The simulation does increase.

2) I really dont know why the line:
bit_clear(TRISA,0);
I copied the program from a book and I really dont understand that line.

3) I dont have MAX232, I direct connect Tx-Rx.

4) If I put valor++; nothing change, still doesnt work.

Thanks..
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Jul 29, 2013 8:08 pm     Reply with quote

Quote:

The simulation does increase.


this is not a real circuit is it ?

it is a simulation which proves NOTHING !!!

proteus /isis right ?

huge waste of time
Sergeant82d



Joined: 01 Nov 2009
Posts: 55
Location: Central Oklahoma

View user's profile Send private message

Re: USART PIC16F873
PostPosted: Mon Jul 29, 2013 11:06 pm     Reply with quote

ocnarf wrote:

The simulation works perfect but in the real life it doesnt happen.


asmboy wrote:

this is not a real circuit is it ?


He appears to be doing both a simulation and a real circuit. Very helpful at times, regardless of how poorly most "commercial" simulations (proteus/isis) perform.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 30, 2013 4:54 am     Reply with quote

Back to the basics:
- ALWAYS post your compiler version number. It might be you are using an old compiler version where this problem has long been solved.
-
Code:
   setup_spi(SPI_SS_DISABLED);
This is a wrong line from the CCS Code Wizard and creates an invalid configuration with unknown side effects. Correct is:
Code:
   setup_spi(FALSE);
Fix this in both your master and slave program.

Quote:
bit_clear(TRISA,0);
I copied the program from a book and I really dont understand that line.
Rolling Eyes
Get rid of this line, it is not needed.
Then also remove these lines:
Code:
#BYTE TRISA=0X85
#BYTE PORTA=0X05


Code:
   int valor;
Declare variables directly at the function start, that is directly after the '{' character. Now you have it in the middle of your program and sometimes the CCS compiler has problems with this. Try to stick with traditional old style C programming to create less problems for yourself.

Quote:

3) I dont have MAX232, I direct connect Tx-Rx.
For a quick test project this is possible, but keep your connecting wires short. 1 - 2 meter maximum at 9600 baud.

Quote:
4) If I put valor++; nothing change, still doesnt work.
That is not what Jay told you to do. The problem is in that the values below 32 have special meanings. We don't know the details of your display, but you are having a lot of potential failure risks here: just imagine there is a control code for sending the LCD to sleep mode. Read Jay's post again and use one of the ranges he recommends. It is only a tiny change to your original program.

One other improvement to your program: ALWAYS add 'ERRORS' to your #use rs232 lines. This will make the CCS compiler to add error clearing code for the UART. Without this the UART will stop working on receiver buffer overflow (that is just after 3 characters without reading the UART fast enough).

Make the changes to your program and post the new versions here.
ocnarf



Joined: 28 Jul 2013
Posts: 3

View user's profile Send private message

PostPosted: Wed Jul 31, 2013 5:04 pm     Reply with quote

Now its working. The problem was the LCD in the transmitter program. Physically I didn't put the LCD and the PIC crashed.
Thanks everyone.
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