Nothing Special   »   [go: up one dir, main page]

PWM en Mpalb Xc8 Con Teclado Matricial

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1de 15

/*

* File: Main.c

* Author: JESUS CHAVEZ

* Created on 23 de octubre de 2017, 18:48

*/

#include <xc.h>

#include <pic18f4550.h>

#include <stdio.h>

#include <stdlib.h>

#include "timers.h"

#include "pconfig.h"

#include "configuracion.h"

#include "adc.h"

#include "xlcd.h"

#include "delays.h"

#define _XTAL_FREQ 8000000

long calculo;

long calculo1;

unsigned int Ciclo_de_Trabajo;

unsigned int accion;

long duty;
//// Parametros del Motor DC

unsigned int Eg_nominal;

unsigned int Eg;

unsigned int Vm;

float K_phi;

unsigned int Wm_nominal;

unsigned int Wref;

unsigned int Velocidad_Nominal_en_RPM;

#define pi=3.14;

unsigned int Ia_nominal;

; // Inductancia de armadura

unsigned int frecuencia_de_Trabajo;

void init_ADC(void); //Inicializa ADC

void init_XLCD(void); //Inicializa LCD

void DelayFor18TCY( void ); //Retardo de 18 Ciclos para LCD

void DelayPORXLCD (void); // Retardo de 15ms

void DelayXLCD (void); // Retardo de 5ms

int kbd_getc(); //captura de teclado

//#define configura los pines a los cuales estara conectado el teclado matricial.

#define row1port LATBbits.LATB0

#define row2port LATBbits.LATB1


#define row3port LATBbits.LATB2

#define row4port LATBbits.LATB3

#define col1port PORTBbits.RB4

#define col2port PORTBbits.RB5

#define col3port PORTBbits.RB6

//#define col4port PORTBbits.RB7 //SI SE USA UN TECLADO MATRICIAL 4 X 4.

unsigned char const Seven_Segment_MAP[10] = {0,1,2,3,4,5,6,7,8,9};

char const keyPadMatrix[] =

'1', '2', '3',

'4', '5', '6',

'7', '8', '9',

'*', '0', '#',

0xFF

};

char key,old_key;

//

// Funcion para crear retardos en segundos

//

void Delay_Seconds(unsigned char z)

unsigned char x,y;

for(y = 0; y < z; y++)


{

for(x = 0; x < 100; x++)__delay_ms(10);

// Funcion para limpiar pantalla de LCD

//

void LCD_Clear()

while(BusyXLCD());

WriteCmdXLCD(0x01);

//

// Esta funcion mueve la posicion del cursor a la posicion FILA, COLUMNA

//

void LCD_Move(unsigned char row, unsigned char column)

char ddaddr = 0x40*(row-1) + column;

while( BusyXLCD() );

SetDDRamAddr( ddaddr );

// Funcion PWM

//PR2< 255
void PWM ( int canal,long frecuencia, long duty ){

//

if(canal==1){

calculo =_XTAL_FREQ/4;

calculo=calculo/frecuencia;

if(calculo>255){

calculo=calculo/4;

T2CONbits.T2CKPS0=1;

if(calculo>255){

calculo=calculo/4;

T2CONbits.T2CKPS1=1;

else{

T2CONbits.T2CKPS0=0;

T2CONbits.T2CKPS1=0;

calculo=calculo-1;

PR2=calculo; // PR2=199 if f=10000 y duty=40


calculo=PR2+1;

calculo=calculo*duty;

calculo=calculo*4;

calculo=calculo/100; // este es el valor CCPRXL guardado en calculo (320)

Ciclo_de_Trabajo=calculo;

// ciclo de trabajo *****

CCPR1L=calculo>>2;

CCP1CONbits.DC1B=calculo;

//*************************

TRISCbits.RC2=0;

CCP1CONbits.CCP1M3=1;

CCP1CONbits.CCP1M2=1;

CCP1CONbits.CCP1M1=0;

CCP1CONbits.CCP1M0=0;

T2CONbits.TMR2ON=1;

accion=0;

while( accion==0){

if (accion==0){
// variamos el ciclo de trabajo

for (unsigned int x=0; x<Ciclo_de_Trabajo+1; x++) {

CCPR1L=x>>2;

CCP1CONbits.DC1B=x;

__delay_ms(1);

if(x==Ciclo_de_Trabajo){

accion=1;

//**************Declaracion de variables globales**************************************

unsigned int ADCResult=0;

unsigned short Vin_Max ;

unsigned char IA_Max;// Voltaje de campo de referencia

float ActualTemp;
unsigned char stringKey[10],stringKeyActual[10];

unsigned long keypress;

float ActualVel;

#define CONTACTOR PORTCbits.RC6

#define FAN PORTAbits.RA2

#define ON 1

#define OFF 0

void main (void) {

OSCCON=0b01110010; // 8Mhz

// CONFIGURACION DE PUERTOS DEL PIC18F4550

TRISB = 0xF0; //Usa PORTB para teclado

TRISD = 0x00; //Use PORTD para LCD

PORTD = 0x00;

TRISAbits.RA0 = 1; //ENTRADA ANALOGICA

TRISAbits.RA1 = 1; //ENTRADA ANALOGICA

TRISAbits.RA2 = 0; //RA2 SALIDA

TRISCbits.RC6 = 0; // SALIDA CONTACTOR

TRISBbits.RB7 = 0; // SALIDA ALARMA

LATB = 0x00;

TRISCbits.RC0=0;

TRISCbits.RC1=0;

TRISCbits.RC2=0;
// TRISCbits.RC6=0;

TRISCbits.RC7=0;

LATC=0x04; // Colocamos en pin RC2 en 5v para que no se active el motor al principio

// Parametros del motor

Vin_Max=220;

IA_Max=22;

Wm_nominal=1700;

K_phi=98;

init_XLCD(); //LLAMA LA INICIALIZACIÓN DE LA LCD

init_ADC(); //LLAMA LA INICIALIZACION DEL ADC

LCD_Move(1,0); // MUEVE A FILA=1,COLUMNA=0

putrsXLCD("CONTROLADOR DE "); // MUESTRA MENSAJE INICIAL 1RA LINEA

LCD_Move(2,0); // MUEVE A FILA=2 COLUMNA=1

putrsXLCD(" VELOCIDAD"); // MUESTRA MENSAJE INICIAL 2DA LINEA

Delay_Seconds(2); // RETARDO DE 2 SEGUNDOS

LCD_Clear(); //BORRA LA PANTALLA

CONTACTOR=OFF;

START:

LCD_Clear(); //BORRA LCD

LCD_Move(1,0); // MUEVE A FILA=1,COLUMNA=0

putrsXLCD("INGRESE VELOCIDAD");
Wref =0;

LCD_Move(2,0); // MUEVE A FILA=2 COLUMNA=1

putrsXLCD("Wref: ");

while (1){

keypress = kbd_getc();

if ( keypress == '#' )break; //SI SE PRESIONA #

if ( keypress == '*' )goto START; //SI SE PRESIONA '*' INICIA

if(keypress!=0xFF){

putcXLCD(keypress ) ;

Wref = (10*Wref ) + Seven_Segment_MAP[keypress-'0'];

LCD_Clear(); //BORRA LCD

LCD_Move(1,1); // MUEVE A FILA 1 COLUMNA 1

putrsXLCD("W REF ");

itoa (stringKey,Wref ,10); //CONVIERTE Vel_Ref A STRING

putsXLCD(stringKey); //MUESTRA LA VELOCIDAD

LCD_Move(2,0); // MUEVE A FILA 2 COLUMNA 0.

putrsXLCD("Pulse # -> cont");

keypress =0;
while(keypress!='#')

do

keypress = kbd_getc(); // LEE TECLADO

while(!keypress);

CONTACTOR=ON;

Delay_Seconds(5); // retardo 5 segundos

// float Ra=0.9575; // Resistencia de armadura

// float L=0.0105; // Induntancia

//

// Wm_nominal=(Wm_nominal)*(0.10471); /// Convertimos La velocidad nominal de RPM A


radianes sobre segundos

// Eg_nominal= Vin_Max-IA_Max*(Ra);

// K_phi= (Eg_nominal)/(Wm_nominal); // Constante del Motor

//

//

//

// ///// CALCULAMOS EL CICLO UTIL

//

Wref=Wref*(0.10471); /// Convertimos la velocidad de referencia a radianes sobre segundos


Eg=K_phi*Wref;

Vm=Eg;

//

float duty_cycle=Vm/Vin_Max ;

duty_cycle=duty_cycle;

// PWM (canal,frecuencia,duty)

PWM(1, 2000,duty_cycle);

while(1){

SetChanADC (ADC_CH0);

ADCResult =0;

ConvertADC(); //INICIA CONVERSION DE ADC

while(BusyADC()); //ESPERA A QUE LA CONVERSION SEA FINALIZADA

ADCResult = ReadADC(); //LEE EL DATO CONVERTIDO

ActualVel = (ADCResult*1959.6)/1024;

itoa(stringKeyActual,ActualVel, 10 ); // CONVIERTE EL VALOR EN STRING

putsXLCD(stringKeyActual);

LCD_Move(2,0); // Move to row=2,column=1

putrsXLCD("vel: ");

putsXLCD(stringKeyActual); //MUESTRA LA TEMPERATURA ACTUAL

while(BusyXLCD());

putrsXLCD("RPM");
while(BusyXLCD());

putrsXLCD(" ");

SetDDRamAddr(0xC0);

return;

int kbd_getc(){ // RUTINA DE CAPTAR TECLAS PULSADAS

char key = 0, row;

for( row = 0b00000001; row < 0b00010000; row <<= 1 )

row1port = (row & 0x0001)>>0;

row2port = (row & 0x0002)>>1;

row3port = (row & 0x0004)>>2;

row4port = (row & 0x0008)>>3;

__delay_ms(1);

if( col1port )break; key++;

if( col2port )break; key++;

if( col3port )break; key++;

//if( col4port )break; key++; //PARA 4 COLUMNAS

}
row1port = 0;

row2port = 0;

row3port = 0;

row4port = 0;

if (key!=old_key){

old_key=key;

return keyPadMatrix[ key ];

else

return keyPadMatrix[ 0x0C ];

void init_XLCD(void) //INICIALIZA LA LCD

OpenXLCD(EIGHT_BIT&LINES_5X7); //CONFIGURA LA LCD EN 8 BIT, 2LINEAS

//Y CARACTERES 5X7

while(BusyXLCD()); //VERIFICA SI LA LCD ESTA OCUPADA ANTES DE ENVIAR NUEVOS


COMANDOS

WriteCmdXLCD(0x06); //MUEVE EL CURSOR HACIA LA DERECHA NO MUEVE LA PANTALLA

WriteCmdXLCD(0x0C); //ENCIENDE DISPLAY CON CURSOR ENCENDIDO.

void init_ADC(void) //INICIALIZA LA ADC

OpenADC(ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_2_TAD,

ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,


ADC_1ANA); //PARAMETROS PARA TRABAJAR CON 1 CALA AN0

void DelayFor18TCY( void ) //18 cycles delay

Delay10TCYx(20);

void DelayPORXLCD (void) //Delay of 15ms

Delay1KTCYx(30);

void DelayXLCD (void) //Delay of 5ms

Delay1KTCYx(10);

También podría gustarte