Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need help with some code

Status
Not open for further replies.

cyb0rg777

New Member
I am trying to use the usart on a 16f628a to send a ps2 signal to my computer.
This short piece of Mikroc code is used to load the txreg and then wait for transmission.

txreg=byte;
while(!pir1.txif);

I also tried this in assembly

txreg=byte;
asm{
btfss PIR1,4 //check transmit bit txif
goto $-1
}

Both of these pieces of code stop transmission altogether,and I can't understand why.
:?
 
Have you setup the USART correctly.
You need to set the baud rate.
spbrg = 64
This is for 20MHz clock and 19200 baud
Also
txsta=36
OR
txsta.txen=1
txsta.brgh=1

This maybe setup automatically by your basic compiler or you may need an initialization command.

Mike.
 
I'm using synchronous mode 9 bits (8 +parity) with the 4mhz internal oscillator .
I get a serial signal without the delay ,
but I am assuming it's not correct because it isn't moving my mouse .
I get no serial signal with the delay.
Here is my code .
Code:
#include "ps2mouse.c"
//   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
//synchronous
//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)
//only send if state changes
unsigned short byte0,byte1,byte2;
void main ()
{
//0=lb,1=dt,2=ck,3=rb,4=X+,5=X-,6=y+,7=y-
cmcon=7;//turn off comparators
trisb=0xff;
PORTB=0;
txsta.brgh =1;
spbrg=0x33;//need to set baud rate
init9();
do{
byte0.f0=PORTB.f0;  //L
byte0.f1=PORTB.f3;  //R
 if(PORTB.f4==PORTB.F5)byte1=0;
  else
   {
   byte1=20;
   if(PORTB.f4)byte0.f4=1;  //XS=up
   if(PORTB.f5)byte0.f4=0;  //XS=down
   }
 if(PORTB.f6==PORTB.F7)byte2=0;
  else
   {
   byte2=20;
   if(PORTB.f6)byte0.f5=1;  //YS=left
   if(PORTB.f7)byte0.f5=0;  //YS=right
   }

byte0.f2=0;         //D2 unused
byte0.f3=1;         //D3
byte0.f6=0;     //no overflow
byte0.f7=0;     //no overflow
if(byte1||byte2||byte0.f0||byte0.f1)
{
send9(byte0);
send9(byte1);
send9(byte2);
}
}while(1);

}


//ps2mouse.c
void parity(unsigned short byte);
void init9()
{
//ports
// 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)
{
parity(byte);
txsta.txen=1;
txreg=byte;
asm{
    btfss PIR1,4 //check transmit bit txif
    goto $-1
    }
//while(!pir1.txif);//wait for transmission
//while(!txsta.trmt);//wait for transmission
txsta.txen=0; //turn off transmission

}
unsigned short rcv9(void)
{

}
void parity(unsigned short byte)
{
asm{
        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 may be grasping at straws hoping someone can help.
I know it's something minor.
Any troubleshooting tips ,information ,links .Thank you
 
I have one quick question. In synchronous mode of the PIC's USART, is the byte sent framed by a start and stop bits? I don't think so.

If it is not, then it is incompatible with the PS2 interface. All PIC based PS2 examples I have encountered on the web don't use the USART. They are all bit-banged.
 
I think your problem may be that you are clearing the txen bit before the byte has been transmitted. The txif bit only signals that the byte has been transfered to the shift register. You could check the trmt bit instead. An alternative way to do this would be to check the txif bit before loading the txreg, this allows the cache to be loaded before the last byte has been transmitted. You would of course leave the txen bit set.

Mike.
 
Huh,looks like your right ,after checking the timing diagram and stuff in the data sheet.
Asynchronous has the start and stop bits but synchronous doesn't .
I just kind of assumed it did .
Kind of disapointed.
Thanks anyway.
Sure would have made it easy.
Do any other chips have the required 11 bits?
 
I got a working version of the circuit I needed ,but I had to use someone elses code adapted for the 16f628a.
The good part is I did it with no external parts ,just the chip.
Here is the PS/2 Mouse Emulator by Adam Chapweske if anyone is interested in this type of project.
I can't show my changes though cause he says not to.
 

Attachments

  • acces151.zip
    23.5 KB · Views: 131
I also use this mouse emulator,bit it is difference form the real mouse

cyb0rg777 said:
I got a working version of the circuit I needed ,but I had to use someone elses code adapted for the 16f628a.
The good part is I did it with no external parts ,just the chip.
Here is the PS/2 Mouse Emulator by Adam Chapweske if anyone is interested in this type of project.
I can't show my changes though cause he says not to.

I also use this mouse emulator,but it is difference form the real mouse, I succed recept datas from the emulator mouse with my program,but when I use a real ps/2 mouse,my program can't receive any data!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top