Blinking LED using PIC Microcontroller(PIC18F4580)
All digital I/O’s are grouped together into “Ports”, called A, B ,D. Here GPIO means “general purpose input output”. The PIC18F4580 features a total of 36 I/O-pins, called PORTA to PORTD. But there’s more. The device comes with so many additional internal units, that not all features could be connected to dedicated pins of the device package at any one time. The solution is: multiplex. This means, one single physical pin of the device can be used for different functions and it is up to the programmer to decide which function is selected.
Proteus simulation for blinking LED project
Before going to the programming you should understand the following things.
When a digital I/O operation is required, then register TRIS defines the direction of the I/O. Setting a bit position to zero configures the line as an input, setting the bit position to 1 configures the line as an 'output'. For instance, PORTA0 or RA0 to Output Pin, one can use the instruction:
TRISA.RA0 = 0;
A data write to an required port can also be performed with PORT register. Logic one(1) indicates the pin is at logic high and logic zero(0) indicates the pin is logic low.
Additionally, PORT register can be utilized to read digital logic from an Input pin.
CSS Programming Code for Blinking Led.
Blinking led code for pic microcontroller |
#inc
The following CSS program blinks an LED with a delay of 1 second
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
while(1)
{
output_toggle(PIN_A0);
delay_ms(1000);
}
}
{
output_toggle(PIN_A0);
delay_ms(1000);
}
}
mikroC Programming Code for Blinking Led.
The following mikroC program blinks an LED with a delay of 1 second
void main()
{
ADCON1 = 0xff; //Configure Analog Pin as a digital IO
CMCON = 0; // Disable Comparators
TRISA.RA0 = 0; //Makes PORTA0 or RA0 Output Pin
while(1) //Infinite Loop
{
PORTA.RA0 = 1; //LED ON
Delay_ms(1000); //1 Second Delay
PORTA.RA0 = 0; //LED OFF
Delay_ms(1000); //1 Second Delay
}
}
How To Test Blinking led Project Using Oscilloscope
Have you ever found yourself troubleshooting a circuit, needing additional info than an easy multimeter will provide? If you would like to uncover info like frequency, noise, amplitude, or the other characteristic which may some variations over time, oscilloscope is one of that instrument.Now so as to feature the cathode-ray oscilloscope within the circuit, 1st click on the Virtual Instruments Mode as shown within the below figure.
Now drag that cathode-ray oscilloscope and place it within the space,
as you'll be able to see below this element has total four
legs means that you'll be able to read total four differing
kinds of signals and might additionally compare them, if you
would like to.
Okay, lets get started!
Testing Blinking Led
This question gets asked on a daily basis by the students: What resistance do I connect with my LED? Therefore I've place how to work it out.
- Consider the Following Electrical Parameter:
- LED Voltage generally "Forward Voltage" however sometimes simply abbreviated "V".
- LED Current generally "Forward Current". This is often listed in milliamps or "mA"
- Supply voltage This is often what proportion power you're connecting with the LED, in our case 5V are there if Microcontroller pin goes high.
Red LED: 2V 15mA
Green LED: 2.1V 20mA
Blue LED: 3.2V 25mA
While LED: 3.2V 25mA
Green LED: 2.1V 20mA
Blue LED: 3.2V 25mA
While LED: 3.2V 25mA
Okay, lets get started!
in our case consider the following schematic
I have led with the following electrical specification
LED_Red: 2V ,7.5mA
Now we have to calculate R1.
imp Note(Before going to calculate resistor you must check Absolute maximum ratings of Electrical specification for "Maximum current Sourced by any I/O pin" ,of microcontroller form its datasheet in our case see fig below)
From Datasheet we can see that Current(mA) Requirement in our case is less than maximum output current sourced by I/O pin.
Friends for further learning about PIC18 checkout:
Comments
Post a Comment