Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me to use 16f628A for simulating a mouse

Not open for further replies.


Newbie level 5
Jan 20, 2006
Reaction score
Trophy points
Activity points
Hello to everyone.

I am trying to use a 16f628a to simulate a mouse.

I know I need a synchronous serial signal for a ps/2 mouse.

Can I use the built in usart on the 16f628A?
If so how do I set it up.

I am looking for code examples ,advice,or cooperation .
Any advice is welcome ,thank you.

16f628A mouse


i know that mikrobasic and mikroc compiler
have ps2 library

i think u can use it

hope that helped u

by the way these 2 compiler generate asm code too


    Points: 2
    Helpful Answer Positive Rating
Re: 16f628A mouse

Mikroc looks good but it's a demo version and I have no $$ .
I think it will work for the 16f628a because the memory is not much over 2k.
I have a lot of work for my little PIC .

Do you know any free compiler with ps/2 libraries or another way to do this?

Linux programs are ok.I like using Ktechlab.

I know all the assembly instructions but not the special register bits and stuff.
When I get my program wrote I will be sure to post it.

16f628A mouse

i told u before that
this compiler convert c code which u wrote into asm
so u can get the asm code and use it in any comipler
u can also use mplab with c code


Re: 16f628A mouse

I forgot to thank you ,How rude of me.
I have already got my program wrote in mikroc but I need to verify it.
I could not use ps2send so I'm using usuart library.
mikroc does not integrate well with mpasm the asm is missing portion,and it has line numbers .Here is the code I have so far...

unsigned short byte0, byte1, byte2;
//11 bit byte
//low start bit
//odd parity bit
//hi stop bit
//2400 bps

void main() {

  CMCON  = 0x07;     // Disable analog comparators (comment this for PIC18)
  TRISA  = 0x3f;
  TRISB  = 0x03;
  INTCON = 0;        // Disable all interrupts
  Usart_Init(56000); // Init uart
//  Delay_ms(100);     // Wait for keyboard to finish
//   D7 D6 D5 D4 D3 D2 D1 D0
//1st YV XV YS XS 1 0 R L
//2nd X7 X6 X5 X4 X3 X2 X1 X0
//3rd Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0

//L Left Button State (1 = pressed down)
//R Right Button State (1 = pressed down)
//X0-X7 Movement in the X direction
//Y0-Y7 Movement in the Y direction
//XS Direction of the movement in the X axis (1 = UP)
//YS Direction of the movement in the Y axis (1 = LEFT)
//XV,YV Overflow of the movement data bits (1 = overflow has occured)

 byte0.f0=PORTA.f0;  //L
 byte0.f1=PORTA.f1;  //R
 byte0.f2=0;         //D2
 byte0.f3=1;         //D3
//don't move if both or neither switch is on
   if(PORTA.f2&&!PORTA.f3)byte0.f4=1;  //XS=up
   if(PORTA.f3&&!PORTA.f2)byte0.f5=0;  //XS=down
//don't move if both or neither switch is on
   if(PORTA.f4&&!PORTA.f5)byte0.f4=1;  //YS=left
   if(PORTA.f5&&!PORTA.f4)byte0.f5=0;  //YS=right
 byte0.f6=0;     //no overflow
 byte0.f7=0;     //no overflow
//rb1=clk rb0=dt
If any thing is incorrect or missing please let me know.
I want to use as few external parts as possible.
I hope I have a nice long thread that results in a working device.:D

Re: 16f628A mouse

Major trouble writing this code.
Mikroc won't do the parity bit.
A C library for doing this would be a blessing.
Doing this in asm will drive me cracy(er).
The link has some very neat code but it is not exactly what I had in mind.
I want to use the ps/2 port .
It may be impossible to simulate with my current setup.


I will keep trying .
I have a really good circuit idea.
It is an eye position sensor .
It uses infrared .
I have already proven it will work but I have not tried it with a modulated signal to block out ambient light.
Also ,I'm not sure but it may burn out your eyeballs.
If someone can help me I will share more details.

Re: 16f628A mouse

O.K. I went back to the data sheet.

Can someone help with my code?

Here are my mouse functions so far.
void init9()
// baud rate
txsta.sync=1;//synchrous mode
rcsta.spen=1;//set clk and dt lines  Serial Port ENable
txsta.csrc=1;//master mode
txsta.tx9=1;//select 9 bit
void send9(unsigned short byte)
txsta.txen=1;//transmit enable
txreg=byte;//send byte
void parity(unsigned short byte)
         swapf   FARG_parity, w
        xorwf   FARG_parity, f
        rrf     FARG_parity, w
        xorwf   FARG_parity, f
        btfsc   FARG_parity, 2
        incf    FARG_parity, f
txsta.tx9d=!byte.f0;      //put bit in 9th bit register
I know C much betteer than asm .

Do I need to set up a recieve function for usart on ps/2?

I have the circuit I am going to use.
I will share it when I boot back to linux.

Re: 16f628A mouse

Here are 2 circuits that I can't find online anymore .
Maybe they will help someone wanting to do this.
I want to write my own code so I can add some to it .
I will probably use the 16f628a.
These use the 16f84

Not open for further replies.

Part and Inventory Search

Welcome to