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

Page: < 12 Showing page 2 of 2
Author
DarioG
farewell.
  • Total Posts : 53177
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: offline
Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 08:53:27 (permalink)
+1 (1)
mmm which is line 53? post all of your code...

forget about me, subhumans. adieu

#21
DavidBLit
Super Member
  • Total Posts : 1482
  • 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 09:02:05 (permalink)
+2 (2)
He took out main()!
I'm out.

Yeah, "//Code and stuff".
#22
ali6x944
New Member
  • Total Posts : 20
  • 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 09:38:03 (permalink)
0

 
#include <xc.h>
#include "config.h"
/*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*/
void main(void){
/*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*/


 


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;

}
}
}
}

post edited by ali6x944 - 2017/11/17 09:40:38
#23
ali6x944
New Member
  • Total Posts : 20
  • 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 09:40:00 (permalink)
+1 (1)
DavidBLit
He took out main()!
I'm out.


sorry that was a mistake I didn't mean to, I was trying to include the interrupts in...
#24
ali6x944
New Member
  • Total Posts : 20
  • 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 09:46:35 (permalink)
+1 (1)
I just ran a Clean and build the project and it showed no errors!Smile: Smile 
#25
ali6x944
New Member
  • Total Posts : 20
  • 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 09:47:42 (permalink)
0

CLEAN SUCCESSFUL (total time: 100ms)
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
main.c:51: warning: (343) implicit return at end of non-void function
"C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe" --chip=16LF15313 -G -mdist/default/production/FlashLightController.X.production.map --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" --memorysummary dist/default/production/memoryfile.xml -odist/default/production/FlashLightController.X.production.elf build/default/production/main.p1
Microchip MPLAB XC8 C Compiler (Free Mode) V1.42
Build date: Apr 12 2017
Part Support Version: 1.42
Copyright (C) 2017 Microchip Technology Inc.
License type: Node Configuration
:: warning: (1273) Omniscient Code Generation not available in Free mode
Memory Summary:
Program space used 9Dh ( 157) of 800h words ( 7.7%)
Data space used Bh ( 11) of 100h bytes ( 4.3%)
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%)

You have compiled in FREE mode.
Using Omniscient Code Generation that is available in PRO mode,
you could have produced up to 60% smaller and 400% faster code.
See http://www.microchip.com/MPLABXCcompilers for more information.
make[2]: Leaving directory 'C:/Users/hadaw/MPLABXProjects/FlashLightController.X'
make[1]: Leaving directory 'C:/Users/hadaw/MPLABXProjects/FlashLightController.X'
BUILD SUCCESSFUL (total time: 2s)
Loading code from C:/Users/hadaw/MPLABXProjects/FlashLightController.X/dist/default/production/FlashLightController.X.production.hex...
Loading completed

 
#26
qhb
Superb Member
  • Total Posts : 6258
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 13:29:32 (permalink)
+1 (1)
So are you good to go now?
 
#27
DarioG
farewell.
  • Total Posts : 53177
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: offline
Re: Questions setting up PPS, External interrupts, and PWM in PIC16F15313 2017/11/17 15:21:34 (permalink)
+1 (1)
ahem Smile

forget about me, subhumans. adieu

#28
ali6x944
New Member
  • Total Posts : 20
  • 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 20:58:49 (permalink)
0
well not quite yet...grin: grin
I'm still having problems configuring PPS for the ExINT input and PWM output...
also, I'm not sure that the current code format is optimal...
and I want to implement sleep mode in the micro...
 
 
#29
ali6x944
New Member
  • Total Posts : 20
  • 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/18 06:49:57 (permalink)
0
I saw this code for PWM on circuit digest, I think I can modify it and test it...

#define TMR2PRESCALE 4
#include <xc.h>
long PWM_freq = 5000;
PWM_Initialize()
{
  PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1; //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZ
    CCP1M3 = 1; CCP1M2 = 1;  //Configure the CCP1 module 
    T2CKPS0 = 1;T2CKPS1 = 0; TMR2ON = 1; //Configure the Timer module
    TRISC2 = 0; // make port pin on C as output
}
PWM_Duty(unsigned int duty)
{
      if(duty<1023)
  {
    duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE)); // On reducing //duty = (((float)duty/1023)*(1/PWM_freq)) / ((1/_XTAL_FREQ) * TMR2PRESCALE);
    CCP1X = duty & 1; //Store the 1st bit
    CCP1Y = duty & 2; //Store the 0th bit
    CCPR1L = duty>>2;// Store the remining 8 bit
  }
}

this is the modified code I came up with:

#define TMR2PRESCALE 4
#include <xc.h>
long PWM_freq = 5000;
void PWM_Initialize()
{
PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1; //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZ
CCP1CONbits.CCP1EN=1;
CCP1CONbits.CCP1MODE=1;
CCP1CONbits.CCP1MODE1=1;
CCP1CONbits.CCP1MODE2=1;
CCP1CONbits.CCP1MODE3=1;
T2CKPS0 = 1;//Configure the Timer module
T2CKPS1 = 0; //Configure the Timer module
TMR2ON = 1; //Configure the Timer module
TRISAbits.TRISA0=0; // make port pin on RA0 as output
}
PWM_Duty(unsigned int duty)
{
if(duty<1023)
{
duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE)); // On reducing //duty = (((float)duty/1023)*(1/PWM_freq)) / ((1/_XTAL_FREQ) * TMR2PRESCALE);
CCP1X = duty & 1; //Store the 1st bit
CCP1Y = duty & 2; //Store the 0th bit
CCPR1L = duty>>2;// Store the remining 8 bit
}
}

I really did understand what they ment with CCP1Y/X, and I would like some help in that... 
#30
ali6x944
New Member
  • Total Posts : 20
  • 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/19 10:28:08 (permalink)
0
Is there a difference between generating PWM using the PWM and CCP registers? 
#31
ali6x944
New Member
  • Total Posts : 20
  • 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/26 11:21:40 (permalink)
0
I have used the scope and have seen a pwm-like behaviour for a while but not what I anticipated...

#include <xc.h>
#include "config.h"
/*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*/
/*PWM Configuration section*/
long PWM_freq = 5000;
void PWM_Init()
{
PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1; //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZ
CCP1CONbits.CCP1EN=1;
CCP1CONbits.CCP1MODE=1;
CCP1CONbits.CCP1MODE1=1;
CCP1CONbits.CCP1MODE2=1;
CCP1CONbits.CCP1MODE3=1;
CCP1CONbits.FMT=0;
T2CKPS0 = 1;//Configure the Timer module
T2CKPS1 = 0; //Configure the Timer module
TMR2ON = 1; //Configure the Timer module
TRISAbits.TRISA0=0; // make port pin on RA0 as output
}
PWM_Duty(unsigned int duty)
{
if(duty<1023)
{
duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE)); // On reducing //duty = (((float)duty/1023)*(1/PWM_freq)) / ((1/_XTAL_FREQ) * TMR2PRESCALE);
CCPR1L = duty>>2;// Store the remining 8 bit
}
}

void main(void) {
signed int ADCA;
TRISAbits.TRISA1=1;
ADC_Init();
PWM_Init();
while(1){
ADCA=ADC_Read(1);
LATAbits.LATA0=PWM_Duty(ADCA);
}
return;
}

this was my main.c
and this is my 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 = 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 32000000
#define TMR2PRESCALE 4

#32
ali6x944
New Member
  • Total Posts : 20
  • 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/12/05 11:35:09 (permalink)
0
I was finally able to figure out how-to make the mcc work, the PPS works properly now, all the pwm -which are implemented by CCP1 and CCP2, not PWM3/6- are where I want them to be, but for the interrupts  and PWM control I still have problems...
1. the duty cycle is set once, and varying the value dynamically causes jitter even when the value of the dynamic source is stable.
2. the frequency of the pwm is too.
main.c
 
 
int ADCB;
int ADCB_PWM;
#include <xc.h>
#include "config.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/ext_int.h"
#include "mcc_generated_files/pwm1.h"
#include "mcc_generated_files/pwm2.h"
#include "mcc_generated_files/tmr2.h"
 
/*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.
*/
}
 
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(1); //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*/
 

void main(void) {
SYSTEM_Initialize();
ADC_Init();
TMR2_StartTimer();
while(1){
bool status;
ADCB = ADC_Read(1);
ADCB_PWM = ADCB/(2.566416040100251);
PWM1_LoadDutyValue(ADCB_PWM);
status = PWM1_OutputStatusGet();
}

}
 

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 = HFINTPLL // Power-up default value for COSC bits->HFINTOSC with 2x PLL, with OSCFRQ = 16 MHz and CDIV = 1:1 (FOSC = 32 MHz)
#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 16000000
#define TMR2PRESCALE 4
 

notify me if you need the mcc files...
post edited by ali6x944 - 2017/12/05 11:36:29
#33
Page: < 12 Showing page 2 of 2
Jump to:
© 2018 APG vNext Commercial Version 4.5