Hi,
I've been trying to use a 16F877 to control one of the CFAG12864M displays, but I seem to be getting nowhere with getting the display to respond. This is the code that I have been trying to get working. Any suggestions would be greatly appreciated.
Thanks
Sean.
I've been trying to use a 16F877 to control one of the CFAG12864M displays, but I seem to be getting nowhere with getting the display to respond. This is the code that I have been trying to get working. Any suggestions would be greatly appreciated.
Thanks
Sean.
Code:
#include <system.h>
//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF
//Set clock frequency
#pragma CLOCK_FREQ 14745000
#define row 0xB8
#define last_row 0xBF
#define col 0x40
#define last_col 63
#define first_col 0
#define max_col 128
#define max_line 8
#define max_pos 64
#define disp_on 0x3F
#define disp_off 0x3E
#define disp_ram 0xC0
void lcd_e(bit val) // PORT AN1
{
if (val == 0) clear_bit(porta, 1);
else set_bit(porta, 1);
}
void lcd_cd(bit val) // PORT AN0
{
if (val == 0) clear_bit(porta, 0);
else set_bit(porta, 0);
}
void lcd_rw(bit val) // PORT AN2
{
if (val == 0) clear_bit(porta, 2);
else set_bit(porta, 2);
}
void lcd_cs1(bit val) // PORT EN0
{
if (val == 0) clear_bit(porte, 0);
else set_bit(porta, 0);
}
void lcd_cs2(bit val) // PORT EN1
{
if (val == 0) clear_bit(porte, 1);
else set_bit(porte, 1);
}
void lcd_rst(bit val) // PORT AN3
{
if (val == 0) clear_bit(porta, 3);
else set_bit(porta, 3);
}
// PIC16F87x defaults for hardware USART support
#define TX_PORT 0x07
#define TX_TRIS 0x87
#define TX_BIT 6
#define RX_PORT 0x07
#define RX_TRIS 0x87
#define RX_BIT 7
#define e_SPBRG 0x99
#define e_RCREG 0x1a
#define e_TXREG 0x019
#define e_TXSTA 0x98
#define e_RCSTA 0x18
#define e_TXIF_PIR 0x0c
#define e_RCIF_PIR 0x0c
#define e_TXIF_BIT 4
#define e_RCIF_BIT 5
#define MODE (USART_reset_wdt | USART_HW)
#define bit_time 96
void toggle_enable(void) // Toggle enable BIT on LCD screen
{
set_bit(porta, 1);
delay_ms(1);
clear_bit(porta, 1);
delay_ms(1);
}
void lcd_init(void)
{
char tmp;
lcd_cd(0);
lcd_cs1(1);
lcd_cs2(0);
for (tmp = 1; tmp <= 2; tmp++)
{
portd = disp_off;
portd = disp_ram;
toggle_enable();
portd = col;
toggle_enable();
portd = row;
toggle_enable();
portd = disp_on;
toggle_enable();
lcd_cs1(0);
lcd_cs2(1);
}
lcd_cs1(1);
lcd_cs2(0);
}
void lcd_clearall(void)
{
char index, j, k;
lcd_cd(0);
portd = disp_off;
toggle_enable();
lcd_cs1(1);
lcd_cs2(0);
for (index = 1; index <= 2; index++)
{
for (j = row; j <= last_row; j++)
{
lcd_cd(0);
toggle_enable();
portd = col;
toggle_enable();
portd = j;
toggle_enable();
lcd_cd(1);
for (k = first_col; k <= last_col; k++)
{
portd = 10101010b;
toggle_enable();
}
}
}
lcd_cs1(0);
lcd_cs2(1);
lcd_cd(0);
portd = row;
toggle_enable();
portd = disp_ram;
toggle_enable();
portd = col;
toggle_enable();
portd = disp_on;
toggle_enable();
lcd_cs1(1);
lcd_cs2(0);
portd = row;
toggle_enable();
portd = disp_ram;
toggle_enable();
portd = col;
toggle_enable();
portd = disp_on;
toggle_enable();
lcd_cd(1);
lcd_cs1(1);
lcd_cs2(0);
}
void interrupt( void )
{
//Handle timer0 interrupt
if( intcon & (1<<T0IF) )
{
clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit
}
//Handle timer1 interrupt
if( pir1 & (1<<TMR1IF) )
{
clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit
}
//Handle timer2 interrupt
if( pir1 & (1<<TMR2IF) )
{
clear_bit( pir1, TMR2IF ); //clear timer 2 interrupt bit
}
}
#include <rs232_driver.h>
void main( void )
{
char x, y, num;
num = 0;
//Configure port A
trisa = 0x00;
//Configure port B
trisb = 0x00;
//Configure port C
//trisc = 0x00;
//Configure port D
trisd = 0x00;
//Configure port E
trise = 0x00;
//Configure A/D pins
adcon1 = 0x06;
//Initialize port A
porta = 0x00;
//Initialize port B
portb = 0x00;
//Initialize port C
//portc = 0x00;
//Initialize port D
portd = 0x00;
//Initialize port E
porte = 0x00;
//Set Timer0 mode
clear_bit( option_reg, T0CS ); //configure timer0 as a timer
//Set prescaler assignment
clear_bit( option_reg, PSA ); //prescaler is assigned to timer0
//Set prescaler rate
clear_bit( option_reg, PS2 ); //prescaler rate 1:2
clear_bit( option_reg, PS1 );
clear_bit( option_reg, PS0 );
//Set timer0 source edge selection
set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin
//Set timer 1 prescaler rate
clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1
clear_bit( t1con, T1CKPS0 );
//Set timer 1 mode
clear_bit( t1con, TMR1ON ); //disable timer 1
//Set timer 2 prescaler rate
clear_bit( t2con, T2CKPS1 ); //prescaler rate 1:1
clear_bit( t2con, T2CKPS0 );
//Set timer 2 postscaler rate
clear_bit( t2con, TOUTPS3 ); //postscaler rate 1:1
clear_bit( t2con, TOUTPS2 );
clear_bit( t2con, TOUTPS1 );
clear_bit( t2con, TOUTPS0 );
//Set timer 2 mode (enable or disable)
clear_bit( t2con, TMR2ON ); //enable timer 2
//Enable interrupts (Timer0)
intcon = 0xA0;
delay_ms(10000);
lcd_init();
lcd_clearall();
//Endless loop
uart_init(1,95); // set high speed divisor mode and divisor value
puts("Hello, world");
while (1)
{
if (kbhit())
{
putc(getc());
}
}
}
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.