im using MPLAB 8.84 and Hi-Tech C Compiler 9.83 (lite mode)...
there is some problem that i cannot solve.
hope someone can check the coding for me..
fyi. i just use the ps2 to control the movement for forward, reverse, right n left..
- CODE: SELECT_ALL_CODE
// include
//=======================================================================
#include <pic16f628a.h>
// configuration
//=======================================================================
__CONFIG ( 0x3F32 );
// define
//=======================================================================
#define motor_fwd RA3
#define motor_rvs RA2
#define motor_rg RA1
#define motor_lf RA0
//skps protocol
#define p_select 0
#define p_joyl 1
#define p_joyr 2
#define p_start 3
#define p_up 4
#define p_right 5
#define p_down 6
#define p_left 7
#define p_l2 8
#define p_r2 9
#define p_l1 10
#define p_r1 11
#define p_triangle 12
#define p_circle 13
#define p_cross 14
#define p_square 15
#define p_joy_lx 16
#define p_joy_ly 17
#define p_joy_rx 18
#define p_joy_ry 19
#define p_joy_lu 20
#define p_joy_ld 21
#define p_joy_ll 22
#define p_joy_lr 23
#define p_joy_ru 24
#define p_joy_rd 25
#define p_joy_rl 26
#define p_joy_rr 27
#define p_con_status 28
#define p_motor1 29
#define p_motor2 30
// global variable
//=======================================================================
// function prototype
//=======================================================================
void init(void);
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void forward(void);
void backward (void);
void reverse (void);
void left(void);
void right(void);
void uart_send(unsigned char data);
unsigned char uart_rec(void);
unsigned char skps(unsigned char data);
void skps_vibrate(unsigned char motor, unsigned char value);
// interrupt prototype
//=======================================================================
static void interrupt isr(void)
{
}
// main function
//=======================================================================
void main(void)
{
unsigned char up_v, down_v, left_v, right_v;
init(); //initialize microcontroller
stop(); //stop all motor
while(1)
{
//button control for mobilility
if(skps(p_up)==0) //check "up" button
{
forward(); //move forward
}
else if(skps(p_down)==0) //check "down" button
{
backward(); //move backward
}
else if(skps(p_left)==0) //check "left" button
{
left(); //rotate left
}
else if(skps(p_right)==0) //check "right" button
{
right(); //rotate right
}
}
}
// initailization
//=======================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00001111; //set RA0-RA2 pin as input,other as output
TRISB = 0b00000110; //set RB1-RB2 pin as input, other as output
// TMR 0 configuation
T0CS = 0;
PSA = 0;
PS2 = 1; // prescale 1:32
PS1 = 1; //
PS0 = 1; //
TMR0IE = 0; // TMR0 Interrupt
TMR0 = 0;
//setup USART
SPBRG = 0x81; //set baud rate to 9600 for 20Mhz
BRGH = 1; //baud rate high speed option
TXEN = 1; //enable transmission
TX9 = 0;
CREN = 1; //enable reception
SPEN = 1; //enable serial port
RX9 = 0;
RCIE = 0; //disable interrupt on eachdata received
// enable all unmasked interrupt
GIE = 0;
PEIE = 0;
}
// uart function
//=======================================================================
void uart_send(unsigned char data) //function to send out a byte via uart
{
while(TXIF==0); //wait for previous data to finish send out
TXREG=data; //send new data
}
unsigned char uart_rec(void) //function to wait for a byte receive from uart
{
unsigned char temp;
while(RCIF==0); //wait for data to received
temp=RCREG;
return temp; //return the received data
}
// skps function
//=======================================================================
unsigned char skps(unsigned char data) //function to read button and joystick
{ //information on ps controller
uart_send(data);
return uart_rec();
}
void skps_vibrate(unsigned char motor, unsigned char value)
{ //function to control the vibrator motor
uart_send(motor); //on ps controller
uart_send(value);
}
// motor control function
//=======================================================================
void forward ()
{
motor_fwd = 0;
motor_rvs = 1;
motor_rg = 0;
motor_lf = 1;
}
void backward ()
{
motor_fwd = 1;
motor_rvs = 0;
motor_rg = 1;
motor_lf = 0;
}
void left()
{
motor_rg = 1;
motor_lf = 0;
motor_fwd = 0;
motor_rvs = 1;
}
void right()
{
motor_rg = 0;
motor_lf = 1;
motor_fwd = 1;
motor_rvs = 0;
}