|
|
View previous topic :: View next topic |
Author |
Message |
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
how to convert this code to c |
Posted: Tue Jun 09, 2009 10:42 am |
|
|
Code: | serout pakout,baud,[$0B,$EB]
serin pakin\fpin,baud,[keyin,p1,xreg,yreg]
if keyin=$FA then parse
debug "Didn't get $FA on mouse poll",cr |
and
Code: | ymov=ymov- ((yreg-1)^$FF) |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 09, 2009 12:21 pm |
|
|
Learn the C language. Take a course in it, or read a book. Read the
CCS manual.
Then you will understand how to use the #use rs232() statement, and
getc() statement, and printf() statements to write that code in CCS C. |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
how to convert this code to c |
Posted: Tue Jun 09, 2009 3:04 pm |
|
|
ok. I already tried to convert this code into c
Code: | serout pakout,baud,[$0B,$F0]
serin pakin\fpin,baud,[keyin]
' should get $FA
if keyin=$FA then top
Debug "Didn't get $FA from remote mode set",cr |
is it correct?
Code: | #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,STREAM=COM_A,parity=N)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2,STREAM=COM_B)
#byte escape =0x0b;
#byte remote_mode =0xf0;
#byte read_data =0xfa;
fputs(COM_B, escape);
fputs(COM_B, remote_mode);
num=getc(COM_B);
if( num == 0xfa)
{
Top();
}
else
printf(COM_A,"Didn't get $FA from remote set\n");
} |
|
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 09, 2009 3:08 pm |
|
|
Comments:
1) There needs to be an actual program 'block' (a 'main' function, containing the code you want to run).
2) #byte, locates a variable _at_ a location. You don't want this. You want to declare 'named values'. #define is the operator for this (with no equals sign).
3) You need a processor header, and clock header.
Look at the supplied example files, to see how a simple program works.
Probably more, but start with these |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
how to convert this code to c |
Posted: Tue Jun 09, 2009 3:12 pm |
|
|
Code: | #include <16f877a.h>
#use delay(clock=20000000)
#fuses hs, nowdt, nolvp, noprotect
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,STREAM=COM_A,parity=N)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2,STREAM=COM_B)
#org 0x1F00, 0x1FFF void loader16F877A(void) {}
#org 0x0005, 0x0007 {}
void main()
{
int8 position;
int8 xreg;
int8 yreg;
int16 xmov;
int16 ymov;
#bit yover = position.7;
#bit xover = position.6;
#bit ysign = position.5;
#bit xsign = position.4;
#bit m_button = position.2;
#bit r_button = position.1;
#bit l_button = position.0;
output_high(PIN_B0); //pin enable2=1
#byte reset =0xff;
fputs(COM_B, reset); //x sure
delay_ms(500);
xmov = 0;
ymov = 0;
#byte raw =0x02;
fputs(COM_B, raw); //x sure
#byte escape =0x0b;
#byte remote_mode =0xf0;
#byte read_data =0xfa;
fputs(COM_B, escape); //x sure
fputs(COM_B, remote_mode); //x sure
num=getc(COM_B);
if( num == 0xfa)
{
Top();
}
else
printf(COM_A,"Didn't get $FA from remote set\n");
}
void Top()
{
fputs(COM_B, escape);
fputs(COM_B, read_data); //x sure
num=getc(COM_B);
} |
|
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
how to convert this code to c |
Posted: Tue Jun 09, 2009 3:14 pm |
|
|
This is the code that I want to convert
Code: | pakout con 15
pakin con 14
baud con 84
fpin con 13
keyin var byte
' This assumes a 3 byte packet
' Some mice can do 4 byte packets
' (mainly those with wheels) but
' you have to turn on that mode
p1 var byte ' mouse status
xreg var byte ' raw x motion
xmov var word ' x accumulator
yreg var byte ' raw y motion
ymov var word ' y accumulator
' flags in p1
yover var p1.bit7 ' y overflow
xover var p1.bit6 ' x overflow
ysign var p1.bit5 ' y sign
xsign var p1.bit4 ' x sign
midbtn var p1.bit2 ' button flags
rightbtn var p1.bit1
leftbtn var p1.bit0
high fpin
' Reset PAK and wait for device reset
serout pakout,baud,[$FF]
pause 500
xmov=0
ymov=0
' Enter raw mode
serout pakout,baud,[2]
' Set remote mode
serout pakout,baud,[$0B,$F0]
serin pakin\fpin,baud,[keyin]
' should get $FA
if keyin=$FA then top
Debug "Didn't get $FA from remote mode set",cr
top:
' read movement packet
serout pakout,baud,[$0B,$EB]
serin pakin\fpin,baud,[keyin,p1,xreg,yreg]
if keyin=$FA then parse
debug "Didn't get $FA on mouse poll",cr
goto top
parse:
if yover or xover then overflow
if xsign=0 then xplus
' convert negative number
xmov = xmov - ((xreg-1)^$FF)
goto doy
xplus:
xmov=xmov+xreg
doy:
if ysign=0 then yplus
' convert negative number
ymov=ymov- ((yreg-1)^$FF)
goto ydone
yplus:
ymov=ymov+yreg
ydone:
' Display results
debug cls, sdec xmov, " ", sdec ymov ,cr
if leftbtn=0 then checkmid
Debug "Left button down",cr
checkmid:
if midbtn=0 then checkr
Debug "Mid button down",cr
checkr:
if rightbtn=0 then top
Debug "Right button down",cr
goto top
overflow:
Debug "Overflow occured",cr
goto top |
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jun 10, 2009 2:19 am |
|
|
As already stated you should be using #define for your values.
#define raw 0x02
etc..
Also you should be using fputc for single chars
fputc(raw, COM_B);
fputs expects a string!
Also the help for CCS shows fputc and fputs as having the data first and the strem desciptor second. So you have it the wrong way around.
I think fputs may still work but is wrong! |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
|
Posted: Wed Jun 10, 2009 4:03 am |
|
|
when i change it into define, error occured when i compiled it. i'm expecting to send a byte but if i use putc. does it mean i send a character
Code: |
#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs, nowdt, nolvp, noprotect
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,STREAM=COM_A,parity=N)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2,STREAM=COM_B)
#org 0x1F00, 0x1FFF void loader16F877A(void) {}
#org 0x0005, 0x0007 {}
#byte reset =0xff
#byte raw =0x02
#byte escape =0x0b
#byte remote_mode =0xf0
#byte read_data =0xfa
int8 position;
int8 xreg;
int8 yreg;
int16 xmov;
int16 ymov;
int8 num;
#bit yover = position.7
#bit xover = position.6
#bit ysign = position.5
#bit xsign = position.4
#bit m_button = position.2
#bit r_button = position.1
#bit l_button = position.0
void Top();
void main()
{
output_high(PIN_B0); //pin enable2=1
fputs(reset , COM_B); //x sure
delay_ms(500);
xmov = 0;
ymov = 0;
fputs(raw,COM_B); //x sure
fputs(escape, COM_B); //x sure
fputs(remote_mode, COM_B); //x sure
num=getc(COM_B);
if( num == 0xfa)
{
Top();
}
else
fprintf(COM_A,"Didn't get $FA from remote set\n");
}
void Top()
{
fputs(escape, COM_B);
fputs(read_data, COM_B); //x sure
num=getc(COM_B);
} |
|
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
|
Posted: Wed Jun 10, 2009 4:25 am |
|
|
do anyone know how to convert this lines. or what it means
serin pakin\fpin,baud,[keyin,p1,xreg,yreg]
if keyin=$FA then parse
debug "Didn't get $FA on mouse poll",cr |
|
|
Ttelmah Guest
|
|
Posted: Wed Jun 10, 2009 4:46 am |
|
|
A character _is_ a byte.
You don't get an error using #byte, since you have defined a variable. Puts, expects a variable containing the address of a string, so doesn't complain, but what you have defined, is a variable pointing to a location in memory, that could contain _anything_, so garbage will be output. Putc, expects a character (or byte).
I'd 'guess', that it says get a character from the serial, at the defined baud rate 'baud', from specified pin.
Check if character is '0xFA'. If so, call the 'parse' routine. Else display the debug message.
Best Wishes |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
|
Posted: Wed Jun 10, 2009 5:03 am |
|
|
I changed it to define
#define escape =0x0b
fputc(escape, COM_B);
but when i compiled, error stated "a numeric expression must appear here" after escape. I already checked the manual but still couln't figure out what when wrong
for the
serin pakin\fpin,baud,[keyin,p1,xreg,yreg]
do i need to create a loop. let say 3 loop, first loop for p1, second for xreg and last for yreg |
|
|
Ttelmah Guest
|
|
Posted: Wed Jun 10, 2009 5:07 am |
|
|
Note my earlier comment. _no equals sign_.
#define escape (0x0b)
The brackets are not 'needed', but are good programming practice. Avoids certain types of syntax error in more complex useage.
Best Wishes |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
|
Posted: Wed Jun 10, 2009 5:28 am |
|
|
serin pakin\fpin,baud,[keyin,p1,xreg,yreg]
as keyin is 3 bytes how can i specify which byte are p1 , xreg and yreg |
|
|
nasbyc
Joined: 27 Apr 2009 Posts: 50
|
|
Posted: Thu Jun 11, 2009 3:32 pm |
|
|
I have question regarding reading from serial. I should expect 5 bytes but when I executing this code, I only manage to get 1 byte. This data comes from another pic (pak-xi). What's wrong with it?
Code: |
do{
keyin=fgetc(COM_B);
fprintf(COM_A,"keyin baru = %LX\n",keyin);
fprintf(COM_A,"i = %d\n",i);
i++;
}while(keyin!=0xfa);
if( keyin == 0xfa)
{
goto Parse;
}
|
When I executes it i variable only be 1. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jun 11, 2009 3:57 pm |
|
|
First you have to know the UART can only buffer up to 3 characters, when more data is received before you read this data, the UART will stop until the error is reset.
Assuming you are transmitting data at COM_A at the same speed as you are receiving on COM_B. What do you think will happen when you are transmitting about 22 characters for every single character you receive?
One method for resetting the UART error is by adding the 'ERRORS' keyword to the '#use rs232' line. Note that this only resets the error, it is still possible to lose data when you are not reading the UART often enough.
My suggestion is you try to solve your problem in another way. The example code you have is in a programming language you don't understand and you are trying to write code in C that you don't understand either. I don't know your background but to me it looks like you are trying to do a too difficult project for your current knowledge level. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|