Skip to content
Snippets Groups Projects
Commit 722ffbfa authored by Ahmad Fares's avatar Ahmad Fares
Browse files

-m updating C-programming

parent c2bbaf32
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -127,7 +127,7 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
<p>The following code explains the macron method</p>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span>
<span class="co">This code was created by Ahmad Fares on 4th-Dec-2017.</span>
<span class="co">The code is used to switch on an LED when a Button is pressed using macron method</span>
<span class="co">The code is used to switch on an LED when a Button is pressed using Macron</span>
<span class="co">MIT License</span>
<span class="co">*/</span>
......@@ -150,11 +150,11 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
<span class="kw">while</span>(<span class="dv">1</span>) {
<span class="kw">if</span>(testbit(PINB, PB2)){
<span class="kw">if</span>(testbit(PINB, PB2)){ <span class="co">//Read button statues</span>
setbit(PORTA, PA3);
setbit(PORTA, PA3); <span class="co">//Switch off LED</span>
}<span class="kw">else</span>{
clearbit(PORTA, PA3);
clearbit(PORTA, PA3); <span class="co">// Switch on LED</span>
}
}
......@@ -176,11 +176,11 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
<span class="ot">#define clearbit(register, bit) (register) &amp;= ~(1 &lt;&lt; (bit))</span>
<span class="ot">#define testbit(register, bit) (register) &amp; (1 &lt;&lt; (bit))</span>
<span class="ot">#include &lt;avr/interrupt.h&gt;</span>
<span class="ot">#include &lt;avr/io.h&gt;</span>
<span class="ot">#include &lt;avr/interrupt.h&gt; </span><span class="co">//include interrupt library</span>
<span class="ot">#include &lt;avr/io.h&gt; </span><span class="co">//include io library</span>
<span class="co">// Interrup function, PCINT1_vect is the vector for interrupt request </span>
<span class="co">// interrupt function, PCINT1_vect is the vector for interrupt request</span>
ISR(PCINT1_vect)
{
<span class="kw">if</span> ( PINB &amp; (<span class="dv">1</span>&lt;&lt;PB2) ) <span class="co">// test PINA7 if true then</span>
......@@ -204,10 +204,73 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
}
</code></pre></div>
<h2 id="timers">Timers</h2>
<h2 id="count-1-seconds-for-1-mhz-rule-of-thirds">Count 1 seconds for 1 Mhz (Rule of thirds)</h2>
<p>Timers are registers inside the microcontroller, but run independently and can be used to run a specific code after a certain time. In my case, the ATTiny44 has three 8-bit counter registers: <strong>TCNT0</strong>, <strong>TCNT1L</strong> and <strong>TCNT1H</strong>.</p>
<p>Those registers don't actually count time, they count cycles and from these cycles we can count time. There are two operation modes: normal and CTC (clear time on compare match). In the normal mode a specific block of code is executed after the register overflows whereas in the CTC mode which is the most commonly used, a block of code is executed after a certain number of cycles or seconds.</p>
<h4 id="how-to-count-1-second">How to count 1 second</h4>
<p>ATTiny44 runs at 1Mhz, which means 1 cycle every 1 micro seconds, if a normal mode is chosen so the specific task will be executed every 0.065536 seconds which is very fast, In my case I wanted to toggle the LED state every second so I have chose CTC mode and counted the 1 second as follows:</p>
<p>When the register TCNT overflows at normal mode, the number is 65536 which is 2^16 (16 bit since TCNT is two 8-bit register) so a prescalar is used to slow it down. The prescalars available are 1, 8, 64, 256, 1024.</p>
<p>First is choose the first prescalar which gives a time higher than the required 1 second.</p>
<table>
<thead>
<tr class="header">
<th align="left"><strong>1 </strong></th>
<th align="left">** 0.065536**</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><strong>8 </strong></td>
<td align="left"><strong>0.524288</strong></td>
</tr>
<tr class="even">
<td align="left"><strong>64 </strong></td>
<td align="left">** 4.194304**</td>
</tr>
</tbody>
</table>
<p>64 prescalar gives 4.194304 seconds, using the rule of third, I have calculated the number corresponds to 1 second as follows</p>
<p>4.194304 --&gt; 65536 1 ---------&gt; X</p>
<p>X = 65536/4.194304 = 15625 exact.</p>
<h4 id="example">Example</h4>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span>
<span class="co">This code was created by Ahmad Fares on 10-Dec-2017</span>
<span class="co">Timer with timer/counter 1 hardware to toggle LED state every 1 seconds</span>
<span class="co">MIT License</span>
<span class="co">*/</span>
<span class="ot">#define setbit(register, bit) (register) |= (1 &lt;&lt; (bit))</span>
<span class="ot">#define clearbit(register, bit) (register) &amp;= ~(1 &lt;&lt; (bit))</span>
<span class="ot">#define testbit(register, bit) (register) &amp; (1 &lt;&lt; (bit))</span>
<span class="ot">#define togglebit(register,bit) (register) ^= (1&lt;&lt;bit)</span>
<span class="ot">#define F_CPU 1000000UL</span>
<span class="ot">#include &lt;avr/io.h&gt;</span>
<span class="ot">#include &lt;util/delay.h&gt;</span>
<span class="ot">#include &lt;avr/interrupt.h&gt;</span>
ISR(TIM1_COMPA_vect){
togglebit(PORTA,PA3);
}
<span class="dt">int</span> main (<span class="dt">void</span>) {
DDRA |= (<span class="dv">1</span> &lt;&lt; PA3); <span class="co">// set LED pin as output</span>
OCR1A = <span class="dv">15625</span>; <span class="co">// at 1/64 prescalar this is exactly 1 second.</span>
setbit(TCCR1B, WGM12);
sei();
setbit(TIMSK1,OCIE1A); <span class="co">// enable CTC interrupt when TCNT1 = OCR1A</span>
setbit(TCCR1B,CS11);
setbit(TCCR1B,CS10); <span class="co">//These 2 registers set prescalar to 1/64 and start the counter.</span>
<span class="co">// setbit(PORTA,PA3);</span>
<span class="kw">while</span>(<span class="dv">1</span>) {
}
}</code></pre></div>
<h2 id="adc">ADC</h2>
<h2 id="sample-code.">Sample Code.</h2>
<p><strong>In sample code put also problems see notebook</strong> <strong>Descrip all the codes</strong> <strong>Push LED, Interrupt, delay, withoutPullUp</strong></p>
<p><strong>In sample code put also problems see notebook</strong></p>
<p><strong>Write more about registers in interrupt and other</strong></p>
<p><strong>Push LED, Interrupt, delay, withoutPullUp</strong></p>
<p><strong>Add video of each code</strong></p>
</body>
</html>
......@@ -107,7 +107,7 @@ The following code explains the macron method
```C
/*
This code was created by Ahmad Fares on 4th-Dec-2017.
The code is used to switch on an LED when a Button is pressed using macron method
The code is used to switch on an LED when a Button is pressed using Macron
MIT License
*/
......@@ -130,11 +130,11 @@ int main (void)
while(1) {
if(testbit(PINB, PB2)){
if(testbit(PINB, PB2)){ //Read button statues
setbit(PORTA, PA3);
setbit(PORTA, PA3); //Switch off LED
}else{
clearbit(PORTA, PA3);
clearbit(PORTA, PA3); // Switch on LED
}
}
......@@ -163,11 +163,11 @@ MIT License
#define clearbit(register, bit) (register) &= ~(1 << (bit))
#define testbit(register, bit) (register) & (1 << (bit))
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/interrupt.h> //include interrupt library
#include <avr/io.h> //include io library
// Interrup function, PCINT1_vect is the vector for interrupt request
// interrupt function, PCINT1_vect is the vector for interrupt request
ISR(PCINT1_vect)
{
if ( PINB & (1<<PB2) ) // test PINA7 if true then
......@@ -195,13 +195,77 @@ while(1){
## Timers
## Count 1 seconds for 1 Mhz (Rule of thirds)
Timers are registers inside the microcontroller, but run independently and can be used to run a specific code after a certain time. In my case, the ATTiny44 has three 8-bit counter registers: **TCNT0**, **TCNT1L** and **TCNT1H**.
Those registers don't actually count time, they count cycles and from these cycles we can count time. There are two operation modes: normal and CTC (clear time on compare match). In the normal mode a specific block of code is executed after the register overflows whereas in the CTC mode which is the most commonly used, a block of code is executed after a certain number of cycles or seconds.
#### How to count 1 second
ATTiny44 runs at 1Mhz, which means 1 cycle every 1 micro seconds, if a normal mode is chosen so the specific task will be executed every 0.065536 seconds which is very fast, In my case I wanted to toggle the LED state every second so I have chose CTC mode and counted the 1 second as follows:
When the register TCNT overflows at normal mode, the number is 65536 which is 2^16 (16 bit since TCNT is two 8-bit register) so a prescalar is used to slow it down. The prescalars available are 1, 8, 64, 256, 1024.
First is choose the first prescalar which gives a time higher than the required 1 second.
| **1 ** |** 0.065536** |
|----|----------|
| **8 ** | **0.524288** |
| **64 **|** 4.194304** |
64 prescalar gives 4.194304 seconds, using the rule of third, I have calculated the number corresponds to 1 second as follows
4.194304 --> 65536
1 ---------> X
X = 65536/4.194304 = 15625 exact.
#### Example
```C
/*
This code was created by Ahmad Fares on 10-Dec-2017
Timer with timer/counter 1 hardware to toggle LED state every 1 seconds
MIT License
*/
#define setbit(register, bit) (register) |= (1 << (bit))
#define clearbit(register, bit) (register) &= ~(1 << (bit))
#define testbit(register, bit) (register) & (1 << (bit))
#define togglebit(register,bit) (register) ^= (1<<bit)
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
ISR(TIM1_COMPA_vect){
togglebit(PORTA,PA3);
}
int main (void) {
DDRA |= (1 << PA3); // set LED pin as output
OCR1A = 15625; // at 1/64 prescalar this is exactly 1 second.
setbit(TCCR1B, WGM12);
sei();
setbit(TIMSK1,OCIE1A); // enable CTC interrupt when TCNT1 = OCR1A
setbit(TCCR1B,CS11);
setbit(TCCR1B,CS10); //These 2 registers set prescalar to 1/64 and start the counter.
// setbit(PORTA,PA3);
while(1) {
}
}
```
## ADC
## Sample Code.
**In sample code put also problems see notebook**
**Descrip all the codes**
**Write more about registers in interrupt and other**
**Push LED, Interrupt, delay, withoutPullUp**
**Add video of each code**
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment