PRACTICAS CON PIC 18f45k50
Encender LED con pic
CODIGO EN ARDUINO
#include <18F45K50.h> //Incluye el microcontrolador con el que se va a trabajar
#use delay( internal = 48MHz ) // Tipo de oscilador y frecuencia dependiendo del microcontrolador
#build( reset = 0x02000, interrupt = 0x02008 ) // Asigna los vectores de reset e interrupción para la versión con bootloader
#org 0x0000, 0x1FFF {} // Reserva espacio en memoria para el bootloader
#define LED PIN_A4 // Asigna el pin A4 al LED del X-TRAINER en versiones anteriores se recomienda utilizar el pin A1
#define BOTON PIN_A2 // Asigna el pin A2 al boton BOOT del X-TRAINER
void main( void ) {
set_tris_a( 0b11101111 ); // Pin A4 como salida
while ( 1 ) { // Ciclo repetitivo
if ( 0 == input( BOTON ) ) { // Lee el estado del boton
output_high( LED ); // Si se presiona el boton enciende el LED
} else {
output_low( LED ); // De lo contrario el LED se apaga
}
}
}
FOTOS DE LA PRACTICA
VIDEO DEMOSTRATIVO
Parpadeo de un LED
CODIGO EN ARDUINO
#include <18F4550.h> // Incluye el microcontrolador con el que se va a trabajar
#use delay( clock = 48Mhz, crystal ) // Tipo de oscilador y frecuencia dependiendo del microcontrolador
#build( reset = 0x02000, interrupt = 0x02008 ) // Asignación de los vectores de reset e interrupción
#org 0x0000, 0x1FFF {} // Reserva espacio en la memoria para la versión con bootloader
#define LED PIN_A4 // Pin donde está conectado el LED del X-TRAINER
void main( void ) {
set_tris_a( 0b11101111 ); // Pin RA4 como salida
while ( 1 ) {
output_HIGH( LED ); // Cambio de estado en el pin RA4
delay_ms( 500 ); // Retardo
output_LOW( LED ); // Cambio de estado en el pin RA4
delay_ms( 500 ); // Retardo
}
}
FOTO DE LA PRACTICA
En la parte superior izquierda de la placa se encuentra un led el cual es el que va a prender y apagar.
Teclado matricial
CODIGO DE ARDUINO
#include <18F45K50.h> //Incluye el microcontrolador con el que se va a trabajar
#use delay(internal=48MHz) // Tipo de oscilador y frecuencia dependiendo del microcontrolador
#build(reset=0x02000,interrupt=0x02008) // Asigna los vectores de reset e interrupción para la versión con bootloader
#org 0x0000,0x1FFF {} // Reserva espacio en memoria para el bootloader
#define LED PIN_A4 //Pin donde está conectado el LED del X-TRAINER
char TECLA_PRESS;
/*
---------------------------------------------------------------------------
CONFIGURACIÓN DE PINES TECLADO
---------------------------------------------------------------------------
*/
#define row0 PIN_B0 //Filas del teclado colocar resistencia pullup
#define row1 PIN_B1 //Filas del teclado colocar resistencia pullup
#define row2 PIN_B2 //Filas del teclado colocar resistencia pullup
#define row3 PIN_B3 //Filas del teclado colocar resistencia pullup
#define col0 PIN_B4 //Columnas del teclado
#define col1 PIN_B5 //Columnas del teclado
#define col2 PIN_B6 //Columnas del teclado
#define col3 PIN_B7 //Columnas del teclado
#include "Teclado4x4.h" //Librería de teclado
void main()
{
kbd_init (); //inicialización del teclado.
while(TRUE)
{
TECLA_PRESS = kbd_getc () ;
if (TECLA_PRESS == '1')
{
output_HIGH (LED); //Cambio de estado en el pin RA1
}
if (TECLA_PRESS == '0')
{
output_LOW (LED); //Cambio de estado en el pin RA1
}
}
}
FOTO DE PRACTICA
VIDEO DEMOSTRATIVO
LCD
CODIGO DE ARDUINO
#include <18F45K50.h> //Incluye el microcontrolador con el que se va a trabajar
#use delay(internal=48MHz) // Tipo de oscilador y frecuencia dependiendo del microcontrolador
#build(reset=0x02000,interrupt=0x02008) // Asigna los vectores de reset e interrupción para la versión con bootloader
#org 0x0000,0x1FFF {} // Reserva espacio en memoria para el bootloader
/*
------------------------------------------------------------------------------
CONFIGURACION DE PINES LCD
------------------------------------------------------------------------------
*/
#define LCD_RS PIN_B0 //CONTROL del LCD
#define LCD_RW PIN_B1
#define LCD_E PIN_B2
#define LCD_DB4 PIN_B3 //DATOS del LCD (4 lineas)
#define LCD_DB5 PIN_B4
#define LCD_DB6 PIN_B5
#define LCD_DB7 PIN_B6
#include "flex_lcd.h" //LIBRERIA LCD
void main()
{
lcd_init (); // Inicialización del lcd.
delay_ms (50) ;
while(1)
{
printf (lcd_putc, " MICROSIDE"); //Entre comillas escribimos el mensaje a mostrar
delay_ms (2000);
lcd_gotoxy (1, 2); //Segundo renglón
printf (lcd_putc, " TECHNOLOGY"); //Entre comillas escribimos el mensaje a mostrar
delay_ms (2000);
lcd_gotoxy (1, 1); //regresa cursor al inicio
lcd_init (); //limpia display
delay_ms (1000);
printf (lcd_putc, " BIENVENIDOS"); //Entre comillas escribimos el mensaje a mostrar
delay_ms (2000);
lcd_init (); //limpia display
delay_ms (2000);
}
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
LiquidCrystal Autoscroll
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
// set the cursor to (0,0):
lcd.setCursor(0, 0);
// print from 0 to 9:
for (int thisChar = 0; thisChar < 10; thisChar++) {
lcd.print(thisChar);
delay(500);
}
// set the cursor to (16,1):
lcd.setCursor(16, 1);
// set the display to automatically scroll:
lcd.autoscroll();
// print from 0 to 9:
for (int thisChar = 0; thisChar < 10; thisChar++) {
lcd.print(thisChar);
delay(500);
}
// turn off automatic scrolling
lcd.noAutoscroll();
// clear screen for the next loop:
lcd.clear();
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
CURSOR
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// Turn off the cursor:
lcd.noCursor();
delay(500);
// Turn on the cursor:
lcd.cursor();
delay(500);
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION
BLINK
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// Turn off the blinking cursor:
lcd.noBlink();
delay(3000);
// Turn on the blinking cursor:
lcd.blink();
delay(3000);
}
FOTO DE LA PRACTICA
SIMULACION
CUSTOM CHARACTERS
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// make some custom characters:
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
byte smiley[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b10001,
0b01110,
0b00000
};
byte frownie[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b00000,
0b01110,
0b10001
};
byte armsDown[8] = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp[8] = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
void setup() {
// initialize LCD and set up the number of columns and rows:
lcd.begin(16, 2);
// create a new character
lcd.createChar(0, heart);
// create a new character
lcd.createChar(1, smiley);
// create a new character
lcd.createChar(2, frownie);
// create a new character
lcd.createChar(3, armsDown);
// create a new character
lcd.createChar(4, armsUp);
// set the cursor to the top left
lcd.setCursor(0, 0);
// Print a message to the lcd.
lcd.print("I ");
lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte
lcd.print(" Arduino! ");
lcd.write((byte)1);
}
void loop() {
// read the potentiometer on A0:
int sensorReading = analogRead(A0);
// map the result to 200 - 1000:
int delayTime = map(sensorReading, 0, 1023, 200, 1000);
// set the cursor to the bottom row, 5th position:
lcd.setCursor(4, 1);
// draw the little man, arms down:
lcd.write(3);
delay(delayTime);
lcd.setCursor(4, 1);
// draw him arms up:
lcd.write(4);
delay(delayTime);
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION
DISPLAY
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// Turn off the display:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
delay(500);
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION

HOLA MUNDO
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("FELIZ CUMPLEANOS");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION
SCROLL
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("saca las chelas chabela!");
delay(1000);
}
void loop() {
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
// wait a bit:
delay(150);
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
// delay at the end of the full loop:
delay(1000);
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION
SET CURSOR
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// these constants won't change. But you can change the size of
// your LCD using them:
const int numRows = 2;
const int numCols = 16;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(numCols, numRows);
}
void loop() {
// loop from ASCII 'a' to ASCII 'z':
for (int thisLetter = 'a'; thisLetter <= 'z'; thisLetter++) {
// loop over the columns:
for (int thisRow = 0; thisRow < numRows; thisRow++) {
// loop over the rows:
for (int thisCol = 0; thisCol < numCols; thisCol++) {
// set the cursor position:
lcd.setCursor(thisCol, thisRow);
// print the letter:
lcd.write(thisLetter);
delay(200);
}
}
}
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION
TEXT DIRECTION
CODIGO DE ARDUINO
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int thisChar = 'a';
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// turn on the cursor:
lcd.cursor();
}
void loop() {
// reverse directions at 'm':
if (thisChar == 'm') {
// go right for the next letter
lcd.rightToLeft();
}
// reverse again at 's':
if (thisChar == 's') {
// go left for the next letter
lcd.leftToRight();
}
// reset at 'z':
if (thisChar > 'z') {
// go to (0,0):
lcd.home();
// start again at 0
thisChar = 'a';
}
// print the character
lcd.write(thisChar);
// wait a second:
delay(1000);
// increment the letter:
thisChar++;
}
FOTO DE LA PRACTICA
VIDEO DEMOSTRATIVO
SIMULACION
No hay comentarios:
Publicar un comentario