筆者將 Microchip PIC24FJ64GA008 A/D 類比數位轉換器程式和脈衝寬度調變(PWM) 程式合併,調節 ADC 類比數位轉換器的 AN0 可變電阻器來控制 PWM 佔空比(Duty Cycle),祇要讀取 AN0 的數值,然後將 OC1RS 的數值改變,便可控制 PWM 佔空比。
Microchip PIC24FJ64GA008 ADC + PWM 控制佔空比程式顯示 |
Microchip PIC24FJ64GA008 ADC + PWM 控制佔空比程式顯示 |
Microchip
PIC24FJ64GA008 ADC + PWM 初始化程式:
// Initialise Timer2
CloseTimer2(); //
Timer2 Close
ConfigIntTimer2(T2_INT_ON);
// Timer2 Interrupt ON
SetPriorityIntT2(1); //
Set Interrupt Priority 0~7
OpenTimer2
(
T2_ON & // Timer2 ON
T2_IDLE_CON & // Idle Time Continue Run
T2_GATE_OFF
& // Timer2 Gate Time
Accumulation OFF/ON
T2_32BIT_MODE_OFF & //
Timer2 32-bit Mode OFF/ON
T2_PS_1_1
& // Prescaler 1:1/8/64/256
T2_SOURCE_INT, //
Timer2 Source Oscillator INT/EXT
99 // PRx=0~65535 99=25us
);
// ADC Setup
AD1PCFG = 0xFFFC; // Configure
AN0 and AN1 Analog
AD1CON1 = 0x2202; // Configure sample clock source
// and conversion trigger mode.
// Unsigned Fraction format (FORM<1:0>=10),1:0>
// Manual conversion trigger (SSRC<3:0>=000),3:0>
// Manual start of sampling (ASAM=0),
// No operation in Idle mode (ADSIDL=1).
AD1CON2 = 0x0000; // Configure A/D voltage reference and
buffer fill modes.
// Vr+ and Vr- from AVdd and AVss
(VCFG<2:0>=000),2:0>
// Inputs are not scanned, Interrupt after every
sample
AD1CON3 = 0x0100;
AD1CHS = 0x0000; // Select analog input channel AN0
AD1CSSL = 0; // No scanning required
IFS0bits.AD1IF = 0; // Clear A/D conversion interrupt
AD1CON1bits.ADON = 1; // Turn On ADC
|
Microchip
PIC24FJ64GA008 ADC + PWM 中繼程式:
// Timer2 Interrup
void __attribute__((interrupt,
no_auto_psv)) _T2Interrupt(void) {
IFS0bits.T2IF = 0;
AD1CON1bits.SAMP =1; //
Start Sampling
while(!AD1CON1bits.SAMP);
AD1CON1bits.SAMP =0;
while(AD1CON1bits.SAMP);
while(!AD1CON1bits.DONE){
an0 = ADC1BUF0;
OC1RS = an0/655;
}
}
|
Microchip PIC24FJ64GA008 ADC + PWM 主程式:
while(1){
}
|
Microchip PIC24FJ64GA008 ADC + PWM 控制佔空比程式 |
2016年 9月 15日 天氣報告
氣溫:29.1度 @ 23:00
相對濕度:百分之 73%
天氣:天色大致良好※ 中秋節快樂
沒有留言:
張貼留言