Can PIC16F877A be used with LCD and Relay at same time?

Posted: 12/12/2016 8:57:15 AM
mkioiu

Joined: 8/26/2016

I had try to do some exercise about PIC16f877A about LCD counter with relay function by using miKroC with Protues, but I failed and can't get the point where is the error. Someone can teach me? I really need some help.

here is the pic16f877a datasheet  in case you need it: pic16f877a datasheet
// LCD module connections
sbit LCD_RS at RB4_bit; // LCD_RS assigned to PORT RB4;
sbit LCD_EN at RB5_bit; // LCD_EN assigned to PORT RB5;
sbit LCD_D4 at RB0_bit; // LCD_D4 assigned to PORT RB0;
sbit LCD_D5 at RB1_bit; // LCD_D5 assigned to PORT RB1;
sbit LCD_D6 at RB2_bit; // LCD_D6 assigned to PORT RB2;
sbit LCD_D7 at RB3_bit; // LCD_D7 assigned to PORT RB3;

sbit LCD_RS_Direction at TRISB4_bit; // LCD_RS assigned to TRIS B4;
sbit LCD_EN_Direction at TRISB5_bit; // LCD_EN assigned to TRIS B5;
sbit LCD_D4_Direction at TRISB0_bit; // LCD_D4 assigned to TRIS B0;
sbit LCD_D5_Direction at TRISB1_bit; // LCD_D5 assigned to TRIS B1;
sbit LCD_D6_Direction at TRISB2_bit; // LCD_D6 assigned to TRIS B2;
sbit LCD_D7_Direction at TRISB3_bit; // LCD_D7 assigned to TRIS B3;
// End LCD module connections

char Message1[]="RECENT NUMBER"; // Message for line1;
unsigned int number = 0;

char *digit = "0000";

void Display_init() // define display_init;
{
digit[0] = number/1000 + 48; // thousands digit;
digit[1] = (number/100)%10 +48; // hundreds digit;
digit[2] = (number/10)%10 + 48; // tens digit;
digit[3] = number%10 +48; // unit digit;
Lcd_Out(2,7,digit); // display on LCD from column 2, character 7;
}

void main() // main;
{
CMCON= 0x07; // turn off analogue comparator and make PORTA to digital I/O;
TRISA.f0 = 1; // make PORT RA0 as input for button;
TRISA.f1 = 1; // make PORT RA1 as input for button;
PORTA = 0; // Turn ON PORTA;
TRISB = 0; // Set PORTB direction to be output;
PORTB = 0; // Turn ON PORTB;
PORTB.F7=1;


Lcd_init(); // LCD Initialization;
Lcd_cmd(_LCD_CLEAR); // Clear LCD;
Lcd_cmd(_LCD_CURSOR_OFF); // Cursor mode, off;
Lcd_out(1,2,Message1); // display message1 from column 1, character 3;
number=0;
display_init();

do{
if(PORTA.F0==1){
Delay_ms(1000); // If button is pressed, delay 0,2s and increment "number" with 1;
number++;
}
if(PORTA.F1==1){
Delay_ms(1000); // If button is pressed, delay 0,2s and decrement "number" with 1;
number--;
}
if (number > 9999u) // if it's more than 9999 go to 0;
number = 0;
display_init(); // call display_init();

if (number>=1){
PORTA.F7=1;
delay_ms(2000);
}
else{
PORTA.F7=0;
delay_ms(2000);
}
}while(1);
}

Posted: 12/12/2016 1:28:54 PM
Thierry

From: Colmar, France

Joined: 12/31/2007

a) Wrong forum...

b) There must most probably be a resistor in series with the transistor's base.

c) Code is difficult to discuss if there are no line numbers.

d) Here is for sure an error since nothing is connected to PORTA.F7. The relay transistor is on PORTB.F7 :

if (number>=1){

PORTB.F7=1;
delay_ms(2000);
}
else{
PORTB.F7=0;
delay_ms(2000);
}

 

 

You must be logged in to post a reply. Please log in or register for a new account.