interfacing PIC18F4520

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

interfacing PIC18F4520

Postby knull » Sun Jul 28, 2019 5:01 am

Hi newbie here, i have some questions
1) How do i display a 2 digit No. on keypress?
2) When I press 5 on the keypad, the LCD displays '5'. How do i then store the value ive inputted?
I want to compare the number ive pressed to a randomly generated number.

How do i save the keys to memory as they are pressed? keypad uses 74C922 encoder


CODE: SELECT_ALL_CODE
#include <xc.h>
#include "config.h"

#define DA PORTAbits.RA5 // set dataAvailable PORT A bit 5
#define LCD_DATA PORTD
#define LCD_RS   PORTBbits.RB1   // RS signal for LCD
#define LCD_E    PORTBbits.RB2   // E signal for LCD
#define bulb PORTBbits.RB3 // assign PORT B bit 3 to light bulb
void Init_LCD(void);// Initialize the LCD
void LCD_sendCW(char);// 8-bit Control word for LCD
void LCD_sendData(char);// 8-bit Text Data for LCD
void readKey();

void main(void)
{
    ADCON1=0x0F; // Setports A,B & E as digital I/O
    TRISB = 0b11111001;// RB1 & 2 for LCD interface RS & E
    TRISD = 0;         // Port D as LCD data output
    TRISE = 0b11101111;// RE4:OUTPUT to make PORTD digital I/O
    Init_LCD();// Init LCD 8-bit interface,multiple line
   
   
   
   
    char x;
     int num,num2,i;

     
    while(1)
       
    {
        readKey();
       
       
       
       
   
       
   
   
    }
}

void Init_LCD()
{
LCD_sendCW(0b00111000);// Function Set -8-bit, 2 lines, 5X7
LCD_sendCW(0b00001100);// Display on, cursor on
LCD_sendCW(0b00000110);// Entry mode -inc addr, no shift
LCD_sendCW(0b00000001);// Clear display 
LCD_sendCW(0b00000010);// Return cursor to home position
}

void LCD_sendCW(char x)
{
LCD_RS = 0;
LCD_E = 1;
LCD_DATA = x;
LCD_E= 0;
_delay(1000);// 2ms delay
}

void LCD_sendData(char x)
{
 LCD_RS= 1;
 LCD_E= 1;
 LCD_DATA = x;
 LCD_E= 0;
 _delay(500);// 1ms delay
 }

void readKey()
{
   
     int key[]={1,2,3,'F',4,5,6,'E',7,8,9,'D','A',0,'B','C'};
    char dataIn;
    TRISA=0b11111111;
    ADCON1=0X0F;
   
   
   
     
 if(DA==1)
        {
            dataIn = PORTA&0b00001111;
           
            if(dataIn==0b00000000)
            {
               
                LCD_sendData(key[0]+'0');
                LCD_sendCW(0b10000000);
               
               
            }
            else if(dataIn==0b00000001)
            {
               
                LCD_sendData(key[1]+'0');
                LCD_sendCW(0b10000000);
            }
           
            else if(dataIn==0b00000010)
            {
               
                LCD_sendData(key[2]+'0');
                LCD_sendCW(0b1000000);
            }
            else if(dataIn==0b00000011)
            {
               
                LCD_sendData(key[3]);
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00000100)
            {
               
                LCD_sendData(key[4]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00000101)
            {
               
                LCD_sendData(key[5]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00000110)
            {
               
                LCD_sendData(key[6]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00000111)
            {
               
                LCD_sendData(key[7]);
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001000)
            {
               
                LCD_sendData(key[8]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001001)
            {
               
                LCD_sendData(key[9]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001010)
            {
               
                LCD_sendData(key[10]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001011)
            {
               
                LCD_sendData(key[11]);
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001100)
            {
               
                LCD_sendData(key[12]);
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001101)
            {
               
                LCD_sendData(key[13]+'0');
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001110)
            {
               
                LCD_sendData(key[14]);
                LCD_sendCW(0b10000000);
            }
            else if(dataIn==0b00001111)
            {
               
                LCD_sendData(key[15]);
                LCD_sendCW(0b10000000);
            }         
        }
}
knull
Fledgling
 
Posts: 1
Joined: Sun Jul 28, 2019 4:51 am

Re: interfacing PIC18F4520

Postby GustaBertie » Wed Mar 04, 2020 1:42 am

Hi...I had an issue trying to get a mcp25xx module to work too, I was using a non standard bitrate because the deafult xtal frequency in the lib I was using wasnt the xtal freq in my modules.

order pcb
GustaBertie
Fledgling
 
Posts: 1
Joined: Wed Mar 04, 2020 1:40 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 6 guests