超級終端機 沒反應~~
|
||||
---|---|---|---|---|
新會員
|
動作 :將 UART1 (RS232) 規劃成接收中斷,所以當使用者每次按電腦鍵盤時,進行類比訊號轉換,並將轉換結果輸出至UART
鮑率9600 問題:與超級終端機連線時只會出現"Exercise 12 - UART" 而按任何按鈕沒反應,請問問題是............. /////////////////////////////////////////////////// #define __dsPIC30F4011__ #include <p30F4011.h> #include <uart.h> // 將UART函式的原型宣告檔案含入 #include <timer.h> // 將Timer函式的原型宣告檔案含入 #include <adc10.h> // 將adc10函式的原型宣告檔案含入 #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\nExercise 12 - UART\r\n" ; // 宣告字串於 Program Memory (因為 const 宣告) void Init_ADC(void) ; void Init_UART(void) ; void Show_ADC(void) ; void sitoa(unsigned char, unsigned char *TXdata ) ; void _ISR _U1RXInterrupt(void) // UART1接收資料中斷副程式 { unsigned char dummy; dummy = ReadUART1() ; // 將UART讀取資料暫存器的內容讀出 Show_ADC(); IFS0bits.U1RXIF = 0 ; } int main( void ) { Init_ADC( ) ; // 將ADC進行初始化設定 Init_UART( ) ; // 對 UART 模組作初始化設定 putsUART1( (unsigned int *) My_String1 ) ; // 將存在 Program Memory 的字串輸出 while(1) ; CloseUART1(); } /***********************************************/ // 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; // 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_MANUAL & // Conversion trigger manually (SSRC) ADC_SAMPLE_INDIVIDUAL & // Sample channels individually (SIMSAM) ADC_AUTO_SAMPLING_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_CH0 & // Convert only channel 0 (CHPS) ADC_SAMPLES_PER_INT_1; // 1 sample between interrupt (SMPI) Adcon3_reg = ADC_SAMPLE_TIME_10 & // 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_AN0 & // CH0 Pos. : AN0, Neg. : Nominal Vref- Defined in ADCON2 ADC_CH0_NEG_SAMPLEA_NVREF ; // (ADCHS) SetChanADC10(Channel); ConfigIntADC10(ADC_INT_DISABLE); // Disable ADC interrupt } /***********************************************/ // 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 Time on UART void Show_ADC(void) { unsigned char dummy ; unsigned int ADCValue; unsigned char TXdata[4]; ADCON1bits.SAMP = 1; // start sampling ... for ( dummy = 0 ; dummy < 200 ; dummy ++ ); ConvertADC10(); while (BusyADC10()); // conversion done? ADCValue = (ADCBUF0 >> 2); // get ADC value sitoa(ADCValue, (unsigned char *)TXdata); putsUART1( (unsigned int *) "VR2 : "); 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/8/5 13:14
|
|||
|