C language

Internet Webed: C language

Internet Webed: C language

Showing posts with label C language. Show all posts
Showing posts with label C language. Show all posts

LCD library for 20x4 display in 4 Bit Mode

20x4 LCD






Below is the code for running 20x4 display LCD in 4 bit mode. You can copy the code and save it as a header file (.h) and then include the same in your main program.

Please don't forget to change the PINs according to your sketch.

Please leave a comment for any queries or feedback

/*

 * lcd_lib.h

 * Created By: Ramandeep Singh

 * Created date: 11-Sep-2023

 */

#include <inttypes.h>

//Defines your PINS accordingly to your configuration

#define LCD_CONTROL_DDR DDRC

#define LCD_CONTROL_PORT PORTC

#define LCD_RS_PIN 6

#define LCD_RW_PIN 5

#define LCD_ENABLE_PIN 4

//

#define LCD_DATA1_DDR DDRC

#define LCD_DATA1_PORT PORTC

#define LCD_DATA1_PIN PINC

#define LCD_D4 0

#define LCD_D5 1

#define LCD_D6 2

#define LCD_D7 3


 void lcd_cmd(uint8_t command)  //used to write instruction to LCD

 {

PORTC = command>>4;

PORTC &= (~(1<<LCD_RS_PIN)); //RS=0

PORTC &= (~(1<<LCD_RW_PIN));  //RW=0

PORTC |= (1<<LCD_ENABLE_PIN); //EN=1

_delay_ms(10); //10ms

PORTC &= (~(1<<LCD_ENABLE_PIN)); //EN=0

 

PORTC = (command & 0x0F);

PORTC &= (~(1<<LCD_RS_PIN)); //RS=0

PORTC &= (~(1<<LCD_RW_PIN));  //RW=0

PORTC |= (1<<LCD_ENABLE_PIN); //EN=1

_delay_ms(10); //10ms

PORTC &= (~(1<<LCD_ENABLE_PIN)); //EN=0

 

}

 

  void lcd_data(uint8_t data)  //for writing data to LCD

  {

  PORTC = data>>4 ;

  PORTC |=(1<<LCD_RS_PIN);  //RS=1

  PORTC &=(~(1<<LCD_RW_PIN));  //RW=0

  PORTC |=(1<<LCD_ENABLE_PIN);  //EN=1

  _delay_ms(10);  //10ms

  PORTC &=(~(1<<LCD_ENABLE_PIN)); //EN=0

  

  PORTC = (data & 0x0F);

  PORTC |=(1<<LCD_RS_PIN);  //RS=1

  PORTC &=(~(1<<LCD_RW_PIN));  //RW=0

  PORTC |=(1<<LCD_ENABLE_PIN);  //EN=1

  _delay_ms(10);  //10ms

  PORTC &=(~(1<<LCD_ENABLE_PIN)); //EN=0

  

  }

  

  void LCD_print(char *str) /* Send string to LCD function */

  {

  int i;

  for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */

  {

  lcd_data(str[i]);

  }

  }


void LCD_print_xy(uint8_t x, uint8_t y, char *str) {

uint8_t position = 0x80; // Starting address of the first line

if (y == 1) {

position = 0xC0; // Starting address of the second line

} else if (y == 2) {

position = 0x94; // Starting address of the third line

} else if (y == 3) {

position = 0xD4; // Starting address of the fourth line

}

position += x; // Add the x-coordinate to the position

lcd_cmd(position);  // Send the command to set the cursor position

LCD_print(str);

}


void LCD_goto_XY(uint8_t x, uint8_t y) {

// Define the starting addresses for each line of a 20x4 LCD

static const uint8_t line_offsets[] = {0x00, 0x40, 0x14, 0x54};

// Ensure x and y are within bounds

if (x >= 20 || y >= 4) {

return;  // Invalid coordinates

}

// Calculate the DDRAM address based on x and y

uint8_t position = line_offsets[y] + x;

// Set the cursor to the desired position

lcd_cmd(0x80 | position);

}

 void LCD_init()

 {

lcd_cmd(0x02);   //Set cursor to home position

lcd_cmd(0x28);  //4bit mode

lcd_cmd(0x06); //Entry Mode

lcd_cmd(0x0c); //display ON cursor OFF

lcd_cmd(0x01);   //clear the display

lcd_cmd(0x80);   //set cursor at line1

 }

 

void  LCD_clear()

{

lcd_cmd(0x01);

lcd_cmd (0x80);

}


//how to use:

//LCD_init();

//LCD_Print("My String");

//LCD_print_XY(0,0,"My String");

//LCD_goto_XY(16,3); LCD_print("My String");

//LCD_Clear();

Programme for Binary search

Programme for Binary search
#include<stdio.h>
#include<conio.h>
int arr[20];
int x;
void input()
{
int i;
printf("How many elements out of 20:\n");
scanf("%d",&x);
for(i=0;i<x;i++)
 {
    printf("Enter no.=");
    scanf("%d",&arr[i]);
 }
}
void search()
{
    int n,i,mid,low,high,flag=0;
    printf("Enter no which you want to search: \n");

Programme to transpose a matrix

Programme to transpose a matrix
/* Programme to print transpose of matrix */

#include<stdio.h>
#include<conio.h>
void matrix(int x[3][3]);
void main()
{
    int x[3][3],i,j;
    clrscr();
    printf("Enter elements of matrix:\n");
    for(i=0;i<3;i++)
   {
    for(j=0;j<3;j++)
    {
    scanf("%d",&x[i][j]);
    }
   }
   printf("Entered matrix is:\n");

Programme to check Sparse matrix

Programme to check Sparse matrix
/* wap to print sparse matrix */

#include<stdio.h>
#include<conio.h>
#define n 3
void main()
{
int i,j,a[n][n],count=0;
clrscr();
printf("Enter elements of matrix\n");
for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
printf("You Entered matrix:\n");

How To Make a HUT in C language

In this tutorial I had made a programme by which you can make a HUT in C language with the help of graphics. I had also added a image to get directions for you so you can make it...
Enjoy.. :)

/* Programme to make a HUT in C language with graphics */

#include<graphics.h>
#include<conio.h>
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
line(20,50,150,50);
line(20,50,5,100);
line(20,50,40,100);
rectangle(5,100,40,180);
rectangle(40,100,180,180);
line(150,50,180,100);
rectangle(85,120,125,160);
line(105,120,105,160);
line(85,140,125,140);
rectangle(10,120,35,180);
line(10,120,17,130);
line(10,180,17,170);
line(35,120,25,130);
line(35,180,25,170);
line(17,130,17,170);
line(25,130,25,170);
circle(20,85,5);
line(10,180,60,340);
line(35,180,180,340);
circle(35,200,5);
circle(55,250,15);
circle(90,310,25);
line(140,200,140,220);
line(160,200,160,220);
line(140,210,160,210);
rectangle(170,200,190,220);

C programme for Double Link-list or Doubly by using structure

C programme for Double Link-list or Doubly by using structure
Link to Turbo C: HERE

/* programme of single Double link-list*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>    /* for malloc function */
struct student        /*structure name defined */
{
    int rno;
    char name[20];
    struct student *nadd,*padd;
}*f,*m,*ptr;
void menu();
void create();
void insert();
void insertfirst();
void insertany_before();
void deletefirst();
void deletelast();
void deleteany();
void display();
void ext();
void main()

C programme for Single Link-List by using Structure

C programme for Single Link-List by using Structure

Link to Turbo C: HERE 

/* programme of single link-list*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>    /* for malloc function */
struct student        /*structure name defined */
{
    int rno;
    char name[20];
    struct student *add;
}*f,*m,*ptr;
void menu();
void create();
void insert();
void insertfirst();
void insertany_before();
void deletefirst();
void deletelast();
void deleteany();
void display();
void ext();
void main()