TODOPIC
Microcontroladores PIC => Lenguaje C para microcontroladores PIC => Mensaje iniciado por: MONSERRAT en 30 de Mayo de 2011, 02:53:01
-
Hola a todos los miembros del foro, soy estudiante de comunicaciones y realmente no conozco mucho sobre los PIC's.
Estoy intentando hacer un proyecto con el 18F4550 y C18, creo que si se programar en C, mi problema es con los registros internos del pic y todas las posibilidades que hay con este integrado y es que nunca había programado en C un PIC.
Se trata de un juguete de memoria con 4 opciones, quizá pueda hacer lo mismo con menos instrucciones pero creo q funcionaría si hago que los delay no me marquen error y aprendo a poner una interrupción, como un exit() en C, que me regrese al inicio del programa. Aquí esta mi código, espero puedan ayudarme, es muy básico lo que busco, solo que no se que me haga falta.
Preguntando a mis profesores, me dieron una plantilla para empezar mi programa en C y no me explicaron algo sobre que es cada instrucción. prácticamente me dijeron no le muevas, así funciona; yo se que depende de lo que buscamos se configura pero no entiendo que hace cada instrucción y a la hora de correr el programa marca errores en la pantilla
#include <p18F4550.h>
#include <stdlib.h>
#include <stdio.h>
/**************** Bits de Configuracion *************************************/
#pragma config PLLDIV = 5 // (20 MHz crystal on PICDEM FS USB board)
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2 // Clock source from 96MHz PLL/2
#pragma config FOSC = HSPLL_HS
#pragma config FCMEN = OFF //Me marca un error, nose porque dice q no lo reconoce
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = ON
#pragma config BORV = 3 //Lo mismo, error, pero no se a que se deba
#pragma config VREGEN = ON //USB Voltage Regulator
#pragma config WDT = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = ON
#pragma config LPT1OSC = OFF
#pragma config PBADEN = OFF
#pragma config CCP2MX = ON
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming
#pragma config XINST = OFF // Extended Instruction Set
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTB = ON // Boot Block Write Protection
#pragma config WRTC = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
//*Este programa prueba el hardware de la tarjeta con el bootloader
#define L1 LATAbits.LATA4
#define L2 LATAbits.LATA5
#define L3 LATCbits.LATC0
#define L4 LATCbits.LATC1
/** ************ Entradas Digitales *****************************************************/
#define mInitAllSwitches() TRISBbits.TRISB4=1;TRISBbits.TRISB5=1;
#define mInitSwitch2() TRISBbits.TRISB4=1;
#define mInitSwitch3() TRISBbits.TRISB5=1;
#define sw2 PORTBbits.RB5
#define sw3 PORTBbits.RB4
//****************************************************************************************
/** V A R I A B L E S ********************************************************/
#pragma udata
//****************** DEFINICON DE ALGUNAS CONSTANTES *************************************
#define TIMER0L_VAL 0xE5
#define TIMER0H_VAL 0x48
//****************************************************************************************
/************* DECLARACION DE LOS PROTOTIPOS *******************************/
void YourHighPriorityISRCode(void);
void YourLowPriorityISRCode(void);
//void Mi_delay(void);
void EEPROM_Write(unsigned char address,unsigned char databyte);
//****************************************************************************************
/******************** VECTOR REMAPPING *******************************************/
#define REMAPPED_RESET_VECTOR_ADDRESS 0x1000
#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x1008
#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x1018
/*********************************************************************************/
extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
void _reset (void)
{
_asm goto _startup _endasm
}
/*********************************************************************************/
#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
void Remapped_High_ISR (void)
{
_asm goto YourHighPriorityISRCode _endasm
}
/*********************************************************************************/
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR (void)
{
_asm goto YourLowPriorityISRCode _endasm
}
/*********************************************************************************/
/*********************************************************************************/
#pragma code
//******************** VECTOR DE ALTA PRIORIDAD ************************************************
#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode()
{
//AGREGAR CODIGO
} //This return will be a "retfie fast", since this is in a #pragma interrupt section
//****************************************************************************************
//******************** VECTOR DE INTERRUPCION BAJA PRORIDAD ************************************************
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode()
{
//AGREGAR CODIGO
}
/************************ PROGRAMA PRINCIPAL *************************************/
/*********************************************************************************/
void asigna(void);
void compara(void);
int num[60];
int in[60];
int n,i,time,ban;
void main(void)
{ n=i=ban=0;
time=1500;
ADCON1 |=0x0F;
TRISB=0xFF;
TRISA=0X00;
PORTA=0x00;
do
{
num[n]=rand()%4+1;
for(i=0;i<=n;i++)
{ asigna(); }
i=0;
while(i<=n)
{ compara();
if(ban==0)
exit(); //quisiera que regresara al inicio del programa
i++;
}
n++;
time=time-100;
} while (n<60);
}
void asigna(void)
{ switch (num[n])
{ case 1:
LATA=0x01;
Delay1TCYx(time); // dice que no tiene argumento este prototipo, he intentado esta estructura que muchos manejan en ejemplos de la red
LATA=0x00;
break;
case 2:
LATA=0x02;
Delay_ms(time); // otra forma que lo he visto
LATA=0x00;
break;
case 3:
LATA=0x04;
Delay_ms(time);
LATA=0x00;
break;
default:
LATA=0x08;
Delay_ms(time);
LATA=0x00;
break;
}
}
void compara(void)
{ switch (num[n])
{ case 1:
if(PORTBbits.RB0==0 && PORTBbits.RB1==1 && PORTBbits.RB2==1 && PORTBbits.RB3==1 )
ban=1;
break;
case 2:
if(PORTBbits.RB1==0 && PORTBbits.RB0==1 && PORTBbits.RB2==1 && PORTBbits.RB3==1 )
ban=1;
break;
case 3:
if(PORTBbits.RB2==0 && PORTBbits.RB0==1 && PORTBbits.RB1==1 && PORTBbits.RB3==1 )
ban=1;
break;
case 4:
if(PORTBbits.RB3==0 && PORTBbits.RB0==1 && PORTBbits.RB2==1 && PORTBbits.RB1==1 )
ban=1;
break;
default:
LATA=0x0F;
Delay_ms(2300);
LATA=0x00;
ban=0;
break;
}
}
-
Hola: Monserrat. Te recomiendo que leas el tutorial de Suky. En pocos pasos podrás salir adelante.
http://www.micros-designs.com.ar/tutorial-mplab-c18/
Más los ejemplos del foro que puedes ver aquí:
http://www.todopic.com.ar/foros/index.php?topic=14634.0
Saludos.
Jukinch