Reading pot and output square wave. Sounds simple but not......PIC12F1572

Status
Not open for further replies.

GoFourIt

New Member
Hello to all, Ok. Yes I'm new here, but not to electronics.
Today, I spent over 5 hrs reading over similar posts in this forum and I'm still unsure how to implement this.
I'm trying to replace a 555 adjustable Astable timer circuit (10Hz - 4KHz) , with a PIC12F1572 (aka as the inevitable 12F675 replacement), without inheriting the unavoidable change of duty cycle of the 555. In other words I would like to output a true square wave and be able to adjust the frequency. No PWM needed. My biggest hurdle is understanding what happens in the chip that converts "voltage" to digital?? Is it all black magic? Do I have to use the comparetor module or is the ADC enough?
How to best process the bits in ADRESH and convert it to a digital number to implement them into a useful variable time delay. From the delay on, I know how to turn the output on off.
I've look at quite a few tutorials and Yes the analog portion is somewhat overwhelming.
Maybe this is too simple for some, but I'm pushing 60 and things seem to get more difficult as years progress. Thanks for your time.

P.S. I only program in Assembler.
 
Last edited:
I think it was either Elektor or possibly the UK Everyday Practical Electronics magazine did a couple of articles on using a PIC as a 555.

I'll see if I can dig out which one it was and when - you should be able to still download the code from their site and have a look at how they did it (I think it was in assembler).

Will post details as soon as I find them if someone else doesn't beat me to it.
 
Elektor Nov/Dec 2015 - part three of the PIC assembler series of articles.

This link will take you to the page where you can download the code and study it https://www.elektormagazine.com/magazine/elektor-201511/28192

When you open the zip file, it is codes marked as 13 & 14 near the bottom of the list that use a pot to vary the frequency (listing 14 uses interrupts).

Edit:- There is also this series from Nuts & Volts magazine that is similar, the articles and code is free online from their site https://www.nutsvolts.com/magazine/article/December2016_Replacing-555-with-PICs

The links to the other articles is in the panel on the right of the page.
 
Last edited:
Thanks Augustinetez. I now have some reading to do. Looks like this has been done before and if the code is good, I'll just have to change it a bit to get it working with the new chip(12F1572). I'll update whether I have something working or not. Again thanks for the help.
 
P.S. I only program in Assembler.
How are you doing this? MPLABX won't do assembly and MPLAB V8.92 doesn't support that device.

I have an 8 pin 12F1822 here that I could probably get working but can't do assembly due to above reasons.

Mike.
 
You just need to use the ADC module.

Assuming you're using the internal 32MHz clock and the voltage is on AN3,
Set ADCON0 to 0b00001101, ADCON1 to 0b11100000

Then setting bit 1 of ADCON0 will set it going.
When bit 1 is cleared then it's finished and the result is in ADREH and ADRESL

Don't worry about acquisition times as you are only reading 1 input.

Mike.
Edit, I suspect that the 12F1572 datasheet has the same typo as the 12f1822 and AN3 is actually on RA4 as RA3 is MCLR.
 
Last edited:
It's a pretty trivial thing to do - just read a pot via the ADC, and use that value to set the PWM to the frequency you want (just set the PWM to 50%, and leave it at that). However, the ADC is only 10 bit, so you only have 1000 potential frequencies, but that should be plenty anyway - and represents 0.1%.

There are also a number of extra peripherals on some of the later enhanced devices, which might make a better oscillator than the PWM for you, such as the NCO - the 16F18313 is a pretty competent enhanced 8 pin PIC.
 
Using the PWM module for low frequencies (10Hz) can be tricky. Not played with the NCO yet. Might have to setup a 313 to have a play.

Mike.
 
I had a play with this on a 12F1822. This is different from yours as it has a CCP module rather than a PWM only module.

This is in C but will go from about 7Hz to 7kHz as the pot is varied. Output on RA5 pot on RA4.
Code:
#include <xc.h>
#include <stdint.h>

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection->INTOSC oscillator: I/O function on CLKIN pin
#pragma config WDTE = SWDTEN    // Watchdog Timer Enable->WDT controlled by the SWDTEN bit in the WDTCON register
#pragma config PWRTE = OFF    // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = ON    // MCLR Pin Function Select->MCLR/VPP pin function is MCLR
#pragma config CP = OFF    // Flash Program Memory Code Protection->Program memory code protection is disabled
#pragma config CPD = OFF    // Data Memory Code Protection->Data memory code protection is disabled
#pragma config BOREN = ON    // Brown-out Reset Enable->Brown-out Reset enabled
#pragma config CLKOUTEN = OFF    // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
#pragma config IESO = ON    // Internal/External Switchover->Internal/External Switchover mode is enabled
#pragma config FCMEN = ON    // Fail-Safe Clock Monitor Enable->Fail-Safe Clock Monitor is enabled

// CONFIG2
#pragma config WRT = OFF    // Flash Memory Self-Write Protection->Write protection off
#pragma config PLLEN = ON    // PLL Enable->4x PLL enabled
#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 LVP = OFF    // Low-Voltage Programming Enable->High-voltage on MCLR/VPP must be used for programming

#define pot 4
#define out 5
uint32_t period;

void toggle(uint32_t);
    
void main(void){
    // SCS FOSC; SPLLEN disabled; IRCF 8MHz_HF; 
    OSCCON = 0x70;
    OSCTUNE = 0x00;
    BORCON = 0x00;
    while(PLLR == 0);
    // WDTPS 1:65536; SWDTEN OFF; 
    WDTCON = 0x16;
    TRISA = 0x3F-(1<<out);      //out pin output
    ANSELA = 1<<pot;
    WPUA = 0x00;
    OPTION_REGbits.nWPUEN = 1;
    ADCON0=0b00001101;          //select AN3 and ADC on.
    ADCON1=0b11100000;          //Fosc/64 = 2uS conversion time
    T1CON=0b00000001;           //Timer 1 runs at 8MHz
    CCP1CON=0b00001010;         //only set CCP1IF
    while(1){
        GO=1;
        while(GO);
        period=(long)(ADRES+1)<<9;     //*512
        toggle(period);
        PORTA|=1<<out;
        toggle(period);
        PORTA&=255-(1<<out);
    }
}

void toggle(uint32_t per){
    while(per>65535){
        CCPR1+=8000;
        per-=8000l;
        while(!CCP1IF);
        CCP1IF=0;
    }
    CCPR1+=(int)per; 
    while(!CCP1IF);
    CCP1IF=0;    
}

As you work in Assembly this is probably no use to you but may help others or guide you.

Mike.
 
I had a play with this on a 12F1822. This is different from yours as it has a CCP module rather than a PWM only module.

This is in C but will go from about 7Hz to 7kHz as the pot is varied. Output on RA5 pot on RA4.

Nice, pretty decent range.

As you work in Assembly this is probably no use to you but may help others or guide you.

To be fair, it's really all about setting peripheral registers - so should be pretty easy to convert to assembler (or any language) anyway - it's the principle which is important.

And as usual, almost half of the code is setting the fuses
 
To be fair, it's really all about setting peripheral registers - so should be pretty easy to convert to assembler (or any language) anyway - it's the principle which is important.
And as usual, almost half of the code is setting the fuses
True, and setting registers is pretty much the same in any language, ADCON0=0b00001101; changes to MOVLW 0b00001101 movwf ADCON0 (plus bank selection).

And, true about the fuses.

Tomorrow is the time to have a play with the NCO. Beer time now. 8PM here on a Friday night.

Mike.
 
True, and setting registers is pretty much the same in any language, ADCON0=0b00001101; changes to MOVLW 0b00001101 movwf ADCON0 (plus bank selection).

A lot of stuff is easier in assembler than C - as assembler is designed to work with the PIC hardware, which C isn't.

And, true about the fuses.

Tomorrow is the time to have a play with the NCO. Beer time now. 8PM here on a Friday night.

10:00am here, at work

Enjoy your beer!
 
I'll be beavering away here while you're still in your bed in the morning. Enjoy your work.

Mike.
 
Thanks Nigel and Pommie. I didn't realize that MPLAB 8.92 didn't support 12F1572. I guess C is the way of the future since Microchip is more than likely abandoning assembly or it seems like it by not having it supported in MPLABX. I've downloaded and gone through Nigel's ADC tutorial and they've been a lot of help. Hmmm. Well I guess I will have to find another chip. One that will work with MPLAB 8 and a PicKit3.
To learn another programming language for me is just not worth it. The only other chips I have on hand is a 16f88. I'll look into the 12F1822 as I'm really limited by board space and being that it will be nothing more than a square wave generator it would fit nicely where the 555 used to be. I'll keep you guys posted.
Again Thanks for the suggestions.
 
How are you doing this? MPLABX won't do assembly and MPLAB V8.92 doesn't support that device.
If you want to use ASM with MPLABX, your best bet is to install MPLABX 5.35.
That's the last version that included the MPASM assembler v5.87, which supports both the 12F1572 and 12F1822.

Later versions of MPLABX include the new PIC-AS assembler as part of the XC8 compiler package, but PIC-AS isn't compatible with MPASM syntax.

MPLABX 5.35 is the last "32-bit" version. If you install it on a 64-bit win OS, MPLABX will complain about MPASM not being 64-bit but you can ignore that. It works.
 
MPLAB isn't the only choice.. Has anyone assembled with Eclipse??? There has been comments on Stack overflow about using Visual Studio..

Personally I use VSM from Labcenter but it cost ££££'s...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…