0
votes

So I am trying to translate some code for operating an LCD from Arduino to C for an MSP430F5529. The LCD uses SPI for communication and I am confused on how this code is causing the Arduino to communicate with it properly, and was hoping that someone with decent Arduino experience would be able to explain some things (I have never used one). I have linked to the entire program on pastebin at the end, but this is the section I am focused on right now:

void comm_out(char c)
{
  int i;
  digitalWrite(CS, LOW);
  digitalWrite(AO, LOW);
  for(i=0;i<8;i++)
  {
    if((c&0x80) == 0x80)
    {
      PORTA |= 0x80;
      PORTA &= ~0x40;
      PORTA |= 0x40;
    }
    else
    {
      PORTA &= ~0x80;
      PORTA &= ~0x40;
      PORTA |= 0x40;
    } 
    c = c<<1;
  }
  digitalWrite(CS, HIGH);
  digitalWrite(AO, HIGH);
}

So I understand any of the code that has to do with setting some of the digital pins on the Arduino (looks like they are only ever output because the program never sets a direction for them) to high or low as that is pretty straight forward, but I don't quite get what the for loop is doing in terms of SPI.

I get what the function is doing directly. It gets an 8-bit input that it decided was a character (though that doesn't really mater) and checks to see if the first bit is a 1 or 0. If it is a 1 it sets PORTA to the following in 3 steps:

1: 1xxx xxxx 2: 10xx xxxx 3: 11xx xxxx

and if it is a 0 then it sets PORTA to the following in 3 steps:

1: 0xxx xxxx 2: 00xx xxxx 3: 01xx xxxx

It then shifts the input so that the next bit is highest and does the check again until its checked all 8 bits. But I have no clue what this is doing to the Arduino that makes it properly communicate to the LCD over serial. From all the documentation I've checked I can't even determine what PORTA is. I had assumed that it might be mapped to some kind of module for serial communication but it doesn't seem like it.

The other thing I was confused about were the declarations at the top

int SCLK = 28; // SCL signal connected to digital pin 28 of Arduino Mega     
int SI = 29; // SI signal connected to digital pin 29 of Arduino Mega     
int CS = 30; // CS signal connected to digital pin 30 of Arduino Mega     
int RES = 31; // RES signal connected to digital pin 31 of Arduino Mega     
int AO = 32; // A0 signal connected to digital pin 32 of Arduino Mega

A0, RES, and CS are all fine because those are just static pins that are set up for down, but SI is supposed to be serial input and SCLK is supposed to be the clock signal for SPI, but as far as I can tell in doccumentation port 28 and 29 aren't anything special and just generic I/O ports.

Here is the port map I was referring to: http://pighixxx.com/atmega2560v3_0.pdf

Here is the the link to the entirety of the code: http://pastebin.com/DxMGJZDu

Any advice or rather, any anything? I am struggling to make sense of this.

1
Minor: igitalWrite(CS, LOW); digitalWrite(AO, LOW); --> to me, it makes sense to do this the other way around. Establish port A0 before enabling the chip select. Also PORTA=0 before chip select and after the final CS de-select.chux - Reinstate Monica
I think you missed exactly what the question was about. The code I have pasted was given to me by the manufacturer of the LCD and I am translating it to regular C for use on an MSP430. I need help understanding what part of the code is doing, I have no sense of how the Arduino works other than the documentation I've looked at, which hasn't been too helpfuloblivioncth
I think you missed exactly what the comment was about. It was meant to be a minor thought about the issues you are having and to help identify potential issues others may see a leading to a solution. If it was a solution, it would be posted as an answer. So I take it the request for "any anything?" meant to not comment on things but give me the answer.chux - Reinstate Monica
Sorry, I didn't mean to come off as abrasive, I was genuinely confused. I appreciate your time to look over the code. The problem was that since this is the official code that was used for testing the LCD I was planning on modifying it at all, using it only as a base through which to write my own version for the MSP430. Your comment is helpful for optimizing my implementation, but not for actually converting the code. I failed to think of that at the time I replied. I will try your suggestion in my code, I have it working now. Thanks.oblivioncth
Coding tip: key element in bit-banging code is that all bets are off unless initial conditions are met. To that end 1) at program start, assume inputs are in a unknown state (0,1,z) and code needs to clearly establish them. 2) in function start, code should avoid assuming pin values are in a particular state. Code could un-select CS, set pins as needed, then assert CS. 3) at function end, un-select CS, and set outputs, as needed to an inactive state be that 0,1, or z. In debugging transactions, too often the transaction was OK, it was just that the initial condition was not met. Good luck.chux - Reinstate Monica

1 Answers

1
votes

This code implements SPI with bit banging, which

is a technique for serial communications using software instead of dedicated hardware. Software directly sets and samples the state of pins on the microcontroller, and is responsible for all parameters of the signal: timing, levels, synchronization, etc.

In SPI, the receiving device reads the state of the data line when a certain edge happens on the clock line (raising or falling; either is possible, depending on the device).

For each bit, this code sets the value of the data line, and then cycles the clock line low and high.