help with code changes for attiny10

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello to all,
sorry for my bad english, as the title suggests, I need someone to modify the code for an attiny10, I can't do it, the code is used to drive a led driver made with QX5241, I have attached the code that I found but it doesn't work as I want and I don't know how to fix it, I need these features,
three-level ignition,
off,
1 - 100%
2 - 60%
3 - 30%
off

the driver can be powered indifferently with 2s or 3s battery packs, if possible I would like there was a way that the software identifies which battery pack is there at that moment and when it reaches a limit volt value of 6.5v x 2s and 9 75v x 3s lower the light to 50% to save battery

the software must also have the debouce to avoid accidental switching on

and must also have sleep when it is turned off to save batteries.

I hope someone does this to me
Thank you

 

//v0 5/1/2014 by Everett
	//initial version
	//simple flashlight 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

#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{
	max=0,
	med=1,
	low=2,
	off=3,
};
#define max_mode 3
#define default_mode 0

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

char pressed;
char new_press;
char switch_count;
unsigned char isr_prescale;

void configure(void);
unsigned char read_voltage(void);
void initialize_mode(void);
void debounce(void);
void shutdown(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;
		debounce();
		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

	mode=default_mode;
	initialize_mode();
	pressed=0; new_press=0; switch_count=10;
	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()<100){	//set threshold for external voltage here
					mode=med;
					initialize_mode();
				}
			}
		}

		if(new_press){
			new_press=0;
			mode++;
			if(mode>max_mode) mode=0;
			initialize_mode();
		}

		if(mode==off)
		{
			shutdown();	//shutdown will lock up here until a press wakes the device
		}

	}
}

void shutdown(void)
{
	TIMSK0=0;	//stop timer interrupt
	pwm=0;	//zero output
	TCCR0A=0; //turn off pwm
	#ifdef pwm_invert
	PORTB|=0b00000010;
	#else
	PORTB=0;		//ensure pin is low
	#endif
	ADCSRA=0;	//adc off
	PRR=3;	//power reduce on adc and timer0

	while(1)	//make this a loop so we stay here until sure the switch went down
	{
		do{
			debounce();
			_delay_ms((char)1);
		}while(pressed);	//ensure switch is up

		EICRA=0;	//interrupt on low level of int0
		EIMSK=1;
		SMCR=0b00000101;	//enable sleep
		sleep_cpu();
		EIMSK=0;
		SMCR=0b00000100;	//disable sleep	

		pressed=0; switch_count=10;
		for(char i=0; i<40; i++){	//watch for up to 40ms for a solid press
			debounce();
			_delay_ms((char)1);
			if(pressed) break;	//if pressed break out of for loop
		}
		if(pressed) break;	//if pressed break out of sleep loop
	}

	configure();	//set up hardware for operation

}

void debounce(void)
{
	static char port_copy=0xff;
	#define switch_mask 0b00000001	//this selects PB0 as the switch

	if((PINB&switch_mask)==port_copy)	//if the current state matches previous state
	{
		if(--switch_count==0)	//count down samples. if 10 consecutive samples matched
		{
			switch_count=10;	//reset sample counter
			if(PINB&switch_mask) pressed=0;	//if the state is high, switch is up
			else		//else switch is down. check for new press
			{
				if(pressed==0) new_press=1;	//if last state of pressed was 0, this is a new press
				pressed=1;	//switch is now down
			}
		}
	}
	else	//state doesn't match,
	{
		switch_count=10;	//reset sample counter
		port_copy=(PINB&switch_mask);	//get new sample
	}
}

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;
		case off:
		pwm=0;
		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
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't you already have a thread on this exact thing?:

https://www.avrfreaks.net/forum/attiny10-not-scheduled

 

Lidas wrote:
I need someone to modify the code for an attiny10, I can't do it
The way this forum works is that the community helps those who help themselves.  You need to show what work you've done, explain the problems you are having, what you've done to try to solve those problems, and how those efforts have failed.  The community doesn't generally offer complete solutions.  If you want that, post in the 'Marketplace' forum and offer something (money, beer, etc.) in return for the help you are seeking.

 

Simply saying "Do this for me pleeeeease!" won't get you very far.  Creating multiple threads on the same subject will not serve you.

 

So, explain what you've done to try to achieve your goals, and what the results of those efforts were, and you might get some traction here.

 

Might be a good idea to have a look at these:

https://www.avrfreaks.net/forum/how-ask-questions-smart-way

http://www.catb.org/~esr/faqs/smart-questions.html

 

Best of luck!

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I can't do it

Then why are you working on this?  Maybe then, work on something else.  What can you do?  What have you done?  Saying you can't at step one is a sure path to defeat.  Get up & try something. 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:
Don't you already have a thread on this exact thing?

Well, that one was about blowing a hex file into a chip - so that is a different question.

 

@Lidas: If that question is resolved, then you need to mark the solution - so that people (and the forum) know it's finished.

 

See Tip #5 for how to do that.

 

And you could post a link to this thread with your follow-on question.

 

 

The community doesn't generally offer complete solutions.  If you want that, post in the 'Marketplace' forum and offer something (money, beer, etc.) in return for the help you are seeking.

+1

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is not true it is not the same thing, the other post I asked how to program and in the end I did it, so for me it was over, I also asked there but it seemed to make me confused, then I opened this hoping to do the right thing , I apologize if I was wrong but I prefer here if you agree.

For those who tell me to do what I know how to do, I am doing what I know how to do, I learned to insert a program but I cannot develop a program, that's why I ask for your help I tried it but I couldn't

 

I would like to understand how to decrease the powers with the pwm on that code what needs to be changed, you can tell me

Thank you

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Well, that one was about blowing a hex file into a chip - so that is a different question.

From that thread:

Lidas wrote:

I managed to program attiny10 with the code I put on my first post, but I need your help,
this code is for a flashlight and ATTiny10. This implements a simple 3-level plus off light using a momentary switch and includes voltage monitoring, which I don't understand because I'm not expert,
how do I vary the minimum voltage monitoring,
how do I put a delay on the button presses to remove accidental starts,
how do I if possible have a pwm at 40kHz

can you help me thanks

Lidas wrote:
hi, no programming expert to help modify this firmware, please
thank you

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lidas wrote:
I cannot develop a program

 

Then you need to learn how to do that.

 

Begin at the beginning - don't leap straight into something over your head.

 

Here are some 'C' learning & reference materials for you:

 

http://blog.antronics.co.uk/2011...

 

I (and others) would strongly suggest that you do not attempt to learn 'C' on a small microcontroller.

 

Instead, learn the language and the basics of programming on a PC (or similar) - it will be a far more comfortable experience!

 

Then - once you have a good grasp of the language and the basics of programming - move on to applying that to an embedded microcontroller.

 

For microcontroller Getting Started hints, see Tip #6.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for marking the solution in the other thread

 

Lidas wrote:
then I opened this hoping to do the right thing

You still need to go back and give a link to this thread - so people don't waste time in the old one because you didn't say where the discussion has moved to!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lidas wrote:
then I opened this hoping to do the right thing

You still need to go back and give a link to this thread - so people don't waste time in the old one because you didn't say where the discussion has moved to!

 

 

I hope I did everything sorry again

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lidas wrote:
I hope someone does this to me

AVRFreaks are professional programmers, we write code for a living, but we lend help to others that want to learn for free.

So if you want someone to do the programming for you, you need to post your request in the marketplace forum and say how much your willing to pay!

 

If you want to learn how to program, then you need to tell us your experience level and what your interested in learning.

Arduino is a great place to start to learn programming, the hardware is cheap, and needs no special programming hardware, just a usb cable, or a USB serial ttl adapter.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps Lidas has not yet grasped the relationship between timer register settings, PWM duty cycle, and motor speed? Maybe this will help. Or, maybe not!

 

To change the speed of a motor that is driven by a PWM signal, you need to change the PWM "Duty Cycle". This is the fraction of time that the PWM is "on" compared to the complete period of the PWM signal. Typically, increasing duty cycle increases speed. But, exactly what is required depends on how the motor is driven (e.g. "bridge driver" vs one transistor open collector/drain, for example).

 

The next point is how to change the duty cycle? You do this by changing a compare (OCRx) register, where "x" is the timer number (e.g. OCR0 for TIMER0, and so forth). Sometimes, there is more than one compare register. You need to change the one that controls your specific motor output. These are typically named like OCR0A, OCR0B, and such. The compare register value can be increased up to the maximum count for the timer, or decreased down to zero. Often times, the extreme cases (maximum count and zero) do not always give the results that are precisely what you want, so you might avoid those, if you can.

 

Hope this helps with some fundamentals.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Mon. Mar 23, 2020 - 07:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

Perhaps Lidas has not yet grasped the relationship between timer register settings, PWM duty cycle, and motor speed? Maybe this will help. Or, maybe not!

 

To change the speed of a motor that is driven by a PWM signal, you need to change the PWM "Duty Cycle". This is the fraction of time that the PWM is "on" compared to the complete period of the PWM signal. Typically, increasing duty cycle increases speed. But, exactly what is required depends on how the motor is driven (e.g. "bridge driver" vs one transistor open collector/drain, for example).

 

The next point is how to change the duty cycle? You do this by changing a compare (OCRx) register, where "x" is the timer number (e.g. OCR0 for TIMER0, and so forth). Sometimes, there is more than one compare register. You need to change the one that controls your specific motor output. These are typically named like OCR0A, OCR0B, and such. The compare register value can be increased up to the maximum count for the timer, or decreased down to zero. Often times, the extreme cases (maximum count and zero) do not always give the results that are precisely what you want, so you might avoid those, if you can.

 

Hope this helps with some fundamentals.

 

Jim

 

my circuit is used to pilot in pwm using a button the ignition of a led torch (led driver QX5241), if i understood when pwm = 255 vul say maximum, what i can't and i don't understand is how to lower it because if it varies these numbers do not change the light intensity of the led, what you told me to see you do not find on my code,
when i give the 5v on the driver the driver is at max power
when i give 0v it's off
in between there are the various powers, what do I have to change to get this
thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How is the LED driver connected to the Tiny10? Specifically, what pin of the Tiny10 is used to control the DIM pin of the driver?

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Tue. Mar 24, 2020 - 04:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I put you the circuit diagram

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lidas wrote:
I put you the circuit diagram

Pin 4 of U2 does not seem to be connected to a power source!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If correct, that is a significant error. Needs to be connected to +5V

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no it is not so pin 4 is a 5v output that must be connected to pin 2 if the pwm pilot is not used

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm. you are correct. That is not how IC pins are normally marked. Perhaps, there is a voltage regulator inside?

 

Do you have a voltmeter? You can use it to monitor the PWM signal. The voltmeter reading should change as you change the  OCR value.

 

I do not find where OCR0A is ever set or changed.

 

Jim

 

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


ka7ehk wrote:
Perhaps, there is a voltage regulator inside?

Indeed there is:

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I measured with a multimeter and these are the values that the PWM gives me with each press of the button
off = 0.04
1 = 4.29
2 = 4.74
3 = 4.70
off = 0.04

I think it should have different values

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, that is not correct.

 

Again, I do not see OCR0A being changed anywhere in your code. Somewhere, you need to do

 

OCR0A = pwm;

HOWEVER, TIMER0 is a 16 bit timer. Unless you have set TOP to be only 256, your values  of pwm (largest is 255 while the counter counts up to 65535 = 0xFFFF) will only give a very dim LED. I would change initialize_mode() to be:

 

void initialize_mode(void)
{
	switch(mode){	//initialize current mode
		default:
		case max:
		    pwm=0xFFFF; //max duty cycle
		    break;
		case med:
		    pwm=0x4000; //1/4 duty cycle
	            break;
		case low:
		    pwm=1;      //very low duty cycle
		    break;
		case off:
		    pwm=0;
		    break;
	}
}

And I would add, to the end of ISR(TIM0_OVF_vect):

OCR0A = pwm;

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Tue. Mar 24, 2020 - 06:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know if I did it right, maybe explain it better, but it gives me two warnings

Warning        large integer implicitly truncated to unsigned type [-Woverflow]    Attiny10-Eswitch    C:\Users\Lidiano\Documents\Atmel Studio\7.0\Attiny10-Eswitch\Attiny10-Eswitch\Attiny10-Eswitch.c    167
Warning        large integer implicitly truncated to unsigned type [-Woverflow]    Attiny10-Eswitch    C:\Users\Lidiano\Documents\Atmel Studio\7.0\Attiny10-Eswitch\Attiny10-Eswitch\Attiny10-Eswitch.c    164

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, missed a small point.

 

You did #define pwm OCR0BL

 

I missed that. So, you ARE changing an OCR0 register (OCR0B, to be precise). My bad! And, you are using OCR0B output pin. That is correct.

 

BUT, there is a problem with what you are doing. OCR0BL is the low 8 bits of the 16-bit OCR0B register. Unless you are doing something with the  maximum counter value (called TOP in the mode table), TIMER0 is a 16 bit counter. That means that it counts up from 0x0000 to 0xFFFF, then resets to 0x0000 and starts over. You are only using the low half of the compare register so the  on time can only  go from 0x0000 to 0x00FF (or, the inverse, if you invert the output).

 

The easy solution is to simply do

 

#define pwm OCR0B

That will solve your truncation error problem because OCR0B is a 16-bit register. And, you will get a full brightness range.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nothing always changes as before

 

To understand if I'm wrong, you could tell me the sequence I do to program the attiny10 is right,
- start AtmelStudio,
- I open the project,
I go to Instrument - device programming - select AVRISP mkll - Apply - Read then go to Memories - Program the programmer inserts the program and gives me this result
"Deleting device ... OK
Flash programming ... OK
Check Flash ... OK "

it's all right, because I don't see results

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you "build" the project?

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes yes I built the project, and then I program it

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then, please step back for a bit.

 

You are using a programming tool that does not do debugging. You have no way to tell whether the program is running correctly other than brightness changes in an LED. Yet, you have pushbuttons with complex debouncing and other details. The odds that you have gotten all this "right" are very low. 

 

So, you need to figure out how to tell what is going on without using the normal development tools. That is not very easy.

 

As a very first step, I suggest that you make sure that PWM works. So, do the program with a fixed value for OCR0B and prevent changes by not calling initialize_mode(). Set one value (say 0xEFFF for slightly less than maximum brightness) and burn the program. Note how bright the LED is and measure the average PWM with your voltmeter. Then, change the value quite a bit (say 0x1000) and reburn. Does the LED brightness change and the voltmeter reading change? If there is no change, then you need to work on PWM. If it does change, then you need to work on controlling the duty cycle. 

 

The next step follows from the results of the first.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!