ATTiny starter code

Yes, it’s that simple to change the order, what you have written above will work perfectly

Thankyou, Sir. You have become my private programming consultant. :bigsmile:

Excuse me, me again.

I tried to reconstruct the Eswitch to the power cycle but failed. The problem seemed to be in function main. The AVRstudio gave me the comment that too many arguments to function ‘delayms’.
I hope that you have time to correct the home work of your student.

//v0 5/1/2014 by Everett
//initial version
//simple flash light controller. mode change on power cycle
//v1 5/3/2014 by Everett
//adapted to momentary switch
//v2 5/6/2014 by Everett
//ported to Attiny10 device
//5/10/2014 by Wally
//try to reconstruct to mode change on power cycle for Attiny10

#define F_CPU 1000000
#define pwm OCR0BL
#define pwm_invert
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>

unsigned char mode;
enum mode{
low=0,
med=1,
max=2,
};
#define max_mode 2
#define default_mode 0

unsigned int key;
unsigned int v_timer;
char v_sample;
#define voltage_rate 100 //milliseconds

unsigned char isr_prescale;

void delayms(void);
void configure(void);
unsigned char read_voltage(void);
void initialize_mode(void);

ISR (TIM0_OVF_vect) //fires at 4kHz{
{ isr_prescale;
if(isr_prescale>=4){ //prescale since timer only has the 4kHz option
isr_prescale=0;
if(++v_timer==voltage_rate){v_timer=0; v_sample=1;}
}
}

ISR (INT0_vect){
//no action, but ISR must be included since it will execute upon waking
}

int main(void)
{
configure(); //set up hardware peripherals
delayms(15); //short delay to avoid power glitches incrementing mode
if(key==12345){ //ram retention trick to detect quick power cycles
mode; //go to next mode
if(mode>max_mode) {mode=0;}
}
else{ //long power loss. default to first mode
mode=default_mode;
key=12345;
}
initialize_mode();
sei(); //turn on interrupts

while(1){

if(v_sample){
v_sample=0;
if(mode==max){ //if battery goes below threshold in max mode, force down to medium mode
if(read_voltage()<196){ //set threshold for external voltage here
mode=med;
initialize_mode();
}
}
}

}
}

void initialize_mode(void)
{
switch(mode){ //initialize current mode
default:
case max:
pwm=255;
break;
case med:
pwm=25;
break;
case low:
pwm=1;
break;
}
}

unsigned char read_voltage(void)
{
ADCSRA|=(1<<ADSC);
while(ADCSRA&(1<<ADSC));
return ADCL;
}

void configure(void)
{
PRR=0;
TIMSK0=0b00000001; //interrupt on t0 overflow
pwm=0;
#ifdef pwm_invert
TCCR0A=0b00110001; //output B inverted
#else
TCCR0A=0b00100001; //output B on, not inverted, 8bit pwm,
#endif
TCCR0B=0b00001001; //no prescale for 3906Hz pwm and interrupt
GTCCR=0;

PORTB=0;
DDRB=0b00000010; //PB1 output
DIDR0=0b00000100; //PB2 analog
PUEB=0b11111001; //pull up switch and reset

ADMUX=2; //PB2
ADCSRA=0b10000011; //

SMCR=0b00000100; //enable power down mode
}

I don’t know why the contents of #include are disappeared and the line setting are so odd to read.

Hi Microa,
The reason you’re getting this error is that you have delayms declared as “void delayms(void)” meaning it doesn’t accept any passed arguments. The next issue is that the function isn’t actually defined in the body of the code, so even if the prototype was correct, nothing would actually run when you called it. Take a look at my original PIC code to see how I declare and define the function delayms().
Now, just to be more confusing, you can avoid doing this at all since delayms is only used once in the power cycle version of the code. Instead of prototyping and defining the function, you could just use the compiler macro _delay_ms(15); and be done with it. Make sure you include <util/delay.h> to make this work.

One more thing, you’ll need to put the “persistent” qualifier in front of the variables “key” and “mode” when you declare them at the top. This directive tells the compiler not to initialize these two variables; without it, the compiler will automatically add instructions at the beginning of the code that will set these variables to zero, which will prevent us from changing modes when the power cycles.

The AVRstudio does not allow me to put the “persistent” before the “unsigned char mode” and “unsigned int key” and also cause undeclared of “key” and “mode” errors.

looks like the AVR compiler makes this a bit more complicated. According to this page I found, the way to do it is:

unsigned int key attribute ((section (“.noinit”)));
unsigned char mode attribute ((section (“.noinit”)));

Hey Everett or Microa, I know this has been a while but I’ve picked it up, I need to find a general purpose AVR code for the tiny10 and get it ready for release, trying to get some more people setup with it…

What are the fuse settings to use while flashing? I’ve got the code ready (I think), just need to flash and start testing!

I made a 8 pin ATtiny/NANJG programming board, would anyone be interested in one for the ATTiny10 (since there isn’t any other viable way to program it)?

wight and RMM both have produced similar
One of the OSHPark guys will definitely step up to the plate for sure

I also believe C_K has successfully used my board to flash pre-installed ATTiny13’s this way too (I bet he will confirm or deny soon :slight_smile: )

I got myself something like this:

End up I change my mind using Attiny13A instead and don’t try this firmware. I thought you should use the default fuse setting 0xFF to flash your chip. The fuse has 3 selectable items:
Disable external reset,
Output external clock,
Watch dog timer always on

We usually don’t programmed RSTDISBL only we require the reset pin as I/O pin. The other two items are dependent on your firmware.

Hi everyone, I am interested in this circuit you have done, but I am not an expert, this software can also be used on attiny85 or not, it is complicated to convert it into Arduino language, thanks