Author Topic: thoughts on my project  (Read 11025 times)

0 Members and 1 Guest are viewing this topic.

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #50 on: April 16, 2020, 02:36:43 pm »
things to be going ok :-+ :-+
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #51 on: April 18, 2020, 05:02:29 pm »
for the ADC and TMR2 and CCP setups and routines:
should they be done in functions outside the main function and called from within the main loop?
for the to be tested ADC and TMR2 and CCP functionalities I think I will test both TMR2 and CCP last cus of their sheer complexity, but I am finding it hard to basically test it without the other two...
anyways this is what I came up with:
Code: [Select]
.
.
.
/*INCLUDES AND DEFINITIONS*/
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 4000000

/*VARIABLE DEFFENITION SECTION*/
uint16_t RESULT; // ADC RESULT VARIABLE

#define BIG_SW PORTAbits.RA4 //READ RA4 AND ASSIGN IT TO BIG_SW
#define SMALL_SW PORTAbits.RA5 //READ RA5 AND ASSIGN IT TO SMALL_SW


/*FUNCTION INTITILIZATION SECTION*/
void ADC_INIT()
{
    ADCON0bits.CHS=0b000010; // ANALOG CHANNEL SELECTION BITS (RA2)
    ADCON1bits.ADCS=0b111; // ADC COVERSION CLOCK SELECTION BITS (ADCRC)
    ADCON0bits.ADON=1; // ANALOG CHANNEL SELECTION BITS (ADCON HIGH)
    ADCON1bits.ADPREF=0b00; //ADC POSITIVE VOLTAGE REFRENCE CONFIGRATION bits (VREF+ IS VDD)
    ADCON1bits.ADFM=1; // ADC RESULT FORMAT SELECTION BITS (RIGHT JUSTIFIED)
}
void ADC_RESULT()
    {
        ADCON0bits.GOnDONE=1; // ADC CONVERSION STATUS BITS
        while(ADCON0bits.GOnDONE==1); // WAIT WHILE ADC STATUS IS ON
        RESULT = ADRESH << 8; //Load ADC high value
        RESULT |= ADRESL; //Load ADC low value
    }
void ADC_TEST()
{
    if ((SMALL_SW||BIG_SW)&&(RESULT>512))
    {
        LATAbits.LATA1 = 1; //LED ON
        LATAbits.LATA0 = 1; //LED ON
        __delay_ms(1000);
        LATAbits.LATA1 = 0; //LED OFF
        LATAbits.LATA0 = 0; //LED OFF
        __delay_ms(1000);
    }
    else if(SMALL_SW&&(RESULT<512))
    {
        LATAbits.LATA1 = 1; //LED ON
    }
    else if (BIG_SW&&(RESULT<512)) 
    {
        LATAbits.LATA0 = 1; //LED ON
    }
}

/*MAIN FUNCTION*/
void main(void)
{
    OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
    //(BIG_SW&&SMALL_SW)||(!BIG_SW&&!SMALL_SW)
    /*TRISx AND ANSELx ASSIGNMENT */
    TRISAbits.TRISA0=0; // TRISA0(RA0) BIG LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA1=0; // TRISA1(RA1) SMALL LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA2=1; // TRISA2(RA2) ANALOG POT, INPUT
    TRISAbits.TRISA4=1; // TRISA4(RA4) BIG LIGHT SWITCH, INPUT
    TRISAbits.TRISA5=1; // TRISA5(RA5) SMALL LIGHT SWITCH, INPUT
    ANSELAbits.ANSA0=0; // ANSELA0(ANSA0) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA1=0; // ANSELA0(ANSA1) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    ANSELAbits.ANSA4=0; // ANSELA4(ANSA4) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA5=0; // ANSELA5(ANSA5) DISABLED, INPUT DIGITAL
   
    /*LATx STEADY-STATE*/
    LATAbits.LATA1 = 0;
    LATAbits.LATA0 = 0;
   
    /*FUNCTION INTITILIZATION CALL*/
    ADC_INIT();
   
    /*MAIN LOOP*/
    while(1)
    {
        if(BIG_SW==SMALL_SW)
        {
            LATAbits.LATA0 = 0; //LED OFF
            LATAbits.LATA1 = 0; //LED OFF
           
        }
        else if(SMALL_SW)
        {
            ADC_RESULT();
            ADC_TEST();
        }
        else if (BIG_SW)
        {
            ADC_RESULT();
            ADC_TEST();
        }
    }
   
}
It seems to work pretty nicely however it glitches around the 512 marks, or when if I switch between the big and small switch, but as a proof of operation, I am stuck trying to do the same but for TMR2 and CCP as the individual test function is kinda confusing to write independently from ADC.
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #52 on: April 19, 2020, 02:10:29 am »
for the ADC and TMR2 and CCP setups and routines:
should they be done in functions outside the main function and called from within the main loop?
Yeah, that would probably be neater.

for the to be tested ADC and TMR2 and CCP functionalities I think I will test both TMR2 and CCP last cus of their sheer complexity, but I am finding it hard to basically test it without the other two...
Since you know that you have your input code working then it would be fine to add the PWM to that.

It seems to work pretty nicely however it glitches around the 512 marks, or when if I switch between the big and small switch, but as a proof of operation, I am stuck trying to do the same but for TMR2 and CCP as the individual test function is kinda confusing to write independently from ADC.
The glitching could be noise on the ADC/input. If you had an Arduino and printed the ADC value to serial, you would see it fluctuate a bit.
Do you want to explain how you're stuck?
As the first test of PWM, I would leave out the switch logic and just make the pot control the dimming of the LEDs.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #53 on: April 19, 2020, 03:58:45 pm »
It seems to work pretty nicely however it glitches around the 512 marks, or when if I switch between the big and small switch, but as a proof of operation, I am stuck trying to do the same but for TMR2 and CCP as the individual test function is kinda confusing to write independently from ADC.
The glitching could be noise on the ADC/input. If you had an Arduino and printed the ADC value to serial, you would see it fluctuate a bit.
Do you want to explain how you're stuck?
As the first test of PWM, I would leave out the switch logic and just make the pot control the dimming of the LEDs.
well, this is where I am stuck:
1. TMR2 and CCP initialization function:
A) is it better to initialize them together in a single function or in separate functions?
B) in section "28.3.2 SETUP FOR PWM OPERATION" step 1:
Quote
1. Use the desired output pin RxyPPS control to
select CCPx as the source and disable the
CCPx pin output driver by setting the associated
TRIS bit.
so the CCP here should be an input? but how when I am clearing the TRIS bit?
2.test methodology:
A) how can I verify the operation of the CCP or the TMR2 if the total PWM function does not work during implementation?  i.e. how can I test them independently in their intended role?
B) how can I eliminate faults caused by signal noise or physical irregularities during testing, so I am testing only the code?
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #54 on: April 29, 2020, 04:57:19 pm »
Sorry about the late reply. I hope these answers are still useful to you.

well, this is where I am stuck:
1. TMR2 and CCP initialization function:
A) is it better to initialize them together in a single function or in separate functions?
I think I would initialise them together since you have no use for using them independently.

B) in section "28.3.2 SETUP FOR PWM OPERATION" step 1:
Quote
1. Use the desired output pin RxyPPS control to
select CCPx as the source and disable the
CCPx pin output driver by setting the associated
TRIS bit.
so the CCP here should be an input? but how when I am clearing the TRIS bit?
This is just part of the initialisation. You see it's step 1? It's so the PWM isn't being used while it's being configured, therefore reducing glitches. The TRIS bit is cleared in step 6.


2.test methodology:
A) how can I verify the operation of the CCP or the TMR2 if the total PWM function does not work during implementation?  i.e. how can I test them independently in their intended role?
B) how can I eliminate faults caused by signal noise or physical irregularities during testing, so I am testing only the code?
A) I don't think you can. Without setting up the CCP, you can't make PWM happen. Without setting up TMR2, you have no control over the pulse width.
B) Have your pulse width input be controlled by a for loop, for example. I wouldn't bother since you already know the ADC code is working.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #55 on: June 20, 2020, 04:48:07 pm »
hi again,
so I am kinda stuck with the PWM subroutine...
Code: [Select]
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 4000000

/*VARIABLE DEFFENITION SECTION*/
uint16_t RESULT; // ADC RESULT VARIABLE

#define BIG_SW PORTAbits.RA4 //READ RA4 AND ASSIGN IT TO BIG_SW
#define SMALL_SW PORTAbits.RA5 //READ RA5 AND ASSIGN IT TO SMALL_SW


/*FUNCTION INTITILIZATION SECTION*/
void ADC_INIT()
{
    ADCON0bits.CHS=0b000010; // ANALOG CHANNEL SELECTION BITS (RA2)
    ADCON1bits.ADCS=0b111; // ADC COVERSION CLOCK SELECTION BITS (ADCRC)
    ADCON0bits.ADON=1; // ANALOG CHANNEL SELECTION BITS (ADCON HIGH)
    ADCON1bits.ADPREF=0b00; //ADC POSITIVE VOLTAGE REFRENCE CONFIGRATION bits (VREF+ IS VDD)
    ADCON1bits.ADFM=1; // ADC RESULT FORMAT SELECTION BITS (RIGHT JUSTIFIED)
}
void ADC_RESULT()
{
    ADCON0bits.GOnDONE=1; // ADC CONVERSION STATUS BITS
    while(ADCON0bits.GOnDONE==1); // WAIT WHILE ADC STATUS IS ON
    RESULT = ADRESH << 8; //Load ADC high value
    RESULT |= ADRESL; //Load ADC low value
}
void CCP1_TMR2_INIT()
{
    CCP1PPSbits.CCP1PPS=0b00000; // BIG LIGHT DRIVER (PWM->RA0), OUTPUT
    TRISAbits.TRISA0=0; // TRISA0(RA0) BIG LIGHT DRIVE, OUTPUT
    PR2bits.T2PR=0b10; // PR2 SET TO 2
    CCP1CONbits.MODE=0b1111; // CPP1 MODE (PWM)
    CCP1CONbits.FMT=1; // RIGHT-ALIGNED PWM FORMAT
    PIR4bits.TMR2IF=0; // TIMER2 INTERUPT FLAG CLEARED
    T2CONbits.CKPS=0b000; // 1:1 TIMER2 PRESCALER VALUE
    T2CONbits.OUTPS=0b0000; // 1:1 TIMER2 POSTSCALER VALUE
    T2CONbits.ON=1; // TIMER2 ON 
}
void CCP2_TMR2_INIT()
{
    CCP2PPSbits.CCP2PPS=0b00000; // SMALL LIGHT DRIVER (PWM->RA1), OUTPUT
    TRISAbits.TRISA1=0; // TRISA1(RA1) SMALL LIGHT DRIVE, OUTPUT
    PR2bits.T2PR=0b10; // PR2 SET TO 2
    CCP2CONbits.MODE=0b1111; // CPP2 MODE (PWM)
    CCP2CONbits.FMT=0; // LEFT-ALIGNED PWM FORMAT
    PIR4bits.TMR2IF=0; // TIMER2 INTERUPT FLAG CLEARED
    T2CONbits.CKPS=0b000; // 1:1 TIMER2 PRESCALER VALUE
    T2CONbits.OUTPS=0b0000; // 1:1 TIMER2 POSTSCALER VALUE
    T2CONbits.ON=1; // TIMER2 ON 
}
void PWM1_SBR()
{
    if (BIG_SW)
    {
        CCP1CONbits.CCP1EN=1; // CCP1 MODULE ON
        CCPR1L:CCPR1H=RESULT; // LOAD RESULT IN CCPR1L:CCPR1H REGISTER
    }
    else
    {
        CCP1CONbits.CCP1EN=0; // CCP1 MODULE OFF
    }
   
}
void PWM2_SBR()
{
    if (SMALL_SW)
    {
        CCP2CONbits.CCP2EN=1; // CCP2 MODULE ON
        CCPR2L:CCPR2H=RESULT; // LOAD RESULT IN CCPR2L:CCPR2H REGISTER
    }
    else
    {
        CCP2CONbits.CCP2EN=0; // CCP2 MODULE OFF
    }
   
}


/*MAIN FUNCTION*/
void main(void)
{
    OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
    //(BIG_SW&&SMALL_SW)||(!BIG_SW&&!SMALL_SW)
    /*TRISx AND ANSELx ASSIGNMENT*/
    TRISAbits.TRISA0=0; // TRISA0(RA0) BIG LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA1=0; // TRISA1(RA1) SMALL LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA2=1; // TRISA2(RA2) ANALOG POT, INPUT
    TRISAbits.TRISA4=1; // TRISA4(RA4) BIG LIGHT SWITCH, INPUT
    TRISAbits.TRISA5=1; // TRISA5(RA5) SMALL LIGHT SWITCH, INPUT
    ANSELAbits.ANSA0=0; // ANSELA0(ANSA0) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA1=0; // ANSELA0(ANSA1) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    ANSELAbits.ANSA4=0; // ANSELA4(ANSA4) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA5=0; // ANSELA5(ANSA5) DISABLED, INPUT DIGITAL
   
    /*CCPx ASSIGNMENT*/
    CCP1PPSbits.CCP1PPS=0b00000; // BIG LIGHT DRIVER (PWM->RA0), OUTPUT
    CCP2PPSbits.CCP2PPS=0b00001; // SMALL LIGHT DRIVER (PWM->RA1), OUTPUT
   
    /*LATx STEADY-STATE*/
    LATAbits.LATA1 = 0;
    LATAbits.LATA0 = 0;
   
    /*FUNCTION INTITILIZATION CALL*/
    ADC_INIT();
    CCP1_TMR2_INIT();
    CCP2_TMR2_INIT();
    /*MAIN LOOP*/
    while(1)
    {
        if(BIG_SW==SMALL_SW)
        {
            LATAbits.LATA0 = 0; //LED OFF
            LATAbits.LATA1 = 0; //LED OFF
           
        }
        else if(SMALL_SW)
        {
            ADC_RESULT();
            PWM2_SBR();
           
        }
        else if (BIG_SW)
        {
            ADC_RESULT();
            PWM1_SBR();
           
        }
    }
   
}

I think I followed the steps correctly, but I'm not really sure...
I think the problem is in PWMx_SBR I tried to manipulate it but with no real improvement  |O
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #56 on: July 18, 2020, 12:21:46 pm »
am I missing something in the initialization or the actual pwm subroutine function?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf