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

-m updating C-programming

parent dbf9aaf9
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -131,7 +131,9 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
<span class="co">MIT License</span>
<span class="co">*/</span>
<span class="ot">#define F_CPU 1000000UL</span>
<span class="ot">#define F_CPU 1000000UL </span><span class="co">//Define speed</span>
<span class="co">//Define Macron</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>
......@@ -159,6 +161,48 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
}</code></pre></div>
<h2 id="interrupts">Interrupts</h2>
<p>Interrupts are used to force the microcontroller to pause the code and execute a specific block of code inside ISR function.</p>
<p>I have used interrupts to switch on an LED when a button is pressed instead of asking the microcontroller &quot;Is button pressed?&quot; by continuously reading the PB2 statues.</p>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span>
<span class="co">This program was created by Ahmad Fares on 7th-Dec-2017</span>
<span class="co">The purpose of this program is to use interrupt to detect a button statues</span>
<span class="co">MIT License</span>
<span class="co">*/</span>
<span class="ot">#define F_CPU 1000000UL </span><span class="co">//Define speed</span>
<span class="co">//Define Macron</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">#include &lt;avr/interrupt.h&gt;</span>
<span class="ot">#include &lt;avr/io.h&gt;</span>
<span class="co">// Interrup 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>
setbit(PORTA,PA3); <span class="co">//turn off LED</span>
<span class="kw">else</span>
clearbit(PORTA,PA3); <span class="co">//Turn on LED</span>
}
<span class="dt">int</span> main (<span class="dt">void</span>){
sei(); <span class="co">// activate interrupts globally</span>
setbit(DDRA, PA3); <span class="co">// set PA3 as output in DDRA.</span>
clearbit(DDRB, PB2); <span class="co">// set PB2 as input in DDRB.</span>
setbit(PORTB, PB2); <span class="co">// activate pull up resistor.</span>
setbit(GIMSK,PCIE1); <span class="co">//Enable pin change interrupt PORTB</span>
setbit(PCMSK1, PCINT10); <span class="co">//Enabl pin change interrupt in pin PB2</span>
setbit(PINA,PA3);
<span class="kw">while</span>(<span class="dv">1</span>){
<span class="co">//Doing nothing until the interrupt occurs</span>
}
}
</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>
<h2 id="adc">ADC</h2>
......
......@@ -111,7 +111,9 @@ The code is used to switch on an LED when a Button is pressed using macron metho
MIT License
*/
#define F_CPU 1000000UL
#define F_CPU 1000000UL //Define speed
//Define Macron
#define setbit(register, bit) (register) |= (1 << (bit))
#define clearbit(register, bit) (register) &= ~(1 << (bit))
#define testbit(register, bit) (register) & (1 << (bit))
......@@ -143,7 +145,53 @@ int main (void)
## Interrupts
Interrupts are used to force the microcontroller to pause the code and execute a specific block of code inside ISR function.
I have used interrupts to switch on an LED when a button is pressed instead of asking the microcontroller "Is button pressed?" by continuously reading the PB2 statues.
```C
/*
This program was created by Ahmad Fares on 7th-Dec-2017
The purpose of this program is to use interrupt to detect a button statues
MIT License
*/
#define F_CPU 1000000UL //Define speed
//Define Macron
#define setbit(register, bit) (register) |= (1 << (bit))
#define clearbit(register, bit) (register) &= ~(1 << (bit))
#define testbit(register, bit) (register) & (1 << (bit))
#include <avr/interrupt.h>
#include <avr/io.h>
// Interrup function, PCINT1_vect is the vector for interrupt request
ISR(PCINT1_vect)
{
if ( PINB & (1<<PB2) ) // test PINA7 if true then
setbit(PORTA,PA3); //turn off LED
else
clearbit(PORTA,PA3); //Turn on LED
}
int main (void){
sei(); // activate interrupts globally
setbit(DDRA, PA3); // set PA3 as output in DDRA.
clearbit(DDRB, PB2); // set PB2 as input in DDRB.
setbit(PORTB, PB2); // activate pull up resistor.
setbit(GIMSK,PCIE1); //Enable pin change interrupt PORTB
setbit(PCMSK1, PCINT10); //Enabl pin change interrupt in pin PB2
setbit(PINA,PA3);
while(1){
//Doing nothing until the interrupt occurs
}
}
```
## Timers
......
# 3D Design
## Introduction
## Antimony
## FreeCad
## Opnescad
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