How to program a 12F508

Thread Starter

q12x

Joined Sep 25, 2015
1,691
I will definitely use this code in my personal project with the "led wings" !!!
I am building a sound system for the sensor activation (and also leds activation). That's the target usage of my 12f508 ! Im practically using the same sensor output for 2 IC's. One Ic is the led driver lm3914, driving 50 leds, and the next IC (not implemented yet) will be this PIC, being the sound system, beeping for each layer/10 of the lm3914. That's why I needed this ADC for the PIC !
Here is a reminder of the monstruosity I build so far haha
1624213529958.png
 
Last edited:

trebla

Joined Jun 29, 2019
542
In my schematic is grounded only one lead of the capacitor, other end is going to pot and the pot wiper is connected to safety resistor 2k which is connected to GPIO.2.
If we make GPIO.2 output high for longer time, we charge the cap to voltage level, close to Vdd.
Then we must start discharge the cap and measure remaining charge. To accomplish this with one digital pin we must use PWM method to discharge with shorting cap to ground.
To avoid GPIO damage we must ensure that discharge current is below safety level (about 20 mA), for this we use 2k resistor.
With keeping PWM pulses short (1-2us) we achieve more precicion . At 4MHz clock is minimum switching time 1us with assembler code, but with CCS C i can achieve only 2us now, maybe is there some faster method.
Then we switch pin to input, in this mode has pin high impedance and it does not discharge the cap well.
10 instruction cycles delay can be made smaller, i think 5 cycles works also well, it must be tested. Smaller delay gives faster measuring cycle but can inject more noise.
After measuring high level at the input we give next PWM discharge pulse and so on until voltage drops below logic 1 treshold level (see the PIC electrical specs in datasheet).
 

Thread Starter

q12x

Joined Sep 25, 2015
1,691
Hello again.
I promised myself for a very long time (around 1998) I will make a page like this and now I finally made it.
Back then I started with pic16F84 tutorials, but now im smarter to understand the pic with something more basic than that, meaning, with 12F508.
So... here it is. I want you to point me mistakes (because I bet I made some) and also suggest new and interesting points to add.
The idea is to ---visualize--- the functionality of a pic.
q20210902 - easyPIC copy 1.jpg
 

boostbuck

Joined Oct 5, 2017
515
I think you have misunderstood the SFR address map - eg the INDF is a single byte of 8 bits at 00h NOT an array of addresses 00xxh. And the same for the other registers - each a simple 8 bit byte at a single address.
 

Thread Starter

q12x

Joined Sep 25, 2015
1,691
Thank you for the correction !!! I made the correction for the horizontal bits.
Also, I was wondering over the Vertical bits, for example, the 03h, they start at 0300 and end with 03F0, in total 16 bits which is 2bytes.... How many bits is the vertical range?
q20210903 - easyPIC copy 1.jpg
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,225
I think you have misunderstood the SFR address map - eg the INDF is a single byte of 8 bits at 00h NOT an array of addresses 00xxh. And the same for the other registers - each a simple 8 bit byte at a single address.
Even experts do a faceplant occasionally. It really really hurts!
 

boostbuck

Joined Oct 5, 2017
515
Also, I was wondering over the Vertical bits, for example, the 03h, they start at 0300 and end with 03F0, in total 16 bits which is 2bytes.... How many bits is the vertical range?
I'm not clear on what you are saying, and what your diagram is showing. The address of the 'STATUS' FSR for example is 03h, that is THREE, and within the STATUS FSR is 8 bits of status data. It does NOT have an extent of 0300h - 03F0h (which is a non-contiguous 16 bytes of 8 bits). It is just 03h, a value of 3, an index pointing to a register of 8 bits. There is no vertical range.
 

Thread Starter

q12x

Joined Sep 25, 2015
1,691
Excellent explained mister @boostbuck , that is exactly what I wanted to know !
So in their datasheet representation, they are quite literal. I over interpret them a bit. But now I get it. I kind of getting it before, but the uncertainty was too great so I had to start (as hard as it was) something. Thank you very much. Now is way more clear !!! You really help !
Here is the updated artpage:
q20210903c - easyPIC copy 1.jpg
 
Last edited:

trebla

Joined Jun 29, 2019
542
What do yo mean while showing 64 bytes inside a SFR, for example TMR0 holding bytes with adresses 0100h to 0170h? The TMR0 resides at adress 01h and can hold one byte at a time. PIC12f508 SRAM address range is 00h-1Fh and program memory range is 0000h-03FFh. Each SRAM address can hold one byte (00 to FF) and each program memory address can hold 12 bits (instruction + data).
 

Thread Starter

q12x

Joined Sep 25, 2015
1,691
... The TMR0 resides at adress 01h and can hold one byte at a time. PIC12f508 SRAM address range is 00h-1Fh and program memory range is 0000h-03FFh. Each SRAM address can hold one byte (00 to FF) and each program memory address can hold 12 bits (instruction + data).
Yes, very good points. I address them now:
(+corrected some problems)
q20210903c - easyPIC copy 3.jpg
 

Thread Starter

q12x

Joined Sep 25, 2015
1,691
Another update and I think I finish this one. At least I put in there anything I could think of.
Please verify it for me, if you want and is cool with you.
Thanks !
q20210904 - easyPIC copy 1.jpg
 
Last edited:

boostbuck

Joined Oct 5, 2017
515
I can 'ignore each bit value' recognising that you have cut and pasted them, BUT for completeness I'll point out that your 'bit' values in your register map diagram are hexadecimal 16 bits, and can only be 1 or 0 in the actual register.
 

Thread Starter

q12x

Joined Sep 25, 2015
1,691
Last edited:

trebla

Joined Jun 29, 2019
542
As i understand, hidden registers are accessible only with special instructions like TRIS, OPTION and MOVLW when operating with baseline PIC port direction, control and working registers. But the program (flash) memory registers have addresses too and you can point to them by address. Of course, you can write to flash only when program the PIC12f508 but you can read them. Also, configuration bits registers are hidden.
 
Top