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

RS-232 PIC 12F675

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







RS-232 PIC 12F675
PostPosted: Fri Aug 28, 2009 2:06 pm     Reply with quote

Hello, I'm working on my first PIC project, and it's the time to RS-232 using a MAX232.
I wrote this code:
Code:

#include<12F675.h>
#fuses XT,NOWDT,NOCPD,NOPROTECT,MCLR,NOPUT,INTRC_IO
#use delay(clock=4000000)
//#use rs232(baud=2400,xmit=PIN_A2,rcv=PIN_A1)
#use rs232(baud=9600,xmit=PIN_A2,rcv=PIN_A1)

#define LED1 PIN_A0

void config(void){
      set_tris_a(0x02);

      setup_comparator(NC_NC_NC_NC);

//      setup_uart(9600);
}


int encendido;

void main(void){
      int x;
      encendido=0;

      output_low (LED1);
      config();

      do{
         x=getc();

         switch(x){
               case 49:{ //se recibe un 1
                  if(encendido==1){
                     output_low(LED1);
                     printf("L1Apa");
                     encendido=0;
                  }else{
                     output_high(LED1);
                     encendido=1;
                     printf("L1Enc");
                  }
               };
               break;
          }
      }while(TRUE);
}


The compilation is ok and the LED1 is working as the code says, but the problem is 'printf(""L1Enc")' and printf("1!Apa"). Sometimes it works and other it doesn't works.
Even on one execution, every time I Push '1' the Led switch, but only a few times the text apear on the screen.
OK thank you. See you soon.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 28, 2009 3:46 pm     Reply with quote

I re-wrote your program to make it easier to understand. I fixed some
problems that you had, such as using both the XT and INTRC_IO fuses.
(You should only use one oscillator fuse). I also improved the formatting
to make it easier to read.

It works. If I press the '1' key on the PC I get this output in the
TeraTerm window:
Quote:

L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc

Also the LED flashes on and off.

Code:

#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_A2,rcv=PIN_A1)

#define LED1 PIN_A0

//=======================
void main(void)
{
int8 c;
int8 state;

output_low(LED1);

state = FALSE;

while(1)
  {
   c = getc();

   if(c == '1')
     {
      if(state)
        {
         output_low(LED1);   
         printf("L1Apa\n\r");
         state = FALSE;
        }
      else
        {
         output_high(LED1);   
         printf("L1Enc\n\r");
         state = TRUE;
        }
     }
  }     

}
elcesal
Guest







PostPosted: Mon Aug 31, 2009 8:28 am     Reply with quote

I modified my code and I have the same problem, sometimes the sentence L1Enc or L1Apa appears but mucho other, the led goes off or it goes on with no line on terminal.


PCM programmer wrote:
I re-wrote your program to make it easier to understand. I fixed some
problems that you had, such as using both the XT and INTRC_IO fuses.
(You should only use one oscillator fuse). I also improved the formatting
to make it easier to read.

It works. If I press the '1' key on the PC I get this output in the
TeraTerm window:
Quote:

L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc
L1Apa
L1Enc

Also the LED flashes on and off.

Code:

#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_A2,rcv=PIN_A1)

#define LED1 PIN_A0

//=======================
void main(void)
{
int8 c;
int8 state;

output_low(LED1);

state = FALSE;

while(1)
  {
   c = getc();

   if(c == '1')
     {
      if(state)
        {
         output_low(LED1);   
         printf("L1Apa\n\r");
         state = FALSE;
        }
      else
        {
         output_high(LED1);   
         printf("L1Enc\n\r");
         state = TRUE;
        }
     }
  }     

}
Ttelmah
Guest







PostPosted: Mon Aug 31, 2009 9:43 am     Reply with quote

How is your LED connected?.
What you describe, is perhaps typical for a chip that is getting 'spiked', by noise on the supply line. Classic 'reasons' would be insufficient HF decoupling close to the chip, or an LED that is overloading the output pin.

Best Wishes
elcesal
Guest







PostPosted: Tue Sep 01, 2009 7:15 am     Reply with quote

The chip has a 1uF between vss and vcc. The led has 560 ohms resistor.
I'm going to try with the lower baudrate speed.
elcesal
Guest







PostPosted: Tue Sep 01, 2009 7:33 am     Reply with quote

I tried with a baudrate = 300 and I have the same problem. I don't know what can I do...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 12:22 pm     Reply with quote

Are you using the exact program that I posted ? Are you using it with
a MAX232-type chip (to connect to your PC) ? Are you using TeraTerm ?
I suggest that you do these things.

Instructions on how to install TeraTerm:
http://www.ccsinfo.com/forum/viewtopic.php?t=39388&start=18


What is your compiler version ?
Ttelmah
Guest







PostPosted: Tue Sep 01, 2009 3:25 pm     Reply with quote

I'd also query more about the supply.

What is feeding this?.
Where is the capacitor placed relative to the PIC?.
If you are using a voltage regulator, where are it's capacitors placed relative to the regulator?.
What other smoothing is on the rail?.
What is the 'technology' of the 1uF capacitor?. Electrolytic?. If so, try putting a small ceramic or polyester capacitor in parallel with it.

Think of the capacitors on the supply rail a doing two separate jobs. The first is to provide a 'reservoir' to smooth ripple. For this 'bigger is better'. The second though is to provide a _local_ reservoir, to smooth the tiny instantaneous power demands as gates switch, and prevent this causing voltage swings along the supply wires. For this, a capacitor with good HF response is needed. In general, electrolytic types, _do not do this_.
If you look at a PC motherboard, you will see a large number of big capacitors at the supply, but then dozens of small capacitors across the board. These are doing this later job.
It is _vital_ that these are really close to the chips generating the spikes.
You also have a 'third' job, when dealing with amplifiers, or voltage regulators, to avoid oscillation. This is why the manufacturers of chips like the 7805, specify a small capacitor _close_ to the output.

Best Wishes
mvanvliet



Joined: 02 Jun 2009
Posts: 123
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 02, 2009 3:03 am     Reply with quote

I found out that trying a different computer also make sense in the RS-232 signal. Maybe through a different COM IC on the motherboard. If possible you can try a other pc. Is the calibration of the pic still ok?
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