Attiny instead of PIC12F

Hi everybody,

I would like to make this little thing : Picobat
I never used PIC stuff, and it seems bit difficult than Aduino & AVR stuff.
I already have an USB ISP Programmer...

Can I subsitute the PIC12F683 by a Attiny45 (or Attiny85) ?

Thank you,
k.

(deleted)

Thank you for your fast reply! :slight_smile:
So no difference for this application.
The PIC code is so weird for me, but after some tests and maybe some burned chips, I think I can do something!
k.

It's actually very easy.

CMCON0 = 7 ;

Sets the comparator input pins to digital GPIO operation. Irrelevant for an ATtiny, the pins are GPIO by default.

ANSEL = 0 ;

All pins set to digital operation instead of analog input. Also irrelevant to ATtiny, since pins are digital by default.

TRISIO = 0 ;

Sets all pins to output. TRISIO is roughly equivalent to the DDRx register in an AVR.

GPIO = 0b01 ;

Initializes bit 0 as HIGH, all others as LOW. GPIO is roughly equivalent to the PORTx register in AVRs.

So ends the code you would put into setup(). Now loop(), where there is only one statement:

 GPIO ^= 0b11 ;

Perform a bitwise XOR that inverts only bits 0 and 1 of the GPIO register, which inverts their pin values.

The piezo speaker is connected between the two GPIO pins corresponding to bits 0 and 1 of the register (physical pins 7 and 6). This is called a bridge tied load arrangement.

Thank you again for these tips.
I think I'll use my USBasp and my Arduino IDE to inject the code (like describe here).
So, I can use the classic Arduino syntax, no ?
k.

Pretty much. The sketch is dead simple too.

void setup()
{
	DDRB = 0b11;	// DDR logic is opposite of the PIC TRISx registers
					// 1 = OUTPUT
					// 0 = INPUT
				
	PORTB = 0b01;	// PB0 initialized HIGH
					// PB1 inilialized LOW
}

void loop()
{
	PORTB ^= 0b11; // invert PB0 and PB1
}

All you need to do then is fiddle around with the clock fuse setting, and maybe change the clock prescaler to adjust how fast the bits are toggled.

All you need to do then is fiddle around with the clock fuse setting, and maybe change the clock prescaler to adjust how fast the bits are toggled.

Yes, the attiny85 have an internal clock, and I need to bypass it to use the ultrasonic sensor as an external clock.
I can use avrdude to modified the "lfuse" setting with AVR® Fuse Calculator – The Engbedded Blog
But, I need to do this AFTER my ATtiny is programmed, because without a clock, it may be difficult to connect the device, right?

Thank you very much, this's pretty complex, especially when you manipulate only Arduino classic stuff... :smiley:

kmtdev:
But, I need to do this AFTER my ATtiny is programmed, because without a clock, it may be difficult to connect the device, right?

I assume by "connect the device" you mean reprogram the ATtiny...

Any reasonable clock signal on the XTAL1 pin is enough to reprogram the target. There are versions of the ArduinoISP sketch that will configure an Uno to output such a clock signal.

I assume by "connect the device" you mean reprogram the ATtiny...

Yes, but I haven't clock signal, just my piezo...

This's my setup for low-frequency external crystal (because the piezo gives around 20 khz - 200 khz... (I'm right?)

avrdude -c usbasp -p attiny85 -P /dev/ttyS4 -b 19200 -U lfuse:w:0xC6:m

PS : Now my ATtiny doesn't want to connect to avrdude :slight_smile: need to find a crystal somewhere!

Use an Arduino to tone a frequency to the ATtiny85's XTAL1 pin, then try to program it.

Use an Arduino to tone a frequency to the ATtiny85’s XTAL1 pin, then try to program it.

Not working :frowning:

With pin 8 on XTAL1 pin :

int piezoPin = 8;
void setup() {
}
void loop() {
  tone(piezoPin, 1000);
}

Note it is interesting hack but it is not "correct". When suplying clock to a MCU you should ensure some characteristics are met. Mostly maximum tick to tick difference and minimal time for high and low time. The output of piezzo receiver should be slow enough bouth is no problem but who knows if no glitch can occur? The way I would use is to run from internal RC oscillator quickly and use internal counter clocked from external source (the receiver) to divide the frequency as you want.
Using hardware counter without any MCU is maybe the most elegant solution.

Smajdalf:
Note it is interesting hack but it is not “correct”. When suplying clock to a MCU you should ensure some characteristics are met. Mostly maximum tick to tick difference and minimal time for high and low time. The output of piezzo receiver should be slow enough bouth is no problem but who knows if no glitch can occur? The way I would use is to run from internal RC oscillator quickly and use internal counter clocked from external source (the receiver) to divide the frequency as you want.
Using hardware counter without any MCU is maybe the most elegant solution.

Thank you for your advice, I’m on this way too (many schematics on the web). But I give a chance to this little ‘hack’, if it works, it can be a very cheap solution to localize bats in the dark.

kmtdev:
Not working :frowning:

With pin 8 on XTAL1 pin :

int piezoPin = 8;

void setup() {
}
void loop() {
  tone(piezoPin, 1000);
}

Use a higher frequency than that. The clock frequency has to be a few times higher than the SPI frequency the programmer uses.

Jiggy-Ninja:
Use a higher frequency than that. The clock frequency has to be a few times higher than the SPI frequency the programmer uses.

I've tried a biig range of values, 10 000 to 10 000 000 ...

You connected GND between the Arduino and ATtiny?

Do a tone with a frequency of something like 100000, then add this option to the command line: -B 1000 (must be capital B)

Also, paste the actual error you're getting here, don't just say it isn't working.

You connected GND between the Arduino and ATtiny?

Yes, GRD to GRD and pin 8 (tone) to Pin 2 on ATtiny.

This is my error message :

$ avrdude -c usbasp -p attiny85 -P /dev/ttyS4 -b 19200 -B 1000 -U lfuse:r:-:i -v 

avrdude: Version 6.3
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/home/remy/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyS4
         Using Programmer              : usbasp
         Overriding Baud Rate          : 19200
         Setting bit clk period        : 1000.0
         AVR Part                      : ATtiny85
         Chip Erase delay              : 4500 us
         PAGEL                         : P00
         BS2                           : P00
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65     6     4    0 no        512    4      0  4000  4500 0xff 0xff
           flash         65     6    32    0 yes      8192   64    128  4500  4500 0xff 0xff
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

         Programmer Type : usbasp
         Description     : USBasp, http://www.fischl.de/usbasp/

avrdude: set SCK frequency to 1000 Hz
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: error: program enable: target doesn't answer. 1 
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.

Thank you!

PS : The 12F PIC version of the projet works very well on my breadboard.

Apparently the USBASP has a jumper to select between slow and fast clock speeds, see if changing that will help. It looks like the clock speed can't be changed from the command line, so just forget the -B 1000 option I mentioned.

I've never used a USBASP though. How about loading the ArduinoISP sketch onto the Uno and using that? Move the tone to pin 6 so it doesn't interfere with the Error light the sketch uses, and put the tone statement in the setup() function. ArduinoISP has a #define SPI_CLOCK value that lets you specify the SPI clock speed. I'd try something super low like 10000. Just change these programmer options (-cstk500v1 -b19200) and the serial port number and hopefully you'll be good to go.

Jiggy-Ninja:
Apparently the USBASP has a jumper to select between slow and fast clock speeds, see if changing that will help. It looks like the clock speed can’t be changed from the command line, so just forget the -B 1000 option I mentioned.

I’ve never used a USBASP though. How about loading the ArduinoISP sketch onto the Uno and using that? Move the tone to pin 6 so it doesn’t interfere with the Error light the sketch uses, and put the tone statement in the setup() function. ArduinoISP has a #define SPI_CLOCK value that lets you specify the SPI clock speed. I’d try something super low like 10000. Just change these programmer options (-cstk500v1 -b19200) and the serial port number and hopefully you’ll be good to go.

Same error with the jumper on JP3 (slow speed clock), and with this new sketch too.

int piezoPin = 6;
void setup() {
    tone(piezoPin, 100000);
}
void loop() {
}

I have found a little crystal (16 Mhz) but same error (crystal between 2 and 3 with capa to ground…)

Last chance with ArduinoISP… Keep in touch.
Thanks again.