Foro Micro Robotica

Anuncio
Foro Micro Robotica -
Generado el: 18 November, 2016, 05:13
Programación Básica del Pic III (Un Ejemplo)
Publicado por 45838321B - 16 Abr 2012 15:28
_____________________________________
Después de todo lo que hemos visto llega el momento de probarlo. Implementaremos un programa para
PIC18f45k20, donde lo que haremos será controlar unos leds que irán encendiéndose uno detrás de
otro de manera cíclica. Con un potenciómetro lo que haremos será controlar la velocidad a la que
cambianlos leds, por lo tanto usaremos el ADC y el TIMER. Con un switch cambiaremos el sentido.
La topología empleada es la siguiente :
7 leds conectados con sus respectivas resistencias de 1 k conectados las patillas de salida del PORTD
1 Potenciómetro conectado al Pin0 del PORTA.
1 Switch (Pulsador) conectado al Pin0 del PORTB.
Aquí podéis encontrar un PDF que he creado, sobre como hacer todo esto en MPLABX
www.mediafire.com/?2857v34fijkwz2s
--------------------------------------------CÓDIGO --> probaadc.c
--------------------------------------------------------------------#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF
// CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30
#pragma config WDTEN = OFF, WDTPS = 32768
// CONFIG2L
// CONFIG2H
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC
#pragma config STVREN = ON, LVP = OFF, XINST = OFF
// CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
// CONFIG5L
// CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
// CONFIG3H
// CONFIG6L
// CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
// CONFIG7L
1/6
Foro Micro Robotica -
Generado el: 18 November, 2016, 05:13
#pragma config EBTRB = OFF
// CONFIG7H
/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
typedef enum { LEFT2RIGHT,
RIGHT2LEFT} LEDDirections;
typedef enum {FALSE, TRUE} BOOL;
// DEFINICIONES
#define Switch_Pin
PORTBbits.RB0
#define DetectsInARow 5
void Timer0_Init(void);
void ADC_Init(void);
unsigned char ADC_Convert(void);
/** V A R I A B L E S *************************************************/
#pragma udata
unsigned char LED_Display; // 8-bit variable
/** D E C L A R A T I O N S *******************************************/
#pragma code
// declare executable instructions
2/6
Foro Micro Robotica -
Generado el: 18 November, 2016, 05:13
void main (void)
{
LEDDirections Direction = LEFT2RIGHT;
BOOL SwitchPressed = FALSE;
LED_Display = 1;
// inicializar el puerto de leds
// Init I/O
TRISD = 0b00000000;
// PORTD bits 7:0 son todo outputs (0)
TRISAbits.TRISA0 = 1; // La patilla An0 es un Input PortA.RA0 Donde tenemos conectado el
potenciómetro
INTCON2bits.RBPU = 0; // Encendemos las resistencias pull_up PuertoB (Donde tenemos el SWITCH)
WPUBbits.WPUB0 = 1; // Enable PULL-UB RB0
TRISBbits.TRISB0 = 1;
// RB0 es un Input
// Init Timer0
Timer0_Init();
// Init ADC
ADC_Init();
while (1)
{
3/6
Foro Micro Robotica -
Generado el: 18 November, 2016, 05:13
if (Direction == LEFT2RIGHT)
{
LED_Display = 1;
// Rotamos los leds a la derecha 1 posición
if (LED_Display == 0)
LED_Display = 0x80;
// Si es igual a 0 ponemos un 1 en el bit 7 del puee
}
LATD = LED_Display;
// Lo ponemos en el puerto D y se encenderan los leds
do
{
if (Switch_Pin == 1)
{
SwitchPressed = FALSE;
}
else if (SwitchPressed == FALSE)
{ // Switch apretado
SwitchPressed = TRUE;
// Cambiar dirección
if (Direction == LEFT2RIGHT)
Direction = RIGHT2LEFT;
else
Direction = LEFT2RIGHT;
}
} while (INTCONbits.TMR0IF == 0);
4/6
Foro Micro Robotica -
Generado el: 18 November, 2016, 05:13
INTCONbits.TMR0IF = 0;
// Reset Timer flag
// Coger ADC conversion y usarla con Timer0
TMR0H = ADC_Convert();
para que este se llene antes
TMR0L = 0;
// Con esto lo que hacemos es poner el valor de ADC en el TIMER
// LSB = 0
}
}
void Timer0_Init(void)
{
INTCONbits.TMR0IF = 0;
// Bit Interrupción contador lleno a 1
T0CON = 0b00000001;
// prescale 1:4
TMR0H = 0;
// Borramos el valor del timer
TMR0L = 0;
T0CONbits.TMR0ON = 1;
// Inicializar el timer
}
void ADC_Init(void)
{ // inicializar el conversor analógico digital
ANSEL = 0;
ANSELH = 0;
ANSELbits.ANS0 = 1; // R0 será una entrada analógica
5/6
Foro Micro Robotica -
Generado el: 18 November, 2016, 05:13
ADCON1 = 0; //No se usa
ADCON2 = 0b00111000; //Configuración típica de ADCON2
// Seleccionamos el canal 0 AN0 | Conversor enable (Bit0)
ADCON0 = 0b00000001;
}
unsigned char ADC_Convert(void)
{ // start an ADC conversion and return the 8 most-significant bits of the result
ADCON0bits.GO_DONE = 1;
// Empezamos la conversión
while (ADCON0bits.GO_DONE == 1);
return ADRESH;
// Esperamos a que esta termine Ponga el bit a 0)
// Retornamos su valor
}
------------------------------------FIN
CÓDIGO----------------------------------------------------------------------------------------============================================================================
6/6
Descargar