Nuffnang Ads

Monday, January 19, 2015

Learn Microcontroller PIC : Lesson 1 - Introduction to PIC

If you are new to microcontroller, that not the problem . Because this will guide you how to use it. 

So, What is microcontroller? To answer this, you may see this link : microcontroller (from wikipedia) or you also can find it in other sources. For me, understand what is microcontroller is important but not its history because we are focus on the its application.

Now, I introduce PIC microconroller ,PIC (from wikipedia). 

Before start, we need some components:
HARDWARE :
1) 1 - PIC16F877A
2) 2 - 33pF Ceramic Capacitor
3) 1 - 10k ohm resistor (0.25W)
4) 1 - 20MHz crystal 
5) 1 - PICKIT2 (Uic00b programmer)
6) 1 - Breadboard 
7) 1 - LED (any colour but with low current)(tips : not ultra bright LED or big size LED)
8) 1 - 1k ohm resistor (0.25W)
9) Multimeter (optional)
10) some wire (single core) --> 

SOFTWARE :
1) MPLAB or MPLABX 

Ok, when you prepare all the material. You are ready to start learn.

Procedure :

Connect the circuit as show in the below :


#Important : The MCLR pin is a pin to reset (Hardware reset)  the program in the PIC. The PIC will reset when the MCLR pin receive '0' or GND.
After you connect the circuit, let we install MPLAB or MPLABX. The installation tutorial for MPLAB or MPLABX  . The installation will take some time. 

When you done install, you can start to program the PIC . Before that, are we forgot something? Erh ......... Oh ya ... how to connect programmer to PIC? Ok, this can refer to the datasheet of the UIC00b programmer. To solve this, first we need to know what pin should we need to connect? According to the datasheet of the UIC00b page 7. 
From the picture above, that are 6 pin are used which is pin 3, 4, 5, 6, 7, 8, 9. But for our application, pin 9 is unused. If you are not good on soldering , you may can buy this :

 Or you can build your own one . This I will teach you later (maybe in next post) .

When you are connect the UIC00b programmer to your circuit and plug in the USB to your computer. Now you can start program. First , you can follow this tutorial (MPLAB) or (MPLABX).

At here, you may know how to program PIC to light up a LED.

This is the program :

#include <htc.h>
#define _XTAL_FREQ 20000000 // tell PIC the value of the crystal
__CONFIG(0x3F32); // configuration bits

void main() // main program
{
TRISD = 0; //this is to set the pin direction : 0 for output , 1 for input 
RD1 = 1; // this is to set the pin : to light up LED
while (1); // this is must because the PIC program is infinite loop
}

From here, you should know how to program PIC and its basic connection. And have a nice day.

Friday, January 16, 2015

Small project


在这我分享一下,我之前的小小的project:

Component 如下:
PIC18F4550, crystal(20Mhz) , capacitor(2X 33pF)
- 2 set of IR(LED) 
LM324
- Speaker (8 ohm)
- 一些resistor (0.25W 够了)
- 2 X LED (任何颜色)

连接如图:



这是coding:

CONFIG FOSC=HS
;CONFIG WDT=OFF
LIST P=18F4550         ; the microcontroller that this program is written for
LIST F=INHX8M          ; the file format chosen

#include P18F4550.INC   ; the included file contains all SFR definitions and more
a0  equ 0x00
a1  equ 0x01
        org 0h
        goto    main
        org 50h
main    clrf    TRISD
        setf    TRISC
        bsf     PORTD,7;power on
        bcf     PORTD,4;off speaker led
        bsf     TRISA,0
        movlw   0x0B
        movwf   a1
        btfss   PORTC,0
        call    speaker
        bra     main
speaker;speaker function
        btg     PORTD,0
        bsf     PORTD,4;on speaker led
        call    adcf
        call    ADCc
        call    TSf
        movff   ADRESL,a0
        btfsc   PORTC,0
        return
        bra     speaker
T0f     ;timer 0 = 10us
        movlw   0xFF
        movwf   TMR0H
        movlw   0xCE
        movwf   TMR0L
        movlw   0x88
        movwf   T0CON
        goto    T0P1
T0P1    btfss   INTCON,TMR0IF
        bra     T0P1
        bcf     INTCON,TMR0IF
        return
TSf     ;timer produce due to adc value(max=255)
        call    T0f
        decf    a0
        bnz     TSf
        return
adcf    ;adc init
        movlw   0x03
        movwf   ADCON0
        movlw   0x0E
        movwf   ADCON1
        movlw   0x88
        movwf   ADCON2
        return
ADCc    btfsc   ADCON0,1
        bra     ADCc
        bcf     PIR1,6 
        return
        end


我只是用Timer,ADC和PWM而已。

如何function:
1)接进LM324的IR是为了模仿on-off switch的功能。
2)接进PIC(ADC pin)的IR是要读取它的analogue的data。我用的ADC bit size 是8bit(是可以自己set的)。之后,我把它的data 放进timer function。根据它的data,convert 成delay time。ADC value 越低,frequency越高。