Nuffnang Ads

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越高。

No comments:

Post a Comment