در این وبگاه مطالب علمی، آهنگ های ویژه (قدیمی و جدید و تیتراژ) مطالب کودکانه، طب سنتی، تصاویر جالب، کاریکاتور، شناختن مشاهیر، شعر، دانلود 3gp و amr ، نرم افزار موبایل، کتاب و مقاله، مطالب آسمانی، گل و گیاه و مطالبی در مورد گیم و خودرو ارائه می شود.

فرشید احمدی

جستجو

 

جلسه پنج-تعریف متغیر - برچسب گذاری و نام گذاری پورتها

جمعه 2 دی 1390   01:31 ق.ظ


نوع مطلب : الکترونیک ،میکرو کنترلر PIC ،

تعریف متغیر:

 

Name Var Type

* یک متغیر میتواند یکی از سه حالت زیر را داشته باشد:

 

BIT   0 or 1

 

 

 

 

 

 

 

 

 

Byte   0 to 255

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

word  0 to 65535


          قوانین مهم برای نام گذاری متغیر ها:

1-    نام متغیر نمیتواند با عدد شروع شود.

2-    نام متغیر نمیتواند از کلمات کلیدی برنامه باشد.(IF , END ...)

3-    نام متغیر میتواند از حروف و اعداد و علامات تشکیل شده باشد.

4-    قراردادن فاصله (space) بین حروف مجاز نیست.

5-    بهتر است نامی انتخاب شود که با کاربرد متغیر متناسب باشد.

 

مثال: با اجرای دستور زیر چه مقداری روی پورت A  و B نشان داده میشود.

 

TRISA=0;             A= out put

TRISB=0

a1 Var byte;           a1= 0 to 255

a2 var byte

a3 var byte

a1=$F5;                a1=%11110101

a2=$2E;                a2=%00101110

a3=$04;                 a3=%00000100

PortA=a1&a2

PortB=~a3

END

در نتیجه ی این برنامه:

PortA=%00100100

PortB=%11111011

 

* مجدد یاد آوری میشود حروف بعد از علامت (;) صرفاً جهت توضیح برنامه به دیگران و یاد آوری برای خود نویسنده است و این قسمت که اختیاری است در کامپایلر خوانده نمیشود. در نتیجه تأثیری روی نحوه ی اجرای برنامه ندارد.

 

*پورت ها را نیز میتوان مانند یک متغیر تعریف کرد.

 

مثال: در قطعه برنامه ی زیر C , D , E را بدست آورید.

A Var PortA;         A mean PortA

B Var PortB

C Var Byte;           C is 0 to 255

D Var Word;         D is 0 to 65535 (255×255=65025)

E Var Byte

A=25

B=6

C=A/B

D=A*B

E=A//B

خواهیم داشت.

C=4

D=150

E=1

 

اما تذکر داده میشود که در عملگر های بیتی ، یک بیت ، یک بیت ، اعمال را انجام میدهیم

 

ضریب

 

128

64

32

16

8

4

2

1

A

=

0

0

0

1

1

0

0

1

B

=

0

0

0

0

0

1

1

0

A&B

=

0

0

0

0

0

0

0

0

A|B

=

0

0

0

1

1

1

1

1

A~

=

1

1

1

0

0

1

1

0

 

فرمان پرش و برچسب گذاری:

          برای Label  گذاری کافی است بعد از کلمه از (:) استفاده کنیم. برای پرش نیز از GoTo استفاده میکنیم.

 

مثال: برنامه ای بنویسید که به طور مداوم ورودی روی خروجی نمایش داده شود.




TRISA=$FF

TRISB=0

Loop:

PortB=PortA

GoTo Loop

END

همانطور که دیده میشود برنامه ی فوق یک حلقه ی دائمی است.

          نکات مهم در برچسب گذاری:

1-  نام برچسب مانند متغیر میتواند ترکیبی از حروف و اعداد و علامات باشد به شرطی که با عدد شروع نشود.

2-    نام برچسب از کلمات کلیدی برنامه انتخاب نشود.

3-  برای برچسب گذاری به کاری که قرار است در آن قسمت انجام شود دقت کنید و نام مناسبی انتخاب کنید. این کار در برنامه های طولانی بسیار مفید خواهد بود.


نوشته شده توسط : فرشید احمدی

جلسه چهارم- خواندن و نوشتن- عملگر های ریاضی و منطقی و بیتی

جمعه 2 دی 1390   12:02 ق.ظ


نوع مطلب : الکترونیک ،میکرو کنترلر PIC ،

دستور خواندن ورودی و نوشتن خروجی:

 

برای این کار کافی است از نام پورت و پین استفاده کنیم.

 

مثال: میخواهیم دستور بدهیم LED روی پین 1 از پورت A روشن شود.



PortA.1=1

و برای خاموش کردن

PortA.1=0

 

و برای اینکه دستور دهیم مقدار A.2 را بخواند و روی A.0 بنویسد.

 

PortA.0=PortA.2

 

طرز درست بستن کلید و نامهای آن بصورت زیر است(شکل سمت راست Pool up و سمت چپ Pool Down است):

 

 

* در حالت Pool up وقتی کلید باز است 1 منطقی به پین A.1 منتقل میشود.

 

معرفی عملگرها:

          1- عملگرهای ریاضی:

 

توضیح

علامت

جمع

+

تفریق

-

ضرب

*

تقسیم کامل

/

باقی مانده ی تقسیم

//

 

مثال: برای مدار زیر برنامه ای بنویسید که مقدار کلید ها را بخواند و بر روی LED ها نشان دهد.

 

با توجه به اینکه فقط کلیدها به پورتA وصل هستند و پورت B نیز فقط به LED ها وصل هستند کل پورت A را ورودی و کل پورت B را خروجی میگیریم.

 

TRISA=$FF;          PortA=input

TRISB=0;             PortB=output

PortB=PortA*2;      B=2A

END

          2- عملگر های منطقی:

                             عملگر های منطقی برای دستورات شرطی به کار میروند مثل زمانی که لازم باشد چند شرط وجود داشته باشد تا اتفاقی رخ دهد.

توضیح

علامت

AND منطقی

AND

OR منطقی

OR

Not منطقی

Not

XOR منطقی

XOR

NAND منطقی

NAND

XNOR منطقی

XNOR

 

          3- عملگر های بیتی:

توضیح

علامت

AND بیتی

&

OR بیتی

|

Not بیتی

~

XOR بیتی

^

 

مثال:

PortA=%11100001

PortB=%10111001

PortC=A&B;                     C=%10100001

D=A|B;                           D=%11111001

E=~D;                            E=%00000110

F=A^B;                           F=%01011000


نوشته شده توسط : فرشید احمدی

یلدا مبارک -شبی که آواز نی تو شنیدم

چهارشنبه 30 آذر 1390   08:55 ب.ظ


نوع مطلب : شعر ،

شبی که آواز نی تو شنیدم

چو آهوی تشنه پی تو دویدممینیاتور

دوان دوان تا لب چشمه رسیدم

نشانه ای از نی و نغمه ندیدم

تو ای پری کجایی که رخ نمینمایی

از آن بهشت پنهان دری نمیگشایی

من همه جا پی تو گشته ام

از مه و می نشان گرفته ام

بوی تو را زگل شنیده ام

دامن گل از آن گرفته ام

تو ای پری کجایی که رخ نمینمایی

از آن بهشت پنهان دری نمیگشایی

دل من سر گشته تو نفسم آغشته تو......

 

به باغ رویاها چو گلت بویم

بر آب و آیینه چو مهت جویم

تو ای پری کجایی....

در این شب یلدا ز پی ات پویم

ز خواب و بیداری سخنت گویم

تو ای پری کجایی....

مه و ستاره درد من می دانند

که همچو من پی تو سرگردانند

شبی کنار چشمه پیدا شو

میان اشک من چو گل ها شو

تو ای پری کجایی که رخ نمینمایی

از آن بهشت پنهان دری نمیگشایی...


نوشته شده توسط : فرشید احمدی

جلسه سوم حافظه و تعریف پایه ها

چهارشنبه 30 آذر 1390   04:43 ب.ظ


نوع مطلب : الکترونیک ،میکرو کنترلر PIC ،

ساختار حافظه:

          حافظه ی داده (Data): از نوع 8 بیتی و دارای دو قسمت سد شده و آزاد است. قسمت سد شده شامل ریجستر ها است که عمل تنظیم ، فعال سازی و امکانات تراشه را به عهده دارد و قسمت آزاد در اختیار کاربر است تا اطلاعات خود را در آن ذخیره کند.

          حافظه ی داده به دو بخش موقت و دائم تقسیم میشود. موقت شامل RAM و دائم شامل EPROM است.

          حافظه ی برنامه(Program): حافظه ی برنامه کد های برنامه را نگه میدارد و از نوع 14 بیتی و بصورت Flash یا PROM ساخته میشود.

 

پورت ها و یا دروازه های میکرو کنترلر:

           پورتها به دو صورت سریال و موازی تقسیم میشوند. پورتهای سریال شامل USART و MSSP و پورتهای موازی شامل GPI و معمولاً 8 بیتی هستند.

          پورتها به صورت ورودی و یا خروجی هستند و برای پیکر بندی آنها به عنوان ورودی و یا خروجی از فرمان TRIS استفاده میشود. دستور TRIS به نرم افزار اعلان میکند که این برنامه قرار است با سخت افزاری کار کند که به فلان صورت بسته شده.در دستور TRIS برای معرفی یک پین از پورت به عنوان ورودی آن را برابر 1 و به صورت خروجی برابر 0 قرار میدهیم. برای سریع تر یاد گرفتن کافیست به این نکته دقیق شوید که مقادیر 1/0 متناظر است با حروف I/O به چند مثال دقت کنید.

 

TRISA=0; Port A= Output

TRISB.2=1; Port B, Pin 2=Input

TRISC=5; Port C, Pin 0&4=Input

TRISC=$5; Port C, Pin 0&4=Input

 

همچنین میتوان کاربرد پین های یک پورت را به صورت باینری تعیین کرد.

 

TRISA=%10010011

 

در این دستور در پورت A پین های  0و1و4و7 بصورت ورودی و بقیه بصورت خروجی است. حال به همین دستور به ترتیب به فرم دسیمال و هگزادسیمال دقت کنید.

 

TRISA=147

TRISA=$93

 

          * در کامپایلر PIC Basic Pro برای نمایش اعداد بصورت باینری از % و برای نمایش بصورت هگز از & قبل از عدد استفاده میکنیم. اعداد ده دهی علامت خاصی ندارند.

 

مثال: میخواهیم پایه های 0 تا 3 از پورت  B بصورت ورودی و سایر پایه ها خروجی باشند. از چه دستوراتی میتوانیم استفاده کنیم؟

 

TRISB=%00001111

TRISB=15

TRISB=$F

 

مثال:  برای پورت C پایه های فرد را ورودی و زوج را خروجی تعریف کنید. دقت کنید که شمارش پایه ها از صفر شروع میشود و به صورت 76543210 است پس:

 

TRISC=%01010101

TRISC=85

TRISC=$55

 

مثال: برای میکرو داده شده پایه ها را تعریف کنید. در این مثال بدون توجه به نوع بسته شدن کلید متوجه میشویم که پورت B پین 4 و 6 به عنوان ورودی و پورت A پین 0 و 1 و 2 به LED و به عنوان خروجی است پس مینویسیم:

 TRIS

TRISA.0=0

TRISA.1=0

TRISA.2=0

TRISB.4=1

TRISB.6=1

یا بهتر است برای کوتاه تر شدن برنامه بنویسیم:

TRISA=0

TRISB=%01010000

 

یا میتوانیم کل پورت B را به عنوان ورودی تعریف کنیم همانگونه که کل پورتA را خروجی تعریف کردیم. زیرا سایر پینها در این مثال برای ما مهم نیستند.

TRISB=255

TRISB=$FF

 

مثال: همین کار را برای مدار زیر انجام دهید.

 TRIS

TRISA.0=1

TRISA.1=0

TRISA.2=1

TRISA.3=0

یا بهتر است بنویسیم:

TRISA=%00000101

 

مثال: مدار مربوط به TRISB=%11100110 را حدس بزنید.

Tris


نوشته شده توسط : فرشید احمدی

جلسه دوم پایه های مهم

چهارشنبه 30 آذر 1390   01:27 ق.ظ


نوع مطلب : الکترونیک ،میکرو کنترلر PIC ،

پایه های مهم در میکرو کنترلر PIC:

          1- پایه های تغذیه: دو پایه ی VSS و VDD برای تغذیه ی میکرو کنترلر بکار میرود. VDD به +5V و VSS به زمین وصل میشود. کم بودن ولتاژ باعث کار نکردن تراشه و زیاد بودن ولتاژ باعث سوختن آن میشود. همچنین هرچه نوسان بیشتری در تعذیه داشته باشیم عملکرد تراشه ضعیف تر میشود. ساده ترین مدار تغذیه ی استاندارد به صورت زیر است.

          و درصورتیکه منبع تغذیه ی تثبیت شده دارید فقط کافیست با یک دیود سری کنیم.

          2- اسیلاتور: به چهار روش میتوان پالس ساعت مورد نیاز برای PIC را تأمین نمود: الف- در بعضی از تراشه ها ، اسیلاتور داخلی.  ب- نوسان سازی با شبکه ی مقاومت خازن.  ج- با استفاده از کریستال و خازن.  د- با سیستم پالس ساعت خارجی.

          به عنوان نمونه فرکانس کریستال داخلی برای تراشه های 16F819 یا 12f675 بین 4-8MHz میباشد.

          در شبکه ی مقاومت و خازن:

RC

 

F

C

R

5.4MHz

100pF

5KΩ

3MHz

100pF

10KΩ

328KHz

100pF

100KΩ

 

 

 

 

 

          اسیلاتور با کریستال و خازن:

 Xtal

 

1- LP :کریستال توان پایین______32KHz-----300KHZ

2-XT: کریستال/تشدید کننده_____200KHz-------4MHz

3-HS:سرعت بالا/تشدید کننده____4MHz--------20MHz

 

            3- پایه ی  Reset یا MCLR: ریست به معنی صفر کردن و برگرداندن حالت اولیه است. Reset خارجی (MCLR) از نوع فعال به صفر است و زمانی که این پایه به صفر منطقی برسد برنامه از اول شروع میشود.


نوشته شده توسط : فرشید احمدی

The PIC Tutorial - The Microcontroller Fundamentals

سه شنبه 29 آذر 1390   11:39 ب.ظ


نوع مطلب : میکرو کنترلر PIC ،الکترونیک ،

What is a Microcontroller?
 

 

Basically, a microcontroller is a device which integrates a number of the components of a microprocessor system onto a single microchip and optimised to interact with the outside world through on-board interfaces; i.e. it is a little gadget that houses a microprocessor, ROM (Read Only Memory), RAM (Random Access Memory), I/O (Input Output functions), and various other specialized circuits all in one package.

 

On the other hand, a microprocessor is normally optimised to co-ordinate the flow of information between separate memory and peripheral devices which are located outside itself. Connections to a microprocessor include address, control and data busses that allow it to select one of its peripherals and send to or retrieve data from it. Because a microcontrollers processor and peripherals are built on the same silicon, the devices are self-contained and rarely have any bus structures extending outside their packages.
So a microcontroller incorporates onto the same microchip the following:
 

  • The CPU core

  • Memory (both ROM and RAM)

  • Some parallel digital I/O

Microcontroller Components
Microcontroller's fundamental components

Microcontrollers will also combine other devices such as:

  • A Timer module to allow the microcontroller to perform tasks for certain time periods.

  • A serial I/O port to allow data to flow between the microcontroller and other devices such as a PC or another microcontroller.

  • An ADC to allow the microcontroller to accept analogue input data for processing.
     

Microcontroller Architecture
Basic microcontroller architecture


The microcontroller's building blocks explained

To illustrate the functions and interconnectivity of the building blocks of the microcontroller, we shall construct the microcontroller block by block:

 

Memory unit

Memory is part of the microcontroller whose function is to store data.
The easiest way to explain it is to describe it as one big closet with lots of drawers. If we suppose that we marked the drawers in such a way that they can not be confused, any of their contents will then be easily accessible. It is enough to know the designation of the drawer and so its contents will be known to us for sure.

 Memory components are exactly like that. For a certain input we get the contents of a certain addressed memory location and that's all. Two new concepts are brought to us: addressing and memory location. Memory consists of all memory locations, and addressing is nothing but selecting one of them. This means that we need to select the desired memory location on one hand, and on the other hand we need to wait for the contents of that location. Beside reading from a memory location, memory must also provide for writing onto it. This is done by supplying an additional line called control line. We will designate this line as R/W (read/write). Control line is used in the following way: if r/w=1, reading is done, and if opposite is true then writing is done on the memory location. Memory is the first element, and we need a few operation of our microcontroller.

 

Central Processing Unit

The block that will have a built in capability to multiply, divide, subtract, and move its contents from one memory location onto another is called "central processing unit" (CPU). Its memory locations are called registers.

Registers are therefore memory locations whose role is to help with performing various mathematical operations or any other operations with data wherever data can be found. Look at the current situation. We have two independent entities (memory and CPU) which are interconnected, and thus any exchange of data is hindered, as well as its functionality. If, for example, we wish to add the contents of two memory locations and return the result again back to memory, we would need a connection between memory and CPU. Simply stated, we must have some "way" through data goes from one block to another.

 

Bus

That "way" is called "bus". Physically, it represents a group of 8, 16, or more wires
There are two types of buses: address and data bus. The first one consists of as many lines as the amount of memory we wish to address, and the other one is as wide as data, in our case 8 bits or the connection line. First one serves to transmit address from CPU memory, and the second to connect all blocks inside the microcontroller.

As far as functionality, the situation has improved, but a new problem has also appeared: we have a unit that's capable of working by itself, but which does not have any contact with the outside world, or with us! In order to remove this deficiency, let's add a block which contains several memory locations whose one end is connected to the data bus, and the other has connection with the output lines on the microcontroller which can be seen as pins on the electronic component.
 

Input-output unit

Those locations we've just added are called "ports". There are several types of ports : input, output or bidirectional ports. When working with ports, first of all it is necessary to choose which port we need to work with, and then to send data to, or take it from the port.

When working with it the port acts like a memory location. Something is simply being written into or read from it, and it could be noticed on the pins of the microcontroller.
 

Serial communication

Beside stated above we've added to the already existing unit the possibility of communication with an outside world. However, this way of communicating has its drawbacks. One of the basic drawbacks is the number of lines which need to be used in order to transfer data. What if it is being transferred to a distance of several kilometres? The number of lines times number of kilometres doesn't promise the economy of the project. It leaves us having to reduce the number of lines in such a way that we don't lessen its functionality. Suppose we are working with three lines only, and that one line is used for sending data, other for receiving, and the third one is used as a reference line for both the input and the output side. In order for this to work, we need to set the rules of exchange of data. These rules are called protocol. Protocol is therefore defined in advance so there wouldn't be any misunderstanding between the sides that are communicating with each other. For example, if one man is speaking in French, and the other in English, it is highly unlikely that they will quickly and effectively understand each other. Let's suppose we have the following protocol. The logical unit "1" is set up on the transmitting line until transfer begins. Once the transfer starts, we lower the transmission line to logical "0" for a period of time (which we will designate as T), so the receiving side will know that it is receiving data, and so it will activate its mechanism for reception. Let's go back now to the transmission side and start putting logic zeros and ones onto the transmitter line in the order from a bit of the lowest value to a bit of the highest value. Let each bit stay on line for a time period which is equal to T, and in the end, or after the 8th bit, let us bring the logical unit "1" back on the line which will mark the end of the transmission of one data. The protocol we've just described is called in professional literature NRZ (Non-Return to Zero).

As we have separate lines for receiving and sending, it is possible to receive and send data (info.) at the same time. So called full-duplex mode block which enables this way of communication is called a serial communication block. Unlike the parallel transmission, data moves here bit by bit, or in a series of bits what defines the term serial communication comes from. After the reception of data we need to read it from the receiving location and store it in memory as opposed to sending where the process is reversed. Data goes from memory through the bus to the sending location, and then to the receiving unit according to the protocol.
 

Timer unit

Since we have the serial communication explained, we can receive, send and process data.
However, in order to utilize it in industry we need a few additionally blocks. One of those is the timer block which is significant to us because it can give us information about time, duration, protocol etc. The basic unit of the timer is a free-run counter which is in fact a register whose numeric value increments by one in even intervals, so that by taking its value during periods T1 and T2 and on the basis of their difference we can determine how much time has elapsed. This is a very important part of the microcontroller whose understanding requires most of our time.
 

Watchdog

One more thing is requiring our attention is a flawless functioning of the microcontroller
during its run-time. Suppose that as a result of some interference (which often does occur in industry) our microcontroller stops executing the program, or worse, it starts working incorrectly.
Of course, when this happens with a computer, we simply reset it and it will keep working. However, there is no reset button we can push on the microcontroller and thus solve our problem. To overcome this obstacle, we need to introduce one more block called watchdog. This block is in fact another free-run counter where our program needs to write a zero in every time it executes correctly. In case that program gets "stuck", zero will not be written in, and counter alone will reset the microcontroller upon achieving its maximum value. This will result in executing the program again, and correctly this time around. That is an important element of every program to be reliable without man's supervision.
 

Analog to Digital Converter (ADC)

As the peripheral signals usually are substantially different from the ones that microcontroller can understand (zero and one), they have to be converted into a pattern which can be comprehended by a microcontroller. This task is performed by a block for analog to digital conversion or by an ADC. This block is responsible for converting an information about some analog value to a binary number and for follow it through to a CPU block so that CPU block can further process it.

Finally, the microcontroller is now completed, and all we need to do now is to assemble it into an electronic component where it will access inner blocks through the outside pins. The picture below shows what a microcontroller looks like inside.



Microcontroller Basic Blocks



Thin lines which lead from the center towards the sides of the microcontroller represent wires connecting inner blocks with the pins on the housing of the microcontroller so called bonding lines. Chart on the following page represents the center section of a microcontroller.

For a real application, a microcontroller alone is not enough. Beside a microcontroller, we need a program that would be executed, and a few more elements which make up a interface logic towards the elements of regulation .
 

Program

Program writing is a special field of work with microcontrollers and is called "programming". Try to write a small program in a language that we will make up ourselves first and then would be understood by anyone.

START
REGISTER1=MEMORY LOCATION_A
REGISTER2=MEMORY LOCATION_B
PORTA=REGISTER1 + REGISTER2

END

The program adds the contents of two memory locations, and views their sum on port A. The first line of the program stands for moving the contents of memory location "A" into one of the registers of central processing unit. As we need the other data as well, we will also move it into the other register of the central processing unit. The next instruction instructs the central processing unit to add the contents of those two registers and send a result to port A, so that sum of that addition would be visible to the outside world. For a more complex problem, program that works on its solution will be bigger.
Programming can be done in several languages such as Assembler, C and Basic which are most commonly used languages. Assembler belongs to lower level languages that are programmed slowly, but take up the least amount of space in memory and gives the best results where the speed of program execution is concerned. As it is the most commonly used language in programming microcontrollers it will be discussed in a later chapter. Programs in C language are easier to be written, easier to be understood, but are slower in executing from assembler programs. Basic is the easiest one to learn, and its instructions are nearest a man's way of reasoning, but like C programming language it is also slower than assembler. In any case, before you make up your mind about one of these languages you need to consider carefully the demands for execution speed, for the size of memory and for the amount of time available for its assembly.
After the program is written, we would install the microcontroller into a device and run it. In order to do this we need to add a few more external components necessary for its work. First we must give life to a microcontroller by connecting it to a power supply (power needed for operation of all electronic instruments) and oscillator whose role is similar to the role that heart plays in a human body. Based on its clocks microcontroller executes instructions of a program. As it receives supply microcontroller will perform a small check up on itself, look up the beginning of the program and start executing it. How the device will work depends on many parameters, the most important of which is the skilfulness of the developer of hardware, and on programmer's expertise in getting the maximum out of the device with his program.

Microcontrollers vs. Microprocessors:

Microcontroller differs from a microprocessor in many ways. First and the most important is its functionality. In order for a microprocessor to be used, other components such as memory, or components for receiving and sending data must be added to it. In short that means that microprocessor is the very heart of the computer. On the other hand, microcontroller is designed to be all of that in one. No other external components are needed for its application because all necessary peripherals are already built into it.


نوشته شده توسط : فرشید احمدی