EMBEDDED FOR US
Would you like to react to this message? Create an account in a few clicks or log in to continue.

interrupt-driven button code in CCS C

Go down

 interrupt-driven button code in CCS C Empty interrupt-driven button code in CCS C

Post  Admin Mon Aug 20, 2012 3:12 pm

here is the code:
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include "button.c"

//----------------------------
// DEFINES


#define SELECT_BUTTON  1
#define SET_BUTTON    2

// Button pins
#define SELECT_BUTTON_PIN  PIN_B0
#define SET_BUTTON_PIN    PIN_A4

// The preload value below gives an RTCC interrupt rate of
// about 100 Hz (10 ms period) with a 4 MHz crystal.
#define RTCC_PRELOAD (256 - 39)


//----------------------------
// GLOBAL VARIABLES

// These are the "Bvar" variables required by Button.c.
// There is one variable for each button.
int8 Bvar_select = 0;   
int8 Bvar_set    = 0;   

// Variables used by button buffer code.
#define BUTTON_BUFFER_SIZE  16
int8 button_buffer[BUTTON_BUFFER_SIZE];
int8 next_in = 0;
int8 next_out = 0;

#define button_ready (next_in!=next_out)

//---------------------------------------------------
// The buttons are read in this Timer0 (RTCC) isr.
#int_rtcc
void rtcc_isr(void)
{
int t;

set_rtcc(RTCC_PRELOAD + get_rtcc()); 

// Check if the Select button was pressed. 
// If so, put it in the buffer.
if(button(SELECT_BUTTON_PIN, 0, 50, 10, Bvar_select, 1))   
  {
  button_buffer[next_in] = SELECT_BUTTON;
  t = next_in;
  next_in = (next_in+1) % BUTTON_BUFFER_SIZE;
  if(next_in == next_out)
      next_in = t;          // Buffer full !!
  }

// Check if the Set button was pressed. 
// If so, put it in the buffer.
if(button(SET_BUTTON_PIN, 0, 50, 10, Bvar_set, 1))   
  {
  button_buffer[next_in] = SET_BUTTON;
  t = next_in;
  next_in = (next_in+1) % BUTTON_BUFFER_SIZE;
  if(next_in == next_out)
      next_in = t;          // Buffer full !!
  }


}


int8 get_button(void)
{
int8 c;

while(!button_ready);

c = button_buffer[next_out];
next_out = (next_out+1) % BUTTON_BUFFER_SIZE;
return(c);
}


//================================
void main()
{
int8 button;

printf("Start \n\r");

setup_counters(RTCC_INTERNAL, RTCC_DIV_256);
set_rtcc(RTCC_PRELOAD);
clear_interrupt(INT_RTCC);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

while(1)
  {
  if(button_ready)
    {
      button = get_button();
     
      if(button == SELECT_BUTTON)
        printf("Select \n\r");
      if(button == SET_BUTTON)
        printf("Set \n\r");
    }
  }

}

Somehow I'm getting a get_button function value 25.
And I'm getting it only once because of if function (button_ready).



Code:


while(1)
  {
 
  if(button_ready)
    {
      button = get_button();
    button = a;
    printf("Button value %u \n\r",a); // a value is 25  ?
    printf("Select button  %u \r\n",SELECT_BUTTON); //1
    printf("Set button  %u \r\n",SET_BUTTON);//2
      if(button == SELECT_BUTTON)
        printf(lcd_putc,"Select \n\r");
      if(button == SET_BUTTON)
        printf("Set \n\r");
         
    }
  }
Also I've included a main function code.
Code:


#include "C:\Documents and Settings\PICC\Projects\LCD proov 3 1602\main lcd proov 3.h"

//#include <18F4520.H>
//#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
//#use delay(internal = 8000000)
#include <button.c>


#define SELECT_BUTTON  1
#define SET_BUTTON    2

// Button pins
#define SELECT_BUTTON_PIN  PIN_D0
#define SET_BUTTON_PIN    PIN_D1

// The preload value below gives an RTCC interrupt rate of
// about 100 Hz (10 ms period) with a 4 MHz crystal.
#define RTCC_PRELOAD (256 - 39)


//----------------------------
// GLOBAL VARIABLES

// These are the "Bvar" variables required by Button.c.
// There is one variable for each button.
int8 Bvar_select = 0;   
int8 Bvar_set    = 0;   

// Variables used by button buffer code.
#define BUTTON_BUFFER_SIZE  16
int8 button_buffer[BUTTON_BUFFER_SIZE];
int8 next_in = 0;
int8 next_out = 0;

#define button_ready (next_in!=next_out)


#define RTC_SDA  PIN_C4
#define RTC_SCL  PIN_C3
//int8 D0 = 0;  // For the button on pin A4
//int8 D1 = 0;  // For the button on pin B
#use I2C(MULTI_MASTER,sda=RTC_SDA, scl=RTC_SCL)
#use delay(internal=8M)
#use rs232(uart1, baud=9600)

//#use i2c(master,SDA=PIN_B7, SCL=PIN_B6)

#include <flex_lcd.c>
#include <stdlib.h>
#include <DS1307.c>
#include <muutmine.c>
#include <ADCfunktsioonid.c>

#include <nupud.c>

#int_rtcc
Admin
Admin
Admin

Posts : 49
Join date : 2012-08-15
Location : Mumbai,INDIA

http://embeddedforus.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum