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

Communication between two PICs
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 7:55 am     Reply with quote

Humberto,

thanks very much for clarification. The Pic1 which will send the signal is a 16F506 - which also doesn't have any hardware UART (as far as I can tell).

For the receiving side Pin2 is fine with me, I will use that for receiving. I will look at the function you described in a second, for now my computer is screwed up and for some strange reson I cannot look at any help files...

Inservi

This idea seems easy enough. I am not sure what the PWD is, but I will look it up. Your post gave me an idea though - it may be the same you were describing - you tell me.

Any digital Pin from Pic1 is connected to one digital Pin on Pic2. Pic 2 is configured with an interrupt for that pin.

Pic1's pin goes high for about 2ms - indicating start of communication. this triggers the interrupt on Pic2. Pic 2 delays for 2ms, and checks the level again. the level is low, so it enters into a while cyle till the level is low. Let's say this is another 2ms. Then Pic 1 sends out a precisly timed pulse for the lenght of my variable. Pic2's timer0 or timer1 is trigerred when the pulse goes high again, and calculates the lenght of the pulse - which should be precisly my variable.

Is this what you were thinking about?

Humberto, Guest, what is your opinion about this idea? Would this be a shorter code to write than the one with the serial communication? Will this give me the same precision?

Thanks guys, I really appreciate your help.
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 8:58 am     Reply with quote

Ok, so let's summarize what we know till now:

Here is the modified code:

Code:
#include <12F629.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                   //Internal clock
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOPUT                    //No Power Up Timer
#FUSES BROWNOUT                 //Reset when brownout detected

#use delay(clock=20000000)
#use rs232(baud=4800,parity=N,xmit=PIN_A0,rcv=PIN_A2,bits=8,ERRORS)


also:

Code:
#include "servo_control_12F629.h"

#use fast_io (a)
#define On        1
#define Off       0
#define Servo     PIN_A5          //define pin for outgoing signal

int16 Pulse;
int16 Low_pulse;
int16 High_pulse;
int16 Pause;
int a;

char timed_getc() {
   long timeout;
   timeout=0;
   while(!kbhit()&&(++timeout<50000)) // 1/2 second
          delay_us(10);
   if(kbhit())
          return(getc());
   else {
          return(0);
   }
}

#INT_EXT
void ext_interrupt() {
      Byte a;
      a = timed_getc();
}

void main()
{

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(GLOBAL);
   enable_interrupts(INT_EXT);
   ext_int_edge(H_to_L);
   
   set_tris_a(0b00000010);          //configure A2 to be an input port     Output_low(Servo);               //clear the potput port

   Pulse = 1500;
   Low_pulse = 690;
   High_pulse = 2310;
   
      While(true) {
            Pulse = a*180 +690;              //Calculate Pulse lenght
            Pause = 20000 - Pulse;           //Calculate interval lenght
            Output_high(Servo);              //Set port on high
            delay_us(Pulse);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);

      }


}


Humberto, Is this all I need to do with the ext_int_edge?

I hope I've got it right... I must have edited this because of the ports 5 times... Does GP0 corresponds to A0, GP1 to A1 and so on? I think it is...

Thanks.


Last edited by oxxyfx on Fri May 25, 2007 9:38 am; edited 1 time in total
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri May 25, 2007 9:37 am     Reply with quote

Quote:

You guys will never believe this.... I looked up the PWD in google.

Well, to make sense you should find "PWM" instead.

Code:

 set_tris_a(0b00000010);          //configure A2 to be an input port     

Should be 0b00000100

Regarding your code, you are still using a local variable "a" inside the interupt handler
and another variable "a" declared as global, wich is used in main() and this do not make
sense, pls re-check this.
In this forum there are tons of examples using the EXT_INT to get an incoming char.


Humberto
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 10:04 am     Reply with quote

Sorry, I am getting a little confused with all the things I am reading on the forum about EXT_INT.

I re-read your post and it seems taht you are referring to the Byte a; declaration in the Interrupt handler routine.

I removed that from the code, so I have only one a declrared as global Int a;

This way I can use the received character in the main.

Code:

#include "servo_control_12F629.h"

#use fast_io (a)
#define On        1
#define Off       0
#define Servo     PIN_A5          //define pin for outgoing signal

int16 Pulse;
int16 Low_pulse;
int16 High_pulse;
int16 Pause;
int a;

char timed_getc() {
   long timeout;
   timeout=0;
   while(!kbhit()&&(++timeout<50000)) // 1/2 second
          delay_us(10);
   if(kbhit())
          return(getc());
   else {
          return(0);
   }
}

#INT_EXT
void ext_interrupt() {

      a = timed_getc();
}

void main()
{

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(GLOBAL);
   enable_interrupts(INT_EXT);
   ext_int_edge(H_to_L);
   
   set_tris_a(0b00000100);          //configure A2 to be an input port
   Output_low(Servo);               //clear the potput port

   Pulse = 1500;
   Low_pulse = 690;
   High_pulse = 2310;
   
      While(true) {
            Pulse = a*180 +690;              //Calculate Pulse lenght
            Pause = 20000 - Pulse;           //Calculate interval lenght
            Output_high(Servo);              //Set port on high
            delay_us(Pulse);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);

      }


} 



I hope I understood well what you meant.

Thx.
Ttelmah
Guest







PostPosted: Fri May 25, 2007 10:25 am     Reply with quote

A few little problems left. look carefully at the line:

Pulse = a*180 +690; //Calculate Pulse lenght

In CCS, to save space on the small Micro, arithmetic does not by default 'cast up'. So, the 'a*180', will be performed in 8bit arithmetic (since both values are 8bit).
You need to explicitly 'cast up':

Pulse = (int16)a*180 +690; //Calculate Pulse lenght

As shown, the code will keep looping, and characters may arrive during the timing delays used for the servo. Hence these will 'glitch'. I'd suggest adding a flag to say that a character has arrived, and then d something like:
Code:

      While(true) {
            //wait for a value to arrive
            while (!flag) ;
            Pulse = a*180 +690;              //Calculate Pulse lenght
            Pause = 20000 - Pulse;           //Calculate interval lenght
            Output_high(Servo);              //Set port on high
            delay_us(Pulse);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);
     }

This means the output will only update, when a value is received, but will prevent the timings being glitched.

Best Wishes
}
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 11:04 am     Reply with quote

Thank you Ttelmah.

Let me try to read that back in my plain english - only to make sure I properly understand what you are saying.

The Pulse calculation - I understand this, and thank you for pointing this out.

About the other problem:

The servo expects a pulse between 1000us and 2000us every 20ms. I am streching these values a bit to make the servo turn approx 170 degree, that's why I go from approx 700us to approx 2300us.

Now that we can establish proper serial communication between the two Pics, I can send higher values than I originally thought, between 0-255. The way I plan to calculate this on Pic1 is to send values 0-170 corresponding to teh degrees I want to reach. So for this, I neede to modify the above code to:

Code:

            Pulse = (int16)a*9 +735;         //Calculate Pulse lenght
            Pause = 20000 - Pulse;           //Calculate interval lenght


The 9 comes from: difference = High_pulse - Low_puse = 2265-735 = 1530

Now this divided by 9 is exaclty 170. So we modify the position of the servo by 1 degrees - assuming that a will not jump around...

Now. After all this - to understand what you said about the flag:

Since my PIC's don't have hardware UART, the receiving will be handled by software. So my while loop is looping nicly when the EXT_INT kicks in, and it breaks the loop to wait for the character. Is this right?

If this is the case after it gets the character will go back and continue the loop?

I am not sure I can work with the while(!flag) you added, because this seems to me to hold the previous while loop till a character arrives. I am not sure this will work, because the character will arrive only when I need to move the servo out of it's current position. This may happen now, or only a minute later. Till then the servo has to receive it's signal sequence- provided by the previous loop.

Am I right?
Ttelmah
Guest







PostPosted: Fri May 25, 2007 2:40 pm     Reply with quote

OK.
Good summary.
You will have a jitter problem whenever a character arrives then. :(
The problem is that the character fetch routine, without a hardware shift register, has to wait for the entire character. You can't use very high baud rates (because then there is not time to get into the receive routine, and start fetching the character), so this routine will take a significant time. Unfortunately, if this occurs during the delay specifying the position, you will get a significant timing error.
Three alternatives exist:
1) Use a PIC with a hardware UART.
2) Use a PIC with a hadware CCP, to time the output pulse.
3) Add one more wire, and have the slave send a pulse to the master, whenever it starts the (relatively) unimportant delay between the servo pulses. In the master, whn you have a character to send, you wait to see this line pulse, and then send the character. This way, the transmission is synchronised to a point where it won't interfere with the servo position.

Best Wishes
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 3:15 pm     Reply with quote

Ok, this is a break point.

I was trying to minimize the circuit, but it seems I can't do that. I will change the processor from 12F629 to a 16F627A which I have in stock. This has an ASUART as well as one CCP.

Beside finding the right pins, is thre anything I have to re-think in the above code to have this applied to the 16F627A?

Also the sender chip is 16F506 which does not have a hardware UART. I am not sure but I believe that it is not critical to have an UART on the sending end, since I will be sending only one character at the time - from time to time. Right?
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 9:53 pm     Reply with quote

So I spent some time this afternoon implementing this code for the 16F627. I programmed the chip and put it under my scope, and it seems that I have a problem...

It seems that the clock is running 5 times slower than it should be. Instead of the 20ms pulse intervals the pulses occur every 100ms. I am not sure how to correct this problem....

Here is the code:

Code:

#include <16F627A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES INTRC_IO                    //Internal RC Osc
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,ERRORS)


and the .c file:

Code:

#include "servo_control_16F627A.h"

#use fast_io (a)
#use fast_io (b)
#define On        1
#define Off       0
#define Servo     PIN_B5          //define pin for outgoing signal

int16 Pulse;
int16 Low_pulse;
int16 High_pulse;
int16 Pause;
int16 i;
int8 a;

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

void main()
{

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   clear_interrupt(INT_RDA);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   set_tris_b(0b00000010);          //configure B1 to be an input port
   Output_low(Servo);               //clear the potput port

   Pulse = 1500;
   Low_pulse = 735;
   High_pulse = 2265;
   i = 1500;
   a = 0;

      While(true) {
            Pulse = (int16)a*9 + Low_pulse;              //Calculate Pulse lenght
            Pause = 20000 - Pulse;           //Calculate interval lenght
            Output_high(Servo);              //Set port on high
            delay_us(Pulse);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);
      }

}
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri May 25, 2007 10:16 pm     Reply with quote

Never mind, I figured out. I changed the delay from:

#use delay(clock=20000000)

to:

#use delay(clock=4000000)

and now everything is the correct timing.

Thanks. I'll keep testing and get back soon with questions about the transmitter module.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Sat May 26, 2007 3:51 am     Reply with quote

Hello oxxyfx,

I'm very sorry about the 'PWD' mistake. As Humberto wrote i mean 'PWM'.
The same idea used for controlling servo. You understand my idea very well.

Here are some differences with the 'PWM' version:
- The 'PWM' version must be faster.
- If you use the rs/232 only for this usage, you save a lot of program memory because the 'CCS RS/232 driver' is included in the code only for that.
- I think that the code in c will not be very shorter nor simpler than your actual rs/232 version.
- You lose a little precision but very little.

In conclusion, your 'rs/232' version look close to be ok. Then if it is working good why loose time for an other solution ?

I hope to be able to help you later.

Best regards,
dro.
[/code]
_________________
in médio virtus
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Sun May 27, 2007 9:12 am     Reply with quote

So now I am behind 2 days of testing and I am a little bit further than previously, but I still do not have a 100% working solution.

Let me explain you what I did. The circuit has 2 PIC's. Pic1 is a 12F629 - this is the transmitter and Pic2 is a 16F627A the receiver. After uploading my servo control program to Pic2 I wrote some Servo initialization routines to make sure it is working, then I uploaded the program to Pic1 to send a character - in function of which the servo sould move to a different position. Well, this didn't happen. IT looks like Pic2 is receiving some transmission, but it does not apply it.

Then I disabled all routines to do some further testing, and I came up with these programs:

Pic1 - the transmitter:

Code:

#include <12F629.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //External clock
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOPUT                    //No Power Up Timer
#FUSES BROWNOUT                 //Reset when brownout detected
#use delay(clock=4000000)
#use rs232(baud=2400,parity=N,xmit=PIN_A5,rcv=PIN_A2,bits=8,ERRORS)

#include "servo_control_12F629.h"

#use fast_io (a)
#define  Beeper   PIN_A4

int8 a;
int16 iX;
int16 cX;

void Beep();

void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   a = 0;
   set_tris_a(0b00000000);
   Output_low(Beeper);
   delay_ms(5000);
   for (ix=0; iX<3; iX++) {
         Beep();
         Delay_ms(250);
   }   
      While(true) {
            a = 50;
            printf("%u",a);
            delay_ms(5000);
      }
}
Void Beep() {
     for ( cX = 0; cX < 50; cX++ ) {
     Output_high(Beeper);
     delay_us ( 100 );
     Output_low(Beeper);
     delay_us ( 200 );
     }
}


The first delay is to give time to the Pic2 to do it's init routine. The 2400baud rate is because I read somewhere in the 16F627A's datasheet that at 4Mhz clock 2400 is one off the 2 baud rates teh hardware uart is working at.

Pic2's code:

Code:

#include <16F627A.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES INTRC_IO                 //Internal RC Osc
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#use delay(clock=4000000)
#use rs232(baud=2400,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,ERRORS

#include "C:\Documents and Settings\PittnerZ\My Documents\Pic Projects\Tracker\servo_control_16F627A.h"

#use fast_io (a)
#use fast_io (b)

#define On        1
#define Off       0
#define Servo     PIN_B5          //define pin for outgoing signal
#define Beeper    PIN_B4

int16 Pulse;
int16 Low_pulse;
int16 High_pulse;
int16 Pause;
int16 i;
int16 a;
int16 iX;
int16 cX;
int16 Flag;

#int_RDA
Void RDA_isr(){
      a=getc();
      Flag++;
}
   
void Init_servo();

void Beep();

void main() {
      setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
      setup_timer_1(T1_DISABLED);
      setup_timer_2(T2_DISABLED,0,1);
      setup_comparator(NC_NC_NC_NC);
      setup_vref(FALSE);
      clear_interrupt(INT_RDA); 
      enable_interrupts(INT_RDA);
      enable_interrupts(GLOBAL);
      set_tris_b(0b00000010);          //configure B1 to be an input port
      Output_low(Servo);               //clear the output port
      Pulse = 1500;
      Low_pulse = 300;
      High_pulse = 2300;
      i = 1500;
      Flag = 0;
      for (ix=0; iX<3; iX++) {
            Beep();
            Delay_ms(250);
      }
      Init_servo();
     
While(true) {
      if (Flag != 0) {
            for (ix=0; iX<Flag; iX++) {
                  Beep();
                  Delay_ms(250);
            }
      Delay_ms(2000);
      }
}
}

void Init_servo(){
      Pause = 20000 - High_pulse;
         for (iX=0; iX<50; iX++){
            Output_high(Servo);              //Set port on high
            delay_us(High_pulse);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);
         }
      Pause = 20000 - Low_pulse;
         for (iX=0; iX<50; iX++){
            Output_high(Servo);              //Set port on high
            delay_us(Low_pulse);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);
         }
      Pause = 18700;
         for (iX=0; iX<100; iX++){
            Output_high(Servo);              //Set port on high
            delay_us(1300);                 //Wait for the duration of the pulse
            Output_low(Servo);               //Set port low
            delay_us(Pause);
         }
}
Void Beep() {
      for ( cX = 0; cX < 50; cX++ ) {
         Output_high(Beeper);
         delay_us ( 550 );
         Output_low(Beeper);
         delay_us ( 450 );
      }
}


The reason tof this that I wanted to see if indeed the Pic2 receives the transmission. And it does. According to the beep counts, every time twice.

So the first beep I hear from Pic2 is 2 beeps. next sequence is 4 beeps, then 6 beeps. Like for 1 transmission the interrupt has been called twice. Is this normal?

Thanx, Ox.
Ttelmah
Guest







PostPosted: Sun May 27, 2007 12:54 pm     Reply with quote

First comment. You don't want to use %u. This prints the incoming number as _text_, treating it as an 'unsigned' number. You want to send the raw character For this, use '%c', or putc.
This is why you are getting multiple beeps, since you are getting these multiple text characters.
Second, the comment about 2400bps, is simply wrong.
The chip, can use the master clock, divided by 16, or by 64, and then divided by the value in the BRG register (+1). 2400, is actually 'worse', than 19200bps, with an error of 1.67%, while the latter only has an error of 0.17%. _None_ of the default baud rates, actually work really 'well' with the 4MHz clock (much better to use a rate like 10000bps, which can be perfectly managed), but 2400, is one of the worst rates standard rates...

Best Wishes
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Sun May 27, 2007 1:27 pm     Reply with quote

Thank you. I will make these changes and report back.
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Sun May 27, 2007 5:57 pm     Reply with quote

Hello,

I made the suggested adjustments, and now everything works as it should. I reenabled the servo control routine, and I sent over a sequence of different values to Pic2. The servo is moving to the calculated position, and it didn't even glitch once. It is steady as a rock.

I have to thank you all for all the help I received from you and tell you how much I appreciate it.

This is a great forum, I will be coming back soon, I am sure about that.

Thanks, Oxy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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