Uploaded by Tnen Con

pwm

advertisement
'
'
/\\\\\\\\\
' /\\\///////\\\
' \/\\\
\/\\\
/\\\
/\\\
'
\/\\\\\\\\\\\/
/\\\\\
/\\\\\\\\\\
/\\\\\\\\
/\\\\\\\\\\\
/\\\\\\\\\\\ /\\\\\\\\\
'
\/\\\//////\\\
/\\\///\\\ \/\\\//////
/\\\/////\\\ \////\\\////
\////\\\//// \////////\\\
'
\/\\\
\//\\\
/\\\ \//\\\ \/\\\\\\\\\\ /\\\\\\\\\\\
\/\\\
\/\\\
/\\\\\\\\\\
'
\/\\\
\//\\\ \//\\\ /\\\ \////////\\\ \//\\///////
\/\\\ /\\
\/\\\ /\\
/\\\/////\\\
'
\/\\\
\//\\\ \///\\\\\/
/\\\\\\\\\\ \//\\\\\\\\\\
\//\\\\\
\//\\\\\
\//\\\\\\\\/\\
'
\///
\///
\/////
\//////////
\//////////
\/////
\/////
\////////\//
'
Let's find out together what makes a PIC
Tick!
'
' Procedures for PWM1 on a PIC16F1579 device
'
Device = 16F1579
Declare Xtal = 16
'------------------------------------------------------' Setup the PWM1 peripheral
' Input
: None
' Output
: None
' Notes
: None
'
Proc PWM1_Init()
PWM1INTE = $00
PWM1INTF = $00
PWM1CLKCON = $00
PWM1LDCON = $00
PWM1OFCON = $00
PWM1PHH = $00
PWM1PHL = $00
PWM1DCH = $06
PWM1DCL = $3F
PWM1PRH = $0C
PWM1PRL = $7F
PWM1OFH = $00
PWM1OFL = $1F
PWM1TMRH = $00
PWM1TMRL = $00
PWM1CON = $80
' Standard PWM mode, Polarity is active high, Enabled
EndProc
'------------------------------------------------------'
$define PWM1_Start() PWM1CONbits_EN = 1
$define PWM1_Stop() PWM1CONbits_EN = 0
$define PWM1_LoadBufferSet() PWM1LDCONbits_LDA = 1
'------------------------------------------------------' Alter the phase of the PWM
' Input
: pPhase
' Output
: None
' Notes
: None
'
Proc PWM1_Phase(pPhase As Word)
PWM1PHH = pPhase.Byte1
PWM1PHL = pPhase.Byte0
EndProc
'------------------------------------------------------' Alter the duty cycle of the PWM
' Input
: pDuty
' Output
: None
' Notes
: None
'
Proc PWM1_Duty(pDuty As Word)
PWM1DCH = pDuty.Byte1
PWM1DCL = pDuty.Byte0
EndProc
'------------------------------------------------------' Alter the period of the PWM
' Input
: pPeriod
' Output
: None
' Notes
: None
'
Proc PWM1_Period(pPeriod As Word)
PWM1PRH = pPeriod.Byte1
PWM1PRL = pPeriod.Byte0
EndProc
'------------------------------------------------------' Alter the offset of the PWM
' Input
: pOffset
' Output
: None
' Notes
: None
'
Proc PWM1_Offset(pOffset As Word)
PWM1OFH = pOffset.Byte1
PWM1OFL = pOffset.Byte0
EndProc
'------------------------------------------------------' Initialise the microcontroller's internal oscillator for 16MHz
' Input
: None
' Output
: None
' Notes
: None
'
Proc Osc_16MHz()
OSCCON = $7A
OSCTUNE = $00
BORCON = $00
DelayMs 100
EndProc
'------------------------------------------------------' The main program starts here
'
Main:
Osc_16MHz()
RA4PPS = PPS_Fn_PWM1OUT
PWM1_Init()
Output PORTA.4
' PORTA.4 is PWM1OUT
Download