I have problems in interfacing a ps/2 mouse with 12f629. But it seems that the 12f629 have no data transfer to the mouse or no data recept from the mouse! Here is my c program. Can anyone find out why? Thank you!
#include <pic12f6x.h>
#include "delay.h"
#include "delay.c"
/* difine the data and clock line*/
static volatile bit clk @ (unsigned)&GPIO*8+1; /* bit1 in portA */
static volatile bit dataline @ (unsigned)&GPIO*8+2; /* bit2 in portA */
static volatile bit clk_tris @ (unsigned)&TRISIO*8+1; /* bit1 in trisA */
static volatile bit dataline_tris @ (unsigned)&TRISIO*8+2; /* bit2 in trisA */
/*Define the command to the mouse*/
#define MS_RESET 0xff
#define MS_DATA_REPORTING_ENABLED 0xf4
#define MS_SUCCESS 0xaa
#define MS_DEVICE_ID 0x00
#define MS_ACKNOWLEDGEMENT 0xfa
/* Prototypes */
void wait(unsigned char t);
void sendData(unsigned char data);
unsigned char recvData(void);
void sendData(unsigned char data){
char i;
unsigned char parity = 1;
clk_tris = 0;
dataline_tris = 0; //Both the clock and the data line set to output
clk = 0; //Drive the clock low
DelayUs(100); //For at least 100MS
dataline = 0; //Drive the data low <-------------start bit
DelayUs(10); //For 10 MS.
clk_tris = 1; // Release the clock.
//<-------------------------Send 8 data bits
for(i=0; i < 8; i--) { //8 bit,so perform 8 cycles.
while(clk) DelayUs(10); //Wait for clock low to jump to next
dataline = data & 0x1; // Send the LSB.
parity += data & 0x1; // Calculate parity.
data = data >> 1; // Shift data.
while(!clk) DelayUs(10); // Wait for clock high to jump out.
}
while(clk) DelayUs(10);
dataline = parity & 0x1; // <------------------------Send parity
while(!clk) DelayUs(10);
while(clk) DelayUs(10);
dataline = 1; //<-------------------------Send stop bit.
while(!clk) DelayUs(10);
dataline_tris = 1; //Release the Dataline.
while(dataline); // Wait for acknowledgement bit(Low).
while(clk); // Wait for clock goes low.
while(!clk || !dataline); // Wait for mouse releases clk and dataline to both high.
}
unsigned char recvData(void) {
unsigned char data = 0x0;
unsigned char parity;
char i;
clk_tris = 1;
dataline_tris = 1; //Both dataline and clock is set to high.
while(clk) DelayUs(10);
while(!clk) DelayUs(10); // Recieve the start bit.<------Start bit.
for(i=0; i < 8; i--) { //<------------8 cycles to receive the 8 bit
while(clk) DelayUs(10); // Wait for clock line to go low.
data = data >> 1; // Shift buffer.
data += dataline * 0x80; // Read next bit into buffer.
parity += data & 0x1; // Update parity.
while(!clk) DelayUs(10); // Wait for clock to go high.
}
while(clk) DelayUs(10);
while(!clk) DelayUs(10); //Parity bit
while(clk) DelayUs(10);
while(!clk) DelayUs(10); //Stop bit
return data;
}
void main(void) {
//INTCON=0X00; //
//CMIE=0; //
//CMCON=0x13; //
//CMIF=0; //
//WPU5=1; //
clk_tris=0;
dataline_tris=0;
sendData(MS_RESET); // Reset mouse.
while(recvData() != MS_SUCCESS); // Wait for self-test success.
while(recvData() != MS_DEVICE_ID); // Wait for mouse device id to be
sendData(MS_DATA_REPORTING_ENABLED); // Enable data reporting.
while(recvData() != MS_ACKNOWLEDGEMENT); // Wait for acknowledgement
//*==========================================
//*After that mouse is in data reporting mode
//*==========================================
while(1) {
sendData(0xEB); // Report the status
recvData();
recvData();
recvData(); //Receive the 3 Byte data from the mouse in each cycle.
DelayMs(20); //Delay for 20 miliseconds
}
}