SC16A programming problem

Autonomous Robot, Manual Robot, DC Motor Driver, Stepper Motor Driver, Servo, Multi PWM chip.....

SC16A programming problem

Postby Vicky » Sun Nov 18, 2012 11:25 pm

Hello there..
I'm beginner of this SC16A..
I have connected SC16A with SK40c with PIC16F887,
I have programmed, so that servo channel 1 moves, but it doesn't move..

The connection UART from SC16A and SK40C is correct and i checked 5 times already:
TX to TX
RX to RX
5V to VDD
GND to GND

I tested SC16A with GUI - Servo Control Panel software at cytron webpage,
and the SC16A is functional. So there must be programming problem.

This is the programming part. I use PIC16F887. Can anyone tell me which part is the problem? Please teach me..
Thank you so much :)
*I use this program to move servo channel 1 but it doesn't move*
CODE: SELECT_ALL_CODE
//   include
//==========================================================================
#include <htc.h>
#include "lcd.h"
#include "system.h"

//   Configuration
//==========================================================================

__CONFIG(
   FOSC_HS &    // High Speed Crystal.
   WDTE_OFF &    // Disable Watchdog Timer.
   PWRTE_ON &    // Enable Power Up Timer.
   BOREN_OFF &    // Disable Brown Out Reset.
   LVP_OFF
      );    // Disable Low Voltage Programming.

//   global variable
//=========================================================================
static volatile unsigned int received_servo_position[0x11];   // Array declared to store the feedback position of servo


//   function prototype
//==========================================================================
void send_cmd(unsigned char num, unsigned int data, unsigned char ramp); //UART transmit 4 bytes: servo number, higher byte position, lower byte position and speed
void delay_ms(unsigned int ui_value);
void uart_send(unsigned char data);         //UART transmit
unsigned char uart_rec(void);            //UART receive
void uart_initialize(void);
void request_feedback(unsigned char num);   //UART transmit 2 bytes: start byte '@' and servo number 0x41-0x60 to request position
void get_position(void);               //UART receive 3 bytes: servo number 0x41-0x60, higher byte and lower byte for position value to update the current position of servo

//   main function
//==========================================================================
void main(void)
{
   PORTA = 0;                  // clear PORT
   PORTB = 0;   
   PORTC = 0;               
   PORTD = 0;

   TRISA = 0b00000000;            
   TRISB = 0b00000011;          
   TRISC = 0b00000000;            
   TRISD = 0b00000000;            

   ANSEL = 0;                  // SET PORTA as DIGITAL I/O for PIC16F887
   ANSELH = 0;                // SET PORTB as DIGITAL I/O for PIC16F887

        uart_initialize();
        send_cmd(0x41,1300,63);
       
                           
}

//=======================================================================================================================================//
void delay_ms(unsigned int ui_value)
{
   while (ui_value-- > 0)
   {
      __delay_ms(1);      // macro from HI-TECH compiler which will generate 1ms delay base on value of _XTAL_FREQ in system.h
   }
}

void send_cmd(unsigned char num, unsigned int data, unsigned char ramp)    //send 4 bytes of command to control servo's position and speed
{
   unsigned char higher_byte=0, lower_byte=0;

   //servo channel should start with 0b01XX XXXX
   //therefore needs to change to 0x41-0x60
   num=num|0b01000000;

   //position value from 0-1463 are greater than a byte
   //so needs two bytes to send
   higher_byte=(data>>6)&0x003f;   //higher byte = 0b00xxxxxx
   lower_byte=data&0x003f;         //lower byte  = 0b00xxxxxx


         uart_send(num);                        //First byte is the servo channel 0x41-0x60
         uart_send(higher_byte);                  //second byte is the higher byte of position 0b00xxxxxx
         uart_send(lower_byte);                  //third byte is the lower byte of position 0b00xxxxxx
         uart_send(ramp);                     //fourth byte is the speed value from 0-63

}

void request_feedback(unsigned char num)            //send command to request the current position of servo
{
   //servo channel should start with 0b01XX XXXX
   //therefore needs to change to 0x41-0x60
      num=num|0b01000000;

         uart_send('@');                       //First byte is the start byte: '@' or 0x40
         uart_send(num);                        //Second byte is the requsting servo channle 0x41-0x60
}

void get_position(void)                           //receive 3 bytes from SC16A and update the position of servo
{unsigned int i;
static unsigned int received_servo_num=0, higher_byte=0,lower_byte=0,received_position=0;

   received_servo_num=uart_rec();                                             //First byte to receive: Requesting Servo number 0x41-0x60
   higher_byte=uart_rec();                                                   //Second byte to receive: Requesting Servo higher byte position
   lower_byte=uart_rec();                                                   //Third byte to receive: Requesting Servo lower byte position
   received_servo_num=received_servo_num&0b00011111;                              //Change back to 0x01-0x20
   received_servo_position[received_servo_num]=((higher_byte<<6)|(lower_byte&0x3F));       //Update the servo position value in corresponding array

}

unsigned char uart_rec(void)   //receive uart value
{
   unsigned char rec_data;
   while(RCIF==0);            //wait for data
   rec_data = RCREG;
   return rec_data;         //return the received data
}

void uart_send(unsigned char data)
{
   while(TXIF==0);            //only send the new data after
   TXREG=data;               //the previous data finish sent
}
void uart_initialize(void)
{
   unsigned char dummy = 0;
   BRG16 = 0;
   SYNC = 0;
   TX9 = 0;
   RX9 = 0;
   BRGH = 1;                           // Select high speed baud rate.
   SPBRG = 129;   // Configure the baud rate.
   SPEN = 1;                           // Enable serial port.
   CREN = 1;                           // Enable reception.
   TXEN = 1;                           // Enable transmission.
   dummy = RCREG;      // dummy read, to clear the receive buffer
   dummy = RCREG;
}
Vicky
Newbie
 
Posts: 8
Joined: Fri Mar 02, 2012 6:24 pm

Re: SC16A programming problem

Postby zhenning » Mon Nov 19, 2012 12:14 am

ScreenHunter_12 Nov. 19 00.12.png


RC6 -> TX is connected to SC16A RX and vice versa.
Attachments
pinout-16f877a.gif
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: SC16A programming problem

Postby gadgetng » Mon Nov 19, 2012 2:36 pm

should be

RX to TX
TX to RX
gadgetng
Discoverer
 
Posts: 97
Joined: Tue Jul 24, 2012 11:20 am


Return to Robot Controller

Who is online

Users browsing this forum: No registered users and 17 guests