Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC16F877 Input button not working properly

Status
Not open for further replies.

WStevens_sa

Member level 2
Joined
Jan 5, 2011
Messages
47
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
South Africa
Activity points
1,695
Hi all

My project is as follows. PICF877 programmed to be a RTC using TMR0 with prescaler for 1 second delay which displays day and time on a 2 x 16 LCD as "MON 00:00:00. RC0 sets hours which works and RC1 is supposed to set the minutes but does not work.

Where have I gone wrong. RC1 is not triggering "if (Button(&PORTC, 1, 1, 1)) "

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

// VARIABLES FOR LCD

char* txt1;
int count;
int shiftcount=0;
int tic = 0;
int msec=0;
int timer_set = 0;


// INTERRUPT VARIABLES
unsigned int counter = 0 ;
unsigned int seconds = 0, minutes = 0, hours = 0, days = 0;
char* weekdays[7] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};

// INTERRUPT RTC
void interrupt(void) {

if(T0IF_bit == 1){ // Check for interrupt flag

T0IF_bit=0; //Reset flag
counter++ ;
if(counter > 15625){ // 1 sec 16 Mhz crystal = 16000000 / 4 / 256 =15625
counter = 0 ;
seconds++ ;
if(seconds == 60){
seconds = 0 ;
minutes++ ;
if(minutes == 60){
minutes = 0 ;
hours++ ;
if(hours == 24){
hours = 0 ;
days++;
if (days == 7){
days = 0;
}
}
}
}
}
}
}

void main(){
//LCD
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

Lcd_out(1,7,":");
Lcd_out(1,10,":");

//TMR0
OPTION_REG = 8;
GIE_bit = 1; //Enable Global Interrupt
T0IF_bit = 0; //Clear interrupt flag
T0IE_bit = 1; //Enable TMR0 interrupt
PEIE_bit = 1; //Peripheral Interrupt Enable bit

//PUSH BUTTON SETTINGs
TRISA = 0; //PortA all outputs
PORTA = 0;
TRISC = 1; // Configure PORTC as inputs
PORTC = 0; // Initial PORTC value


// Lcd_Chr(1,1,48+9); // // Only from 0 to 9. Need to use shift registerto

while(1){
//SECONDS
Lcd_Chr(1,12,48+(seconds % 10)); // seconds counter LSB
Lcd_Chr(1,11,48+((seconds /10) % 10)); //Seconds counter MSB

//MINUTES
Lcd_Chr(1,9,48+(minutes % 10)); // minutes counter LSB
Lcd_Chr(1,8,48+((minutes /10) % 10)); //minutes counter MSB

//HOURS
Lcd_Chr(1,6,48+(hours % 10)); // hours counter LSB
Lcd_Chr(1,5,48+((hours /10) % 10)); // hours counter MSB

//DAYS
Lcd_out(1,1,weekdays[days]); // days of the week

//PUSHBUTTON INPUT HOURS RC0_BIT
if (Button(&PORTC, 0, 1, 1)) {
hours++;
Delay_ms(100); // Adjust counting time when button depressed
if (hours ==24) {hours=0;}
}

//PUSHBUTTON INPUT MINUTES RC1_BIT
if (Button(&PORTC, 1, 1, 1)) {
minutes++;
Delay_ms(100); // Adjust counting time when button depressed
if (minutes ==60) {minutes=0;}
}

}
}
 

Hi;

Modify this line
TRISC = 1; // Configure PORTC as inputs
// this configures only PORTC.0 as input

to
TRISC = 3; // Configure PORTC.0 and PORTC.1 as inputs
 
check this sample code may b it will help you.....it is for mikro c compiler u can check on the simulator......i hpe this willl be helpful to you..
Code:
bit oldstate;                                    // Old state flag

void main() {

  ANSEL  = 0;                                    // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 0;

  TRISB0_bit = 1;                                // set RB0 pin as input
  
  TRISC = 0x00;                                  // Configure PORTC as output
  PORTC = 0xAA;                                  // Initial PORTC value
  oldstate = 0;
  
  do {
    if (Button(&PORTB, 0, 1, 1)) {               // Detect logical one
      oldstate = 1;                              // Update flag
    }
    if (oldstate && Button(&PORTB, 0, 1, 0)) {   // Detect one-to-zero transition
      PORTC = ~PORTC;                            // Invert PORTC
      oldstate = 0;                              // Update flag
    }
  } while(1);                                    // Endless loop
}
 
Okay. I understand now. The port set for inputs works accordingly.

TRISC = 5 which is binary 101

RC0 = 1
RC1 = 0
RC2 = 1
RC3 = 0
RC4 = 0
RC5 = 0
RC6 = 0
RC7 = 0

So only RC0 and RC2 will allow inputs.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top