Go Down

Topic: Attiny instead of PIC12F (Read 634 times) previous topic - next topic

kmtdev

Jul 12, 2017, 11:50 pm Last Edit: Jul 13, 2017, 12:02 am by kmtdev
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.

spycatcher2k

Sure, Very similar code as well.
-=-
http://www.uk-pcb.co.uk - UK Based PCB Fab & Assembly Company
Design & Assembly work undertaken - Arduino repairs & testing - PM for details. Arduino & C# PC programming.

kmtdev

Thank you for your fast reply! :)
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.

Jiggy-Ninja

#3
Jul 13, 2017, 01:45 pm Last Edit: Jul 13, 2017, 01:46 pm by Jiggy-Ninja
It's actually very easy.
Code: [Select]
CMCON0 = 7 ;
Sets the comparator input pins to digital GPIO operation. Irrelevant for an ATtiny, the pins are GPIO by default.
Code: [Select]
ANSEL = 0 ;
All pins set to digital operation instead of analog input. Also irrelevant to ATtiny, since pins are digital by default.
Code: [Select]
TRISIO = 0 ;
Sets all pins to output. TRISIO is roughly equivalent to the DDRx register in an AVR.
Code: [Select]
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:
Code: [Select]
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.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

kmtdev

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.

Jiggy-Ninja

#5
Jul 13, 2017, 05:47 pm Last Edit: Jul 13, 2017, 05:47 pm by Jiggy-Ninja
Pretty much. The sketch is dead simple too.
Code: [Select]
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.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

kmtdev

#6
Jul 14, 2017, 10:04 am Last Edit: Jul 14, 2017, 10:25 am by kmtdev
Quote
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 http://www.engbedded.com/fusecalc/
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... :D




Coding Badly

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.



kmtdev

Quote
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?)

Code: [Select]
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  :) need to find a crystal somewhere!

Jiggy-Ninja

Use an Arduino to tone a frequency to the ATtiny85's XTAL1 pin, then try to program it.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

kmtdev

Quote
Use an Arduino to tone a frequency to the ATtiny85's XTAL1 pin, then try to program it.
Not working :(

With pin 8 on XTAL1 pin :

Code: [Select]

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

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.
On 8-bit (i.e. Uno) use "byte" instead of "int" if possible - it is faster and saves resources!

kmtdev

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.

Jiggy-Ninja

Not working :(

With pin 8 on XTAL1 pin :

Code: [Select]

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.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

kmtdev

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 ...

Go Up