Before connecting the PIC, check the supply voltages to ensure that the linear voltage regulator (or other power supply scheme) is supplying optimally 5V. Note (from datasheet), absolute maximum voltage on Vdd with respect to Vss is -0.3V to +7.5V.
Programming the PIC
The C code (using CCS compiler) to debounce two switches connected to PORT B0 (pin 21) and PORT B1 (pin 22) and report the button press via RS232 to a connected PC is as follows:
Code Snippet 1:
#include "debounce.h"
#include "button.c"
#zero_ram //all variables automatically initialised to 0
// These are the "Bvar" variables required by Button.c.
int8 B0 = 0; // For the button on pin B0
int8 B1 = 0; // For the button on pin B1
// Global "button pressed" variables.
int8 B0_pressed = FALSE;
int8 B1_pressed = FALSE;
// The buttons are read in this Timer0 (RTCC) isr.
#int_timer0
void timer0_isr(void) {
if(button(PIN_B0, 0, 50, 10, B0, 1))
B0_pressed = TRUE;
if(button(PIN_B1, 0, 50, 10, B1, 1))
B1_pressed = TRUE;
set_timer0(0); // Reload the Timer - prescaler = 1, frequency ~100mS
}
void main() {
/******************************************************************************
* Function: main()
*
*****************************************************************************/
char recChar; //the received character from the serial port via keyboard
printf("start\n\r");
set_tris_b(0x03); // Set Pin B0 and B1 as input
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);
set_timer0(0); //prescaler = 1, frequency ~100mS
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(TRUE){
if (kbhit()) {
recChar = getc();
//printf("cmd=%c \n\r",recChar);
switch (recChar) {
case 'e':
case 'E': printf("Cmd E\r\n");
break;
default: printf("Unknown cmd\n\r");
break;
}
}
if(B0_pressed == TRUE) {
B0_pressed = FALSE;
printf("pressed B0\n\r");
}
if(B1_pressed == TRUE) {
B1_pressed = FALSE;
printf("pressed B1\n\r");
}
}
}
Code Snippet 2:
#include <18F248.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT //Power Up Timer
#use delay(clock=10000000) //clock=40M) //crystal=10M)
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS)
Code Snippet 3: This is based upon the CCS code (2)
// Button.c
#define read_bit_var(x) bit_test(*(int8 *)(x >> 3), x & 7)
int8 button(int16 pin, int8 downstate, int8 delay, int8 rate, int8 &BVar, int8 action) {
int8 pin_value;
pin_value = read_bit_var(pin);
if(pin_value != downstate){
Bvar = 0;
return(!action);
}
if(Bvar == 0){
if(delay == 0)
Bvar = 255;
else
Bvar = delay;
return(action);
}
Bvar--;
if(BVar == 0){
BVar = rate;
if((delay != 0) && (delay != 255))
return(action);
}
return(!action);
}
Once the circuit has been constructed as per the schematic (photographs of the circuit laid-out on breadboard given in the Photographs Section), the text string "pressed B0" or "pressed B1" will be sent via RS232 to an attached PC.
Only Logged-In Members can add comments