tritiled - Change clock frequency on PIC12LF1571 - Page 1
Forum section: electronics / general

author: joeyjoejoe
author_rank: Frequent Contributor
timestamp: November 09, 2023, 10:38:49 pm
content: Not a PIC guy but trying to get this sorted.https://ww1.microchip.com/downloads/en/devicedoc/40001723d.pdfI'm playing with a Tritiled (https://github.com/tedyapo/tritiled) and I'm getting flickering in the bottom half of the brightness levels.I'm fumbling in the dark here, but I think I want to increase the clock speed from the 31kHz (lowest for power no doubt) to something a bit higher which will hopefully let me drive it faster?I'm trying to put this in the first line of setup()OSCCON = 0b00100000;That should set it to 62.5kHz?This is in addition to the code (https://github.com/tedyapo/tritiled/blob/master/software/tritiled30/src/tritiled_v30_selectable_runtime.c) having// CONFIG1#pragma config FOSC = INTOSC // (INTOSC oscillator; I/O function on CLKIN pin)#pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)// CONFIG2#pragma config WRT = HALF // Flash Memory Self-Write Protection (000h to 1FFh write protected, 200h to 3FFh may be modified by EECON control)#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)#pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled)#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)Can anyone let me know if I'm on the right track?

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 09, 2023, 10:50:26 pm
content: //You must periodically reset WDT in main() or things will flicker.CLRWDT(); //must be somewhere in main() loop/*When the SCSx bits of the OSCCON register = 1x,the system clock source is chosen by the internaloscillator frequency selected by the IRCF<3:0> bitsof the OSCCON register.*///useOSCCONbits.IRCF=nn; //selects INTOSC divider ratio// for examplenn=14; // sets INTOSC to 8mHz//Other OSCCON freqs:IRCF<3:0>: Internal Oscillator Frequency Select bits1111 = 16 MHz HF1110 = 8 MHz or 32 MHz HF (see Section 5.2.2.1 “HFINTOSC”)1101 = 4 MHz HF1100 = 2 MHz HF1011 = 1 MHz HF1010 = 500 kHz HF(1)1001 = 250 kHz HF(1)1000 = 125 kHz HF(1)0111 = 500 kHz MF (default upon Reset)0110 = 250 kHz MF0101 = 125 kHz MF0100 = 62.5 kHz MF0011 = 31.25 kHz HF(1)0010 = 31.25 kHz MF000x = 31 kHz LF

author: joeyjoejoe
author_rank: Frequent Contributor
timestamp: November 09, 2023, 11:03:30 pm
content: Ok thanks. So, I did that, and presumably the clock changed. Flickering is still present.I think WDT is handled.Code:// exit run-time selection modenPOR = 1; // exit startup modeWDTCONbits.WDTPS = 0b10010; // 256s WDT timeout to wake and refresh RAMstart_pwm(mode_idx);SLEEP(); // PWMs run in sleep, pulsing LEDRESET();Does the PWM need some other options to increase the resolution? The prescaler is already at 0.

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 09, 2023, 11:13:18 pm
content: //I don't seeCLRWDT(); //in your code//PWM resolution is quite another story and requires setting a few other things, for example below:void PWM1SETUP(){P1SEL=1; //if P1SEL=1 PWM1out=RA5, else P1SEL=0,RA1=PWM1 (APFCOM)PWM1CLKCONbits.CS=1; //0=FOSC 1=HFINTOSC PWM1 clkPWM1CLKCONbits.PS=0; //0=No Prescale, 1= 1:2 2=1:4 ... 7=128:1PWM1CONbits.POL=0; //Output high at start of PWM cyclePWM1CONbits.MODE=0; //Independent ModePWM1CONbits.OUT=0;PWM1CONbits.EN=1;PWM1CON = 0b11000000;PWM1LDCONbits.LDT=0; //Continuous reload PWM period (standard PWM )PWM1OFCONbits.OFM=0; //No Offset(delay) of duty cyclePWM1LDCONbits.LDA=1; //Load DCyc values after PWM cycle ends, standard PWMPWM1PH=0; //No phase offset of PWM cycle startPWM1DC=0; //Duty cyc PWM1DCL PWM1DCH regs both updated at oncePWM1PR=1024; //Number of PWM OSC cycles = PWM PeriodPWM1OE=1;if(P1SEL==1){ TRISA5=0; //PWM1out on RA5 pin2 when P1SEL=0, else RA1=PWM1out} // else TRISA1=0; //if P1SEL=1 RA5=PWM1outDCyc=0; //DCyc is a 16-bit unsigned var that sets PWM duty cycle somewhere in your code.}

author: joeyjoejoe
author_rank: Frequent Contributor
timestamp: November 09, 2023, 11:16:43 pm
content: It looks like it's expected that after 256s, WDT will trigger and is handled by the main() function?

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 09, 2023, 11:25:28 pm
content: When WDT is triggered, your device is reset, this is a boot, your program is completely reset.So, don't trigger it unless you want WDT to somehow briefly light a LED (this is crude, not a very usable/practical way to do anything)

author: joeyjoejoe
author_rank: Frequent Contributor
timestamp: November 09, 2023, 11:26:17 pm
content: I'm not the original author of the code, and it works. After the initial sequence, the startup section doesn't occur again.

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 09, 2023, 11:29:41 pm
content: So now write some code to do something practical you want done!

author: joeyjoejoe
author_rank: Frequent Contributor
timestamp: November 10, 2023, 12:34:44 am
content: Couldn't figure it out. I adjusted PWM2 PHASE to 2 and that seems to cause it to dim, and changed some of the lookups in modes.h

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 10, 2023, 12:47:11 am
content: You need to not expect anyone to know what you do not reveal codewise. You can change PWm2 phase to whatever you want but it seems you are talking to yourself about Alaska while all I know about is main() street.

author: joeyjoejoe
author_rank: Frequent Contributor
timestamp: November 10, 2023, 12:59:06 am
content: The entire code was in my first post. I'm sorry you missed that.