Electronics > Projects, Designs, and Technical Stuff
thoughts on my project
ali6x944:
things to be going ok :-+ :-+
ali6x944:
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: ---.
.
.
/*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();
}
}
}
--- End code ---
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.
Prehistoricman:
--- Quote from: ali6x944 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?
--- End quote ---
Yeah, that would probably be neater.
--- Quote from: ali6x944 on April 18, 2020, 05:02:29 pm ---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...
--- End quote ---
Since you know that you have your input code working then it would be fine to add the PWM to that.
--- Quote from: ali6x944 on April 18, 2020, 05:02:29 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.
--- End quote ---
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.
ali6x944:
--- Quote from: Prehistoricman on April 19, 2020, 02:10:29 am ---
--- Quote from: ali6x944 on April 18, 2020, 05:02:29 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.
--- End quote ---
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.
--- End quote ---
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.
--- End quote ---
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?
Prehistoricman:
Sorry about the late reply. I hope these answers are still useful to you.
--- Quote from: ali6x944 on April 19, 2020, 03:58:45 pm ---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?
--- End quote ---
I think I would initialise them together since you have no use for using them independently.
--- Quote from: ali6x944 on April 19, 2020, 03:58:45 pm ---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.
--- End quote ---
so the CCP here should be an input? but how when I am clearing the TRIS bit?
--- End quote ---
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.
--- Quote from: ali6x944 on April 19, 2020, 03:58:45 pm ---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?
--- End quote ---
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.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version