Electronics > Projects, Designs, and Technical Stuff

thoughts on my project

<< < (3/12) > >>

rstofer:
Given that you never exit the while(1) loop, how do you expect to get to the  while(BIG_SW==1||SMALL_SW==1) loop?

ali6x944:

--- Quote from: rstofer on January 18, 2020, 10:07:50 pm ---Given that you never exit the while(1) loop, how do you expect to get to the  while(BIG_SW==1||SMALL_SW==1) loop?


--- End quote ---
can't the PIC run both loops? if not, what is the best way to exit the loop yet periodically read the pot ANSA2?


--- Quote from: Prehistoricman on January 16, 2020, 01:06:45 pm ---I'm saying there is a chance your library supports this 16-bit combined register CCPR1. Just try it! I don't know anything about it. You could also read the header files where these names are defined.
If you're using a proper IDE, you can follow names to where they are defined, so this is very helpful.
--- End quote ---

cool, I'm using MPLABX IDE, XC8 (2.00V), so if a word turns blue it is supported, right?
I searched about the definition in the project files and I came empty-handed, I think I'm missing something as usual |O 


--- Quote from: Prehistoricman on January 16, 2020, 01:06:45 pm ---Can you expand on your question about the LAT register? I can't find it in the datasheet.
ADFM basically does the same thing as the FMT bit. Just as with FMT, I would set it and leave it (in setup code, not the loop).

--- End quote ---
and with regards to LATx register, my question was:
do I need to configure the LATx register to get pwm out of the CCPx register? or is not need as the ccp_out pps is enough?

Prehistoricman:

--- Quote from: ali6x944 on January 21, 2020, 03:45:15 pm ---
--- Quote from: rstofer on January 18, 2020, 10:07:50 pm ---Given that you never exit the while(1) loop, how do you expect to get to the  while(BIG_SW==1||SMALL_SW==1) loop?


--- End quote ---
can't the PIC run both loops? if not, what is the best way to exit the loop yet periodically read the pot ANSA2?
--- End quote ---
Haha... good joke.
I guess you have little programming experience. The fundamental idea behind C code is that it goes from top to bottom in order of execution. You have no defined exit to that first loop so the 2nd one will never get reached. Even if you did reach the second loop, you never update BIG_SW or SMALL_SW and you would probably fall out of main(). A good practice is to have an empty infinite loop at the end of main() so you will never ever return from main().
You need to have a think about how the processor will run the code you've written, and how you should rewrite it to function as you wish.



--- Quote from: ali6x944 on January 21, 2020, 03:45:15 pm ---
--- Quote from: Prehistoricman on January 16, 2020, 01:06:45 pm ---I'm saying there is a chance your library supports this 16-bit combined register CCPR1. Just try it! I don't know anything about it. You could also read the header files where these names are defined.
If you're using a proper IDE, you can follow names to where they are defined, so this is very helpful.
--- End quote ---

cool, I'm using MPLABX IDE, XC8 (2.00V), so if a word turns blue it is supported, right?
I searched about the definition in the project files and I came empty-handed, I think I'm missing something as usual |O 
--- End quote ---
I don't know how MPLABX functions. Again, just try it. The answers to these questions should become apparent just by pressing buttons and figuring out how the IDE works. You can always just build the project and see what fails (bad practice but it works).



--- Quote from: ali6x944 on January 21, 2020, 03:45:15 pm ---
--- Quote from: Prehistoricman on January 16, 2020, 01:06:45 pm ---Can you expand on your question about the LAT register? I can't find it in the datasheet.
ADFM basically does the same thing as the FMT bit. Just as with FMT, I would set it and leave it (in setup code, not the loop).

--- End quote ---
and with regards to LATx register, my question was:
do I need to configure the LATx register to get pwm out of the CCPx register? or is not need as the ccp_out pps is enough?

--- End quote ---
I don't see LATx configuring in the PWM section so yeah I assume that is not necessary. LATx is just the output data and if you're using a peripheral, there's no need to set this because it's bypassed.
You do however need to set the RxyPPS register as it says on page 314.

ali6x944:
hi again,
so I tried to make adc_result into a function but it seems not to work, the error messages seem confusing to me...

--- Code: ---uint16_t RESULT; // ADC RESULT VARIABLE
__bit BIG_SW; // VARIABLE INDICATING STATE OF BIG_SWITCH CONTROLLING BIG PANEL
__bit SMALL_SW; // VARIABLE INDICATING STATE OF SMALL_SWITCH CONTROLING SMALL PANEL
void ADC_RESULT(uint16_t 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
        return RESULT;
    }
int main(void)
{
    OSCFRQ=0b010; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 4MHz
   
    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.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    CCP1PPSbits.CCP1PPS=0b00000; // BIG LIGHT DRIVER (PWM->RA4), OUTPUT
    CCP2PPSbits.CCP2PPS=0b00001; // SMALL LIGHT DRIVER (PWM->RA5), OUTPUT
   
    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)
   
    BIG_SW=PORTAbits.RA4; //READ RA4 AND ASSIGN IT TO BIG_SW
    SMALL_SW=PORTAbits.RA5; //READ RA5 AND ASSIGN IT TO SMALL_SW
   
   
   
    while(BIG_SW==1||SMALL_SW==1)
    {
        PMD1bits.TMR2MD=0b0; // TIMER2 MOUDLE ENABLED
        T2CONbits.CKPS=0b000; //TIMER2 PRESCALER SET TO 1:1
        T2CONbits.OUTPS=0b0000; //TIMER2 POST-SCALER SET TO 1:1
        T2PRbits.T2PR=0b01100011;  // PR2 REGESTER SET TO 99
        T2CLKCONbits.CS=0b0011; // TIMER2 CLOCK SOURCE SELECT BIT (HFINTOSC)
       
        CCP1CONbits.CCP1FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
        CCP1CONbits.CCP1MODE=0b1111; // MODE IS SET TO PWM
        CCP2CONbits.CCP2FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
        CCP2CONbits.CCP2MODE=0b1111; // MODE IS SET TO PWM
       
        if(BIG_SW==1)
        {
            ADC_RESULT();
            CCPR1=RESULT;
            PIR4bits.TMR2IF=0b0;
            T2CONbits.T2ON=0b1;
        }
        if(SMALL_SW==1)
        {
            ADC_RESULT();
            CCPR2=RESULT;
            PIR4bits.TMR2IF=0b0;
            T2CONbits.T2ON=0b1;
        }
    }
   
 
}

--- End code ---
and the error messages were:

--- Code: ---main.c:65:9: error: void function 'ADC_RESULT' should not return a value [-Wreturn-type]
        return RESULT;
        ^      ~~~~~~
main.c:106:24: error: too few arguments to function call, single argument 'RESULT' was not specified
            ADC_RESULT();
            ~~~~~~~~~~ ^
main.c:59:1: note: 'ADC_RESULT' declared here
void ADC_RESULT(uint16_t RESULT)
^
main.c:113:24: error: too few arguments to function call, single argument 'RESULT' was not specified
            ADC_RESULT();
            ~~~~~~~~~~ ^
main.c:59:1: note: 'ADC_RESULT' declared here
void ADC_RESULT(uint16_t RESULT)
^
3 errors generated.


BUILD FAILED (exit value 2, total time: 1s)
--- End code ---
so I tried to fix it by removing the return RESULT, and the uint16_t RESULT from inside void ADC_RESULT(uint16_t RESULT)...

--- Code: ---CLEAN SUCCESSFUL (total time: 1ms)
main.c:120:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
   
Memory Summary:
    Program space        used    88h (   136) of   800h words   (  6.6%)
    Data space           used     8h (     8) of   100h bytes   (  3.1%)
    EEPROM space         None available
    Data stack space     used     0h (     0) of    F0h bytes   (  0.0%)
    Configuration bits   used     5h (     5) of     5h words   (100.0%)
    ID Location space    used     0h (     0) of     4h bytes   (  0.0%)

BUILD SUCCESSFUL (total time: 2s)
Loading completed

--- End code ---
however not really sure how to get rid of the 120:1: warning and not really sure what it does...
now is it necessary with my current code set up to do the same with the PWM ccp code sections?
also with this new ADC_result function, will the micro keep looping it when it enters either the if(SMALL_SW==1) or if(BIG_SW==1)?

Prehistoricman:
Functions always specify a result type before the function name. If it's any type except for void, that means the function returns a value. Functions that do return must have a return statement. Functions that don't return must not have a return statement.

In the way you've set up your ADC_RESULT function, you shouldn't return. You're using a global variable RESULT to store the ADC_RESULT rather than a return value and return statement.
You've also incorrectly set up ADC_RESULT to accept an argument called RESULT. You can remove that. That's why your calls to ADC_RESULT are causing errors.

Again, you don't want to ever leave main(). Your while loop will end if BIG_SW and SMALL_SW are not 1. The function will attempt to return to god knows where and if you're lucky, the microcontroller will reset.
And again, you're not updating BIG_SW or SMALL_SW. You only read from the GPIO port once.

So, my suggested changes are:
Remove the condition from the while loop and change it to while (true) or while (1). This is known as an infinite loop.
Inside the loop, read the button values into BIG_SW and SMALL_SW.
Think about all the register operations you have in the loop there. Do you need to run these all the time?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod