Thursday, 27 December 2007

Improving the PC-> PIC Interface

The first assembler program simply checked continually for data over the RS232 line and stored the information in a byte of memory. It then shunted the raw ASCII data to the LCD. However the LCD requires a time delay of 40us after each character is written before it can accept the next one. This means that the PIC then falls behind in receiving the next ASCII character from the PC.

To get around this, it was necessary to add a delay in the HyperTerminal program between sending each byte of data. This resulted in a tedious process of seeing the text appear one character at a time on the screen.

Therefore, I decided it would be best to send a complete 'screen' of data before attempting to write it. To achieve this it was necessary to create a buffer in the PIC memory, large enough to store 32 characters (bytes). This is easier said than done, since assembler provides no support for arrays and all memory storage must be preceded by a memory address. So I created a special subroutine to do this. Firstly I initialised 32 variables at the start of the program, then in the subroutine I used the provision of the PIC to manually advance the program counter so that with each new byte to be stored, the program jumps to the appropriate line of code and the next free variable. It is best understood by looking at a pseudo code example:



int byte1;
int byte2;
... ;32byte buffer
int byte32;

int current_count = 0;

function add_to_buffer(rs232_byte){
program_counter = program_counter + 2*current_count
byte1 = rs232_byte;
goto FINISH;
byte2 = rs232_byte;
goto FINISH;
......
byte31 = rs232_byte;
goto FINISH;

byte32 = rs232_byte;
goto FINISH;

FINSIH

if(current_count = 32){
current_count = 0;
write_data_to_lcd();
return;
}
else{
current_count++;
return;
}
}

The Result

With the buffer there is no need for a delay between each character since the time it takes to write to the buffer is much less than the interval between bytes sent over the serial connection. Now, once the buffer is full, the contents of the buffer is written immediately to the screen in one go, and the LCD appears to have changed 'all at once' instead of character by character. The end result is much more professional and enjoyable to read.

Also...

Another source of delay and annoyance was having to write 32 blank characters to the display in order to remove the previous text and clear the screen. To get around this I chose to use one of the un-used ASCII codes to act as a 'clear the screen' command which the PIC listens out for.

What Next.. C#!

HyperTerminal is good to prove a principle and for testing hardware but isn't very interesting, flexible or elegant to use. Therefore I chose to use Visual C# to write anything I want, including dynamic data such as emails, now playing info, and rss feeds. It turns out this isn't as difficult as you'd imagine, and in the next blog I will show you how I did it.

The Code

The PIC assembly code for this and previous versions will be available for download from the new DocDrop pages under the folder PIC Project.

(This post was originally available at http://users.ecs.soton.ac.uk/cc1206 and refers to DocDrop which is still availble at http://users.ecs.soton.ac.uk/cc1206/docdrop2.php. DocDrop will soon be 'ported' to this site)

No comments: