Sign in

username:

password:



Not a member?

Search msp430



Search tips

Subscribe to msp430



Ads

Discussion Groups

Discussion Groups | MSP430 | Help needed for translating assembly code

The purpose of this group is to foster exchange of information on the Texas Instruments MSP430 family of microcontrollers and related tools. Everyone welcome, all levels of familiarity/expertise.

Help needed for translating assembly code - Eddy - Aug 13 14:57:50 2008

Hi,

I am trying to build a keypad following the MSP430 application notes,
SLAA139. It's titled as "Implementing an Ultralow-Power Keypad
Interface With the MSP430". I understand most part of the notes, but I
don't get how it does the key scanning.

I read the assembly code and now trying to implementing it in C. Could
someone give me a hand to translate the following assembly code to C
code? I have tried it myself, but I failed to read any key in.

// The following is the assembly code from the application note
/////////////////////////////////////////////////////////////////////
#define KeyMask R15
#define LoopCount R14
#define KeyHex R13
#define KeyVal R5
mov #1,KeyMask ; Initialize scan mask
mov #4,LoopCount ; Initialize loop counter
clr KeyHex ; Clear register
bic.b #07h,&P1OUT ; Clear column bits in P1OUT reg
Scan_1 bic.b #0Fh,&P3OUT ; Stop driving rows
bis.b #07h,&P1DIR ; Set column pins to output and low
bic.b #07h,&P1OUT ; To bleed off charge and avoid
; erroneous reads
bic.b #07H,&P1DIR ; Set column pins back to input
Mov.b KeyMask,&P3OUT ; Drive row
bit.b #7h,&P1IN ; Test if any key pressed
jz Scan_2 ; No key pressed
bis.b KeyMask,KeyHex ; If yes, set bit for row
mov.b &P1IN,R12 ; Read column inputs
and.b #07h,R12 ; Clear unused bits
rla.b R12 ;
rla.b R12 ; Rotate column bit
rla.b R12 ;
rla.b R12 ;
bis.b R12,KeyHex ; Set column bit in KeyHex
Scan_2 rla.b KeyMask ; Rotate mask
dec LoopCount ; Decrement counter
jnz Scan_1 ; Continue scanning if not done
; Check to see if any key is being pressed. If not, set flag and return.
tst.b KeyHex ; Test KeyHex
jnz EndScan ; If not 0 return
bis.b #NoKey,Error_Flags ; Set flag
EndScan bis.b #0Fh,&P3OUT ; Drive rows again

// C code I have tried
/////////////////////////////////////////////////////////////////////
key KeyScan()
{
BSP_TOGGLE_LED1();
P2OUT &= ~0x07; // clear bits on Port 2

key enter;
enter.column = 0x00;
enter.column = 0x00;
enter.flag = 0;

for(int mask = 0; mask <= 3; mask++)
{
P4OUT &= ~0x78; // Stop Driving Rows
P2DIR |= 0x07; // Set Column pins to output and low
P2OUT &= ~0x07; // To discharge

P2DIR &= ~0x07; // Set Column pins back to input

P3OUT = 0x01<
// Check if key pressed and save the column & row
if (P2IN)
{

enter.column = P2IN;
enter.row = 0x01< enter.flag = 0;
}
// else save column & row to zero and set flag for no key being
pressed
else
{
enter.column = 0x00;
enter.row = 0x00;
enter.flag = 1;
}
}

P4OUT |= 0x78; // Drive row again
return enter;
}
------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )


Re: Help needed for translating assembly code - Jon Kirwan - Aug 13 23:24:14 2008

On Wed, 13 Aug 2008 18:57:43 -0000, you wrote:

>I am trying to build a keypad following the MSP430 application notes,
>SLAA139. It's titled as "Implementing an Ultralow-Power Keypad
>Interface With the MSP430". I understand most part of the notes, but I
>don't get how it does the key scanning.
>
>I read the assembly code and now trying to implementing it in C. Could
>someone give me a hand to translate the following assembly code to C
>code? I have tried it myself, but I failed to read any key in.

Are you recoding the entire application from scratch, using the
assembly as a template? Or are you just trying to replace a single
subroutine within an otherwise assembly program? Huge difference --
in fact, you may not even be able to get the c code generator to meet
the prologue, epilogue, and register usage requirements if all you are
doing is trying to write c code to drop into the otherwise assembly
program. If you are replacing the entire application in c, then that
is doable without worrying as much. (But in that case, your code
snippet is incomplete.)

What c compiler? The entire application recoded? Did you renumber
the port numbers like you might an array index going between c and
BASIC, as well?

Jon

------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )

RE: Help needed for translating assembly code - Pablo Villaverde Padilla - Aug 14 0:14:35 2008

Hi,

First a little comment.I strongly suggest you study the assembly code in
more detail to discover how it does the key scanning and why it works,
otherwise you are working with no clear idea of what your C code is supposed
to do, no matter what compiler you use. You see, that little code you
already have in C is not all that's required to make the app work, you also
need an interrupt routine for the port you are using for key scanning. Do
you have that working now? Have you tried to debug the code to see the port
interrupts are working ok, or if other parts of the code are doing what they
should?

I do know that when you eventually grasp the mechanics of that key scanning,
you'll be able make your code work.

Good luck!

Pablo

De: m...@yahoogroups.com [mailto:m...@yahoogroups.com] En nombre de Eddy
Enviado el: Wednesday, August 13, 2008 1:58 PM
Para: m...@yahoogroups.com
Asunto: [msp430] Help needed for translating assembly code

Hi,

I am trying to build a keypad following the MSP430 application notes,
SLAA139. It's titled as "Implementing an Ultralow-Power Keypad
Interface With the MSP430". I understand most part of the notes, but I
don't get how it does the key scanning.

I read the assembly code and now trying to implementing it in C. Could
someone give me a hand to translate the following assembly code to C
code? I have tried it myself, but I failed to read any key in.

// The following is the assembly code from the application note
/////////////////////////////////////////////////////////////////////
#define KeyMask R15
#define LoopCount R14
#define KeyHex R13
#define KeyVal R5
mov #1,KeyMask ; Initialize scan mask
mov #4,LoopCount ; Initialize loop counter
clr KeyHex ; Clear register
bic.b #07h,&P1OUT ; Clear column bits in P1OUT reg
Scan_1 bic.b #0Fh,&P3OUT ; Stop driving rows
bis.b #07h,&P1DIR ; Set column pins to output and low
bic.b #07h,&P1OUT ; To bleed off charge and avoid
; erroneous reads
bic.b #07H,&P1DIR ; Set column pins back to input
Mov.b KeyMask,&P3OUT ; Drive row
bit.b #7h,&P1IN ; Test if any key pressed
jz Scan_2 ; No key pressed
bis.b KeyMask,KeyHex ; If yes, set bit for row
mov.b &P1IN,R12 ; Read column inputs
and.b #07h,R12 ; Clear unused bits
rla.b R12 ;
rla.b R12 ; Rotate column bit
rla.b R12 ;
rla.b R12 ;
bis.b R12,KeyHex ; Set column bit in KeyHex
Scan_2 rla.b KeyMask ; Rotate mask
dec LoopCount ; Decrement counter
jnz Scan_1 ; Continue scanning if not done
; Check to see if any key is being pressed. If not, set flag and return.
tst.b KeyHex ; Test KeyHex
jnz EndScan ; If not 0 return
bis.b #NoKey,Error_Flags ; Set flag
EndScan bis.b #0Fh,&P3OUT ; Drive rows again

// C code I have tried
/////////////////////////////////////////////////////////////////////
key KeyScan()
{
BSP_TOGGLE_LED1();
P2OUT &= ~0x07; // clear bits on Port 2

key enter;
enter.column = 0x00;
enter.column = 0x00;
enter.flag = 0;

for(int mask = 0; mask <= 3; mask++)
{
P4OUT &= ~0x78; // Stop Driving Rows
P2DIR |= 0x07; // Set Column pins to output and low
P2OUT &= ~0x07; // To discharge

P2DIR &= ~0x07; // Set Column pins back to input

P3OUT = 0x01<
// Check if key pressed and save the column & row
if (P2IN)
{

enter.column = P2IN;
enter.row = 0x01< enter.flag = 0;
}
// else save column & row to zero and set flag for no key being
pressed
else
{
enter.column = 0x00;
enter.row = 0x00;
enter.flag = 1;
}
}

P4OUT |= 0x78; // Drive row again
return enter;
}

[Non-text portions of this message have been removed]
------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )

Re: Help needed for translating assembly code - Eddy - Aug 14 14:35:58 2008

Hi guys,
Thanx for replying. Few things I did not mention in the last msg. I am
using IAR to code. The codes I posted are just a function of the
keypad application. I read over the application notes and understood
most of it, but just not this KeyScan function. I am trying to rewrite
this entire program in C, not just trying to use the compiler to
translate.

I think I got the basic idea of how this KeyScan function is working.
So I wrote my own C code to try to perform the same routine. Perhaps,
I am missing some key points in the function; my C code doesn't return
the proper KeyScan result. And yes, I have the port2 interrupt working
fine, but I still need to determine which key is being pressed.

Could someone just help me to explain this assembly code just the
KeyScan function?

Thanx,

Eddy

--- In m...@yahoogroups.com, "Pablo Villaverde Padilla"
wrote:
>
> Hi,
>
> First a little comment.I strongly suggest you study the assembly code in
> more detail to discover how it does the key scanning and why it works,
> otherwise you are working with no clear idea of what your C code is
supposed
> to do, no matter what compiler you use. You see, that little code you
> already have in C is not all that's required to make the app work,
you also
> need an interrupt routine for the port you are using for key
scanning. Do
> you have that working now? Have you tried to debug the code to see
the port
> interrupts are working ok, or if other parts of the code are doing
what they
> should?
>
>
>
> I do know that when you eventually grasp the mechanics of that key
scanning,
> you'll be able make your code work.
>
>
>
> Good luck!
>
>
>
> Pablo
>
>

------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )