JosephGaudet
New member
So long and short I've hooked this display up to a uController from free scale, i've got it to turn on and it appears to accept the odd instruction, however I have yet to be able to get it to do anything relevant.
I am simply try to write characters to it, i've been trying to get it to write a over and over again.
please find attached my code.
If you have any suggestions they would be most appreciated.
Some things of note, the DDRA registers switch the direction of data flow through the IO ports in the free scale micro.
main.c
LCD.C
LCD.h
I am simply try to write characters to it, i've been trying to get it to write a over and over again.
please find attached my code.
If you have any suggestions they would be most appreciated.
Some things of note, the DDRA registers switch the direction of data flow through the IO ports in the free scale micro.
main.c
Code:
#include <hidef.h> /* common defines and macros */
#include <mc9s12dt256.h> /* derivative information */
#include "LCD.h"
#pragma LINK_INFO DERIVATIVE "mc9s12dt256b"
void wait(){
int i;
for(i = 0; i < 1000; i++)
asm("nop");
}
void main(void) {
/* put your own code here */
//EnableInterrupts;
initDisplayInterface();
setLCDCharMode();
setCharPitch(VP6,HP6);
setNumberOfCharactersPerLine(30);
setTimeDiv(128);
//setCursorPosition(0);
for(;;) {
writeChar(41);
} /* wait forever */
/* please make sure that you never leave this function */
}
Code:
#include "LCD.h"
#include <stdtypes.h>
#include <mc9s12dt256.h>
#define RW PORTA_BIT0
#define RS PORTA_BIT1
#define DATA PORTB
#define E PORTA_BIT2
// Basic command for drawing a line, provide two points, from this we determine
// the slope intercept form and generate bit set commands.
void drawLine(Byte x1, Byte y1, Byte x2, Byte y2){
int m = (y2 - y1)/(x1 - x2);
int c = y2-m*x2;
Byte x, y;
for(x = x1; x <= x2; x++){
y = m*x + c;
setBitAtPosition(x%8&0xff,positionFromCoordinate(x,y));
}
}
void drawBox(Byte x1, Byte y1, Byte x2, Byte y2){
// horizontal edges
drawLine(x1,y1,x2,y1);
drawLine(x1,y2,x2,y2);
// vertical edges
drawLine(x1,y1,x1,y2);
drawLine(x2,y1,x2,y2);
}
void clearDisplay(void){
int i;
setCursorPosition(0);
for(i = 0; i < 0xF00; i++){
Byte j;
for(j = 0; j < 8; j++){
bitClear(j);
}
}
}
void fillDisplay(void){
int i;
setCursorPosition(0);
for(i = 0; i < 0xF00; i++){
Byte j;
for(j = 0; j < 8; j++){
bitSet(j);
}
}
}
/* Basic Display Operation Stuff */
void setBitAtPosition(Byte bitNumber, int position){
setCursorPosition(position);
bitSet(bitNumber);
}
void clearBitAtPosition(Byte bitNumber, int position){
setCursorPosition(position);
bitClear(bitNumber);
}
void setCursorPosition(int position){
writeInstruction(INST_CURSOR_LOWER_ADD);
writeData(position&0xff);
writeInstruction(INST_CURSOR_UPPER_ADD);
writeData((position>>8)&0xff);
}
int positionFromCoordinate(Byte x1, Byte y1){
int column = x1/8;
int rowOffset = 30*y1;
return column + rowOffset;
}
void initDisplayInterface(void){
DDRA = DDRA|0x03; // Setting the two control lines to output lines
DDRB = 0xFF; // Setting Port B to output for default functionality
}
void writeInstruction(Byte inst){
waitForNotBusy();
RW = 0;
RS = 1;
E = 1;
DATA = inst;
E = 0;
RW = 0;
RS = 0;
}
void setLCDCharMode(void){
Byte instruction = DSP_ON_OFF;
writeModeControl(instruction);
}
void writeModeControl(Byte mode){
writeInstruction(INST_MODE_CONTROL);
writeData(mode);
}
void writeData(Byte data){
RW = 0;
RS = 0;
E = 1;
DATA = data;
E = 0;
}
Byte readData(void){
byte result;
RW = 1;
RS = 0;
E = 1;
DDRB = 0x00;
result = PORTB;
DDRA = 0xFF;
E = 0;
RW = 0;
RS = 0;
return result;
}
// The Byte number is between 0 and 7
void bitClear(Byte bitNumber){
writeInstruction(INST_BIT_CLEAR);
writeData(bitNumber&0x7);
}
void setCharPitch(Byte verticalPitch, Byte horizontalPitch){
writeInstruction(INST_CHAR_PITCH);
writeData(verticalPitch|horizontalPitch);
}
void setNumberOfCharactersPerLine(Byte numberOfCharacters){
writeInstruction(INST_HN);
writeData(numberOfCharacters);
}
void setTimeDiv(Byte timeDiv){
writeInstruction(INST_DISPLAY_DUTY);
writeData(timeDiv);
}
void bitSet(Byte bitNumber){
writeInstruction(INST_BIT_SET);
writeData(bitNumber&0x7);
}
void writeDisplayData(Byte data){
writeInstruction(INST_W_DSP_DATA);
writeData(data);
}
Byte readDisplayData(void){
writeInstruction(INST_R_DSP_DATA);
return readData();
}
void writeChar(char value){
writeDisplayData((Byte)value);
}
void waitForNotBusy(void){
byte i;
for(i = 0; i < 10; i++)
asm("nop");
//while(isBusy()){
// asm("nop");
//}
}
Bool isBusy(){
byte busyFlag;
Bool result;
RS = 1;
RW = 1;
E = 1;
DDRB = 0x00;
busyFlag = DATA;
E = 0;
RS = 0;
RW = 0;
DDRB = 0xFF;
busyFlag = busyFlag>>7;
result = FALSE;
if(busyFlag != 0)
result = TRUE;
return result;
}
Code:
#include <stdtypes.h>
//Function Prototypes
// drawing Methods
void drawLine(Byte x1, Byte y1, Byte x2, Byte y2);
void drawBox(Byte x1, Byte y1, Byte x2, Byte y2);
void clearDisplay(void);
void fillDisplay(void);
void writeChar(char value);
void setBitAtPosition(Byte bitNumber, int position);
void clearBitAtPosition(Byte bitNumber, int position);
void setCursorPosition(int position);
int positionFromCoordinate(Byte x1, Byte y1);
void initDisplayInterface(void);
void setLCDCharMode(void);
// Internal LCD operation
void writeInstruction(Byte inst);
void writeModeControl(Byte mode);
void writeData(Byte data);
Byte readData(void);
void bitClear(Byte bitNumber);
void bitSet(Byte bitNumber);
void writeDisplayData(Byte data);
Byte readDisplayData();
void writeChar(char value);
void waitForNotBusy(void);
Bool isBusy(void);
void setCharPitch(Byte verticalPitch, Byte horizontalPitch);
void setNumberOfCharactersPerLine(Byte numberOfCharacters);
void setTimeDiv(Byte timeDiv);
/* Mode Control */
// Mode control register
#define INST_MODE_CONTROL 0x0
// Mode Masks
#define EXTERNAL_BUILT_IN_CG 1
#define DSP_MODE 2
#define CURSOR 4
#define BLINK 8
#define MASTER_SLAVE 16
#define DSP_ON_OFF 32
/* Pitch */
// pitch register
#define INST_CHAR_PITCH 1
// Horizontal Pitch
#define HP0 0
#define HP1 1
#define HP2 2
#define HP3 3
#define HP4 4
#define HP5 5
#define HP6 6
#define HP7 7
#define HP_MASK 3
// Vertical Pitch
#define VP0 0
#define VP1 16
#define VP2 32
#define VP3 48
#define VP4 64
#define VP5 80
#define VP6 96
#define VP7 112
#define VP8 128
#define VP9 144
#define VP10 160
#define VP11 176
#define VP12 192
#define VP13 208
#define VP14 224
#define VP15 240
#define VP_MASK 240
/* Character Numbers */
// Number of Characters per line Register - HN
#define INST_HN 0x02
/* Time Division */
// Display Duty Register - this value should be 160.
#define INST_DISPLAY_DUTY 0x03
/* cursor */
// Cursor Position Register
#define INST_CURSOR_POSITION 0x04
#define INST_CURSOR_UPPER_ADD 0x0A
#define INST_CURSOR_LOWER_ADD 0x0B
#define CURSOR_POSITION_MASK 0x0F
/* Display Addresses */
// Start Lower Address
#define INST_DSP_LOWER_ADD 0x08
#define INST_DSP_UPPER_ADD 0x09
/* Display Data */
// Display Data Register
#define INST_W_DSP_DATA 0x0D
#define INST_R_DSP_DATA 0x0C
#define INST_BIT_CLEAR 0x0E
#define INST_BIT_SET 0x0F
// some memory address for display cells
#define TOP_LEFT 0x0;
#define TOP_RIGHT 0x1D;
#define BOTTOM_RIGHT 0xEFF;
#define BOTTOM_LEFT 0xECF;
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.