Autor Tema: Mi pic no trabaja correctamente  (Leído 1076 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado isfan

  • PIC16
  • ***
  • Mensajes: 145
Mi pic no trabaja correctamente
« en: 05 de Noviembre de 2013, 19:38:47 »
Tengo un código PIC18F4550 que no funciona, es muy simple supone que leer un voltaje y sobre la base de los cambios (en mV) producir un PWM (cambiar la duración), pero no produce ninguna salida, necesito a alguien encontra el error no se porque no esta trabajando.


#include <p18f4550.h>
#include <delays.h>
#include <adc.h>
#include <portb.h>
#include <pwm.h>
#pragma config WDT = OFF   /* Turns the watchdog timer off */
#pragma config LVP = OFF   /* Turns low voltage programming off */
#pragma config DEBUG = OFF /* Compile  without extra debug code, this will save space for future*/

/** VECTORREMAPPING *******************************************/

extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
    _asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
    ;
}

#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
    ;
}
#pragma code
//** DECLARATIONS**************************************************//
#pragma code
//**********************Delay function*****************************//
void delayms(unsigned int time)
{
    unsigned int i,j;
    for (i=0; i<time; i++)
        for (j=0; j<135; j++);
}
//************************main function*****************************//
float   x_acc=0;
float   adc_vref_plus=4;
float   adc_vref_min=1;
float   angle=0;
float   duty_cycle=0;
int null_value_voltage=512;     //this is calibration factor which will be changed in ISR

void main(void)
{
//**************** configure INT0 with pullups enabled, falling edge *********************************
    char config_int=0;
    config_int = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON;
    OpenRB0INT(config_int );

//******************PWM initialization**********************************************
        OpenPWM1( 0x00);            //Configure PWM module and initialize PWM period
        SetDCPWM1(0);       //set the duty cycle
    SetOutputPWM1( SINGLE_OUT, PWM_MODE_1); //output PWM in respective modes

//********************ADC initialisation****************************//
    ADCON1 |= 0xFF; // Default all pins to digital
    TRISAbits.TRISA0=1;

    TRISB=0xff;
    TRISC=0;
    TRISD=0;


//**********************Open ADC function**************************//
    OpenADC( ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_1ANA , ADC_CH0 & ADC_INT_OFF & ADC_REF_VREFPLUS_VREFMINUS   ,0b1010);
    delayms( 1 );           // Delay for 50

    //OpenADC( ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_1ANA , ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 0b1010);
    //ADC startup ends

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

    while(1)
        {

            if( INTCONbits.INT0IF == 1)         //Check for INT0
                {
                    ADCON1 = 0x08;          //first 6 channels ADC : on
                    SetChanADC( ADC_CH0 );  // channel 0 set as input
                    ConvertADC();           // Start conversion
                    while( BusyADC() );     // Wait for completion
                    null_value_voltage = ReadADC();     // Read result and store it in x_acc
                    INTCONbits.INT0IF = 0;  //Clear INT0
                }

            ADCON1 = 0x08;          //first 6 channels ADC : on

            SetChanADC( ADC_CH0 );  // channel 0 set as input
            ConvertADC();           // Start conversion
            while( BusyADC() );     // Wait for completion
            x_acc = ReadADC();      // Read result and store it in x_acc

            /*
                formula for converting delta mV to angle is
                angle=(current_volage-mid_voltage)/17
            */
            angle   =   (x_acc - null_value_voltage)    /   17;
                
                if (angle>=10)
                    angle=10;
                else if (angle<=-10)
                    angle=-10;
            /*
                formula for converting angle to duty cycle is
                duty_cycle=4*(angle)+50
            */
            duty_cycle=4*angle + 50;        //this is the % PWM duty_cycle to be set //needs to be changed


        //-------------------set pwm output---------------------------------------------------------.
        SetDCPWM1(duty_cycle*10);       //set the duty cycle

         delayms( 50 );         // Delay for 50
        }

ClosePWM1();
CloseADC();
}