/************************************************************* * jl16-6.c - Demonstrate interface to a serial LCD display * * This program will print a message on an LCD display * using a serial interface. It is designed to work with a * Matrix Orbital LK204-25 using an RS-232 interface. * * Revision History * Date Author Description * 11/07/07 A. Weber First Release * 02/26/08 A. Weber Code cleanups * 03/03/08 A. Weber More code cleanups * 04/22/08 A. Weber Added "one" variable to make warning go away * 09/01/11 A. Weber Changed some function names * 07/09/12 A. Weber Removed code to set security bytes * 05/29/13 A. Weber Renamed CWJL-6.c to jl16-6.c *************************************************************/ #include /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */ void lcd_init(void); void lcd_out(int, int, unsigned char *); void del1m(int); void sci_init(unsigned char, unsigned char); void sci_out(unsigned char); void sci_outs(unsigned char *); /* This pragma sets the area for allocating variables to the zero page of RAM (0x00 to 0xff) for more efficient access. */ #pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE unsigned char delay0; unsigned char delay1; /* This pragma sets the area for allocating variables back to the default area of RAM as defined in the PRM file. */ #pragma DATA_SEG DEFAULT /* Declare these strings as "const" so they are allocated in ROM */ const unsigned char str1[] = "12345678901234567890"; const unsigned char str2[] = ">> USC EE459L <<"; const unsigned char str3[] = ">>> jl16-6.c <<<"; const unsigned char str4[] = "- November 7, 2007 -"; void main(void) { unsigned char one = 1; // EnableInterrupts; /* enable interrupts */ /* include your code here */ CONFIG1_COPD = 1; // disable COP reset sci_init(0, 1); // Initialize the SCI port to 19,200 // Assumes a 9.8304MHz clock // SCBR_SCP = 0 => Baud rate prescaler = 1 // SCBR_SCR = 1 => Baud rate divisor = 2 lcd_init(); // Initialize the LCD lcd_out(1, 1, (unsigned char *) str1); // Print string on line 1 lcd_out(3, 2, (unsigned char *) str2); // Print string on line 2 lcd_out(3, 3, (unsigned char *) str3); // Print string on line 3 lcd_out(1, 4, (unsigned char *) str4); // Print string on line 4 while (one) { // Loop forever } } /* lcd_init - Initialize the LCD */ void lcd_init() { del1m(500); // Wait 500msec for the LCD to start up sci_out(0xfe); // Clear the screen sci_out(0x58); } /* lcd_out - Print the contents of the character string "s" starting at LCD location "col" and "row". The upper left corner is col=1, row=1. The string must be terminated by a zero byte. */ void lcd_out(int col, int row, unsigned char *s) { sci_out((unsigned char) 0xfe); // Set the cursor position sci_out((unsigned char) 0x47); sci_out((unsigned char) col); sci_out((unsigned char) row); sci_outs(s); // Output the string } /* ----------------------------------------------------------------------- */ /* sci_init - Initialize the SCI port */ void sci_init(unsigned char scp, unsigned char scr) { SCC1_ENSCI = 1; // Enable SCI SCBR_SCP = scp; // Baud rate prescaler SCBR_SCR = scr; // Baud rate divisor SCC2_TE = 1; // Enable transmitter } /* sci_out - Output a byte to SCI port */ void sci_out(unsigned char ch) { while (SCS1_SCTE == 0); SCDR = ch; } /* sci_outs - Print the contents of the character string "s" out the SCI port. The string must be terminated by a zero byte. */ void sci_outs(unsigned char *s) { unsigned char ch; while ((ch = *s++) != (unsigned char) NULL) sci_out(ch); } /* ----------------------------------------------------------------------- */ /* del1m - Delay about 1msec times the "d" argument. */ void del1m(int d) { while (d--) { asm { ; The following code delays 1 millisecond by looping. ; Total time is 4 + 5 * (4 + 68 * (4 + 3) + 4 + 3) = 2439 cycles ; A 9.8304MHz external clock gives an internal CPU clock of ; 2.4576MHz (407ns/cycle). Delay is then .993 milliseconds. mov #5,delay1 ; 4 cycles m1: mov #68,delay0 ; 4 cycles m0: dec delay0 ; 4 cycles bne m0 ; 3 cycles dec delay1 ; 4 cycles bne m1 ; 3 cycles } } }