新會員
Joined
: 2006/11/8 19:16 最後登入時間
: 2008/5/12 14:16
Group:
註冊會員
Level : 1 HP : 0 / 8 MP : 1 / 446 EXP : 34
|
版主及各位高手您好: 在此先感謝版主幫小弟解決了終端機顯示之問題,但目前希望能將原本僅用OC1作可變電阻控制PWM DUTY調變改為利用2組OCx作同功能之可變電阻控制PWM DUTY調變(可分別例用VR1與VR2做2組PWM DUTY之控制),是否可以請教版主另一組OC須如何設定以及搭配可變電阻去控制且依樣可顯示於終端機之上,還煩請各位回答了,以下是我的程式內容。
#include <p30fxxxx.h> #include <adc10.h> // 將adc10函式的原型宣告檔案含入 #include <timer.h> // 將Timer函式的原型宣告檔案含入 #include "C30EVM_LCD.h" #include <uart.h> #include <outcompare.h> // 將Compare函式的原型宣告檔案含入
#define FCY 7372800 * 2 // 因為使用頻率為將外部 7.3728 MHz * 8 的模式 , 每一指令週期需 4 個 clock // 所以 FCY = (7.3728 * 8 / 4 ) MHz = 7372800* 2
_FOSC(CSW_FSCM_OFF & XT_PLL8); // XT with 8xPLL oscillator, Failsafe clock off _FWDT(WDT_OFF); // Watchdog timer disabled _FBORPOR(PBOR_OFF & MCLR_EN); // Brown-out reset disabled, MCLR reset enabled _FGS(CODE_PROT_OFF); // Code protect disabled
const char My_String1[]="\r\nDimming Level\r\n" ; char My_String2[]="VR1:"; char My_String3[]="VR2:";
#define LED1 LATEbits.LATE0 #define CTRL_LED1 TRISEbits.TRISE0 #define LED7 LATBbits.LATB4 #define SW5 PORTEbits.RE8 #define INPUT 0 #define OUTPUT 1
void Init_ADC(void) ; void SW5_Status(void); void Init_Timer2(void); void Init_UART(void) ; void Show_ADC(void) ; unsigned int Read_VR2(void); void Init_OC1(void) ;
void _ISR _T2Interrupt(void) {
LED1=!LED1; IFS0bits.T2IF = 0 ;
}
int main( void ) { SRbits.IPL=4; ADPCFG=0xffff; TRISB=0xffff; LATB=0xffff; SW5=INPUT;
Init_ADC( ) ; // 將ADC進行初始化設定 OpenLCD(); setcurLCD(0,0); putrsLCD( My_String2 ); setcurLCD(0,1); putrsLCD( My_String3); Init_UART( ) ; // 對 UART 模組作初始化設定
putsUART1( (unsigned int *) My_String1 ) ; Init_Timer2( ); Init_OC1(); TRISDbits.TRISD0=0; CTRL_LED1 = 0 ;
while(1) { SW5_Status();
} }
/***********************************************/
void SW5_Status(void) { while(SW5) OC1RS=Read_VR2( ); Show_ADC( ) ; // 將類比轉換結果顯示 OC1R = ADCBUF0<<6 ;
} // Subroutine to configure the OC module
void Init_OC1(void) { CloseOC1();
ConfigIntOC1(OC_INT_OFF & OC_INT_PRIOR_7); // IEC0bits.OC1IE = 0 ; // IFS0bits.OC1IF = 0 ; // IPC0bits.OC1IP = 7 ;
OpenOC1(OC_IDLE_CON & OC_TIMER2_SRC & OC_PWM_FAULT_PIN_DISABLE, 0, 0); // OC1CON= 0x0006; }
/***********************************************/ // Subroutine to configure the A/D module
void Init_ADC(void) {
unsigned int Channel, PinConfig, Scanselect, Adcon3_reg, Adcon2_reg, Adcon1_reg;
ADCON1bits.ADON = 0; /* turn off ADC */
PinConfig = ENABLE_AN0_ANA & ENABLE_AN6_ANA; // Select port pins as analog inputs ADPCFG<15:0>
Adcon1_reg = ADC_MODULE_ON & // Turn on A/D module (ADON) ADC_IDLE_STOP & // ADC turned off during idle (ADSIDL) ADC_FORMAT_INTG & // Output in integer format (FORM) ADC_CLK_AUTO & // Conversion trigger manually (SSRC) ADC_SAMPLE_SIMULTANEOUS & // Sample channels individually (SIMSAM) ADC_AUTO_SAMPLING_OFF&ADC_SAMP_OFF; // Sample trigger manually (ASAM)
Adcon2_reg = ADC_VREF_AVDD_AVSS & // Voltage reference : +AVdd, -AVss (VCFG) ADC_SCAN_OFF & // Scan off (CSCNA) ADC_ALT_BUF_OFF & // Use fixed buffer (BUFM) ADC_ALT_INPUT_OFF & // Does not alternate between MUX A & MUX B (ALTS) ADC_CONVERT_CH_0A& // Convert only channel 0 (CHPS) ADC_SAMPLES_PER_INT_1; // 1 sample between interrupt (SMPI)
Adcon3_reg = ADC_SAMPLE_TIME_16 & // Auto-Sample time (SAMC) ADC_CONV_CLK_SYSTEM & // Use system clock (ADRC) ADC_CONV_CLK_4Tcy; // Conversion clock = 4 Tcy (ADCS) // ADCS = 2*(154ns)/(1/Fcy)-1 = 3.5416
Scanselect = SCAN_NONE; // ADC scan no channel (ADCSSL)
OpenADC10(Adcon1_reg, Adcon2_reg, Adcon3_reg, PinConfig, Scanselect);
Channel = ADC_CH0_POS_SAMPLEA_AN6 & // CH0 Pos. : AN0, Neg. : Nominal Vref- Defined in ADCON2 ADC_CH0_NEG_SAMPLEA_NVREF & ADC_CHX_POS_SAMPLEA_AN0AN1AN2& ADC_CHX_NEG_SAMPLEA_NVREF; // (ADCHS) SetChanADC10(Channel);
ConfigIntADC10(ADC_INT_DISABLE); // Disable ADC interrupt
}
/***********************************************/ // Subroutine to configure the Timer 2
void Init_Timer2(void) { ConfigIntTimer2( T2_INT_PRIOR_6 & T2_INT_ON ) ; // Timer2 的中斷優先等級設 6 // Timer2 的中斷 OFF
OpenTimer2( T2_ON & T2_IDLE_STOP & T2_GATE_OFF & // Timer2 的 Period 設為每 64ms T2_PS_1_1 & T2_32BIT_MODE_OFF & T2_SOURCE_INT, 1023) ; } /***********************************************/ // Subroutine to read analog voltage from VR2
unsigned int Read_VR2(void) { unsigned char Loop ;
ADCON1bits.SAMP = 1; // start sampling ...
for ( Loop = 0 ; Loop < 100 ; Loop ++ );
ConvertADC10(); while (BusyADC10()); // conversion done? return ADCBUF0 ; // get ADC value }
/***********************************************/ // Subroutine to initialize UART module
void Init_UART(void) { /* Holds the value of baud register */ unsigned int baudvalue; /* Holds the value of uart config reg */ unsigned int U1MODEvalue; /* Holds the information regarding uart TX & RX interrupt modes */ unsigned int U1STAvalue; /* Turn off UART1module */ CloseUART1(); /* Configure uart1 receive and transmit interrupt */ ConfigIntUART1(UART_RX_INT_EN & UART_RX_INT_PR6 & UART_TX_INT_DIS & UART_TX_INT_PR2); /* Setup the Buad Rate Generator */ baudvalue = 95; //UxBRG = ( (FCY/Desired Baud Rate)/16) – 1 //UxBRG = ( (7372800*2/9600)/16-1) = 95 /* Configure UART1 module to transmit 8 bit data with one stopbit. Also Enable loopback mode */ U1MODEvalue = UART_EN & UART_IDLE_CON & UART_DIS_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_NO_PAR_8BIT & UART_1STOPBIT; U1STAvalue = UART_INT_TX_BUF_EMPTY & UART_TX_PIN_NORMAL & UART_TX_ENABLE & UART_INT_RX_CHAR & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR; OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);
return;
}
/***********************************************/ // Subroutine to show ADC on UART
void Show_ADC(void) { unsigned int ADCValue; int Loop ; ADCValue=0; unsigned char TXdata[4]; ADCON1bits.SAMP = 1; // start sampling ...
for ( Loop = 0 ; Loop < 100 ; Loop ++ ); ADCON1bits.SAMP = 0;
ConvertADC10(); while (BusyADC10()); // conversion done? ADCValue=ReadADC10(0); setcurLCD(4,1); put_Num_LCD(ADCValue>>2); ADCValue=ReadADC10(1); setcurLCD(12,1); put_Num_LCD(ADCValue>>2); ADCValue = (ADCBUF0 >> 2); // get ADC value
sitoa(ADCValue, (unsigned char *)TXdata);
putsUART1( (unsigned int *) "VR2 : "); putsUART1( (unsigned int *) TXdata ) ; putsUART1( (unsigned int *) "\r\n" ); putsUART1( (unsigned int *) "VR1 : "); putsUART1( (unsigned int *) TXdata ) ; putsUART1( (unsigned int *) "\r\n" );
}
//*********************************************** // Put a unsigned byte in decimal format // to UART Module
void sitoa( unsigned char The_Number, unsigned char *buff) {
unsigned char Temp_Char_100, Temp_Char_10, Temp_Char ; unsigned char *temp = (unsigned char *)buff ;
Temp_Char_100 = The_Number /100 ; //取百位數的數字 temp[0] = Temp_Char_100 + '0' ; //取百位數的數字 Temp_Char_10 = (The_Number - Temp_Char_100*100) /10 ; //取十位數的數字 temp[1] = Temp_Char_10 + '0' ; //取十位數的數字 Temp_Char = The_Number - ( Temp_Char_100*100 + Temp_Char_10 * 10 ) ; //取個位數的數字 temp[2] = Temp_Char + '0' ; //取個位數的數字 temp[3] = 0 ; }
發表於: 2008/5/12 12:34
|