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

Motor control Via RS232.

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



Joined: 11 Nov 2005
Posts: 6

View user's profile Send private message

Motor control Via RS232.
PostPosted: Fri Nov 18, 2005 8:28 pm     Reply with quote

Hi,

I have written some code to control two motors speed and direction via RS232 the transmitting pic works great but the receiving pic will only turn pins A4, B0 and B7 on/off.

Transmitter code:

Header:
Code:
#include <16F88.h>
#device adc=10
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOMCLR, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT, FCMEN, IESO
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)
//#include <stdlib.h>


Program:
Code:
#include "C:\Documents and Settings\C\Desktop\Test\Comms\Comms_TX.h"

//Configaration.

SET_TRIS_A(0x00);                     //Sets pins 17 & 18 to Inputs.
SET_TRIS_B(0x80);                     //Sets pin 13 to be an Input.
SETUP_ADC(ADC_CLOCK_INTERNAL);      //Sets ADC clock to internal clock.
SETUP_ADC_PORTS(sAN0,sAN1,VSS_VDD);   //Sets Analog Inputs 0 & 1.

//Variables.

int TX=0;      //Variable to be transmitted.
int TXR=0;     //Right side variable to be transmitted.
int TXL=0;     //Left side variable to be transmitted.
int Light=0;   //Variable to be transmitted for lights.
int16 ADC0=0;  //variable for ADC channel 0.
int16 ADC1=0;  //variable for ADC channel 1.
int16 RF=0;      //Analog value for Right Forward.
int16 RR=0;      //Analog value for Right Reverse.
int16 LF=0;      //Analog value for Left Forward.
int16 LR=0;      //Analog value for Left Reverse.
int16 Right=0; //PWM Variable for the right side.
int16 Left=0;  //PWM Variable for the left side.

//Progarm.

void Main()
{
   while(1)
//Right Side.
   {
   set_adc_channel(0);        //select ADC channel 0.
   delay_us(10);              //Wait for channel to be set.
   ADC0 = read_adc();         //Read ADC.
   ADC0>>=6;                  //Format ADC0.
//   printf("Reading ADC0\n\r"); //Debuging
//   printf("ADC0 = %lu\n\r",ADC0);

   if(ADC0>=512)              //If ADC0 is greater than 512, then start processing for the right side to move forward.
      {
//    printf("Starting RF\n\r");    //Debuging
      RF = ADC0 - 512;        //Set RF equal to ADC0 - 512, so RF is between 0 & 512.
      if(RF >= Right)         //This is because the forward value is high to go faster.
         {
         TXR = 0x01;          //To turn the right pin on the reciver on, to move the right side forward.
         }
      else
         {
         TXR = 0x00;          //To turn the right pin on the reciver off so the right side isn't continuously on.
         }

      if(Right <= 0)            //When Right equals 0 set back to 511 continue the PWM.
         {
         Right = 511;
         }
      else
         {
         Right--;               //Decrement Right.
         }
//    printf("Finishing RF\n\r");   //Debuging
      }
   else                       //If ADC0 is less than 512, then start processing for the right side to move backward.
      {
      //   printf("Starting RR\n\r");     //Debuging
      RR = ADC0;               //Set RR equal to ADC0.
      if(RR <= Right)         //This is because the reverse value is lower to go faster.
         {
         TXR = 0x02;          //To turn the right pin on the reciver on, to move the right side backward.
         }
      else
         {
         TXR = 0x00;          //To turn the right pin on the reciver off so the right side isn't continuously on.
         }

      if(Right >= 511)         //When Right equals 511 set back to 0 continue the PWM.
         {
         Right = 0;
         }
      else
         {
         Right++;               //Increment Right.
         }
//    printf("Finishing RR\n\r");   //Debuging
      }

//Left Side.

   set_adc_channel(1);        //select ADC channel 1.
   delay_us(10);              //Wait for channel to be set.
   ADC1 = read_adc();         //Read ADC.
   ADC1>>=6;                  //Format ADC1.
//   printf("Reading ADC1\n\r");   //Debuging
//   printf("ADC1 = %lu\n\r",ADC1);


   if(ADC1>=512)               //If ADC1 is greater than 512, then start processing for the left side to move forward.
      {
//    printf("Starting LF\n\r");    //Debuging
      LF = ADC1 - 512;         //Set LF equal to ADC1 - 512, so LF is between 0 & 512.
      if(LF >= Left)            //This is because the forward value is high to go faster.
         {
         TXL = 0x04;          //To turn the right pin on the reciver on, to move the left side forward.
         }
      else
         {
         TXL = 0x00;          //To turn the right pin on the reciver off so the left side isn't continuously on.
         }

      if(Left <= 0)           //When Left equals 0 set back to 511 continue the PWM.
         {
         Left = 511;
         }
      else
         {
         Left--;               //Decrement Left.
         }

//    printf("Finishing LF\n\r");   //Debuging
      }
   else                       //If ADC0 is less than 512, then start processing for the left side to move backward.
      {
//    printf("Starting LR\n\r");    //Debuging
      LR = ADC1;               //Set LR equal to ADC1.
      if(LR <= Left)            //This is because the reverse value is lower to go faster.
         {
         TXL = 0x08;          //To turn the right pin on the reciver on, to move the left side backward.
         }
      else
         {
         TXL = 0x00;          //To turn the right pin on the reciver off so the left side isn't continuously on.
         }

      if(Left >= 511)         //When Left equals 511 set back to 0 continue the PWM.
         {
         Left = 0;
         }
      else
         {
         Left++;               //Increment Left.
         }
//    printf("Finishing LR\n\r");   //Debuging
      }

   delay_ms(20);              //Wait 10mS

//Lights.

//   printf("Starting Lights\n\r");   //Debuging
   if(input(PIN_B7))            //If pin B7 is high set Light to turn the lights on.
      {
      Light = 0x08;           //to turn the pin on the reciver on, to turn the lights on.
      }
   else
      {
      Light = 0x00;           //To turn the light pin on the reciver off so the lights are not continuously on.
      }
//   printf("Finishing Lights\n\r");  //Debuging

// Transmit.

//   printf("Starting Trans\n\r");    //Debuging
   TX = (TXL + TXR + Light);   //Combine TXL, TXR & Light ready to be send.
   putc(TX);                  //Transmit TX.
//   printf("%U\n\r",TX);
//   printf("Finishing Trans\n\r");   //Debuging

   }
}


Receiver code:

Header:
Code:
#include <16F88.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOMCLR, NOBROWNOUT, LVP, NOCPD, NOWRT, NODEBUG, NOPROTECT, FCMEN, IESO
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,errors)


Program:
Code:
#include "C:\Documents and Settings\C\Desktop\Test\Comms\Comms_RX.h"

//Configaration.

setup_adc_ports(no_analogs);  //Set no analog Inputs.
SET_TRIS_A(0x20);               //Sets pin A5 to be an Input.
SET_TRIS_B(0x00);               //Sets all pins to Outputs.
output_a(0);
output_b(0);

//Variables.

char RX;   //Variable to be Recived.

//Progarm.

void Main()
{
   while(1)
   {
   RX = getc();           //Recives RX.
//   if(input(PIN_A5))         //If pin A5 is high, then the vehicle can only move backwards.
//      {
//      bit_clear(RX,0);
//      bit_clear(RX,2);
//      }
//   else
//      {
//      RX=RX;
//      }
//
   if(bit_test(RX,0)||bit_test(RX,1))   //To turn pin A0 on/off depending on RX, bit 0,1.(Right PWM)
      {
      output_high(PIN_A0);
      }
   else
      {
      output_low(PIN_A0);
      }
     
   if(bit_test(RX,2)||bit_test(RX,3))   //To turn pin A3 on/off depending on RX, bit 2,3.(Left PWM)
      {
      output_high(PIN_A3);
      }
   else
      {
      output_low(PIN_A3);
      }

   if(bit_test(RX,0))      //To turn pin A1 on/off depending on RX, bit 0.(RF)
      {
      output_high(PIN_A1);
      }
   else
      {
      output_low(PIN_A1);
      }

   if(bit_test(RX,1))      //To turn pin B7 on/off depending on RX, bit 1.(RR)
      {
      output_high(PIN_B7);
      }
   else
      {
      output_low(PIN_B7);
      }
   
   if(bit_test(RX,2))      //To turn pin A2 on/off depending on RX, bit 2.(LF)
      {
      output_high(PIN_A2);
      }
   else
      {
      output_low(PIN_A2);
      }

   if(bit_test(RX,3))      //To turn pin A4 on/off depending on RX, bit 3.(LR)
      {
      output_high(PIN_A4);
      }
   else
      {
      output_low(PIN_A4);
      }

   if(bit_test(RX,4))      //To turn pin B0 on/off depending on RX, bit 4.(Lights)
      {
      output_high(PIN_B0);;
      }
   else
      {
      output_low(PIN_B0);
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 18, 2005 11:55 pm     Reply with quote

Can you explain why your configuration code is floating off in space
somewhere above main() ? This code should be placed right at the
start of main().

Quote:
#include "C:\Documents and Settings\C\Desktop\Test\Comms\Comms_TX.h"

//Configaration.

SET_TRIS_A(0x00); //Sets pins 17 & 18 to Inputs.
SET_TRIS_B(0x80); //Sets pin 13 to be an Input.
SETUP_ADC(ADC_CLOCK_INTERNAL); //Sets ADC clock to internal clock.
SETUP_ADC_PORTS(sAN0,sAN1,VSS_VDD); //Sets Analog Inputs 0 & 1.

//Variables.
int TX=0; //Variable to be transmitted.
int TXR=0; //Right side variable to be transmitted.
int TXL=0; //Left side variable to be transmitted.
int Light=0; //Variable to be transmitted for lights.
int16 ADC0=0; //variable for ADC channel 0.
int16 ADC1=0; //variable for ADC channel 1.
int16 RF=0; //Analog value for Right Forward.
int16 RR=0; //Analog value for Right Reverse.
int16 LF=0; //Analog value for Left Forward.
int16 LR=0; //Analog value for Left Reverse.
int16 Right=0; //PWM Variable for the right side.
int16 Left=0; //PWM Variable for the left side.

//Progarm.

void Main()
{
<--- Put it here.

while(1)
//Right Side.
{
set_adc_channel(0); //select ADC channel 0.
delay_us(10); //Wait for channel to be set.



Quote:
#include "C:\Documents and Settings\C\Desktop\Test\Comms\Comms_RX.h"

//Configaration.

setup_adc_ports(no_analogs); //Set no analog Inputs.
SET_TRIS_A(0x20); //Sets pin A5 to be an Input.
SET_TRIS_B(0x00); //Sets all pins to Outputs.
output_a(0);
output_b(0);
//Variables.

char RX; //Variable to be Recived.

//Progarm.

void Main()
{
<--- Put it here.

while(1)
{
RX = getc(); //Recives RX.
Christian



Joined: 11 Nov 2005
Posts: 6

View user's profile Send private message

PostPosted: Sat Nov 19, 2005 12:39 am     Reply with quote

I don't know where I picked it up from, could be a copy and paste mistake. The transmitter code works great after a few code modifications, but now the receiver code dose not seem to work at all.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 19, 2005 12:44 am     Reply with quote

Quote:
#fuses NOWDT,HS, NOPUT, NOMCLR, NOBROWNOUT, LVP,
NOCPD, NOWRT, NODEBUG, NOPROTECT, FCMEN, IESO

The receiver has the LVP fuse set. Are you using LVP mode for this PIC ?
Christian



Joined: 11 Nov 2005
Posts: 6

View user's profile Send private message

PostPosted: Sat Nov 19, 2005 12:53 am     Reply with quote

No, I have changed the fuse to NOLVP but the code still does not work.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 19, 2005 1:22 am     Reply with quote

I looked a bit more closely at your code. Before, I didn't look at
in terms of functioning, I just looked for obvious things.

Your TRIS statements are wrong. You have the TRIS for the UART
receiver (pin B2) set to be an output pin. It's supposed to be set to
an input.


Are you using "fast_io" mode ?
Is "fast_io" mode specified in the Comms_TX.h" and Comms_RX.h files ?
Christian



Joined: 11 Nov 2005
Posts: 6

View user's profile Send private message

PostPosted: Sat Nov 19, 2005 1:56 am     Reply with quote

Thank you, Very Happy Very Happy
I fixed the TRIS statments, and also add fast_io to both Comms_TX and Comms_RX for both port a & b, and it is working great.
-Christian
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