<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" 
  xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:atom="http://www.w3.org/2005/Atom" 
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
  xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>avr on Ben&#39;s ideas and projects</title>
    <link>https://ben.the-collective.net/tags/avr/</link>
    <description>Recent content in avr on Ben&#39;s ideas and projects</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>locutus@the-collective.net (Ben Mason)</managingEditor>
    <webMaster>locutus@the-collective.net (Ben Mason)</webMaster>
    <copyright>©2022, All Rights Reserved</copyright>
    <lastBuildDate>Sun, 08 Jul 2018 12:07:25 -0400</lastBuildDate>
    <sy:updatePeriod>daily</sy:updatePeriod>
    
        <atom:link href="https://ben.the-collective.net/tags/avr/index.xml" rel="self" type="application/rss+xml" />
    

      
      <item>
        <title>AVR Development</title>
        <link>https://ben.the-collective.net/my-notes/avr-development/</link>
        <pubDate>Sun, 08 Jul 2018 12:07:25 -0400</pubDate>
        <author>locutus@the-collective.net (Ben Mason)</author>
        <atom:modified>Sun, 08 Jul 2018 12:07:25 -0400</atom:modified>
        <guid>https://ben.the-collective.net/my-notes/avr-development/</guid>
        <description>AVR Notes Simulation GitHub – buserror/simavr: simavr is a lean, mean and hackable AVR simulator for linux &amp;amp; OSX
arduino /simduino.elf -d ~/Projects/Rhme-2016/challenges/binaries/casino/casino.hex
GCC flags avr-gcc -c -mmcu=atmega328p -I. -gstabs -D__AVR_ATmega328P__ -Os -Wall -Wstrict-prototypes -std=gnu99 main.c -o main.o
avr-objcopy -O ihex -R .eeprom max7219.elf max7219.hex avr-objcopy -j .eeprom --set-section-flags=.eeprom=&amp;#34;alloc,load&amp;#34; \ --change-section-lma .eeprom=0 -O ihex max7219.elf max7219.eep avr-objcopy: --change-section-lma .eeprom=0x0000000000000000 never used avrdude -p atmega328p -c usbtiny -v -v -U flash:w:max7219.hex Ports &amp;lt;avr/io.</description>
        <content:encoded>&lt;h1 id=&#34;avr-notes&#34;&gt;AVR Notes&lt;/h1&gt;
&lt;h2 id=&#34;simulation&#34;&gt;Simulation&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/buserror/simavr&#34;&gt;GitHub – buserror/simavr: simavr is a lean, mean and hackable AVR simulator for linux &amp;amp; OSX&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;arduino&#34;&gt;arduino&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;/simduino.elf -d ~/Projects/Rhme-2016/challenges/binaries/casino/casino.hex&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;gcc-flags&#34;&gt;GCC flags&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;avr-gcc -c -mmcu=atmega328p -I. -gstabs -D__AVR_ATmega328P__  -Os -Wall -Wstrict-prototypes -std=gnu99  main.c -o main.o&lt;/code&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;avr-objcopy -O ihex -R .eeprom max7219.elf max7219.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom=&amp;#34;alloc,load&amp;#34; \
	--change-section-lma .eeprom=0 -O ihex max7219.elf max7219.eep
avr-objcopy: --change-section-lma .eeprom=0x0000000000000000 never used
avrdude -p atmega328p  -c usbtiny  -v -v  -U flash:w:max7219.hex
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;ports&#34;&gt;Ports&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;avr/io.h&amp;gt;&lt;/code&gt; – Aliases for IO register addresses&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GPIO connections are set in groups of ports&lt;/li&gt;
&lt;li&gt;Default to Input&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;DDRx – data direction register (in or out)&lt;/p&gt;
&lt;p&gt;PORTx – Port output register&lt;/p&gt;
&lt;p&gt;PINx – Port in put register&lt;/p&gt;
&lt;p&gt;8bit in width&lt;/p&gt;
&lt;h3 id=&#34;bit-operations&#34;&gt;Bit operations&lt;/h3&gt;
&lt;p&gt;BV is a macro for the bit shift operator&lt;/p&gt;
&lt;p&gt;&lt;code&gt;_BV(x) = (1&amp;lt;&amp;lt;x)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Set a Bit (or)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BYTE |= (1 &amp;lt;&amp;lt; i)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Clear a bit (not)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BYTE &amp;amp;= ~(1 &amp;lt;&amp;lt; i)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Toggle a bit (xor)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BYTE ^= (1 &amp;lt;&amp;lt; i)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Set first and forth bits&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BTYE = (1 &amp;lt;&amp;lt; 2) | (1&amp;lt;&amp;lt; 4)&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&#34;checking-bits-on-ports&#34;&gt;Checking bits on ports&lt;/h3&gt;
&lt;p&gt;The following in avr/sfr_defs.h imported by in io.h&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;/* avr/sfr_defs.h - macros for accessing AVR special function registers */

 bit_is_set(sfr, bit)
 bit_is_clear(sfr, bit)
 loop_until_bit_is_set(sfr, bit)
 loop_until_bit_is_clear(sfr, bit)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;interrupts&#34;&gt;Interrupts&lt;/h2&gt;
&lt;p&gt;Global variables should be declared as volatile ref: &lt;a href=&#34;https://ben.the-collective.net/my-notes/c-notes/&#34;&gt;C Notes&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;isr&#34;&gt;ISR()&lt;/h3&gt;
&lt;p&gt;Interrupt service routines are executed when a interrupt is triggered. You can not pass variables into an ISR, you will need to reference a global variable.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ISR(PCINT2_vect){
  ....
}
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;external-interrupts&#34;&gt;External Interrupts&lt;/h3&gt;
&lt;h4 id=&#34;int0-amp-int1&#34;&gt;INT0 &amp;amp; INT1&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Flexible can trigger on rising, falling, change, or continuously on low voltage&lt;/li&gt;
&lt;li&gt;only on PD2 and PD3&lt;/li&gt;
&lt;li&gt;high priority&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&#34;pcint--pin-change-interrupt&#34;&gt;PCINT – Pin Change Interrupt&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Detects only change on a pin&lt;/li&gt;
&lt;li&gt;Banked with the Port Groups&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;https://i0.wp.com/ben.the-collective.net/wp-content/uploads/2018/07/Screen-Shot-2018-01-19-at-20.34.13.png?resize=796%2C276&#34; alt=&#34;&#34; /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PCMSKx is used to determine which pins will trigger the interrupt&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ISR(PCINT2_vect){
  ....
}

&amp;lt;span class=&amp;#34;sf_code_syntax_project&amp;#34;&amp;gt;void initPinChangeInterrupt18(&amp;lt;span class=&amp;#34;sf_code_syntax_project&amp;#34;&amp;gt;void){
	/* set pin-change interrupt for D pins */
  PCICR |= (&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; PCIE2);
	/* set mask to look for PCINT18 / PD2 */     
  PCMSK2 |= (&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; PCINT18);
	/* this will also work for the pin mask */  
  &amp;lt;span class=&amp;#34;sf_code_syntax_comment&amp;#34;&amp;gt;// PCMSK2 |= (1 &amp;lt;&amp;lt; PD2);
	/* set (global) interrupt enable bit */   
  sei();                     
}

/* ---------------------------------------------- */

&amp;lt;span class=&amp;#34;sf_code_syntax_project&amp;#34;&amp;gt;void initPinChangeInterrupt(&amp;lt;span class=&amp;#34;sf_code_syntax_project&amp;#34;&amp;gt;void) {
	/* enable Pin-change interrupts 1 (bank C) */
  PCICR |= (&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; PCIE1);
	/* enable specific interrupt for our pin PC1 */    
  PCMSK1 |= (&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; PC1);   
}

ISR(PCINT1_vect) {
	/* count this change */
  chargeCycleCount++;                            
	/* output mode */
  CAP_SENSOR_DDR |= (&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; CAP_SENSOR);
	/* charging delay */                  
  _delay_us(&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1);                                      

	/* set as input */
  CAP_SENSOR_DDR &amp;amp;= ~(&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; CAP_SENSOR);  
	/* clear the pin-change interrupt */                
	PCIFR |= (&amp;lt;span class=&amp;#34;sf_code_syntax_number&amp;#34;&amp;gt;1 &amp;lt;&amp;lt; PCIF1);             
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;sei()&lt;/code&gt; – Globally enable interrupts&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cli()&lt;/code&gt; – Globally disable interrupts&lt;/p&gt;
&lt;p&gt;Interrupts are disabled when inside of a &lt;code&gt;ISR()&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html&#34;&gt;avr-libc: &amp;lt;avr/interrupt.h&amp;gt;: Interrupts&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;timers--counters&#34;&gt;Timers / Counters&lt;/h2&gt;
&lt;p&gt;Counter can be connected to the system clock for and external pin. Value is stored in TCNTn register. This register can be read and written to, a write can be used to reset the register to 0.&lt;/p&gt;
&lt;p&gt;Clock prescaller setting the TCCRnB register using the CSx0, CSx1, CSx2 Clock Select x bits&lt;/p&gt;
&lt;p&gt;&lt;code&gt;TCCR1B |= (1 &amp;lt;&amp;lt; CS11) | (1 &amp;lt;&amp;lt; CS10);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://i0.wp.com/ben.the-collective.net/wp-content/uploads/2018/07/81279C21-26B9-439F-A11F-80B51C8F26A7.png?resize=840%2C218&#34; alt=&#34;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;There are multiple modes for the Waveform&lt;/p&gt;
&lt;p&gt;Normal Mode – counts until max value for the counter and stores value in TCNTn register.&lt;/p&gt;
&lt;p&gt;CTC Mode – TCNTn is compared value to OCRnx register and triggers when they match&lt;/p&gt;
&lt;h2 id=&#34;adc&#34;&gt;ADC&lt;/h2&gt;
&lt;p&gt;PORTC connected to ADC on atmega328p&lt;/p&gt;
&lt;p&gt;To make full use of the ADC you can or must set:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The ADC clock prescaler (default: disabled, which means no conversions)
&lt;ul&gt;
&lt;li&gt;The ADC on the mega chips does not work at full clock rate and must be scaled down to between 50khz and 200 khz&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;https://i0.wp.com/ben.the-collective.net/wp-content/uploads/2018/07/Screen-Shot-2018-01-13-at-15.48.28.png?resize=746%2C914&#34; alt=&#34;&#34; /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The voltage reference that defines the full scale (default: an externally supplied voltage)&lt;/li&gt;
&lt;li&gt;The analog channel to sample from (default: external pin PC0)&lt;/li&gt;
&lt;li&gt;An ADC trigger source (default: free-running if specified)&lt;/li&gt;
&lt;li&gt;Interrupts to call when an ADC conversion is complete (default: none)&lt;/li&gt;
&lt;li&gt;Other miscellaneous options including an 8-bit mode, turning off the digital input circuitry to save power, and more&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are two modes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Free running – Continually runs conversions.&lt;/li&gt;
&lt;li&gt;Single Conversion – executes a single run to create a sample and will only start a next run when ADSC is set HIGH&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When a conversion is complete, the result is written to the ADC Data Registers (ADCL and ADCH), and the ADC Interrupt Flag (ADCSRA.ADIF) is set. In Single Conversion mode, ADCSRA.ADSC is cleared simultaneously. The software may then set ADCSRA.ADSC again, and a new conversion will be initiated on the first rising ADC clock edge.&lt;/p&gt;
&lt;p&gt;ADLAR – Left Adjust, Shift the bit up into the ADCH register. Limit is the top 8bits in resolution.&lt;/p&gt;
&lt;p&gt;Mux Bits&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;+------+------+------+------+------+------+------+------+
|Bit 7 |Bit 6 |Bit 5 |Bit 4 |Bit 3 |Bit 2 |Bit 1 |Bit 0 |
+------+------+------+------+------+------+------+------+
|REFS1 |REFS0 |ADLAR |   -  | MUX3 | MUX2 | MUX1 | MUX0 |
+------+------+------+------+------+------+------+------+
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;span class=&#34;hashtag&#34;&gt;#coding/c &lt;span class=&#34;hashtag&#34;&gt;#microcontroller/avr &lt;span class=&#34;hashtag&#34;&gt;#7-notes&lt;/p&gt;
</content:encoded>
        <dc:creator>Ben Mason</dc:creator>
        
        
        
        
          
            
              <category>avr</category>
            
          
            
              <category>electronics</category>
            
          
            
              <category>notes</category>
            
          
        
        
        
      </item>
      

    
  </channel>
</rss>
