Full Version : mouse PS2 interfacing
avr >>C PROGRAMMING (AVR) >>mouse PS2 interfacing


Laxs- 02-13-2007
dear frend,

i usually work out with assembler, so in 3 month ago i want to learn about the C. my current project is interfacing PS2 mouse into my atmega8515,

PORTB.0|- <-- mouse data -->
PORTD.2|- <-- mouse clock-->
(INT 0)|

Atmega 8515 Mcu direct to mice device without transistor like as usual

i use the 7805 as power supply.

this my program with codevision
----------------------------
#include <mega8515.h>
#include <string.h>

#asm
.equ __lcd_port=0x1B ;PORTA
#endasm

#include <lcd.h>
#include <stdio.h>
#include <delay.h>

//--------- mouse section-----------
#define reset 0xFF
#define test 0xAA
#define enable 0xF4
#define PS2_clock PORTD.2
#define PS2_data PORTB.0
#define c_dir DDRD.2
#define d_dir DDRB.0
//----------------------------------

unsigned char buffer[2],i,count,data,n=0;
bit parity, start, ack;


// Declare your global variables here

void send_cmd(unsigned char command){

char parity=0;
char mask = 0x01;
parity = (command & 0x01) ^ //Calculate odd parity
((command & 0x02) >> 1) ^
((command & 0x04) >> 2) ^
((command & 0x08) >> 3) ^
((command & 0x10) >> 4) ^
((command & 0x20) >> 5) ^
((command & 0x40) >> 6) ^
((command & 0x80) >> 7);
c_dir=1;
d_dir=1;
PS2_clock=0;
delay_ms(12);
PS2_data=0;
PS2_clock=1;
c_dir=0;

while(PIND.2==1);
for (i=0; i<8; i++) {
while(PIND.2==0);
PS2_data=PS2_data|((command>>i)&0x01);
while(PIND.2==1);
}

while(PIND.2==0);
PS2_data=parity;
while(PIND.2==1);;

PS2_data=1; // send stopbit

while(PIND.2==0);
while(PIND.2==1){d_dir=0;};

while (PINB.0!=0); //wait for ack
while (PINB.0==0);

// PORTC.0=0; //-----------> my led is on, so my init is success?
}


// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// PORTC.0=0; // ----> my led is on, so my interrupt 0 is work at falling edge
while (PIND.2==0);
for (i=0;i<8;++i){
while (PIND.2==1);
data=data|PINB.0;
while (PIND.2==0);
data=data<<1;
}

// if (buffer==3){goto jump;}
// buffer[n]=data;
// i=0;
// ++n;
//jump:
}

void init_receive() {
c_dir=0;
d_dir=0;
GICR|=0x40;
MCUCR=0x00;
EMCUCR=0x00;
GIFR=0x40;
}

void disable_receive() {

GICR = 0; //Disable interrupts
}

void mouse_init()
{
send_cmd(0xFF);

c_dir=1;
d_dir=1;
PS2_clock=1;
PS2_data=1; //set2idle

init_receive();

/* while(data != 0xFA);
while(data != 0xAA);
while(data != 0x00);
*/


}

void main(void)
{
//lcd_init(16);
//lcd_clear();

// Global enable interrupts
#asm("sei")
DDRC=0xFF;
PORTC=0xFF;

n=0;
count=0;
mouse_init();
while (1)
{

while(data != 0xFA) {PORTC=data;} //----> my PORTC is say 0b11111110 or sometime 0b01111110 is never 0b11111010... why?
while(data != 0xAA);
while(data != 0x00);

}
}
----------------------------


why i never received ack data from mice? that's i missed something?


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.