In this example we will connect an RGB led to our Pic 18F4520 Port D. We will then light the red, green and blue individually.
We will connect this up as follows
DO - RED LED
D1 - GREEN LED
D2 - BLUE LED
Lets see a simple schematic of this
Schematic
Code
This was written using mikroC Pro for PIC, a free evaluation version is available from http://www.mikroe.com/mikroc/pic/
/*
* Project name:
RGB led
*/
void main() {
TRISD = 0; // set direction to be output
do {
LATD = 0xFF; // Turn OFF LEDs on PORTB, common anode RGB LED
Delay_ms(200); // 1 second delay
LATD = 0xFE; // Turn ON RED LED
Delay_ms(200); // 1 second delay
LATD = 0xFF; // Turn OFF LEDs on PORTB, common anode RGB LED
Delay_ms(200); // 1 second delay
LATD = 0xFD; // Turn ON GREEN LED on PORTB
Delay_ms(200); // 1 second delay
LATD = 0xFF; // Turn OFF LEDs on PORTB, common anode RGB LED
Delay_ms(200); // 1 second delay
LATD = 0xFB; // Turn ON BLUE LED
Delay_ms(200); // 1 second delay
LATD = 0xFF; // Turn OFF LEDs on PORTB, common anode RGB LED
Delay_ms(200); // 1 second delay
} while(1); // Endless loop
}
microcontrollers
Sunday, 11 January 2015
attiny pinout
Another excelelnt pinout, this time for an ATtiny , again from http://pighixxx.deviantart.com/gallery/
Attiny85 LED flasher
This simple flashes and led connected to an Attiny
Schematic
Code
The code was written in Atmel Studio
#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
// Define the I/O port to be used for the LED.
#define LED_PORT PB3
int main(void) {
// Set the LED port number as output.
DDRB |= (1 << LED_PORT);
// Start infinite loop.
while (1)
{
// Set the LED bit to "1" - LED will be "on".
PORTB |= (1 << LED_PORT);
// The delay function simply does N-number of "empty" loops.
_delay_ms(200);
// Set the LED bit to "0" - LED will be "off".
PORTB &= ~(1 << LED_PORT);
_delay_ms(400);
}
return (0);
}
And another way
#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
// Define the I/O port to be used for the LED.
#define LED_PORT PB3
int main(void) {
// Set the LED port number as output.
DDRB |= (1 << LED_PORT);
// Start infinite loop.
while (1)
{
// Set the LED bit to "1" - LED will be "on".
PORTB |= (1 << LED_PORT);
// The delay function simply does N-number of "empty" loops.
_delay_ms(200);
// Set the LED bit to "0" - LED will be "off".
PORTB &= ~(1 << LED_PORT);
_delay_ms(400);
}
return (0);
}
Schematic
Code
The code was written in Atmel Studio
#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
// Define the I/O port to be used for the LED.
#define LED_PORT PB3
int main(void) {
// Set the LED port number as output.
DDRB |= (1 << LED_PORT);
// Start infinite loop.
while (1)
{
// Set the LED bit to "1" - LED will be "on".
PORTB |= (1 << LED_PORT);
// The delay function simply does N-number of "empty" loops.
_delay_ms(200);
// Set the LED bit to "0" - LED will be "off".
PORTB &= ~(1 << LED_PORT);
_delay_ms(400);
}
return (0);
}
And another way
#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
// Define the I/O port to be used for the LED.
#define LED_PORT PB3
int main(void) {
// Set the LED port number as output.
DDRB |= (1 << LED_PORT);
// Start infinite loop.
while (1)
{
// Set the LED bit to "1" - LED will be "on".
PORTB |= (1 << LED_PORT);
// The delay function simply does N-number of "empty" loops.
_delay_ms(200);
// Set the LED bit to "0" - LED will be "off".
PORTB &= ~(1 << LED_PORT);
_delay_ms(400);
}
return (0);
}
Subscribe to:
Posts (Atom)