PDA

View Full Version : 12F675 code sample



marad73
- 20th May 2006, 19:44
Can anyone help me find some sample front end code in PBP to set up a 12F675? What I need is something that I can modify to suit my application that includes defines, configs, etc. Need to use WDT, POR, BOD and two of the ADCs to compare 2 inputs. Also want to wake up from SLEEPUNCAL using WDT, use internal EEPROM, etc. Once I have something to start with, I can add stuff as necessary!
This chip is new to me! Help will be greatly appreciated!
Ron

Not a lazy horse - just don't know where the start of the 12F675 race is!!

mister_e
- 20th May 2006, 21:26
Here's something really basic and untested to start with



'
' PIC Configuration
' =================
@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
' Internal Oscillator
' Enable watch dog timer
' Enable power up timer
' Disable MCLR pin
' Enable brown out detect

'
' Hardware configuration
' ======================
TRISIO = %001111 ' GPIO<5:4> as output
' GPIO<3:0> as input
'
CMCON = 7 ' disable analog comparator
ADCON0 = %10000001 ' Right justified results & enable A/D
ANSEL = %00110011 ' Clock source=FRC
' AN<1:0> : analog other to digital

DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds


'
' Hardware connection
' ===================
LED0 var GPIO.4
LED1 var GPIO.5

'
' Variables definition
' ===================
ADChannel0 var word
ADChannel1 var word

'
' Hardware/Software initialisation
' ================================
GPIO = 0 ' clear all output
Pause 50 ' internal oscillator settle time

'
' Main Program
' ============
Start:
adcin 0,adchannel0 ' Read AN0
adcin 1,adchannel1 ' Read AN1
'
' A/D results duty
' ================
if adchannel0 < adchannel1 then
High Led0
Low Led1
write 0, adchannel0.highbyte ' Store result in internal EEPROM
write 1, adchannel0.lowbyte

else
High Led1
low Led0
write 0, adchannel1.highbyte ' Store result in internal EEPROM
write 1, adchannel1.lowbyte

endif
pause 500
goto start

Have fun!

mister_e
- 20th May 2006, 21:30
Why do you need to use sleep? If it's in case you want to sleep when results change.. you may use the internal comparator interrupt... it should work... it should.

marad73
- 21st May 2006, 00:55
It is sooooo.... much easier when someone who has already done something shows the "new kid on the block" how to!
PS Now the horse found the starting gate and even knows which way to run at the bell!!!!
Thanks again
Ron

marad73
- 21st May 2006, 01:09
Steve, I need sleep to really conserve power. This thing gets its power from photovoltaics in daylight. It stores Vcc in large electrolytic caps!
Also, it only has to operate for about 20 seconds every 15 minutes or so. (wake by WDT) I also need to store data in the eeprom - but nothing in the registers can reset when it goes to sleep. Using sleepuncal to do that. Don't give a flip about the actual sleep time tolerance. I Think it will work, but not sure yet!!
Thanks
Ron

Ron Marcus
- 21st May 2006, 14:29
Be careful using the internal oscillator and the MCLR pin as an input. We just discussed this on another thread. You will not be able to reprogram it if you do.

Melanie
- 21st May 2006, 15:17
> Be careful using the internal oscillator and the MCLR pin as an input. We just discussed this on another thread. You will not be able to reprogram it if you do.

There is NO PROBLEM with this combination - it's used all the time - in fact it's my preferred choice for this PIC. There is no correlation internally within the PIC between your OSCILLATOR choice and your MCLR choice. Simply drop it into your programmer and away you go with whatever settings you desire.

mister_e
- 21st May 2006, 19:18
And now... it also depend of how good your device programmer is. An crapy Programming sequence timing and signal will ruine your life.

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=878&stc=1&d=1148149000">

As many here use free solution, there's many chance that it do some bugs. But i do understand why some choose these solution as PIC programming is not their main job or they simply can't afford the price of a good programmer.

I heard that some have fix some bugs when using a driver as in the ICSP guide of MicroChip... i can't say... http://ww1.microchip.com/downloads/en/DeviceDoc/30277d.pdf

marad73
- 22nd May 2006, 23:39
Steve, thanks for all your help - really appreciated.
One more thing I've got to do is:
When the reset (mclr) is pulled low, (pushbutton) this thing goes into a reset routine, which I can write with no problem. This routine must reset (or stop) the wdt cycle, turn on a couple of outputs in sequence and run the ADs each time until they are equal. Then, it has to engage the wdt again and go to sleep for 2 minutes or so. (outputs actually running motors until ADs are equal)
Without pushing mclr putton, it needs to then wake up from sleep every 2 minutes, run the ADs, then turn on the outputs, recheck ADs until equal, then go to sleep again for another 2 minutes.
These two functions are baffling to me because I find no info about how to use the wdt or mclr to reset as above, in the manual at all. If you can help with some sample code to do this, or tell me where to find it, that will be REALLY appreciated.
Thanks all!
Ron
PS This thing, times 2, is for an azimuth drive for my telescope-in case you are curious! As mentioned before, it operates most of the night, totally off of sunlight it gets during the day.
PPS I understand seeing stars better than PIC code!!!

mister_e
- 23rd May 2006, 13:53
Section 9.6 watch dog timer, have to be your friend now. Set the right prescaller in OPTION register and use the TO bit of STATUS.

This should be something good enough to start.