You are on page 1of 3

/* Count switch pressings and display on 7-segment display.

If the PIC detects


the switch in the ON position, increment the counter and check again after
1 second, if it is still in the ON position, wait till it goes to the OFF
then after 0.5 seconds check for an ON condition to increment the
counter and repeat till the counter reaches 5 then put high on pin RA1,
when it reaches 7 put low on RA1 and high on pin RA2, and when it
reaches 8, clear the counter and turn off pin RA2.
Write the code in C language, test it on the simulator when you are sure
that it is working properly burn it on the chip and put it in the circuit
and verify that it is functioning correctly.
Number of key presses should be displayed on seven segment display.
Note that Flasher.asm and Flasher1i.asm contain Delay routines.
RB0 to RB3 of PORTB will be used to drive the 7-segment display through 7448.
RA0 for reading the switch while RA1 and RA2 for writing to the LEDs. */
#include "16f84a.h"
#use delay (clock=4000000) // 4MHz crystal
int1 a0, a1, a2;
int a, b;
void main()
{
set_tris_a(0x01); // pins RA0 for i/p, and RA1 and RA2 for o/p
set_tris_b(0x00); // pins 0 to 7 of port B are outputs
b=0;
output_B(b); // clear port B
while(1)
{
if (input(pin_a0)==1) // read the switch connected to RA0
{
b++; // increment b
output_B(b); // write to 7-segment display
if (b==5) output_bit(pin_a1,1); // if b=7 set RA1=1
if (b==7) {output_bit(pin_a1,0); output_bit(pin_a2,1);} // if b=9 set RA1=0 and RA2=1
if (b==8) {output_bit(pin_a2,0); b=0; output_B(b);} // if b=11 => RA2=0, clr counter
delay_ms(1000); // wait for 1 second
}
while (input(pin_a0)==1) {}; // wait until a0 goes down
if ((b > 0) & input(pin_a0)==0) delay_ms(500); // now RA0 is off, wait for 0.5 second
}
}
Figure 1: Connect RB3, RB2, RB1, RB0 to pins 6, 2, 1, 7 of 7448 BCD to 7-segment display driver, then pins 9,
10, 11, 12, 13, 15, 14 of 7448 to pins 1, 2, 3, 4, 5, 6, 7 (a, b, c, d, e, f, g) of the common cathode 7-segment display.
Connect 470 between pin 8 of the display and the ground
1
; Lab3-retlw.ASM for 16F84.
; read RA3...RA0 and display on PORTB ... to Common cathode 7seg dispaly
; Usage of PIC 16F84 instructions.
;*********************************************************
#include "p16f84.inc"
; EQUATES SECTION
num equ 0x0c ;b=NUMBER to be displayed on 7seg
;*********************************************************
LIST P=16F84 ; we are using the 16F84.
ORG 0 ; org address in memory is 0
GOTO START ; goto start of program (jump over subroutines)
;**********************************************************************
; Configuration Bits
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC ;H3FF1
;*********************************************************
;SETUP SECTION
START: BSF STATUS,RP0 ;Turns to Bank1.
MOVLW B00001111; bit 0 TO 3 of PORTA as I/P
MOVWF TRISA
MOVLW B00000000
MOVWF TRISB ;PORTB as OUTPUT
;
BCF STATUS,RP0 ;Return to Bank0.
CLRF PORTB ;Clears PortB.
;*********************************************************
;Program starts now.
;ADD NUMBERS FROM LOCATION TABLE AND STORE
;THE RESULT IN PORTB. USE INDIRECT ADDRESSING FOR READING.
BEGIN: movf PORTA,W ; W <- PORTA (hex digit)
CALL MAP ; get from MAP the 7seg display code
MOVWF PORTB ; Output number to display.
GOTO BEGIN ; repeat always
;
MAP: ADDWF PCL
RETLW 0x3f ; 0
RETLW 0x06 ; 1
RETLW 0x5b ; 2
RETLW 0x4f ; 3
RETLW 0x66 ; 4
RETLW 0x6d ; 5
RETLW 0x7d ; 6
RETLW 0x07 ; 7
RETLW 0x7f ; 8
RETLW 0x6f ; 9
RETLW b01110111; A
RETLW b01111100; b
RETLW b01011000; c
RETLW b01011110; d
RETLW b01111001; E
RETLW b01110001; F
;
END
2
Interface without 7448 BCD to 7segment driver
/* Lab3.c
Count switch pressings and drectly display on 7-segment display-Common cathode.
If the PIC detects
the switch in the ON position, increment the counter and check again after
0.5 second, if it is still in the ON position, wait till it goes to the OFF
then after 0.5 seconds check for an ON condition to increment the
counter and repeat till the counter reaches 5 then put high on pin RA1,
when it reaches 7 put low on RA1 and high on pin RA2, and when it
reaches 14 (=0xE), clear the counter and turn off pin RA2.
Write the code in C language, test it on the simulator when you are sure
that it is working properly burn it on the chip and put it in the circuit
and verify that it is functioning correctly.
Number of key presses should be displayed on seven segment display.
Note that Flasher.asm and Flasher1i.asm contain Delay routines.
RB0 to RB6 (7-segment) of PORTB will be used to drive the 7-segment display directly.
RA0 for reading the switch while RA1 and RA2 for writing to the LEDs.
*/
#include "16f84a.h"
#use delay (clock=4000000) // 4MHz crystal
int b;
int map[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0b01110111, 0b01111100, 0b01011000, 0b01011110, 0b01111001, 0b01110001}; // 0..0xF
void main()
{
set_tris_a(0x11); // pins RA0 for o/p, and RA1 and RA22 for o/p
set_tris_b(0x00); // pins 0 to 7 of port B are outputs
b=0;
output_B(map[b]); // clear port B
while(1)
{
if (input(pin_a0)==1) // read the switch connected to RA0
{
b++;
output_B(map[b]); // write to 7-segment display
if (b==5) output_bit(pin_a1,1); // if b=7 set RA1=1
if (b==7) {output_bit(pin_a1,0); output_bit(pin_a2,1);} // if b=9 set RA1=0 and RA2=1
if (b==0xe) {output_bit(pin_a2,0); b=0; output_B(map[b]);}
delay_us(1); // wait for 1u second-Oshon simulator and 500m sec fo hardware
}
while (input(pin_a0)==1) {}; // wait until a0 goes down
if ((b > 0) & input(pin_a0)==0) delay_us(1); // now RA0 is off, wait for 0.5 second
}
}
and in Assembly language
3

You might also like