http://www.codeproject.com/Articles/29528/Programming-the-PIC-Microcontroller-in-C-using-PIC{
Programming the PIC Microcontroller in C using PIC C Lite
Introduction
Programming microcontrollers using assembly language is very frustrating and cumbersome, not to mention archaic.
실망스런 & 성가신 원시적인건 말할것도 없고
As time goes by, developers have started building compilers for high level languages, targeted specifically for microcontrollers.
I use PICs personally, and there is quite a lot of resources for them.
Even good basic compilers like MBasic exist,
but I hate BASIC,
so I won't even talk about it!
I have tried building applications for the PIC16F84,
a very popular microcontroller,
with various compilers like CC5X, SDCC (this is Open Source), and PIC C from HiTech software.
I have not yet used mikroC from Microchip,
because I do not use higher PICs.
Each of these has its own strong points and weak ones,
but by far, the best compiler I would recommend is PIC C.
하지만 훨씬 최고의 컴파일러 는 PIC C 이다.
There is a free Lite version from HiTech Software,
and as expected,
I would not expect anything less from a company
that has its aim at specifically developing C compilers for PIC microcontrollers.
Background
As usual,
for any beginner of any type of coding,
초보자들에게
there must always be that first "hello world" application!
헬로월드가 첫 어플리케이션임에 틀림없다.
In microcontroller programming,
this is most definitely flashing an LED on and off. We shall do this now.
마이크로컨트롤러 프로그램에선
LED ON & OFF이다.
I am using the HI-TIDE Integrated Development Environment with PIC C Pro (45 day trial).
☆ IDE가 붙으면 통합된 개발환경이라는 뜻 (=프로그램을 복합적으로 설치 & 이용가능)
You can use PIC C Lite as well (it will produce a larger hex file because it does not have omniscient code generation).
전지전능 코드 생성기 (=옴니션트)
What's more?
PIC C Lite easily integrates into Microchips MPLAB...so there you go...you have two free products.
PIC C Lite는 쉽게 통합한다. into MPLAB에.
SDCC has an IDE called DevMic (I think it's written in Delphi just like DevC++; they are so similar), and both are Open Source. But, let's reserve SDCC for some other time...I wouldn't say it's better for a beginner.
초보자에게 권장하지않는게 낫겠다.
So, let's get some Zzzzt zzzt as the old CodeProject saying goes!!
The <htc.h> and <pic.h> contain standard definitions,
and
are never to be left out of any source file you are working with.
Since we want the LED to delay a bit,
we shall use a function DelayMs(int)
; this function only takes values up to 255.
↑이 함수는 취한다 갑을 up to 255까지.
This number indicates the number of milliseconds
for which you want the LED to be on or off.
이 숫자는 가리킨다. the 횟수 of 밀리초
for 얼마나 네가 원하는지 LED를 to be ON 혹은 OFF가 되게끔
Furthermore,
you will have to declare which port, or more specifically,
which pins are outputs
and
which ones are inputs.
더 나아가,
넌 선언해야만 할것이다. which 포트를, 좀더 구체적으로
which 핀들이 출력이될지
&
which one들이 입력이될지.
PortB has 8 ports from RB0-RB7,
and
is bidirectional (any pin can be an input or an output).
PortA has pins from RA0-RA5,
and
is also bidirectional.
However,
only RB4 to RB7 can be configured as interrupts. We shall see later what an interrupt is.
나중에보자 무엇인지 인터럽트 란.
Your code should look like this in the editor:
Get the formed HEX file and load it into the microcontroller (I am assuming you know how to do this).
Please remember that we are using a PIC16F84.
☆위 코드와 PIC16F84는 한쌍이다.
If you are using another type, then you will have to specify this detail when making your project.
HI TIDE even enables you to adapt your source code
for any type of PIC anytime,
even as you work with your project.
HI TIDE(IDE = 통합 개발활경)는 심지어 가능케한다. 널 to 적응하도록 네 소스코드를
for 어느 타입of PIC에 언제든,
심지어 as 네가 작업하면서 with 네 프로젝트로.
The PIC has a maximum source current of 25mA (I have measured currents of up to 26.3mA some times),
and
this is enough to drive LEDs directly.
이 전류는 충분하다. to 가동시키는데 LED들을 곧바로.
So, if you connect your LED to the RB0 pin and ground, you should see it flashing at a quarter of a second.
만약 네가 연결하면 네 LED를 to the RB0 핀에 & 접지에, 넌 봐야만한다. 그게 번쩍이는지 at a 1/4 초로.
Please remember that an LED is a p-n junction,
so you must have your connection right (in short, if one orientation doesn't work, the second should!!!).
Interfering with normal operation
Always, in microprocessors and microcontrollers,
we want the device
to control multiple things,
and react differently according to different situations.
우리는 원한다. 디바이스를
to 제어하기위한 다중적인 것들을,
and 반작용하기를 다르게 according to 다른 상황들에 따라서.
In other words,
let there be sequence1 in a normal operation; when such and such a thing happens, activate sequence2.
Normally,
this would be called an “interrupt sequence”,
but to avoid criticism
from more experienced programmers
(interrupt sequences must reside in their own code space
within the program,
and directly cause the execution of the main routine to change),
let us just call it “interfering with normal operation”!
to 비평을 피하기위해
from 더많이 경험함 프로그래머들로부터의
(인터럽 시퀀스들은 must 거주해야만한다. in 그들소유 코드공간안에
within the 프로그램내부의,
and 곧바로 유발한다. the 실행을 of the 메인 루틴의 to 바꾸기위해),
let us 그저 호출하자 그걸 "방해하는 with 일반 연산자와함께"!
There are two ways
to do this in C.
One is to use if
statements,
and another is to use a while
statement.
I hate while
loops, so we shall stick to "if
".
We are going to modify our code a little,
and even add an extra function so that we can specify our "delay" in decimals of seconds
instead of being limited to milliseconds (moreover 255!).
So,
let us create another project in HITIDE or MPLAB,
and
insert this code.
It is called Interrupt.c in my uploaded source code.
I recommend that
you learn how to convert from hexadecimal to binary and vice versa.
난 권한다. that을
네가 배우길 how to 바꾸는 법을 from Hex를 → 2진수로 & 그 반대로.
You need it
if you are going to work with PICs in C. What we have done now is:
만약 네가 작업할꺼라면 with PIC들과 in C언어로. What 네가 이미 마쳐야하는것은
- We have configured pins RB4 to RB7
to accept inputs,
and thus interrupts,
since they are the only pins that can do that, and
우리는 이미 구성했다. 핀들을 RB4 to RB7을
to 받아들이기위해 입력들을
- we have RB7 as our interrupt pin.
- 우린 갖고있다. RB7을 as 인터럽트 핀으로써.
- In normal operations, RB7=1.
- Our interrupt action shall be to ground RB7 and force it to zeroes (the hardware always prevails over the software!).
So,
build the project, and program the PIC.
It should be going on for about 2 (1.8 to be precise) seconds,
then
off for about a second.
Now, connect a wire to RB7 and pin it to the ground.
The sequence will change,
and the LED will be flashing faster, being on for 0.7 seconds, and off for only a tenth of a second.
We have successfully programmed the interrupt!
What's more?
Want to instead flash a big bulb or a DC motor? I will tell you how if you let me know! We just have to amplify the current and use a transistor!
Any questions and troubleshooting are welcome.
I will write more articles on how to build complex algorithms for things like decorative lights, simultaneous LED flashing at different time intervals, and much more!
License
This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)
About the Author
I have deserted general software development and chosen to enter microprocessors and push around 1s and 0s