Pic Example2
Pic Example2
===================
In this project an LEDs is connected to port pin RC0 of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal.
The program flashes the LED con nuously with the following pa ern:
3 flashes with 200 ms delay between each flash
2 s delay
Author: Dogan Ibrahim
Date: August 2013
File: MIKROC-LED2.C
*****************************************************************************/
void main()
{
unsigned char i;
ANSELC = 0; // Configure PORTC as digital
TRISC = 0; // Configure PORTC as output
for(;;) // Endless loop
{
for(i = 0; i < 3; i++) // Do 3 _mes
{
PORTC.RC0 = 1; // LED ON
Delay_ms(200); // 200 ms delay
PORTC.RC0 = 0; // LED OFF
Delay_ms(200); // 200 ms delay
}
Delay_ms(2000); // 2 s delay
}
}
DIP switch SW3: PORTC ON
RANDOM FLASHING LEDS
====================
In this project 8 LEDs are connected to PORTC of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal.
The program uses a pseudorandom number generator to generate a number between 0 and
32767. This number is then divided by 128 to limit it between 1 and 255. The resultant number
is sent to PORTC of the microcontroller. This process is repeated every second.
Author: Dogan Ibrahim
Date: August 2013
File: MIKROC-LED3.C
*****************************************************************************/
void main()
{
unsigned int p;
ANSELC = 0; // Configure PORTC as digital
TRISC = 0; // Configure PORTC as output
srand(10); // Ini alize random number seed
for(;;) // Endless loop
{
p = rand(); // Generate a random number
p = p / 128; // Number between 1 and 255
PORTC = p; // Send to PORTC
Delay_ms(1000); // 1 s delay
}
}
LED BLINKING
====================
void main() {
// set PORTB to be digital output
TRISB = 0;
// Turn OFF LEDs on PORTB
LATB = 0;
while(1) {
// Toggle LEDs on PORTB
LATB = ~LATB;
// Delay 1000 ms
Delay_ms(1000);
}
}
PUSH BUTTON
In this project 1 LED is connected to PORTB.RB0 of a PIC18F45K22 microcontroller and 1 switch
is connected to PORTC.RC0, the microcontroller is operated from an 8 MHz crystal.
The program detects whether the switch is pressed or not. When the switch is pressed, LED
connected to PORTB.RB0 is light up.
*****************************************************************************/
void main() {
TRISC.RC0 = 1; //Configure 1st bit of PORTD as input
TRISB.RB0 = 0; //Configure 1st bit of PORTB as output
PORTB.RB0 = 0; //LED OFF
do
{
if(PORTC.RC0 == 1) //Check whterer the switch is pressed
PORTB.RB0 = 1; //LED ON
else
PORTB.RB0 = 0; //LED OFF
}while(1);
}
DIP switch SW3: PORTC and PORTB ON
Jumper J17 (Bu on Press Level): VCC
LOGIC PROBE
===========
This is a logic probe project. In this project 2 colored LEDs are connected to PORTC pins
RC6 (RED) and RC7 (GREEN). In addi on, RC0 is used as the probe input.
If the logic probe is at logic 0 then the RED LED is turned ON. Otherwise, the GREEN LED is
turned ON.
Author: Dogan Ibrahim
Date: August 2013
File: MIKROC-LED4.C
******************************************************************************
*/
#define PROBE PORTC.RC0
#define RED_LED PORTC.RC6
#define GREEN_LED PORTC.RC7
void main()
{
ANSELC = 0; // Configure PORTC as digital
TRISC0_bit = 1; // Configure RC0 as input
TRISC6_bit = 0; // Configure RC6 as output
TRISC7_bit = 0; // Configure RC7 as output
for(;;) // Endless loop
{
if(PROBE == 0) // If the signal is LOW
{
GREEN_LED = 0; // Turn OFF GREEN LED
RED_LED = 1; // Torn ON RED LED
}
else
{
RED_LED = 0; // Turn OFF RED LED
GREEN_LED = 1; // Turn ON GREEN LED
}
}
}
Jumper J17 (Bu on Press Level): VCC
BUZZER
====================
void main() {
ANSELC=0;
TRISC=0;
PORTC=0;
for(;;)
{unsigned int i,j,k;
for(i=0;i<80;i++)
{PORTC.RC2=1;
Delay_ms(1);
PORTC.RC2=0;
Delay_ms(1);}
for(j=0;j<80;j++)
{ PORTC.RC2=1;
Delay_ms(2);
PORTC.RC2=0;
Delay_ms(2);}
}
}
SIMPLE DICE
==========
In this project 7 LEDs are connected to PORTC of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal. The LEDs are organized as the faces
of a real dice. When a push-bu on switch connected to RB0 is pressed a dice pa ern is
displayed on the LEDs. The display remains in this state for 3 s and a er this period
the LEDs all turn OFF to indicate that the system is ready for the bu on to be pressed again.