Electronics > Projects, Designs, and Technical Stuff
thoughts on my project
ali6x944:
Hi everyone,
I'm posting to ask your opinion of my program structure, anyway I can optimize it or reduce its complexity without resorting to using MCC.
so it is a derivation of an older project, I tried to reduce physical & syntax complexity by eliminating both the pain and confusion of using MCC and the low-power-mode pin:
https://www.microchip.com/forums/m1024225.aspx
this project is not fully improved this project is not complete!!
--- Code: ---/***************************************************************************************************************************
*
* Author:Ali6x944
* Date: 8/1/2020
* Device: PIC16LF15313
* Description: A program designed to run on PIC16LF15313, that facilitates a single-chip
* solution to controlling 2 LED Panels/Arrays using a single multi-position slider switch and a pot to control brightness.
*
***************************************************************************************************************************/
// PIC16LF15313 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out reset disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <limits.h>
#include <stdint.h>
unsigned short 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
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), OUTPUT
CCP2PPSbits.CCP2PPS=0b00001; // SMALL LIGHT DRIVER (PWM), 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(1)
{
ADCON0bits.GOnDONE=1; // ADC CONVERSION STATUS BITS
while(ADCON0bits.GOnDONE==1); // WAIT WHILE ADC STATUS IS ON
RESULT=0b0000000000; // CLEAR THE VARIABLE
RESULT=ADRESH; // ADRESH IS IN THE LOWER 8 BITS
RESULT=RESULT<<8; // ADRESH IS SHIFTED UP 8 BIT TO CORRECT LOCATION
RESULT=RESULT|ADRESL; //ADRESL IS JOINTED TO THE RESULT VARIABLE
}
while(BIG_SW==1||SMALL_SW==1)
{
PMD1bits.TMR2MD=0b0; // TIMER2 MOUDLE ENABLED
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
}
}
--- End code ---
so TBH I am stuck trying to configure TIMER2 and CCP1 and 2 registers.
I am still a noobie who is figuring out how C standards and XC8 peculiarities tie together.
hopefully, I can implement low power mode using only a logical operand of BIG_SW and SMALL_SW for if neither one of them is high the low power mode is engaged.
as for the file this time I am only using a single main.c file without any config.h file or anything other files to keep it simple.
I would love to see your suggestions or good advice with regards to PWM situation
thanks a lot.
ali6x944:
how would the ccp module work?
ali6x944:
So here is what I cam up with for the weird ccp PWM module in the PIC16LF15313
--- Code: ---/***************************************************************************************************************************
*
* Author:Ali6x944
* Date: 8/1/2020
* Device: PIC16LF15313
* Description: A program designed to run on PIC16LF15313, that facilitates a single-chip
* solution to controlling 2 LED Panels/Arrays using a single multi-position slider switch and a pot to control brightness.
*
***************************************************************************************************************************/
// PIC16LF15313 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out reset disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <limits.h>
#include <stdint.h>
unsigned short 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
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(1)
{
ADCON0bits.GOnDONE=1; // ADC CONVERSION STATUS BITS
while(ADCON0bits.GOnDONE==1); // WAIT WHILE ADC STATUS IS ON
RESULT=0b0000000000; // CLEAR THE VARIABLE
RESULT=ADRESH; // ADRESH IS IN THE LOWER 8 BITS
RESULT=RESULT<<8; // ADRESH IS SHIFTED UP 8 BIT TO CORRECT LOCATION
RESULT=RESULT|ADRESL; //ADRESL IS JOINTED TO THE RESULT VARIABLE
}
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)
if(BIG_SW==1)
{
CCP1CONbits.CCP1FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
CCP1CONbits.CCP1MODE=0b1111; // MODE IS SET TO PWM
CCPR1Hbits.RH=RESULT&0b01;
CCPR1Hbits.RH=RESULT&0b10;
CCPR1Lbits.RL=RESULT<<8;
T2CONbits.T2ON=0b1;
}
if(SMALL_SW==1)
{
CCP2CONbits.CCP2FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
CCP2CONbits.CCP2MODE=0b1111; // MODE IS SET TO PWM
CCPR2Hbits.RH=RESULT&0b01;
CCPR2Hbits.RH=RESULT&0b10;
CCPR2Lbits.RL=RESULT<<8;
T2CONbits.T2ON=0b1;
}
}
}
--- End code ---
Any thoughts on it?
plz i need all the help i can get
[edit]
not sure of how those bitwise operations work :-[
Prehistoricman:
--- Quote from: ali6x944 on January 12, 2020, 02:27:42 pm ---[edit]
not sure of how those bitwise operations work :-[
--- End quote ---
What's the issue? You should be able to find a bunch of resources online regarding bitwise operations. Essentially, for registers, they will be setting or resetting particular bits to do a particular operation.
You're asking very general questions that probably most people don't know how to answer. If you have some specific issue related to a single operation (for example) then you're more likely to get an answer to question about it.
ali6x944:
--- Quote from: Prehistoricman on January 12, 2020, 08:13:43 pm ---
--- Quote from: ali6x944 on January 12, 2020, 02:27:42 pm ---[edit]
not sure of how those bitwise operations work :-[
--- End quote ---
What's the issue? You should be able to find a bunch of resources online regarding bitwise operations. Essentially, for registers, they will be setting or resetting particular bits to do a particular operation.
You're asking very general questions that probably most people don't know how to answer. If you have some specific issue related to a single operation (for example) then you're more likely to get an answer to question about it.
--- End quote ---
sorry about that, I am kind of a huge noob in this regard so my questions may be quite general cus IDK what makes my programs fail -or work for that matter- :-//
however, there are 4 issues I am trying to solve:
1) configuration of pwm (ccp/timer2)
2) pps configuration ccp out
3)Adc configuration
4) program structure: loops inside main() vs. user-defined functions
this light dimmer controller is designed for video recording applications -so it has to be of high frequency- to avoid flickering.
I really don't have any issues with memory or ram usage so far, as I have most of the memory still vacant...
thx for all help and replys
Navigation
[0] Message Index
[#] Next page
Go to full version