The code works on one PIC18f242 but when I split the code (like the attached code...) and send it to another PIC18f242 via USART nothing happends... What's wrong?
sending pic
Code: Select all
// Stepper motor control
signed short int move, move1, move2;
signed int adc1, adc2;
adconv(void) //convert 2 adc channel values to stepper motor direction/speed
{
move1 = 0;
move2 = 0;
if (adc1 <= 100) move1 = 1;
if (adc2 <= 100) move2 = 8;
if (adc1 > 100 && adc1 <= 200) move1 = 2;
if (adc2 > 100 && adc2 <= 200) move2 = 16;
if (adc1 > 200 && adc1 < 350) move1 = 3;
if (adc2 > 200 && adc2 < 350) move2 = 24;
if (adc1 > 650 && adc1 < 800) move1 = 4;
if (adc2 > 650 && adc2 < 800) move2 = 32;
if (adc1 >= 800 && adc1 <= 900) move1 = 5;
if (adc2 >= 800 && adc2 <= 900) move2 = 40;
if (adc1 > 900) move1 = 6;
if (adc2 > 900) move2 = 48;
}
main(void)
{
ADCON1 = 0x80;
TRISA = 0xff;
TRISC = 0x80;
Usart_Init(2400);
do {
adc1 = Adc_Read(1); //read data from 2-pot joystick
adc2 = Adc_Read(2);
adconv();
move = move1 + move2; //combine the data, splits in recieving pic
if(move != 0) Usart_Write(move); //GO!
}while(1);
}
recieving pic
Code: Select all
// Stepper motor control
signed short int slow1, slow2, speed, move, move1, move2;
signed short int temp, var1, var2;
//stepper motor values
signed short int data1[8] = {0b00001001, 0b00001000, 0b00001010, 0b00000010, 0b00000110, 0b00000100, 0b00000101, 0b00000001};
signed short int data2[8] = {0b10010000, 0b10000000, 0b10100000, 0b00100000, 0b01100000, 0b01000000, 0b01010000, 0b00010000};
motor(void)
{
move1 = move & 7; //split the data from USART by 'and'-ing the values
move2 = move & 56;
if (move1 == 1) var1-=2; //reverse full step
if (move2 == 8) var2-=2;
if (move1 == 2) var1--; //reverse half step
if (move2 == 16) var2--;
if (move1 == 3)
{
slow1++;
if (slow1 == speed) //reverse half step/speed
{
var1--;
slow1 = 0;
}
}
if (move2 == 24)
{
slow2++;
if (slow2 == speed) //reverse half step/speed
{
var2--;
slow2 = 0;
}
}
if (move1 == 4)
{
slow1++;
if (slow1 == speed) //forward half step/speed
{
var1++;
slow1 = 0;
}
}
if (move2 == 32)
{
slow2++;
if (slow2 == speed) //forward half step/speed
{
var2++;
slow2 = 0;
}
}
if (move1 == 5) var1++; //forward half step
if (move2 == 40) var2++;
if (move1 == 6) var1+=2; //forward full step
if (move2 == 48) var2+=2;
if (var1 > 7 ) var1 = 0; //correction for modulu 7
if (var2 > 7 ) var2 = 0;
if (var1 < 0) var1 = 7;
if (var2 < 0) var2 = 7;
}
main(void)
{
ADCON1 = 0x80;
TRISA = 0xff;
TRISB = 0x0;
TRISC = 0x80;
var1 = 0; //reset
var2 = 0;
slow1 = 0;
slow2 = 0;
speed = 2;
Usart_Init(2400); //init USART
do {
move = 0;
if (Usart_Data_Ready()) move = Usart_Read();
motor();
PORTB = data1[var1] + data2[var2]; //MOVE please....
VDelay_ms(Adc_Read(0)); //external speed control
}while(1);
}