Here is an exampleΒ shows how to make a digital up counter where the number is displayed on a 7Β segment display uses multiplexing technique with shift register.Β
Example circuit schematic is shown below where a common anode 7-segment display and 74HC164N shift register are used.
Other shift registers such as 74HC595Β orΒ CD4094 can be used in this project.
TheΒ displayed number can be incremented usingΒ the button which is connected to RA3 pin.Β
The push button is connected to RA3 pin. The shift register used is 74HC164 but other types can work properly like 74HC595 or CD4094.
In this example all pins of PIC12F1822 are used and there is no free pin.
Interfacing PIC12F1822 with 7-segment displayΒ CCS CΒ code:
//Β InterfacingΒ PIC12F1822Β withΒ 7-segmentΒ display
//Β CommonΒ anodeΒ 7-segmentΒ displayΒ used
//Β http://ccspicc.blogspot.com/
//Β [email protected]
#includeΒ <12F1822.h>
#fusesΒ NOMCLRΒ INTRC_IO
#useΒ delay(clock=8000000)
#useΒ fast_ioΒ (a)
shortΒ s;Β Β Β //Β UsedΒ toΒ knowΒ buttonΒ position
unsignedΒ intΒ j,Β digit,Β digit1,Β digit10,Β digit100;
unsignedΒ longΒ iΒ =Β 0;
unsignedΒ intΒ seg(unsignedΒ intΒ num)Β {
Β Β switchΒ (num)Β {
Β Β Β Β caseΒ 0Β :Β returnΒ 0xC0;
Β Β Β Β caseΒ 1Β :Β returnΒ 0xF9;
Β Β Β Β caseΒ 2Β :Β returnΒ 0xA4;
Β Β Β Β caseΒ 3Β :Β returnΒ 0xB0;
Β Β Β Β caseΒ 4Β :Β returnΒ 0x99;
Β Β Β Β caseΒ 5Β :Β returnΒ 0x92;
Β Β Β Β caseΒ 6Β :Β returnΒ 0x82;
Β Β Β Β caseΒ 7Β :Β returnΒ 0xF8;
Β Β Β Β caseΒ 8Β :Β returnΒ 0x80;
Β Β Β Β caseΒ 9Β :Β returnΒ 0x90;
Β Β Β }
}
voidΒ main()Β {
Β Β setup_oscillator(OSC_8MHZ);Β Β Β Β Β Β Β Β Β Β //Β SetΒ internalΒ oscillatorΒ toΒ 8MHz
Β Β set_tris_a(8);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β ConfigureΒ RA3Β pinΒ asΒ input
Β Β port_a_pullups(8);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β EnableΒ RA3Β internalΒ pull-up
Β Β while(TRUE)Β {
Β Β Β if(input(PIN_A3)Β ==Β 1)
Β Β Β Β Β sΒ =Β 1;
Β Β Β if(sΒ ==Β 1)Β {
Β Β Β Β Β if(input(PIN_A3)Β ==Β 0)Β {
Β Β Β Β Β Β Β sΒ =Β 0;
Β Β Β Β Β Β Β i++;
Β Β Β Β Β Β Β if(iΒ >Β 999)
Β Β Β Β Β Β Β Β Β iΒ =Β 0;
Β Β Β Β Β }
Β Β Β }
Β Β Β digitΒ =Β iΒ %Β 10;
Β Β Β digit1Β =Β seg(digit);
Β Β Β output_a(7);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β TurnΒ offΒ allΒ displays
Β Β Β for(jΒ =Β 0x40;Β jΒ >Β 0;Β jΒ =Β jΒ >>Β 1)Β {
Β Β Β Β Β if(digit1Β &Β j)
Β Β Β Β Β Β Β output_high(PIN_A4);
Β Β Β Β Β else
Β Β Β Β Β Β Β output_low(PIN_A4);
Β Β Β Β Β delay_us(10);
Β Β Β Β Β output_high(PIN_A5);
Β Β Β Β Β delay_us(10);
Β Β Β Β Β output_low(PIN_A5);}
Β Β Β output_low(PIN_A0);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β TurnΒ onΒ displayΒ forΒ ones
Β Β Β delay_ms(1);
Β Β Β digitΒ =Β (iΒ /Β 10)Β %Β 10;
Β Β Β digit10Β =Β seg(digit);
Β Β Β output_a(7);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β TurnΒ offΒ allΒ displays
Β Β Β for(jΒ =Β 0x40;Β jΒ >Β 0;Β jΒ =Β jΒ >>Β 1)Β {
Β Β Β Β Β if((digit10Β &Β j)Β !=Β 0)
Β Β Β Β Β Β Β output_high(PIN_A4);
Β Β Β Β Β else
Β Β Β Β Β Β Β output_low(PIN_A4);
Β Β Β Β Β delay_us(10);
Β Β Β Β Β output_high(PIN_A5);
Β Β Β Β Β delay_us(10);
Β Β Β Β Β output_low(PIN_A5);}
Β Β Β output_low(PIN_A1);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β TurnΒ onΒ displayΒ forΒ tens
Β Β Β delay_ms(1);
Β Β Β digitΒ =Β (iΒ /Β 100)Β %Β 10;
Β Β Β digit100Β =Β seg(digit);
Β Β Β output_a(7);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β TurnΒ offΒ allΒ displays
Β Β Β for(jΒ =Β 0x40;Β jΒ >Β 0;Β jΒ =Β jΒ >>Β 1)Β {
Β Β Β Β Β if((digit100Β &Β j)Β !=Β 0)
Β Β Β Β Β Β Β output_high(PIN_A4);
Β Β Β Β Β else
Β Β Β Β Β Β Β output_low(PIN_A4);
Β Β Β Β Β delay_us(10);
Β Β Β Β Β output_high(PIN_A5);
Β Β Β Β Β delay_us(10);
Β Β Β Β Β output_low(PIN_A5);}
Β Β Β output_low(PIN_A2);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //Β TurnΒ onΒ displayΒ forΒ hundreds
Β Β Β delay_ms(1);
Β Β Β }
}
Interfacing PIC12F1822 with 7-segment display video:
The following video shows project in a hardware circuit with some details.