Friday, June 7, 2013

Attiny10, C and Atmel

Atmel AVR line of microprocessors are for me the best out there.

When I heard about the attiny10 series in a easy to use SOT23-6 I got excited back in ??2009.  Well, it's June of 2013 and I believe I'm getting close to adding the attiny10 to my family of tiny series that I would use on a regular basis.

The attiny10 has 32 bytes of RAM and 1K of code space.  This is enough for many simple projects and would be a lower cost solution than the tiny13 or tiny25.

The catch was getting the Studio4 setup correctly to compile C code with a target device set to attiny10.

I was at wits end watching for upgrades to WinAvr and the last release was dated 2010.  That release did not have the attiny10 in the device list.

After some investigation I was pointed to the version over at Atmel called

Atmel AVR Toolchain 3.4.2 for Windows

The default installation location is under program files \ atmel

In order to use the new toolchain with Atmel Studio 4.18 you need to make a few changes in studio.

xxxx

Once you do this there are few other options that


 Under Project you will find Project Options as shown below.











 Clicking the three checkboxes will make the compiler use 8 bit values vs 16 bit values for Unsigned Chars, bitfields and Enums.

If you don't check these the compiler will use 16 bit lengths consuming more code as simple operations as increment or decrement are now twice as many assembly instructions than incrementing or decrementing an 8 bit register.

Also, some form of optimization must be chosen or again the compiler will generate non optimized code and could easily double the resultant binary code needed.

Setting a value for the Frequency will add the -DF_CPU=xxxxUL.

#ifndef F_CPU
#define F_CPU 1000000
#endif
#define TIMER_TICK_FREQ  122
#define TIMER_0_CLOCK (F_CPU / 256 )
#define t122Hz ((TIMER_0_CLOCK / TIMER_TICK_FREQ) )

Here we are setting the background task schedule to run at 122Hz.

Main starts out setting the clock prescalar to equal F_CPU

int main(void)
{
    DDRB |= _BV(GLED_IO) | _BV(RLED_IO);
    asm volatile(
    "ldi r24, 0x80\n\t"
    "out 0x26, r24\n\t"
    "ldi r24, 0x03\n\t"
    "out 0x26, r24\n\t"
    );

the assembly code ensures that timing requirement to set CLKPR is met.  Depending on whether you optimize your code or not this condition may not be met and CLKPR won't be set as you think.  The simulator won't care and all is well except when loaded on the PCB.

F_CPU can be used as part of you calculation to compute the background task timer count value and schedule your background task to a computed value.

Under the Custom Options at the bottom you can see below the changes to use the toolchain and not WinAVR.  You uncheck "Use WinAVR" and then navigate and select the avr-gcc and make file.

NOTE:  Do not select the avr-gcc32.exe like I did.  That's the incorrect gcc and it won't compile.




Where possible use 8 bit variables.

For an interrupt I would recommend using the parameter ISR_NAKED as shown below.

ISR (TIM0_OVF_vect, ISR_NAKED)






You must add reti(); manually at the end of the routine.


I have made a layout to incorporate the tiny10 and the 6 pin connector to use the AVRISP MK2 USB interface to program the part.  Since I purchased the STK600, adaptor and attiny10 parts I've located the STK600 and adaptor.  Who knows where the attiny10 parts are now.  I'll have to hunt those down and etch the PCB.

I found the following information on bicycle light programming attiny10

AVRISP mkIITargetATTINY10
connectorpin namepin number

1 MISOTPIDATA1
2 VCCVCC5
3 SCKTPICLK3
4 MOSI––
5 RESETRESET6
6 GNDGND2

Once I etch the PCB and solder on an attiny10 I will see if I can program it with the AVRISP MKII and if successful or not I will post and update this blog of the testing results.

Let me also note that since I have not done this and had need for a solution the code I'm referring to is working on an attiny13.  So, that may also be a good reference is to debug and use the tiny13 for simulation and hardware debugging and then switch to the tiny10 since you can use a dragon for in-circuit debugging of the attiny13 and not have to fork out $600.00 for a AVROne that is needed to debug the attiny10.



It costs more to develop for the attiny10 such that end costs of a tiny10 is less than most other tiny family.

It costs less to develop for the tiny13 such that end costs are more than if you had used a tiny10.

Did you catch the difference in the above statements?  Go figure!

=======================================
8JUN2013

I etched the test PCB loaded the 6 pin header and the attiny10 IC. 

In order to program the tiny10 using the AVRISP MK2 I had to install SP3 for AVR Studio 4 going from 4.18 build 684 to 4.18 build 716.  Note: You must install 4.18 build 684 and then install SP3.

There are two differences between the tiny13 and the tiny10 that required changes to my source code.

The timer0 is 8 bits (attiny13) and 16 bits (attiny10)

    TCNT0 = (uint8_t)-(t122Hz);  //Re-load the background timer value.

to

    TCNT0 = (uint16_t)-(t122Hz);  //Re-load the background timer value.



Also, I was incorrect about the header file.  The tiny10 ADC is 8 bits while most of the tiny series are 10 bits.

The A/D routine required the removal of the ADCH value since it's not there as I thought in the tiny10.

Next step is the stop using timer0 and use the watchdog timer for the background task timer so that the part can use the power down deep sleep and very low power consumption mode.  I have some projects that need super long run time and maximize the battery usage.

Also, the configuration bit is not a fuse byte and I don't know how to embed this into the source code so that the ELF contains the correct settings and gets program automatically along with the lock bits.

I have yet to figure this out.

No comments: