#pragma config FOSC = EXTRC // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
(RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Disvalue EEPROM Memory Code Protection bit (Disvalue EEPROM
code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all
program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
/*
* File: classprogramaddition.c
* Author: HAPPY
* Created on March 29, 2025, 9:22 AM
*/
#include <xc.h>
#define _XTAL_FREQ 6000000
void lcd_negnum(signed int); // used to split the negative number digits and save on array
void lcd_posnum(unsigned int); //used to split the positive number digits and save on array
void lcd_disvalue(unsigned char);//used to write the data on lcd
void lcd_configure(unsigned char);//use to direction the data in lcd
void init(void); // used to configure port and lcd
unsigned char j,i; //variables used during program
unsigned char k[5]; //array that store the digits
signed int num1=-500,num2=300,sum,m,n; // variables store a value
unsigned char plus='+',Equal='='; //store a symbols
void main(void) { //program start here
init(); //call the init
lcd_configure(0x80); //starting point of lcd where digits starts to print
if(num1<0) // condition if num less than zero usually negative number
lcd_negnum(num1); // pass the num1 to lcd_negnum function
else
lcd_posnum(num1); //pass the num1 ti lcd_posnum function
lcd_disvalue(plus); //pass the symbol
if(num2<0) //condition if num2 less than zero usually negative number
lcd_negnum(num2); //pass the num2 to the lcd_negnum function
else
lcd_posnum(num2); //pass the num2 to the lcd_posnum function
lcd_disvalue(Equal); //pass the equak to lcd_disvalue
sum=num1+num2; //Add the two numbers and store the sum result
if(sum>0) //condition if sum less than zero usually negative number
lcd_disvalue('+'); //pass the positive symbolto the display
lcd_posnum(sum);
else
lcd_negnum(sum); //pass the sum to lcd_negnum
while(1);
void lcd_negnum(signed int i) //used to display the negative numbers on lcd
m=i; // assign i to the m
if(m<0) //condition if m less than zero usually negative number
lcd_disvalue('-'); //pass the minus to the lcd_disvalue
m=-m;//multiple minus with minus it become plus now m become positive number
unsigned char s,j=1; //declare the variables for local use
while(m!=0) // set the condition for loop
{
s=m-((m/10)*10); // split the digits
k[j]= s; // store each digits seperately from k[1] to k[5]
m=m/10; // m divide by 10 it pass 2 digit on while condition
j++; //j increment by one
j--; //j decrement by 1
while(j!=0) // for splitting the digits provide the condition
n=0x30+k[j]; // convert the digits into equivalent char
lcd_disvalue(n); // display on lcd char
j--; // j decrement by 1
void init(void) // for configure the lcd
TRISD=0x00; // seting RD0 to RD7 as a output (for D0 to D7)
TRISC=0x00; // setting RC0 to RD7 as a output (for Rs and Enable)
lcd_configure(0x30); //initialization
__delay_ms(100);
lcd_configure(0x30); //initialization
__delay_ms(100);
lcd_configure(0x30);//initialization
__delay_ms(100);
lcd_configure(0x38);//selecting the number of lines in display
__delay_ms(100);
lcd_configure(0x0C);//display ON cursor off
__delay_ms(100);
lcd_configure(0x01);//clear display
__delay_ms(100);
void lcd_configure(unsigned char i) { //direction the character
PORTC &= ~0x08; // RS = 0 (command mode)
PORTD = i;
PORTC |= 0x01; // Enable HIGH
__delay_ms(100); // delay
PORTC &= ~0x01; // Enable LOW
void lcd_disvalue(unsigned char i) { //display the character on lcd
PORTC |= 0x08; // RS = 1 (command mode)
PORTD = i;
PORTC |= 0x01; // Enable HIGH
__delay_ms(100); // delay
PORTC &= ~0x01; // Enable LOW
}void lcd_posnum(unsigned int i) //display the positive value on the lcd display
{
unsigned char s,j=1; //declare the local variable
unsigned m; // used to store the i in m
m=i;
while(m!=0) // condition for loop store a digits in array
s=m-((m/10)*10); // split the digits
k[j]= s; //store the digits in array
m=m/10; //pass the num to loop
j++; // increment by 1
j--;
while(j!=0) //for display the digits to equivalent character
n=0x30+k[j]; // add 48(ox30)withdecimal get the equivalent char of digits
lcd_disvalue(n); // display the equivalent character on display
j--;