Author Topic: Help with attiny4 coding  (Read 3406 times)

0 Members and 1 Guest are viewing this topic.

Offline minipowerTopic starter

  • Contributor
  • Posts: 21
  • Country: us
Re: Help with attiny4 coding
« Reply #25 on: November 22, 2020, 04:45:11 am »
I totally agree. Reading and understanding are 2 different things. I'm trying to change the settings to see what it does. Trying to put the different code types and mcu's in context with what I'm reading in the datasheet.

I ran across CodeVison AVR. It has CodeWizard AVR. You can choose the mcu and its options and it will set up the code for you, then you would put your program code in the locations.

It is helping me see and understand how the different mcu options code should be written.

sleemanj  I did look at and try to play around with your IDE. I like it. At the bottom of your git readme, it talks about saving to the eeprom, as an option now or the future.  Would that allow me place the mcu power saving setup inside to free up space. Calling for it when needed ?

 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Help with attiny4 coding
« Reply #26 on: November 22, 2020, 05:08:53 am »
Focus on the part of the datasheet that explains what all of the registers do, then when you want more details about a specific register, go look at the section on the peripheral that it controls. The datasheet explains exactly what registers there are and what each bit in these registers controls.
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Help with attiny4 coding
« Reply #27 on: November 22, 2020, 06:52:53 am »
saving to the eeprom, as an option now or the future.  Would that allow me place the mcu power saving setup inside to free up space. Calling for it when needed ?

No.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline minipowerTopic starter

  • Contributor
  • Posts: 21
  • Country: us
Re: Help with attiny4 coding
« Reply #28 on: December 05, 2020, 12:11:21 am »
With your guyz help and being able to look at how the coding is suppose to look like by using CodeVisionAVR. I was able to understand the datasheet more and how to write the settings I wanted.

I have been able to get a program the builds without errors. My goal was to write a small lean program without unneeded bloat. I'm getting pretty close.

Here is what I have so far

Code: [Select]
/*
 * 5-7 256 WDP 2s.c
  * Author : rascal
  */
  #define F_CPU 1000UL
  #include <avr/io.h>
  #include <avr/interrupt.h>
  #include <avr/sleep.h>
  #include <avr/wdt.h>
  #include <util/delay.h>

  EMPTY_INTERRUPT(WDT_vect)  //clearing the interrupt flag
  int main(void)
  {
  cli();                                   // Disable global interrupts before initialization
  CCP=0xD8;                                // Unlock protected register
  CLKMSR=(0<<CLKMS1) | (1<<CLKMS0);        // Change clock source to Low-power 128 kHz oscillator
  CCP=0xd8;
  CLKPSR=(1<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0); // Clock division factor 256
  PRR = (1<<PRTIM0);                      // Turn timer0 off
  DDRB = 0b00001111;                       // Pins 1 - 3 - 5 - 6 outputs
  //PUEB = (1<<PUEB3);                       // Enable Pull up Resistor Pin 6
  ACSR = (1<<ACD);                         // Turn off Analog comparator
  CCP=0xd8;
  // Watch dog timer prescaler = 2.0s
  WDTCSR=(1<<WDIF) | (1<<WDIE) | (0<<WDP3) | (0<<WDE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
  VLMCSR=(0<<VLMF) | (0<<VLMIE) | (0<<VLM2) | (0<<VLM1) | (0<<VLM0);   // Trigger Level: Voltage Level Monitor Disabled

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);     // sleep mode is set here
  sleep_enable();

  wdt_reset();            // Watchdog reset
  sei();                  //initialize global interrupts before beginning loop

  while (1) {
  // Blink PB0 (Pin 1 RED led) 5 times then sleep
 
  PORTB |=(1<<PB0);   // 1 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB0);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms

  PORTB |=(1<<PB0);   // 2 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB0);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms

  PORTB |=(1<<PB0);   // 3 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB0);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 250ms
 
  PORTB |=(1<<PB0);   // 4 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB0);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms

  PORTB |=(1<<PB0);   // 5 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB0);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms
 
  sleep_enable();        // sleep mode to extend the (pause) between blinking time
  sleep_cpu();


  // Blink PB0 (Pin 2 GREEN led) 7 times then sleep
 
  PORTB |=(1<<PB1);   // 1 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms

  PORTB |=(1<<PB1);   // 2 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms
 
  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms

  PORTB |=(1<<PB1);   // 3 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms
 
  PORTB |=(1<<PB1);   // 4 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms
 
  PORTB |=(1<<PB1);   // 5 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms
 
  PORTB |=(1<<PB1);   // 6 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms

  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(150);               // wait for 150ms
 
  PORTB |=(1<<PB1);   // 7 turn the LED on (HIGH is the voltage level
  _delay_ms(250);               // wait for 250ms
 
  PORTB &=~(1<<PB1);    // turn the LED off by making the voltage LOW
  _delay_ms(250);               // wait for 250ms

  sleep_enable();        // 2 sleep modes to extend the (pause) not blinking time
  sleep_cpu();

  sleep_enable();        // 2 sleep modes to extend the (pause) not blinking time
  sleep_cpu();
  }
  // Watchdog Interrupt Service / is executed when watchdog timed out
  ISR(WDT_vect);
 
  }





Question. Since I'm using the WDT and not the main system clock, can I take this out ?

CLKPSR=(1<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0); // Clock division factor 256

To me, it would seem unneeded. And I get error if I don't put a   ;   here
 }
  // Watchdog Interrupt Service / is executed when watchdog timed out
  ISR(WDT_vect);
 
  }

I see others not have the   ;

One thing all this has taught me is that I magic powers.. The hex files generated are from 700 - 990 bytes but only write a little more than half of that to the chip and it works.

Also, I have been able to write to the chip at the highest speed no matter how slow the mcu is clocked at. I guess, since the mcu speed it set in the code, when the mcu is reset and written to, it defaults to the default speed.
« Last Edit: December 05, 2020, 12:18:43 am by minipower »
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Help with attiny4 coding
« Reply #29 on: December 05, 2020, 02:57:51 am »
The hex files contain a bunch of overhead that is used to package the data to be written to the chip, it will always be larger than the actual program. If you set the compiler to output a .bin file that should be exactly the same size as the flash that it will be written to.
 

Offline minipowerTopic starter

  • Contributor
  • Posts: 21
  • Country: us
Re: Help with attiny4 coding
« Reply #30 on: January 10, 2021, 03:26:16 am »
sleemanj   -- Trying to copy and paste the code from atmel studio 7   to  ATTINY10IDE

 I assume this error is a problem with my computers installed java. Before I try. Would uninstall and reinstalling java fix it ¿

Code: [Select]
os.name: windows 7
os:      WIN
srcZip: toolchains/WinToolchain.zip
Stack Trace:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at ATTinyC.lambda$null$11(ATTinyC.java:576)
at java.lang.Thread.run(Unknown Source)
« Last Edit: January 10, 2021, 03:28:10 am by minipower »
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Help with attiny4 coding
« Reply #31 on: January 10, 2021, 08:27:01 am »
sleemanj   -- Trying to copy and paste the code from atmel studio 7   to  ATTINY10IDE


I do not use either
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline minipowerTopic starter

  • Contributor
  • Posts: 21
  • Country: us
Re: Help with attiny4 coding
« Reply #32 on: January 10, 2021, 02:59:29 pm »
Oops .. embarrassing

I mixed up ATTiny10IDE and your core. 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf