Jump to content
43oh

LCD library in Assembly


Recommended Posts

Hello everyone. I'm a beginner at forum and I don't know if I can post this here. Sorry if im wrong =/

 

So... I'm writing a simple program that sends hours,minutes and seconds to lcd. I know... im very newbie...

Im using a 16x2 LCD 4 bits mode.

I have this c code and I want to send upper and lower nibble like that.

I've just made a search over the forum and only found this in c code.

Anyone have idea how to do this work?

My biggest doubt is how to write that part in assembly:  P2OUT |= ((Data >> 4) & 0x0F);

Thanks.

Beco08

 

 

void lcdcmd(unsigned char Data)
{
 
P2OUT &= ~RS; //because sending command //BIC.B #0X40,P2OUT
P2OUT  &=~EN;                                                //BIC.B #0x10,P2OUT
 
P2OUT &= 0xF0;                                              //BIC.B #0x0F,P2OUT
P2OUT |= ((Data >> 4) & 0x0F);
P2OUT  |=EN;
waitlcd(2);
P2OUT  &=~EN;
 
P2OUT &= 0xF0;
P2OUT  |= (Data & 0x0F);
P2OUT  |=EN;
waitlcd(2);
P2OUT  &=~EN;

}

Link to post
Share on other sites

Assuming the parameter "data" is passed to your function in register R12:

MOV.B R12, R13       ; use another register, we'll need R12 further down 
RRA.B R13             ; rotate right by 1.. 4 times
RRA.B R13             ; I'm surprised there's no RRA.B #4,R13 or some such
RRA.B R13
RRA.B R13
AND.B #0fh, R13
BIS.B R13, P2OUT

not tested and without warranties :smile:

 

PS: a convenient method is to let the compiler do the work and check the generated code in the debugger, or .asm output file if you find the respective compiler switch

Link to post
Share on other sites

As to how the code works, be aware that there are many different LCDs and just about as many protocols to talk to them. Do you have a datasheet or other documentation for the one you're using?

 

The fragment above does:

- set a RS line to low, presumably indicating that we want to write to the LCD (instead of reading)

- set an EN line to low, I guess that's the clock

- set the 4 upper bits of the command on port 2, I guess the LCD uses 4 lines to transfer data, connected to the lower 4 bits of the port

- set EN high, tell the LCD there's something ready to be read

- wait a while (2 microseconds?)

- set EN low

- set the 4 lower bits of the command

- set EN high

 

Any particular reason you need to implement this in assembly? The C compiler will do a pretty good job on these basic routines.

Link to post
Share on other sites

Thank you for reply. I have the lcd datasheet and I know the pins configurations.

The reason is just this. I've implemented the c code e it works perfectly. But i'm a student and about my curiosity I want to write the same project in assembly. I'm 2 years without programming in asm and a lot of details has gone =/ 

My doubt is about to send first upper bits then lower bits cause when I run the code the lcd character is fully filled out. The other routine that program lcd I think its right.

I will test the code you posted.

Thanks again.

Link to post
Share on other sites

Assuming the parameter "data" is passed to your function in register R12:

MOV.B R12, R13       ; use another register, we'll need R12 further down 
RRA.B R13             ; rotate right by 1.. 4 times
RRA.B R13             ; I'm surprised there's no RRA.B #4,R13 or some such
RRA.B R13
RRA.B R13
AND.B #0fh, R13
BIS.B R13, P2OUT

not tested and without warranties :smile:

 

PS: a convenient method is to let the compiler do the work and check the generated code in the debugger, or .asm output file if you find the respective compiler switch

Since you're using the RRA.B instead of RRA, you don't need to do the AND.B anymore; you basically casted Data to a byte, a byte shifted right by 4 is already in the mast 0x0F.

Other than that your code seems right :smile:

 

TI chose to design the MSP430 without a barrel shifter. A barrel shifter allows you to do arbitrary shift operations. The downside of barrel shifters is that they tend to be very big, only surpassed by multiplier in their die size.

So TI implemented only a single shift right by 1 operation and emulate the other shift operations using assembler. So shift right by more is just multiple shifts. Shift left is done by adding the number to itself, so to shift R13 left by 1 you do ADD R13, R13. Any SHL or RLA is just a facade to an addition.

In addition, TI implemented just one shifty operation, the SWPB operation. This can be perceived as a rotate by 8, similar to RLA R13 8 times and then ADC R13, 0 to map the last bit held in the carry flag to the LSB.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...