Code: Select all
/////////////////////////////////////////////////////////////////////
//
// Demo Of how to save RAM for LCD and UART
// PIC 16F887
// 8Mhz Internal OSC
// UART 9600 BAUD
//
//
// Hardware UART connected at 9600BAUD
// LCD connected in 4 bit mode with RW pin grounded
//
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections
char msg[17]; //declare array set to max size required plus 1 [for terminator] for copying into
// copy const to ram string
char * CopyConst2Ram(char * dest, const char * src){
char * d ;
d = dest;
for(;*dest++ = *src++;)
;
return d;
}
const char LCD_txt1[] = "Ram Saving Test.";
const char LCD_txt2[] = "Top Row LCD ";
const char LCD_txt3[] = "Bottom Row LCD ";
const char LCD_txt4[] = "UART output.....";
const char UART_txt1[] = "Ram Test 1\r\n";
const char UART_txt2[] = "Ram Test 2\r\n";
const char UART_txt3[] = "Ram Test 3\r\n";
const char UART_txt4[] = "Ram Test 4\r\n";
const char UART_txt5[] = "Ram Test 5\r\n";
const char UART_txt6[] = "Ram Test 6\r\n";
const char UART_txt7[] = "Ram Test 7\r\n";
const char UART_txt8[] = "Ram Test 8\r\n";
void main() {
OSCCON = 0x71; // settings for 8Mhz internal clock
ANSEL = 0; // Configure AN pins as digital
ANSELH = 0;
CM1CON0 = 0; // Disable comparators
CM2CON0 = 0; // Disable comparators
UART1_Init(9600); // init uart at 9600
Delay_ms(100);
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,CopyConst2Ram(msg,LCD_txt1)); // Write text in first row
Lcd_Out(2,1,CopyConst2Ram(msg,LCD_txt1)); // Write text in second row
while(1)
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,CopyConst2Ram(msg,LCD_txt4)); // Write text in first row
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt1));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt2));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt3));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt4));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt5));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt6));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt7));
Uart1_Write_Text(CopyConst2Ram(msg,UART_txt8));
Delay_ms(2000);
Lcd_Out(1,1,CopyConst2Ram(msg,LCD_txt2)); // Write text in first row
Lcd_Out(2,1,CopyConst2Ram(msg,LCD_txt3)); // Write text in second row
Delay_ms(2000);
};
}
Used RAM (bytes): 26 (7%) Free RAM (bytes): 326 (93%)
Used ROM (program words): 728 (9%) Free ROM (program words): 7464 (91%)