- CODE: SELECT_ALL_CODE
#include <pic.h>
// configuration
//=======================================================================
__CONFIG ( 0x3F10 ); //configuration For PIC16F628A
// define
//=======================================================================
#define _XTAL_FREQ 12000000 //Fosc=12MHz
#define motor_ra RB6
#define motor_rb RB7
//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 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)
{
init(); //initialize microcontroller
while(1)
{
//test button for horn
if((skps(p_l1)==0)||(skps(p_l2)==0)||(skps(p_r1)==0)||(skps(p_r2)==0)||(skps(p_triangle)==0)||(skps(p_circle)==0)) motor_ra=1;
else if((skps(p_up)==0)||(skps(p_down)==0)||(skps(p_right)==0)||(skps(p_left)==0)||(skps(p_cross)==0)||(skps(p_square)==0)) motor_rb=1;
else
{
motor_ra=0;
motor_rb=0;
}
}
}
// initailization
//=======================================================================
void init()
{
CMCON = 0b00000111; // disable comparators
// Tris configuration (input or output)
TRISA = 0b00000001; //set RA0 pin as input,other as output
TRISB = 0b00000010; //set PORTB as output
//setup USART
SPBRG = 0x4D; //set baud rate to 9600 for 12Mhz
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;
motor_ra = 0;
motor_rb = 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);
}