#byte PORTA = 0xF80
#bit RA0 = PORTA.0
#bit RA1 = PORTA.1
#bit RA2 = PORTA.2
#bit RA3 = PORTA.3
#bit RA4 = PORTA.4
#bit RA5 = PORTA.5
#bit RA6 = PORTA.6
#bit RA7 = PORTA.7
#byte PORTB = 0xF81
#bit RB0 = PORTB.0
#bit RB1 = PORTB.1
#bit RB2 = PORTB.2
#bit RB3 = PORTB.3
#bit RB4 = PORTB.4
#bit RB5 = PORTB.5
#bit RB6 = PORTB.6
#bit RB7 = PORTB.7
#byte PORTC = 0xF82
#bit RC0 = PORTC.0
#bit RC1 = PORTC.1
#bit RC2 = PORTC.2
#bit RC3 = PORTC.3
#bit RC4 = PORTC.4
#bit RC5 = PORTC.5
#bit RC6 = PORTC.6
#bit RC7 = PORTC.7
#byte PORTE = 0xF84
#bit RE3 = PORTE.3
/////////////////////////////////////////////////////////////////////////
//// mg1903a.h ////
//// ////
//// This file contains drivers for using a Everbouquet MG1903A with ////
//// a ST7920 display controller. The MG1903A is 192 by 32 pixels. ////
//// The driver treats the upper left pixel as (0,0). ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// LCD Pin connections: ////
//// * 1: VSS is connected to GND ////
//// * 2: VDD is connected to +5V ////
//// * 3: V0 - LCD operating voltage (Constrast adjustment) ////
//// * 4: R/S - Data or Instruction is connected to C7 ////
//// * 5: R/W - Read or Write is connected to C6 ////
//// * 6: Enable is connected to C5 ////
//// * 7-14: Data Bus 0 to 7 is connected to port B ////
//// * 15: Positive voltage for LED backlight is connected to +5V ////
//// * 16: Negavtive voltage for LED backlight is connected to GND ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// glcd_init () ////
//// * Must be called before any other function. ////
//// ////
//// glcd_pixel (x,y,color) ////
//// * Sets the pixel to the given color. ////
//// - color can be ON or OFF ////
//// ////
//// glcd_fillScreen (color) ////
//// * Fills the entire LCD with the given color. ////
//// - color can be ON or OFF ////
//// ////
/////////////////////////////////////////////////////////////////////////
#define GLCD_RS RC7 // Display Data / Control Data.
#define GLCD_RW RC6 // Read / Write
#define GLCD_E RC5 // Enable Signal.
#define GLCD_WIDTH 192
#define RS_FUNCTION 0
#define RS_DATA 1
#define RW_WRITE 0
#define RW_READ 1
#define TRIS_READ 0xFF
#define TRIS_WRITE 0x00
// Valores de color:
#define ON 1
#define OFF 0
typedef union
{
int16 word;
BYTE byte[2];
} Dots;
typedef struct
{
BOOLEAN refrescar;
Dots dots[32][12]; // Diemensiones máximas del display (x,y) = (191,31).
} GDRAM; // El punto (0,0) corresponde a la esquina superior izquierda.
GDRAM gdram;
/*-----------------------------------------------------------------------------------------
* Nombre: glcd_readByte
*
* Propósito: Reads a byte of data from the LCD.
*
* In/Out:
*
* Return: A byte of data read from the LCD.
*
* Requisitos:
*
* Descripción:
*----------------------------------------------------------------------------------------*/
BYTE glcd_readByte (BYTE address)
{
BYTE data; // Stores the data read from the LCD
set_tris_b (TRIS_READ); // Set PORTB to input
GLCD_RS = address;
delay_cycles(1);
GLCD_RW = RW_READ; // Set for reading
delay_cycles (1);
GLCD_E = 1; // Pulse the enable pin
delay_cycles (1);
data = PORTB; // Get the data from the display's output register
GLCD_E = 0;
return data;
}
/*------------------------------------------------------------------------------------------
* Nombre: glcd_writeByte
*
* Propósito: Write a byte of data to the LCD.
*
* In/Out:
*
* Return:
* - data (in): the byte of data to write.
*
* Requisitos:
*
* Descripción:
*
*----------------------------------------------------------------------------------------*/
void glcd_writeByte (BYTE address, BYTE data)
{
GLCD_RS = RS_FUNCTION;
while ( bit_test (glcd_readByte(RS_FUNCTION), 7) ) ; // Whait Busy Flag = FALSE!
set_tris_b (TRIS_WRITE); // Set PORTB to output
GLCD_RS = address;
delay_cycles(1);
GLCD_RW = RW_WRITE; // Set for writing
delay_cycles(1);
GLCD_E = 0;
PORTB = data; // Put the data on the port
delay_cycles (1);
GLCD_E = 1; // Pulse the enable pin
delay_cycles(5);
GLCD_E = 0;
}
/*------------------------------------------------------------------------------------------
* Nombre: glcd_fillScreen
*
* Propósito: Fill the LCD screen with the passed in color.
*
* In/Out:
* - color (in):
* ON - turn all the pixels on.
* OFF - turn all the pixels off.
*
* Return:
*
* Requisitos:
*
* Descripción: Sólo trabaja sobre la caché del display.
*
*----------------------------------------------------------------------------------------*/
void glcd_fillScreen (BYTE color)
{
int8 v, h;
int16 d;
d = (color == ON ? 0xFFFFL : 0x0000L);
for (v=0; v<32; v++)
{
for (h=0; h<12; h++)
{
gdram.dots[v][h].word = d;
}
}
gdram.refrescar = TRUE;
}
/*------------------------------------------------------------------------------------------
* Nombre: glcd_update
*
* Propósito: Update the LCD with data from the display cache.
*
* In/Out:
*
* Return:
*
* Requisitos:
*
* Descripción:
*----------------------------------------------------------------------------------------*/
void glcd_update ()
{
int8 v, h;
if (gdram.refrescar)
{
for (v=0; v<32; v++)
{
glcd_writeByte (RS_FUNCTION, 0x80 | v); // Set Vertical Address.
glcd_writeByte (RS_FUNCTION, 0x80 | 0); // Set Horizontal Address.
for (h=0; h<12; h++)
{
glcd_writeByte (RS_DATA, gdram.dots[v][h].byte[1]); // Write High Byte.
glcd_writeByte (RS_DATA, gdram.dots[v][h].byte[0]); // Write Low Byte.
}
}
gdram.refrescar = FALSE;
}
}
/*------------------------------------------------------------------------------------------
* Nombre: glcd_init
*
* Propósito: Inicialza el LCD.
*
* In/Out:
* - mode (in): OFF/ON - Turns the LCD off/on.
*
* Return:
*
* Requisitos: Call before using any other LCD function.
*
* Descripción:
*----------------------------------------------------------------------------------------*/
void glcd_init ()
{
set_tris_b (TRIS_WRITE); // PORTB as output.
GLCD_RS = RS_FUNCTION;
GLCD_RW = RW_WRITE;
GLCD_E = 0;
delay_ms (40);
glcd_writeByte (RS_FUNCTION, 0x30); // Specify 8 bit interface and basic instruction set.
delay_us (100);
glcd_writeByte (RS_FUNCTION, 0x30); // Specify 8 bit interface and basic instruction set.
delay_us (37);
glcd_writeByte (RS_FUNCTION, 0x0C); // Specify Display on, Cursor off and Blink off.
delay_us (100);
glcd_writeByte (RS_FUNCTION, 0x01); // Display clear.
delay_ms (10);
glcd_writeByte (RS_FUNCTION, 0x06); // AC Increase (cursor move right), don't shift the display.
delay_us (72);
glcd_writeByte (RS_FUNCTION, 0x34); // Select extended instruction set.
delay_us (72);
glcd_writeByte (RS_FUNCTION, 0x36); // Graphic display ON.
glcd_fillScreen (OFF);
glcd_update ();
}
/*------------------------------------------------------------------------------------------
* Nombre:
*
* Propósito: Turn a pixel on a graphic LCD on or off.
*
* In/Out:
* - x (in): the x coordinate of the pixel.
* - y (in): y - the y coordinate of the pixel.
* - color (in): ON or OFF.
*
* Return:
*
* Requisitos:
*
* Descripción:
*----------------------------------------------------------------------------------------*/
void glcd_pixel(int8 x, int8 y, int1 color)
{
int8 v, h, b;
v = y;
h = x/16;
b = 15 - (x%16);
// Modify the actual word.
if (color == ON) bit_set (gdram.dots[v][h].word, b);
else bit_clear (gdram.dots[v][h].word, b);
gdram.refrescar = TRUE;
}
main example
#include "mg1903a.h"
#include "graphics.h"
#include <math.h>
#pragma zero_ram
void main()
{
int1 warn = FALSE;
int16 adc = 0L, adc_old = 0L;
char voltText[] = "Volts", warning[] = "Warning", W[] = "W";
float theta = 0;
inicializarMicro (); // Inicializa los registros de configuración del MCU y perifericos.
inicializarSD (); // Inicializa las estructuras de datos utilizadas por el programa.
activarSalidas (); // Inicializar las salidas.
glcd_rect(1, 5, 126, 15, NO, ON); // Outline the bar
glcd_text57(30, 18, voltText, 1, ON); // Display "Volts"
glcd_circle(145, 16, 10, NO, ON); // Draw the clock circle
while (TRUE)
{
adc = entradaAnalogica.valor; // Read a value from the ADC
displayVoltage (adc); // Display the reading
adc = (adc > 249L) ? 249L : adc; // Keep the value 249 or less
if(adc != adc_old)
{
glcd_rect (adc/2+1, 6, adc_old/2+1, 14, YES, OFF); // Clears the old bar
glcd_rect (1, 6, adc/2+1, 14, YES, ON); // Draws a new bar
adc_old = adc; // Set old value to new
if (adc > 200 && !warn) // Check if over 4 volts.
{
glcd_rect (68, 20, 110, 28, YES, ON); // Draw a filled black rectangle
glcd_text57 (70, 21, warning, 1, OFF); // Write "Warning" on the LCD
glcd_text57 (165, 10, W, 2, ON); // Escribir una W con el doble de tamaño.
warn = TRUE;
}
else if (adc <= 200 && warn)
{
glcd_rect (68, 20, 110, 28, YES, OFF); // Draw a filled white rectangle
glcd_rect (165, 10, 180, 28, YES, OFF); // Draw a filled white rectangle
warn = FALSE;
}
}
// The following 3 lines make the clock hand spin around
glcd_line (145, 16, 145+(int)(8*sin(theta)+.5), 16-(int)(8*cos(theta)+.5), OFF);
theta = (theta > 5.9) ? 0 : (theta += .3);
glcd_line (145, 16, 145+(int)(8*sin(theta)+.5), 16-(int)(8*cos(theta)+.5), ON);
glcd_update ();
// delay_ms(100); // Reduces flicker by allowing pixels to be on much longer than off.
}
}
Fonte: http://www.ccsinfo.com/forum/viewtopic.php?t=32819&highlight=st7920
#include "graphics.h"
#include <math.h>
#pragma zero_ram
void main()
{
int1 warn = FALSE;
int16 adc = 0L, adc_old = 0L;
char voltText[] = "Volts", warning[] = "Warning", W[] = "W";
float theta = 0;
inicializarMicro (); // Inicializa los registros de configuración del MCU y perifericos.
inicializarSD (); // Inicializa las estructuras de datos utilizadas por el programa.
activarSalidas (); // Inicializar las salidas.
glcd_rect(1, 5, 126, 15, NO, ON); // Outline the bar
glcd_text57(30, 18, voltText, 1, ON); // Display "Volts"
glcd_circle(145, 16, 10, NO, ON); // Draw the clock circle
while (TRUE)
{
adc = entradaAnalogica.valor; // Read a value from the ADC
displayVoltage (adc); // Display the reading
adc = (adc > 249L) ? 249L : adc; // Keep the value 249 or less
if(adc != adc_old)
{
glcd_rect (adc/2+1, 6, adc_old/2+1, 14, YES, OFF); // Clears the old bar
glcd_rect (1, 6, adc/2+1, 14, YES, ON); // Draws a new bar
adc_old = adc; // Set old value to new
if (adc > 200 && !warn) // Check if over 4 volts.
{
glcd_rect (68, 20, 110, 28, YES, ON); // Draw a filled black rectangle
glcd_text57 (70, 21, warning, 1, OFF); // Write "Warning" on the LCD
glcd_text57 (165, 10, W, 2, ON); // Escribir una W con el doble de tamaño.
warn = TRUE;
}
else if (adc <= 200 && warn)
{
glcd_rect (68, 20, 110, 28, YES, OFF); // Draw a filled white rectangle
glcd_rect (165, 10, 180, 28, YES, OFF); // Draw a filled white rectangle
warn = FALSE;
}
}
// The following 3 lines make the clock hand spin around
glcd_line (145, 16, 145+(int)(8*sin(theta)+.5), 16-(int)(8*cos(theta)+.5), OFF);
theta = (theta > 5.9) ? 0 : (theta += .3);
glcd_line (145, 16, 145+(int)(8*sin(theta)+.5), 16-(int)(8*cos(theta)+.5), ON);
glcd_update ();
// delay_ms(100); // Reduces flicker by allowing pixels to be on much longer than off.
}
}
Fonte: http://www.ccsinfo.com/forum/viewtopic.php?t=32819&highlight=st7920
Nenhum comentário:
Postar um comentário