Evo kod jeste u PicBasic ali nije tesko razumeti
Code:
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _WRT_ALL & _PLLEN_ON & _STVREN_OFF & _BORV_19 & _LVP_OFF
#ENDCONFIG
;PIC16F1827
define OSC 32
OSCCON=%11110000 ' Interni oscilator 32MHz
CCP1CON=%00001100
CCPTMRS=%11111100
T2CON=%00000101 ; prescaler 1:4 TMR2
PR2=99 ; frekvencija PWM 20kHz
APFCON0 = %00000001
ANSELA = %00000000 ; Port A je digitalni
ANSELB = %00000000 ; Port B je digitalni
PORTA = %00000000
PORTB = %00000000
TRISA = %00100000 ; port A je izlazni osim RA5
TRISB = %00000000 ; port B je izlazni
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
DUTY var word
i var word
vrednost var word
i = 0
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _Int_Sub, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
OPTION_REG = %10000010 '010 preskaler postavi u odnos 1:8
TMR0=6 'Postavi vrednost TMR0 (250us)
@ INT_ENABLE TMR0_INT ; enable Timer 0 interrupts
Pocetak:
Goto Pocetak
Int_Sub:
i = i + 1
if i >= 40 then
i = 0
endif
gosub uzmi_char
CCP1CON.4 = DUTY.0
CCP1CON.5 = DUTY.1
CCPR1L = Duty >>2
@ INT_RETURN
uzmi_char:
lookup i, [5, 30, 59, 89, 117, 145, 173, 199, 223, 247, 269, 289, 307, 324, 339, 351, 361, 370, 375, 379, 380, 379, 375, 370, 361, 351, 339, 324, 307, 289, 269, 247, 223, 199, 173, 145, 117, 89, 59, 30], DUTY
return