Hot!Questions setting up PPS, External interrupts, and PWM in PIC16F15313

Page: 12 > Showing page 1 of 2
Author
ali6x944
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/29 10:36:06
  • Location: 0
  • Status: offline
2017/11/10 12:12:00 (permalink)
0

Questions setting up PPS, External interrupts, and PWM in PIC16F15313

Hi everyone,
I have been trying to make a PWM/mode controller for a broken battery powered flash light I have using PIC16F15313 and the XC8 compiler...
the problem is this is my first project using a PIC micro and I'm a total newbie
I have limited space in the flash light case and I want to operate the micro for as little power as possible.
my inputs to the micro are:
1. Brightness setting Pot input -ADC INPUT-
2. Mode setting switch input -ADC INPUT-
3. Low battery sleep input -Digital INPUT-
4. MCLR pin is Master Clear function
my outputs from the micro are:
1. PWM High control output -PWM3 OUTPUT-
2. PWM Low control output -PWM4 OUTPUT-
this is my pseudo code code:
1. check the Mode setting switch input:
A)if the switch value is bellow or equal to 0.25v or Adc value 256; then:
1.low power mode is engaged (sleep mode)
2.all outputs are set low
B)if the switch value is higher or equal to 0.75v or Adc value 786; then:
1. read Brightness setting Pot input and assign the value to variable ADCB
2.the value of variable ADCB is driving the duty cycle of PWM3
3.PWM3 is outputed on pin RA2
C)if the switch value is between 0.75v and 0.25 or Adc value 786 and 256; then:
1. read Brightness setting Pot input and assign the value to variable ADCB
2.the value of variable ADCB is driving the duty cycle of PWM4
3.PWM4 is outputed on pin RA4
EXint) low battery;then:
the the mode switch is over ridden and the mode is terminated until the battery is charged or external power is connected
1. low power mode is engaged (sleep mode)
2.all outputs are set low
this is my main.c:
#include <xc.h>
#include "config.h"
#include "Interupts.h"
#include "PPSconfig.h"
void main(void) {
TRISAbits.TRISA0=1; // Brightness setting Pot input -ADC INPUT-
TRISAbits.TRISA1=1; // Mode setting switch input -ADC INPUT-
TRISAbits.TRISA2=0; // PWM High control output -PWM3 OUTPUT-
TRISAbits.TRISA4=0; // PWM Low control output -PWM4 OUTPUT-
TRISAbits.TRISA5=1; // Low battery sleep input -Digital INPUT-
ADC_Init(); //Initialize ADC
signed int ADCA;
signed int ADCB;
SLEEP();
while(PORTAbits.RA5!=1){
ADCA=ADC_Read(0);
ADCB=ADC_Read(1);

while(ADCA<=256){
PORTAbits.RA2=0;
PORTAbits.RA3=0;
SLEEP();
}

post edited by ali6x944 - 2017/11/11 12:34:19
#1

30 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 6236
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/10 14:21:03 (permalink)
    +2 (2)
    [1] Your code tag did not work because you omitted the /code tag
     
    [2] Are you using MCC? All the initialisation code is hidden un functions that you have not shown the source for
     
    [3]
    signed int ADCA;
    signed int ADCB;

    XC8 is a "C89" compliant compiler, which means that you cannot create variables in the middle of a block of code, it must be at the start of a block.
    That was a C99 enhancement.
     
    [4] Your main() function should NEVER exit.
    Put a while(1) loop around all the code you want to keep executing over and over.
     
    #2
    NorthGuy
    Super Member
    • Total Posts : 4511
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/10 14:45:57 (permalink)
    +2 (2)
    Your function only sets RA2/RA3 to zero, never one (BTW, you should use LATAbits.LATA2 instead of PORTAbits.RA2 when you write), so you should always see them low regardless of anything.
     
    If SLEEP() puts the PIC asleep, there must be something to wake it up. Hard to tell if you have something configured to do this.
     
    You'll get much lower power consumption with the corresponding LF part (PC16LF15313).
    #3
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/11 02:03:36 (permalink)
    0
    sorry, I didn't copy the code correctly...
    this is the rest of the files:
    this is config.h:
    // 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 = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
     
    // 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 = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
    #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 = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
    #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not 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>
    #define SLEEP() asm("sleep")
    #define _XTAL_FREQ 32000000
    /*ADC Configuration sction*/
    void ADC_Init()
    {
    ADCON0 = 0b00000011;
    /*
    *See the "0b" tells the compiler that this is a binary number,
    * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
    * Bit 7-2 are Analog Channel Select bits, which will select a particular channel out of 6 Analog channels.
    * Bit 1 is A/D Conversion Status bit, which initiates A/D conversion and it will be automatically cleared when the conversion completes.
    * Bit 0 is ADC Enable bit, which basically turns on/off the ADC
    */
    ADCON1 = 0b11110000;
    /*
    *See the "0b" tells the compiler that this is a binary number,
    * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
    * Bit 7 is ADC Result Format Select bit, set for right justified, reset for left justified.
    * Bit 6-4 are ADC Conversion Clock Select bits, which set the ADC sample clock ADCRC (dedicated RC oscillator).
    * Bit 3-2 are not used so set to zero.
    * Bit 1-0 ADC Positive Voltage Reference Configuration bits, which sets the positive reference of the ADC.
    */
    ADACT=0b00000000;
    /*
    *See the "0b" tells the compiler that this is a binary number,
    * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
    * Bit 7-5 are not used so set to zero.
    * Bit 4-0 are Auto-Conversion Trigger Selection bits.
    */
    }
     
    signed int ADC_Read(unsigned int channel)
    {
    if(channel > 5) //Channel range is 0 ~ 5
    {
    return 0;
    }
    ADCON0 &= 00000011; //Clearing channel selection bits
    ADCON0 |= channel<<2; //Setting channel selection bits
    __delay_us(2); //Acquisition time to charge hold capacitor
    ADCON0bits.GOnDONE=1; //Initializes A/D conversion
    while(ADCON0bits.GOnDONE){ //Waiting for conversion to complete
    return ((ADRESL<<8)+ADRESH);
    }
    }

    this is interrupts.h:
    /*Interrupt setup section*/
    INTCONbits.GIE=1; //Enables all active interrupts -Global Interrupt Enable bit-
    INTCONbits.PEIE=1; //Enables all active peripheral interrupts -Peripheral Interrupt Enable bit-
    INTCONbits.INTEDG=1; //Interrupt on rising edge of INT pin -Interrupt Edge Select bit-
     
    PIE0bits.INTE=1; //Enables the INT external interrupt -INT External Interrupt Flag bit-
    PIE0bits.IOCIE=0; //Disables the IOC change interrupt -Interrupt-on-Change Interrupt Enable bit-
    PIE0bits.TMR0IE=0; //Disables the Timer0 interrupt -Timer0 Overflow Interrupt Enable bit-
     
    PIE1bits.ADIE=0; //Disables the ADC interrupt -Analog-to-Digital Converter (ADC) Interrupt Enable bit-
    PIE1bits.CSWIE=0; //The clock switch module interrupt is disabled -Clock Switch Complete Interrupt Enable bit-
    PIE1bits.OSFIE=0; //Disables the Oscillator Fail Interrupt -Oscillator Fail Interrupt Enable bit-
     
    PIE2bits.C1IE=0; //Disables the Comparator C1 interrupt -Comparator C1 Interrupt Enable bit-
    PIE2bits.ZCDIE=0; //Disables the ZCD interrupt -Zero-Cross Detection (ZCD) Interrupt Enable bit-
     
    PIE3bits.BCL1IE=0; //MSSP bus collision interrupt disabled -MSSP1 Bus Collision Interrupt Enable bit-
    PIE3bits.RC1IE=0; //Enables the USART receive interrupt -USART Receive Interrupt Enable bit-
    PIE3bits.SSP1IE=0; //Disables the MSSP interrupt -Synchronous Serial Port (MSSP1) Interrupt Enable bit-
    PIE3bits.TX1IE=0; //Disables the MSSP interrupt -USART Transmit Interrupt Enable bit-
     
    PIE4bits.TMR1IE=0; //Enables the Timer1 overflow interrupt -Timer1 Overflow Interrupt Enable bit-
    PIE4bits.TMR2IE=0; //Disables the Timer2 to PR2 match interrupt -TMR2 to PR2 Match Interrupt Enable bit-
     
    PIE5bits.CLC1IE=0; //CLC1 interrupt disabled -CLC1 Interrupt Enable bit-
    PIE5bits.CLC2IE=0; //CLC2 interrupt disabled -CLC2 Interrupt Enable bit-
    PIE5bits.CLC3IE=0; //CLC3 interrupt disabled -CLC3 Interrupt Enable bit-
    PIE5bits.CLC4IE=0; //CLC4 interrupt disabled -CLC4 Interrupt Enable bit-
    PIE5bits.TMR1GIE=0; //Disables the Timer1 gate acquisition interrupt -Timer1 Gate Interrupt Enable bit-
     
    PIE6bits.CCP1IE=0; //CCP1 interrupt is disabled -CCP1 Interrupt Enable bit-
    PIE6bits.CCP2IE=0; //CCP2 interrupt is disabled -CCP2 Interrupt Enable bit-
     
    PIE7bits.CWG1IE=0; //CWG1 interrupt disabled -Complementary Waveform Generator (CWG) 2 Interrupt Enable bit-
    PIE7bits.NCO1IE=0; //NCO rollover interrupt disabled -NCO Interrupt Enable bit-
    PIE7bits.NVMIE=0; //NVM interrupt not enabled -NVM Interrupt Enable bit-
     
    /*Interrupt Flag Clear section*/
    PIR0bits.INTF=1; //Enables the INT external interrupt -INT External Interrupt Flag bit-
    PIR0bits.IOCIF=0; //Disables the IOC change interrupt -Interrupt-on-Change Interrupt Enable bit-
    PIR0bits.TMR0IF=0; //Disables the Timer0 interrupt -Timer0 Overflow Interrupt Enable bit-
     
    PIR1bits.ADIF=0; //Disables the ADC interrupt -Analog-to-Digital Converter (ADC) Interrupt Enable bit-
    PIR1bits.CSWIF=0; //The clock switch module interrupt is disabled -Clock Switch Complete Interrupt Enable bit-
    PIR1bits.OSFIF=0; //Disables the Oscillator Fail Interrupt -Oscillator Fail Interrupt Enable bit-
     
    PIR2bits.C1IF=0; //Disables the Comparator C1 interrupt -Comparator C1 Interrupt Enable bit-
    PIR2bits.ZCDIF=0; //Disables the ZCD interrupt -Zero-Cross Detection (ZCD) Interrupt Enable bit-
     
    PIR3bits.BCL1IF=0; //MSSP bus collision interrupt disabled -MSSP1 Bus Collision Interrupt Enable bit-
    PIR3bits.RC1IF=0; //Enables the USART receive interrupt -USART Receive Interrupt Enable bit-
    PIR3bits.SSP1IF=0; //Disables the MSSP interrupt -Synchronous Serial Port (MSSP1) Interrupt Enable bit-
    PIR3bits.TX1IF=0; //Disables the MSSP interrupt -USART Transmit Interrupt Enable bit-
     
    PIR4bits.TMR1IF=0; //Enables the Timer1 overflow interrupt -Timer1 Overflow Interrupt Enable bit-
    PIR4bits.TMR2IF=0; //Disables the Timer2 to PR2 match interrupt -TMR2 to PR2 Match Interrupt Enable bit-
     
    PIR5bits.CLC1IF=0; //CLC1 interrupt disabled -CLC1 Interrupt Enable bit-
    PIR5bits.CLC2IF=0; //CLC2 interrupt disabled -CLC2 Interrupt Enable bit-
    PIR5bits.CLC3IF=0; //CLC3 interrupt disabled -CLC3 Interrupt Enable bit-
    PIR5bits.CLC4IF=0; //CLC4 interrupt disabled -CLC4 Interrupt Enable bit-
    PIR5bits.TMR1GIF=0; //Disables the Timer1 gate acquisition interrupt -Timer1 Gate Interrupt Enable bit-
     
    PIR6bits.CCP1IF=0; //CCP1 interrupt is disabled -CCP1 Interrupt Enable bit-
    PIR6bits.CCP2IF=0; //CCP2 interrupt is disabled -CCP2 Interrupt Enable bit-
     
    PIR7bits.CWG1IF=0; //CWG1 interrupt disabled -Complementary Waveform Generator (CWG) 2 Interrupt Enable bit-
    PIR7bits.NCO1IF=0; //NCO rollover interrupt disabled -NCO Interrupt Enable bit-
    PIR7bits.NVMIF=0; //NVM interrupt not enabled -NVM Interrupt Enable bit-

    this is PPS_config.h:
    /*PPS I/O Configuration section*/
    INTPPSbits.INTPPS =

    post edited by ali6x944 - 2017/11/11 04:45:54
    #4
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/11 02:26:45 (permalink)
    0
    NorthGuy
    Your function only sets RA2/RA3 to zero, never one (BTW, you should use LATAbits.LATA2 instead of PORTAbits.RA2 when you write), so you should always see them low regardless of anything.
     
    If SLEEP() puts the PIC asleep, there must be something to wake it up. Hard to tell if you have something configured to do this.
     
    You'll get much lower power consumption with the corresponding LF part (PC16LF15313).


    I'm using the LF part, dose it need any extra code?
    also can I wake up the micro in software?
    I will fix the PORTAbits in the code...
    qhb
    [1] Your code tag did not work because you omitted the /code tag
     
    [2] Are you using MCC? All the initialisation code is hidden un functions that you have not shown the source for
     
    [3]
    signed int ADCA;
    signed int ADCB;

    XC8 is a "C89" compliant compiler, which means that you cannot create variables in the middle of a block of code, it must be at the start of a block.
    That was a C99 enhancement.
     
    [4] Your main() function should NEVER exit.
    Put a while(1) loop around all the code you want to keep executing over and over.
     


    I'm not using MCC, I'll fix the code:
    signed int ADCA;
    signed int ADCB;

    also the interrupts are not quit working with me and I really don't know how to set PPS, I looked in the datasheet and saw table 15-1 page 186 and I'm really lost.
     
     
    #5
    RISC
    Super Member
    • Total Posts : 4582
    • Reward points : 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/11 17:19:29 (permalink)
    +1 (1)
    Hi,
    Do You use MCC to generate initialization code ?
    If not I suggest you do. It is especially useful for the latest PIC16F1xxxx products which have a large number of peripherals and hence a lot of registers to initialize to get the functionality needed.
    Regards
     
    #6
    NorthGuy
    Super Member
    • Total Posts : 4511
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/11 19:29:03 (permalink)
    +2 (2)
    ali6x944
    also the interrupts are not quit working with me and I really don't know how to set PPS, I looked in the datasheet and saw table 15-1 page 186 and I'm really lost.



    I suggest you start from something simple first. Make a blinking LED first, then, once it is working, you add other things - one at a time.
    #7
    DarioG
    leaving this planet
    • Total Posts : 53043
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: online
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/12 02:53:40 (permalink)
    +2 (2)
    Missing a "b" here
     ADCON0 &= 00000011; //Clearing channel selection bits
    [:)]

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #8
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/13 06:51:53 (permalink)
    +1 (1)
    DarioG
    Missing a "b" here
     ADCON0 &= 00000011; //Clearing channel selection bits
    [:)]


    thanks, I will correct it in a moment...
    NorthGuy
    ali6x944
    also the interrupts are not quit working with me and I really don't know how to set PPS, I looked in the datasheet and saw table 15-1 page 186 and I'm really lost.



    I suggest you start from something simple first. Make a blinking LED first, then, once it is working, you add other things - one at a time.


    I did it and here is the code for the blinky light:
    main.c:

    #include <xc.h>
    #include "config.h"
    void main(void) {
    TRISAbits.TRISA0=0;
    while(1){
    LATAbits.LATA0=1;
    __delay_ms(1000);
    LATAbits.LATA0=0;
    __delay_ms(1000);
    }
    return;
    }

    config.h:

    // 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 = LFINT // Power-up default value for COSC bits (LFINTOSC)
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #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 = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
    #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 = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
    #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not 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>
    #define _XTAL_FREQ 31000
     

    RISC
    Hi,
    Do You use MCC to generate initialization code ?
    If not I suggest you do. It is especially useful for the latest PIC16F1xxxx products which have a large number of peripherals and hence a lot of registers to initialize to get the functionality needed.
    Regards
     


    I didn't use MCC at all, also I'm a complete newbie in it sad: sad
    you are right, the PIC16F15323 have a large number of peripherals and even more registers to initialize...
     
    #9
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/14 05:08:56 (permalink)
    +1 (1)
    this is the corrected main.c file:
    #include <xc.h>
    #include "config.h"
    #include "Interupts.h"
    #include "PPSconfig.h"
    signed int ADCA;
    signed int ADCB;`
    void main(void) {
        TRISAbits.TRISA0=1; // Brightness setting Pot input -ADC INPUT-
        TRISAbits.TRISA1=1; // Mode setting switch input -ADC INPUT-
        TRISAbits.TRISA2=0; // PWM High control output -PWM3 OUTPUT-
        TRISAbits.TRISA4=0; // PWM Low control output -PWM4 OUTPUT-
        TRISAbits.TRISA5=1; // Low battery sleep input -Digital INPUT-
        ADC_Init(); //Initialize ADC
        
        SLEEP();
        while(PORTAbits.RA5!=1){
            ADCA=ADC_Read(0);
            ADCB=ADC_Read(1);
            
            while(ADCA<=256){
                LATAbits.LATA2=0;
                LATAbits.LATA4=0;
                SLEEP(); 
            }
            
            while(ADCA>=768){
                LATAbits.LATA2=0;
                
            }
        }
        return;
    }

    and config.h file:
    // 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 = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

    // 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 = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
    #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 = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
    #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not 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>
    #define SLEEP() asm("sleep")
    #define _XTAL_FREQ 32000000
    /*ADC Configuration sction*/
    void ADC_Init()
    {
      ADCON0 = 0b00000011; 
      /*
       *See the "0b" tells the compiler that this is a binary number,
       * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
       * Bit 7-2 are Analog Channel Select bits, which will select a particular channel out of 6 Analog channels.
       * Bit 1 is A/D Conversion Status bit, which initiates A/D conversion and it will be automatically cleared when the conversion completes.
       * Bit 0 is ADC Enable bit, which basically turns on/off the ADC
       */
      ADCON1 = 0b11110000; 
      /*
       *See the "0b" tells the compiler that this is a binary number,
       * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
       * Bit 7 is ADC Result Format Select bit, set for right justified, reset for left justified. 
       * Bit 6-4 are ADC Conversion Clock Select bits, which set the ADC sample clock ADCRC (dedicated RC oscillator).
       * Bit 3-2 are not used so set to zero.
       * Bit 1-0 ADC Positive Voltage Reference Configuration bits, which sets the positive reference of the ADC. 
       */
      ADACT=0b00000000;
      /*
       *See the "0b" tells the compiler that this is a binary number,
       * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
       * Bit 7-5 are not used so set to zero.
       * Bit 4-0 are Auto-Conversion Trigger Selection bits.
       */
    }

    signed int ADC_Read(unsigned int channel)
    {
      if(channel > 5) //Channel range is 0 ~ 5
      { 
        return 0;
      }
      ADCON0 &= 0b00000011; //Clearing channel selection bits
      ADCON0 |= channel<<2; //Setting channel selection bits
      __delay_us(2); //Acquisition time to charge hold capacitor
      ADCON0bits.GOnDONE=1; //Initializes A/D conversion
      while(ADCON0bits.GOnDONE){ //Waiting for conversion to complete
          return ((ADRESL<<8)+ADRESH);
      }
    }

    the ADC is by the looks of it fine I will test it and inform you with the results...
    #10
    qhb
    Superb Member
    • Total Posts : 6236
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/14 05:34:13 (permalink)
    +2 (2)
    Get rid of those SLEEP instructions until you get things working.
    You are still allowing your main function to exit.
    You should NEVER put code into .h files, only definitions.
    Your config.h contains code for void ADC_Init() and ADC_Read().
    They need to go into separate C files, not H files. Just leave them in your main C file until you get things working.
     
    #11
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/14 20:08:36 (permalink)
    0
    qhb
    Get rid of those SLEEP instructions until you get things working.
    You are still allowing your main function to exit.
    You should NEVER put code into .h files, only definitions.
    Your config.h contains code for void ADC_Init() and ADC_Read().
    They need to go into separate C files, not H files. Just leave them in your main C file until you get things working.
     


    fixed...
    but what about the interrupts and pps do they need to be in .c not .h files?
    #12
    qhb
    Superb Member
    • Total Posts : 6236
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/14 20:16:14 (permalink)
    +2 (2)
    Code goes in C files, definitions go in H files.
    If you don't have a good C book that explains this, have a read of:
     
    #13
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/16 22:43:53 (permalink)
    0
    I moved all non-definition terms to main.c, including the interrupts the ADC setup code:
    /*ADC Configuration section*/
    void ADC_Init()
    {
      ADCON0 = 0b00000011; 
      /*
       *See the "0b" tells the compiler that this is a binary number,
       * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
       * Bit 7-2 are Analog Channel Select bits, which will select a particular channel out of 6 Analog channels.
       * Bit 1 is A/D Conversion Status bit, which initiates A/D conversion and it will be automatically cleared when the conversion completes.
       * Bit 0 is ADC Enable bit, which basically turns on/off the ADC
       */
      ADCON1 = 0b11110000; 
      /*
       *See the "0b" tells the compiler that this is a binary number,
       * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
       * Bit 7 is ADC Result Format Select bit, set for right justified, reset for left justified. 
       * Bit 6-4 are ADC Conversion Clock Select bits, which set the ADC sample clock ADCRC (dedicated RC oscillator).
       * Bit 3-2 are not used so set to zero.
       * Bit 1-0 ADC Positive Voltage Reference Configuration bits, which sets the positive reference of the ADC. 
       */
      ADACT=0b00000000;
      /*
       *See the "0b" tells the compiler that this is a binary number,
       * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
       * Bit 7-5 are not used so set to zero.
       * Bit 4-0 are Auto-Conversion Trigger Selection bits.
       */
    }

    signed int ADC_Read(unsigned int channel)
    {
      if(channel > 5) //Channel range is 0 ~ 5
      { 
        return 0;
      }
      ADCON0 &= 0b00000011; //Clearing channel selection bits
      ADCON0 |= channel<<2; //Setting channel selection bits
      __delay_us(2); //Acquisition time to charge hold capacitor
      ADCON0bits.GOnDONE=1; //Initializes A/D conversion
      while(ADCON0bits.GOnDONE){ //Waiting for conversion to complete
          return ((ADRESL<<8)+ADRESH);
      }
    }
    /*End ADC Configuration section*/
    /*Interrupt setup section*/
    INTCONbits.GIE=1; //Enables all active interrupts -Global Interrupt Enable bit-
    INTCONbits.PEIE=1; //Enables all active peripheral interrupts -Peripheral Interrupt Enable bit-
    INTCONbits.INTEDG=1; //Interrupt on rising edge of INT pin -Interrupt Edge Select bit-

    PIE0bits.INTE=1; //Enables the INT external interrupt -INT External Interrupt Flag bit-
    PIE0bits.IOCIE=0; //Disables the IOC change interrupt -Interrupt-on-Change Interrupt Enable bit-
    PIE0bits.TMR0IE=0; //Disables the Timer0 interrupt -Timer0 Overflow Interrupt Enable bit-

    PIE1bits.ADIE=0; //Disables the ADC interrupt -Analog-to-Digital Converter (ADC) Interrupt Enable bit-
    PIE1bits.CSWIE=0; //The clock switch module interrupt is disabled -Clock Switch Complete Interrupt Enable bit-
    PIE1bits.OSFIE=0; //Disables the Oscillator Fail Interrupt -Oscillator Fail Interrupt Enable bit-

    PIE2bits.C1IE=0; //Disables the Comparator C1 interrupt -Comparator C1 Interrupt Enable bit-
    PIE2bits.ZCDIE=0; //Disables the ZCD interrupt -Zero-Cross Detection (ZCD) Interrupt Enable bit-

    PIE3bits.BCL1IE=0; //MSSP bus collision interrupt disabled -MSSP1 Bus Collision Interrupt Enable bit-
    PIE3bits.RC1IE=0; //Enables the USART receive interrupt -USART Receive Interrupt Enable bit-
    PIE3bits.SSP1IE=0; //Disables the MSSP interrupt -Synchronous Serial Port (MSSP1) Interrupt Enable bit-
    PIE3bits.TX1IE=0; //Disables the MSSP interrupt -USART Transmit Interrupt Enable bit-

    PIE4bits.TMR1IE=0; //Enables the Timer1 overflow interrupt -Timer1 Overflow Interrupt Enable bit-
    PIE4bits.TMR2IE=0; //Disables the Timer2 to PR2 match interrupt -TMR2 to PR2 Match Interrupt Enable bit-

    PIE5bits.CLC1IE=0; //CLC1 interrupt disabled -CLC1 Interrupt Enable bit-
    PIE5bits.CLC2IE=0; //CLC2 interrupt disabled -CLC2 Interrupt Enable bit-
    PIE5bits.CLC3IE=0; //CLC3 interrupt disabled -CLC3 Interrupt Enable bit-
    PIE5bits.CLC4IE=0; //CLC4 interrupt disabled -CLC4 Interrupt Enable bit-
    PIE5bits.TMR1GIE=0; //Disables the Timer1 gate acquisition interrupt -Timer1 Gate Interrupt Enable bit-

    PIE6bits.CCP1IE=0; //CCP1 interrupt is disabled -CCP1 Interrupt Enable bit-
    PIE6bits.CCP2IE=0; //CCP2 interrupt is disabled -CCP2 Interrupt Enable bit-

    PIE7bits.CWG1IE=0; //CWG1 interrupt disabled -Complementary Waveform Generator (CWG) 2 Interrupt Enable bit-
    PIE7bits.NCO1IE=0; //NCO rollover interrupt disabled -NCO Interrupt Enable bit-
    PIE7bits.NVMIE=0; //NVM interrupt not enabled -NVM Interrupt Enable bit-

    /*Interrupt Flag Clear section*/
    PIR0bits.INTF=1; //Enables the INT external interrupt -INT External Interrupt Flag bit-
    PIR0bits.IOCIF=0; //Disables the IOC change interrupt -Interrupt-on-Change Interrupt Enable bit-
    PIR0bits.TMR0IF=0; //Disables the Timer0 interrupt -Timer0 Overflow Interrupt Enable bit-

    PIR1bits.ADIF=0; //Disables the ADC interrupt -Analog-to-Digital Converter (ADC) Interrupt Enable bit-
    PIR1bits.CSWIF=0; //The clock switch module interrupt is disabled -Clock Switch Complete Interrupt Enable bit-
    PIR1bits.OSFIF=0; //Disables the Oscillator Fail Interrupt -Oscillator Fail Interrupt Enable bit-

    PIR2bits.C1IF=0; //Disables the Comparator C1 interrupt -Comparator C1 Interrupt Enable bit-
    PIR2bits.ZCDIF=0; //Disables the ZCD interrupt -Zero-Cross Detection (ZCD) Interrupt Enable bit-

    PIR3bits.BCL1IF=0; //MSSP bus collision interrupt disabled -MSSP1 Bus Collision Interrupt Enable bit-
    PIR3bits.RC1IF=0; //Enables the USART receive interrupt -USART Receive Interrupt Enable bit-
    PIR3bits.SSP1IF=0; //Disables the MSSP interrupt -Synchronous Serial Port (MSSP1) Interrupt Enable bit-
    PIR3bits.TX1IF=0; //Disables the MSSP interrupt -USART Transmit Interrupt Enable bit-

    PIR4bits.TMR1IF=0; //Enables the Timer1 overflow interrupt -Timer1 Overflow Interrupt Enable bit-
    PIR4bits.TMR2IF=0; //Disables the Timer2 to PR2 match interrupt -TMR2 to PR2 Match Interrupt Enable bit-

    PIR5bits.CLC1IF=0; //CLC1 interrupt disabled -CLC1 Interrupt Enable bit-
    PIR5bits.CLC2IF=0; //CLC2 interrupt disabled -CLC2 Interrupt Enable bit-
    PIR5bits.CLC3IF=0; //CLC3 interrupt disabled -CLC3 Interrupt Enable bit-
    PIR5bits.CLC4IF=0; //CLC4 interrupt disabled -CLC4 Interrupt Enable bit-
    PIR5bits.TMR1GIF=0; //Disables the Timer1 gate acquisition interrupt -Timer1 Gate Interrupt Enable bit-

    PIR6bits.CCP1IF=0; //CCP1 interrupt is disabled -CCP1 Interrupt Enable bit-
    PIR6bits.CCP2IF=0; //CCP2 interrupt is disabled -CCP2 Interrupt Enable bit-

    PIR7bits.CWG1IF=0; //CWG1 interrupt disabled -Complementary Waveform Generator (CWG) 2 Interrupt Enable bit-
    PIR7bits.NCO1IF=0; //NCO rollover interrupt disabled -NCO Interrupt Enable bit-
    PIR7bits.NVMIF=0; //NVM interrupt not enabled -NVM Interrupt Enable bit-
    /*End Interrupt setup section*/


    #include <xc.h>
    #include "config.h"
    void main(void) {
        TRISAbits.TRISA0=1; // Brightness setting Pot input -ADC INPUT-
        TRISAbits.TRISA1=1; // Mode setting switch input -ADC INPUT-
        TRISAbits.TRISA2=0; // PWM High control output -PWM3 OUTPUT-
        TRISAbits.TRISA4=0; // PWM Low control output -PWM4 OUTPUT-
        TRISAbits.TRISA5=1; // Low battery sleep input -Digital INPUT-
        ADC_Init(); //Initialize ADC
        signed int ADCA;
        signed int ADCB;

        while(PORTAbits.RA5!=1){
            ADCA=ADC_Read(0);
            ADCB=ADC_Read(1);
            
            while(ADCA<=256){
                PORTAbits.RA2=0;
                PORTAbits.RA3=0;
     
            }
            
            while(ADCA>=768){
                PORTAbits.RA4=0;
                
            }
        }
        return;
    }

    but for some reason the ADC setup code is not functioning at all and is not recognised by the compiler.
    this is the config.h file:
     
    // 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 = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

    // 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 = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
    #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 = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
    #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not 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>
    #define SLEEP() asm("sleep")
    #define _XTAL_FREQ 32000000

    post edited by ali6x944 - 2017/11/16 22:56:31
    #14
    DarioG
    leaving this planet
    • Total Posts : 53043
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: online
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 03:44:09 (permalink)
    0
    "not recognised" in what sense? Smile
     
    Also, write to LATx not PORTx

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #15
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 08:17:28 (permalink)
    0
    DarioG
    "not recognised" in what sense? Smile
     
    Also, write to LATx not PORTx


    oh..
    I thought I fixed that? 
    maybe not sad: sad
    here is the error window:


    CLEAN SUCCESSFUL (total time: 60ms)
    make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
    make[1]: Entering directory 'C:/Users/hadaw/MPLABXProjects/FlashLightController.X'
    make -f nbproject/Makefile-default.mk dist/default/production/FlashLightController.X.production.hex
    make[2]: Entering directory 'C:/Users/hadaw/MPLABXProjects/FlashLightController.X'
    "C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe" --pass1 --chip=16LF15313 -Q -G --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/main.p1 main.c 
    make[2]: *** [build/default/production/main.p1] Error 1
    main.c:11: error: (192) undefined identifier "ADCON0"
    make[1]: *** [.build-conf] Error 2
    main.c:19: error: (192) undefined identifier "ADCON1"
    make: *** [.build-impl] Error 2
    main.c:28: error: (192) undefined identifier "ADACT"
    main.c:43: error: (192) undefined identifier "ADCON0"
    main.c:45: warning: (361) function declared implicit int
    main.c:46: error: (192) undefined identifier "ADCON0bits"
    main.c:46: error: (196) struct/union required
    main.c:47: error: (196) struct/union required
    main.c:48: error: (192) undefined identifier "ADRESL"
    main.c:48: error: (192) undefined identifier "ADRESH"
    main.c:50: warning: (343) implicit return at end of non-void function
    main.c:53: warning: (374) missing basic type; int assumed
    main.c:53: error: (314) ";" expected
    main.c:54: warning: (374) missing basic type; int assumed
    main.c:54: error: (314) ";" expected
    main.c:55: warning: (374) missing basic type; int assumed
    main.c:55: error: (314) ";" expected
    main.c:57: warning: (374) missing basic type; int assumed
    main.c:57: error: (314) ";" expected
    main.c:58: warning: (374) missing basic type; int assumed
    main.c:58: error: (314) ";" expected
    main.c:59: warning: (374) missing basic type; int assumed
    main.c:59: error: (314) ";" expected
    main.c:61: warning: (374) missing basic type; int assumed
    main.c:61: error: (314) ";" expected
    main.c:62: warning: (374) missing basic type; int assumed
    main.c:62: error: (314) ";" expected
    main.c:63: warning: (374) missing basic type; int assumed
    main.c:63: error: (314) ";" expected
    main.c:65: warning: (374) missing basic type; int assumed
    main.c:65: error: (314) ";" expected
    main.c:66: warning: (374) missing basic type; int assumed
    main.c:66: error: (314) ";" expected
    main.c:68: warning: (374) missing basic type; int assumed
    main.c:68: advisory: (1) too many errors (21)
    (908) exit status = 1
    nbproject/Makefile-default.mk:106: recipe for target 'build/default/production/main.p1' failed
    make[2]: Leaving directory 'C:/Users/hadaw/MPLABXProjects/FlashLightController.X'
    nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
    make[1]: Leaving directory 'C:/Users/hadaw/MPLABXProjects/FlashLightController.X'
    nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
    BUILD FAILED (exit value 2, total time: 371ms)

    and this is the main.c file:

    /*ADC Configuration section*/
    void ADC_Init()
    {
    ADCON0 = 0b00000011; 
    /*
    *See the "0b" tells the compiler that this is a binary number,
    * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
    * Bit 7-2 are Analog Channel Select bits, which will select a particular channel out of 6 Analog channels.
    * Bit 1 is A/D Conversion Status bit, which initiates A/D conversion and it will be automatically cleared when the conversion completes.
    * Bit 0 is ADC Enable bit, which basically turns on/off the ADC
    */
    ADCON1 = 0b11110000; 
    /*
    *See the "0b" tells the compiler that this is a binary number,
    * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
    * Bit 7 is ADC Result Format Select bit, set for right justified, reset for left justified. 
    * Bit 6-4 are ADC Conversion Clock Select bits, which set the ADC sample clock ADCRC (dedicated RC oscillator).
    * Bit 3-2 are not used so set to zero.
    * Bit 1-0 ADC Positive Voltage Reference Configuration bits, which sets the positive reference of the ADC. 
    */
    ADACT=0b00000000;
    /*
    *See the "0b" tells the compiler that this is a binary number,
    * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
    * Bit 7-5 are not used so set to zero.
    * Bit 4-0 are Auto-Conversion Trigger Selection bits.
    */
    }
    signed int ADC_Read(unsigned int channel)
    {
    if(channel > 5) //Channel range is 0 ~ 5

    return 0;
    }
    ADCON0 &= 0b00000011; //Clearing channel selection bits
    ADCON0 |= channel<<2; //Setting channel selection bits
    __delay_us(2); //Acquisition time to charge hold capacitor
    ADCON0bits.GOnDONE=1; //Initializes A/D conversion
    while(ADCON0bits.GOnDONE){ //Waiting for conversion to complete
    return ((ADRESL<<8)+ADRESH);
    }
    }
    /*End ADC Configuration section*/
    /*Interrupt setup section*/
    INTCONbits.GIE=1; //Enables all active interrupts -Global Interrupt Enable bit-
    INTCONbits.PEIE=1; //Enables all active peripheral interrupts -Peripheral Interrupt Enable bit-
    INTCONbits.INTEDG=1; //Interrupt on rising edge of INT pin -Interrupt Edge Select bit-
    PIE0bits.INTE=1; //Enables the INT external interrupt -INT External Interrupt Flag bit-
    PIE0bits.IOCIE=0; //Disables the IOC change interrupt -Interrupt-on-Change Interrupt Enable bit-
    PIE0bits.TMR0IE=0; //Disables the Timer0 interrupt -Timer0 Overflow Interrupt Enable bit-
    PIE1bits.ADIE=0; //Disables the ADC interrupt -Analog-to-Digital Converter (ADC) Interrupt Enable bit-
    PIE1bits.CSWIE=0; //The clock switch module interrupt is disabled -Clock Switch Complete Interrupt Enable bit-
    PIE1bits.OSFIE=0; //Disables the Oscillator Fail Interrupt -Oscillator Fail Interrupt Enable bit-
    PIE2bits.C1IE=0; //Disables the Comparator C1 interrupt -Comparator C1 Interrupt Enable bit-
    PIE2bits.ZCDIE=0; //Disables the ZCD interrupt -Zero-Cross Detection (ZCD) Interrupt Enable bit-
    PIE3bits.BCL1IE=0; //MSSP bus collision interrupt disabled -MSSP1 Bus Collision Interrupt Enable bit-
    PIE3bits.RC1IE=0; //Enables the USART receive interrupt -USART Receive Interrupt Enable bit-
    PIE3bits.SSP1IE=0; //Disables the MSSP interrupt -Synchronous Serial Port (MSSP1) Interrupt Enable bit-
    PIE3bits.TX1IE=0; //Disables the MSSP interrupt -USART Transmit Interrupt Enable bit-
    PIE4bits.TMR1IE=0; //Enables the Timer1 overflow interrupt -Timer1 Overflow Interrupt Enable bit-
    PIE4bits.TMR2IE=0; //Disables the Timer2 to PR2 match interrupt -TMR2 to PR2 Match Interrupt Enable bit-
    PIE5bits.CLC1IE=0; //CLC1 interrupt disabled -CLC1 Interrupt Enable bit-
    PIE5bits.CLC2IE=0; //CLC2 interrupt disabled -CLC2 Interrupt Enable bit-
    PIE5bits.CLC3IE=0; //CLC3 interrupt disabled -CLC3 Interrupt Enable bit-
    PIE5bits.CLC4IE=0; //CLC4 interrupt disabled -CLC4 Interrupt Enable bit-
    PIE5bits.TMR1GIE=0; //Disables the Timer1 gate acquisition interrupt -Timer1 Gate Interrupt Enable bit-
    PIE6bits.CCP1IE=0; //CCP1 interrupt is disabled -CCP1 Interrupt Enable bit-
    PIE6bits.CCP2IE=0; //CCP2 interrupt is disabled -CCP2 Interrupt Enable bit-
    PIE7bits.CWG1IE=0; //CWG1 interrupt disabled -Complementary Waveform Generator (CWG) 2 Interrupt Enable bit-
    PIE7bits.NCO1IE=0; //NCO rollover interrupt disabled -NCO Interrupt Enable bit-
    PIE7bits.NVMIE=0; //NVM interrupt not enabled -NVM Interrupt Enable bit-
    /*Interrupt Flag Clear section*/
    PIR0bits.INTF=1; //Enables the INT external interrupt -INT External Interrupt Flag bit-
    PIR0bits.IOCIF=0; //Disables the IOC change interrupt -Interrupt-on-Change Interrupt Enable bit-
    PIR0bits.TMR0IF=0; //Disables the Timer0 interrupt -Timer0 Overflow Interrupt Enable bit-
    PIR1bits.ADIF=0; //Disables the ADC interrupt -Analog-to-Digital Converter (ADC) Interrupt Enable bit-
    PIR1bits.CSWIF=0; //The clock switch module interrupt is disabled -Clock Switch Complete Interrupt Enable bit-
    PIR1bits.OSFIF=0; //Disables the Oscillator Fail Interrupt -Oscillator Fail Interrupt Enable bit-
    PIR2bits.C1IF=0; //Disables the Comparator C1 interrupt -Comparator C1 Interrupt Enable bit-
    PIR2bits.ZCDIF=0; //Disables the ZCD interrupt -Zero-Cross Detection (ZCD) Interrupt Enable bit-
    PIR3bits.BCL1IF=0; //MSSP bus collision interrupt disabled -MSSP1 Bus Collision Interrupt Enable bit-
    PIR3bits.RC1IF=0; //Enables the USART receive interrupt -USART Receive Interrupt Enable bit-
    PIR3bits.SSP1IF=0; //Disables the MSSP interrupt -Synchronous Serial Port (MSSP1) Interrupt Enable bit-
    PIR3bits.TX1IF=0; //Disables the MSSP interrupt -USART Transmit Interrupt Enable bit-
    PIR4bits.TMR1IF=0; //Enables the Timer1 overflow interrupt -Timer1 Overflow Interrupt Enable bit-
    PIR4bits.TMR2IF=0; //Disables the Timer2 to PR2 match interrupt -TMR2 to PR2 Match Interrupt Enable bit-
    PIR5bits.CLC1IF=0; //CLC1 interrupt disabled -CLC1 Interrupt Enable bit-
    PIR5bits.CLC2IF=0; //CLC2 interrupt disabled -CLC2 Interrupt Enable bit-
    PIR5bits.CLC3IF=0; //CLC3 interrupt disabled -CLC3 Interrupt Enable bit-
    PIR5bits.CLC4IF=0; //CLC4 interrupt disabled -CLC4 Interrupt Enable bit-
    PIR5bits.TMR1GIF=0; //Disables the Timer1 gate acquisition interrupt -Timer1 Gate Interrupt Enable bit-
    PIR6bits.CCP1IF=0; //CCP1 interrupt is disabled -CCP1 Interrupt Enable bit-
    PIR6bits.CCP2IF=0; //CCP2 interrupt is disabled -CCP2 Interrupt Enable bit-
    PIR7bits.CWG1IF=0; //CWG1 interrupt disabled -Complementary Waveform Generator (CWG) 2 Interrupt Enable bit-
    PIR7bits.NCO1IF=0; //NCO rollover interrupt disabled -NCO Interrupt Enable bit-
    PIR7bits.NVMIF=0; //NVM interrupt not enabled -NVM Interrupt Enable bit-
    /*End Interrupt setup section*/

    #include <xc.h>
    #include "config.h"
    signed int ADCA;
    signed int ADCB;
    TRISAbits.TRISA0=1; // Brightness setting Pot input -ADC INPUT-
    TRISAbits.TRISA1=1; // Mode setting switch input -ADC INPUT-
    TRISAbits.TRISA2=0; // PWM High control output -PWM3 OUTPUT-
    TRISAbits.TRISA4=0; // PWM Low control output -PWM4 OUTPUT-
    TRISAbits.TRISA5=1; // Low battery sleep input -Digital INPUT-
    ADC_Init(); //Initialize ADC
    while(PORTAbits.RA5!=1){
    ADCA=ADC_Read(0);
    ADCB=ADC_Read(1);

    while(ADCA<=256){
    LATAbits.LATA2=0;
    LATAbits.LATA4=0;

    }

    while(ADCA>=768){
    LATAbits.LATA4=0;

    }
    }

    #16
    DarioG
    leaving this planet
    • Total Posts : 53043
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: online
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 08:35:03 (permalink)
    0
    mmmm are you including xc.h file?

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #17
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 08:42:46 (permalink)
    0
    correct
    #18
    ali6x944
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2017/10/29 10:36:06
    • Location: 0
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 08:46:07 (permalink)
    0
    and I also removed the main() thing...
    also if you read the error page there is this 
    main.c:53: error: (314) ";" expected 
    on multiple lines although thay have ;
     
    #19
    DavidBLit
    Super Member
    • Total Posts : 1431
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 08:50:43 (permalink)
    +2 (2)
    1.  All that stuff in the "Interrupt setup section" needs to be in either a function or inside main().
    2. Don't return from main().  Make sure you have an infinite loop so that main() never exits.
    3. Move your #include lines to the top of your .c file.
    4. Don't #include <xc.h>" in your config.h.
    5. Turn LVP off in config.h.
    6. I'm sure there's more...

    Yeah, "//Code and stuff".
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2017 APG vNext Commercial Version 4.5