會員登陸
帳號:

密碼:

記住我



忘記密碼?

現在註冊!
網站導航
最新下載
訪問統計 (自2012/5/3)


正在流覽:   1 名訪客



(1) 2 »


關於 printf 的用法請教
資深會員
註冊日期:
2004/08/16 13:52
來自 台灣台南
所屬群組:
註冊會員
文章: 778
等級: 25; EXP: 16
HP : 0 / 604
MP : 259 / 27617
離線
1.printf 是不是可以設定成輸出到 螢幕及UART ?
2.如果可以應該要怎麼設定呢??
3.輸出到螢幕之後,在那一個視窗呢??
是否為output MPLAB SIM 視窗??目前顯示為
UART-W0005: Uart is enabled but no output destination has been assigned. (File or Output Window)

MCU:dspic30f4011
感謝各位先前的幫忙.

2012/10/17 14:07
轉換PDF檔 列印


Re: 關於 printf 的用法請教
資深會員
註冊日期:
2004/08/16 13:52
來自 台灣台南
所屬群組:
註冊會員
文章: 778
等級: 25; EXP: 16
HP : 0 / 604
MP : 259 / 27617
離線
已找到了在Debugger\Settings\Uart1 IO
設定以下2個
Enable IO
output window

在output視窗會多一個SIM UART1視窗

2012/10/17 14:21
轉換PDF檔 列印


Re: 關於 printf 的用法請教
版主
註冊日期:
2004/04/30 10:53
來自 CAE, Microchip
所屬群組:
站務管理者
註冊會員
MICROCHIP
文章: 15016
等級: 73; EXP: 85
HP : 1092 / 1821
MP : 5005 / 84845
離線
printf( ) 在 PC 是顯示在螢幕上,可是在這微控制器上一般式輸出至 UART 上的。所以在硬體實際上要啟動 UART1 後才可以送出 UART1 的。
使用 SIM 話按照樓上的設定可以將 printf() 的輸出顯示在 Output Window 上的。

你也可以使用 sprintf( ) 將輸出送到 RAM 自己用 Watch Window 檢查輸出結果。

2012/10/19 11:00
轉換PDF檔 列印


Re: 關於 printf 的用法請教
資深會員
註冊日期:
2004/08/16 13:52
來自 台灣台南
所屬群組:
註冊會員
文章: 778
等級: 25; EXP: 16
HP : 0 / 604
MP : 259 / 27617
離線
可以用sprintf丟到螢幕,感謝版主馬上來試試..

剛才試了一下,sprintf確實是把輸出丟到RAM,但我的測試還是直接輸出到螢幕比較方便 ^^

2012/10/19 13:44
轉換PDF檔 列印


Re: 關於 printf 的用法請教
初級會員
註冊日期:
2018/12/26 15:37
所屬群組:
註冊會員
文章: 24
等級: 3; EXP: 62
HP : 0 / 65
MP : 8 / 247
離線
引用:

Ryang 寫道:
printf( ) 在 PC 是顯示在螢幕上,可是在這微控制器上一般式輸出至 UART 上的。所以在硬體實際上要啟動 UART1 後才可以送出 UART1 的。
使用 SIM 話按照樓上的設定可以將 printf() 的輸出顯示在 Output Window 上的。

你也可以使用 sprintf( ) 將輸出送到 RAM 自己用 Watch Window 檢查輸出結果。


想請問目前只能透過printf這個函式輸出到uart output windows嗎
因為我剛剛使用txreg = data直接用最底層的方式是不行輸出的
都有init uart的設定,使用printf就會輸出但用txreg = data不行

在data為純數值,我送hex或是ascii都沒有顯示(+'0')到輸出視窗,想問看看是不是要看到資料送到uart output windows只能使用printf了
因為使用printf在時間掌握會比較困難想說自寫底層的方式去輸出

1月13日 00:22:30
轉換PDF檔 列印


Re: 關於 printf 的用法請教
版主
註冊日期:
2004/04/30 10:53
來自 CAE, Microchip
所屬群組:
站務管理者
註冊會員
MICROCHIP
文章: 15016
等級: 73; EXP: 85
HP : 1092 / 1821
MP : 5005 / 84845
離線
printf( ) 是依據 studio.h 的導向看將輸出到哪個周邊的。基本上他是上一層的韌體,你使用的是暫存器的直接操作這與 printf () 函數無關,而且暫存器的直接操作還擁有最大的權限。
我猜你可能是沒檢查到 TRMT bit (或 TXIF) 導致資料被覆蓋了。
底下是我早期使用 C18 (沒有 printf( ))的函數支援所撰寫的方式,這是C18 所提供的 UART 的基本函數,參考看看:

while (BusyUSART( )); // 檢查資料是否船算完畢?
WriteUSART(Temp_HEX) ; // 自 UART1 傳送資料出去


char Busy1USART(void)
{
if(!TXSTA1bits.TRMT) // Is the transmit shift register empty
return 1; // No, return FALSE
return 0; // Return TRUE
}

void Write1USART(char data)
{
if(TXSTA1bits.TX9) // 9-bit mode?
{
TXSTA1bits.TX9D = 0; // Set the TX9D bit according to the
if(USART1_Status.TX_NINE) // USART1 Tx 9th bit in status reg
TXSTA1bits.TX9D = 1;
}

TXREG1 = data; // Write the data byte to the USART2
}

1月13日 11:37:37
轉換PDF檔 列印


Re: 關於 printf 的用法請教
初級會員
註冊日期:
2018/12/26 15:37
所屬群組:
註冊會員
文章: 24
等級: 3; EXP: 62
HP : 0 / 65
MP : 8 / 247
離線
版大您說的檢查TRMT旗標在實際的硬體周邊是需要這麼作沒錯。
我想問的是在Simulator上的UART IO Windows顯示部分是否僅能透過printf()才能實現。
因為UART IO Windows提到UART IO才想說使用底層韌體的方式寫到U1TXREG上面

模擬的晶片是dspic33EP512MU810
complier: xc16
MPLAB X IDE v5.30

以下不行,加入等TRMT旗標結果是一樣的

U1TXREG
= CLOCK1.Second /10 +'0';
U1TXREG = CLOCK1.Second %10 +'0';

以下可以顯示至輸出結果

printf
("%02d:%02d:%02dn",CLOCK1.Hour,CLOCK1.Minute,CLOCK1.Second);

1月13日 12:26:40
轉換PDF檔 列印


Re: 關於 printf 的用法請教
版主
註冊日期:
2004/04/30 10:53
來自 CAE, Microchip
所屬群組:
站務管理者
註冊會員
MICROCHIP
文章: 15016
等級: 73; EXP: 85
HP : 1092 / 1821
MP : 5005 / 84845
離線
這單純是你在做運算時的型別錯誤所致,自己用一下 Debug + Break 的除錯方式就可找出問題所在。

底下是之前寫的 Hxe Code 轉成 2-bytes 的 ASCII Code 的程式參考看看。


void VT100_puthex
(unsigned char HEX_Val)
{
    
unsigned char Temp_HEX ;

    
Temp_HEX = (HEX_Val >> 4) & 0x0f ;

    if (
Temp_HEX > 9 )Temp_HEX += 0x37 ;
    else
Temp_HEX += 0x30 ;
    while (
BusyUSART( ));
    
WriteUSART(Temp_HEX) ;

    
Temp_HEX = HEX_Val  & 0x0f ;
    if (
Temp_HEX > 9 )Temp_HEX += 0x37 ;
    else
Temp_HEX += 0x30 ;
    while (
BusyUSART( ));
    
WriteUSART(Temp_HEX) ;
}

1月13日 12:40:51
轉換PDF檔 列印


Re: 關於 printf 的用法請教
初級會員
註冊日期:
2018/12/26 15:37
所屬群組:
註冊會員
文章: 24
等級: 3; EXP: 62
HP : 0 / 65
MP : 8 / 247
離線
引用:

Ryang 寫道:
這單純是你在做運算時的型別錯誤所致,自己用一下 Debug + Break 的除錯方式就可找出問題所在。

底下是之前寫的 Hxe Code 轉成 2-bytes 的 ASCII Code 的程式參考看看。


void VT100_puthex
(unsigned char HEX_Val)
{
    
unsigned char Temp_HEX ;

    
Temp_HEX = (HEX_Val >> 4) & 0x0f ;

    if (
Temp_HEX > 9 )Temp_HEX += 0x37 ;
    else
Temp_HEX += 0x30 ;
    while (
BusyUSART( ));
    
WriteUSART(Temp_HEX) ;

    
Temp_HEX = HEX_Val  & 0x0f ;
    if (
Temp_HEX > 9 )Temp_HEX += 0x37 ;
    else
Temp_HEX += 0x30 ;
    while (
BusyUSART( ));
    
WriteUSART(Temp_HEX) ;
}


不是型別的關係不過上面程式我有嘗試過,在UART1 IO Windows也是沒有東西
我也嘗試過 U1TXREG = 0 + '0';
照道理要顯示"0"才對,不該什麼都沒有顯示

if (Flag_Timer1_1S == 1) //  1 Second interval
        
{
            
Flag_Timer1_1S = 0;

            
Update_CLOCK1();

            
VT100_puthex(0x35);
            
//printf("%02d:%02d:%02dn", CLOCK1.Hour, CLOCK1.Minute, CLOCK1.Second);
            
CAN_Send_CLOCK_Remote();
            
Time_Buf_to_LCD();

            if (!
PIN_DSW6)
                
CAN_Send_CLOCK_Message();


            switch (
StateMachine) {
                case
0x00:
                    break;
                case
0x01:
                    break;
                case
0x02:
                    break;
                default:
                    break;
            }
        }



/*******************************************************************************
  ce427 main source file

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

  Summary:
    This file consists of functions that send and receive messages through the
    ECAN1 and ECAN2 when the transmitter and receiver are crosswired.

*******************************************************************************/
/*******************************************************************************
Copyright (c) 2012 released Microchip Technology Inc.  All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <xc.h>
#include "ecan1_config.h"
#include "common.h"
#include <stdint.h>
#include "E_Judger_LCM.h"
#include "E_Judger_Definition.h"
#include "timers.h"
#include <stdio.h>
#include <string.h>



// *****************************************************************************
// *****************************************************************************
// Section: File Scope or Global Constants
// *****************************************************************************
// *****************************************************************************
// Instantiate Drive and Data objects for I2C1

union {

    
struct {
        
unsigned DSW1 : 1;
        
unsigned DSW2 : 1;
        
unsigned DSW3 : 1;
        
unsigned DSW4 : 1;
        
unsigned DSW5 : 1;
        
unsigned DSW6 : 1;
        
unsigned : 2;
    };
    
unsigned char ID_Byte;
}
BOARD_ID;

union {

    
struct {
        
unsigned TX0_Busy : 1;
        
unsigned TX1_Busy : 1;
        
unsigned TX2_Busy : 1;
        
unsigned TX3_Busy : 1;
        
unsigned TX4_Busy : 1;
        
unsigned TX5_Busy : 1;
        
unsigned TX6_Busy : 1;
        
unsigned TX7_Busy : 1;
    };
    
unsigned char ALL_Busy;
}
CANTX_FLAG;

struct {
    
unsigned char Hour;
    
unsigned char Minute;
    
unsigned char Second;
}
CLOCK1;

struct {
    
unsigned char Hour;
    
unsigned char Minute;
    
unsigned char Second;
}
EXT_CLOCK1;

union {

    
struct {
        
unsigned int SID;
        
unsigned long EID;
        
unsigned char DLC;
        
unsigned char IDE_BIT;
        
unsigned char DATA_IN;
        
unsigned int DataWord0;
        
unsigned int DataWord1;
        
unsigned int DataWord2;
        
unsigned int DataWord3;
    };

    
struct {
        
unsigned int Null1;
        
unsigned long Null2;
        
unsigned char Null3;
        
unsigned char Null4;
        
unsigned char Null5;
        
unsigned char Byte0;
        
unsigned char Byte1;
        
unsigned char Byte2;
        
unsigned char Byte3;
        
unsigned char Byte4;
        
unsigned char Byte5;
        
unsigned char Byte6;
        
unsigned char Byte7;
    };
}
FRAME_Buffer0;

union {

    
struct {
        
unsigned int SID;
        
unsigned long EID;
        
unsigned char DLC;
        
unsigned char IDE_BIT;
        
unsigned char DATA_IN;
        
unsigned int DataWord0;
        
unsigned int DataWord1;
        
unsigned int DataWord2;
        
unsigned int DataWord3;
    };

    
struct {
        
unsigned int Null1;
        
unsigned long Null2;
        
unsigned char Null3;
        
unsigned char Null4;
        
unsigned char Null5;
        
unsigned char Byte0;
        
unsigned char Byte1;
        
unsigned char Byte2;
        
unsigned char Byte3;
        
unsigned char Byte4;
        
unsigned char Byte5;
        
unsigned char Byte6;
        
unsigned char Byte7;
    };
}
FRAME_Buffer1;

// Define ECAN Message Buffers
__eds__ ECAN1MSGBUF ecan1msgBuf __attribute__((space(eds), aligned(ECAN1_MSG_BUF_LENGTH * 16)));

// *****************************************************************************
// Section: Static Function declaration
// *****************************************************************************
void OscConfig(void);
void ClearIntrflags(void);
void Ecan1WriteMessage(void);
void CAN_Send_CLOCK_Message(void);
void CAN_Send_CLOCK_Remote(void);

void ADC_Buf_to_LINE2(void);

void CAN_Send_VR1_Message(void);


void LINE_12_Initial(void);
void LINE_12_Write_Default(void);

void SEVEN_SEGMENT_SCAN(void);
void IO_ADC_Config(void);

void Time_Buf_to_LINE1(void);
void Time_Buf_to_LINE2(void);
void ADC_Buf_to_LCD(void);
void Time_Buf_to_LCD(void);

void UART1_Initial(void);

void BOARD_ID_Get(void);
void Update_CLOCK1(void);

void EXT_CLOCK_to_LINE1(void);
void EXT_ADC_Buf_to_LINE2(void);
void Check_CAN_FIFO(void);

char Busy1USART(void);

void Write1USART(char data);

void VT100_puthex(unsigned char HEX_Val);

#define MY_CANID    0x200
#define CLOCK_ID    0x200
#define VR1_ID          0x208

const unsigned long ID_TABLE[20] = {0x100, 0x110, 0x120, 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190,
    
0x200, 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x270, 0x280, 0x290};
unsigned int StateMachine;
unsigned int RXFUL_Loop;
unsigned int RXFUL_Var;
unsigned int Flag_Timer1 = 0;
unsigned int Flag_Timer1_Counter = 0;
unsigned int Flag_Timer1_1S = 0;
unsigned int Flag_Timer1_200MS = 0;
unsigned int VR1_Value = 0;
unsigned int EXT_VR1_Value = 0;

extern unsigned char FIRST_LINE_Data[6];
extern unsigned char SECOND_LINE_Data[6];
extern const unsigned char SEVEN_SEG_PATTERN[16];


// *****************************************************************************
// Section: Function definition
// *****************************************************************************

/******************************************************************************
* Function:        int main(void)
*
* PreCondition:    None
*
* Input:           None
*
* Output:          None
*
* Side Effects:    None
*
* Overview:        Sets CPU clock and initializes CAN1 and CAN2.
*****************************************************************************/
int main(void) {

    
/* Configure Oscillator Clock Source     */
    
OscConfig();
    
IO_ADC_Config();
    
/* Clear Interrupt Flags                 */
    
ClearIntrflags();

    
BOARD_ID_Get(); //  Read Board ID into BOARD_ID.ID_Byte
    
BOARD_ID.ID_Byte &= 0x0f;

    
LCM_Init();
    
Timer1_Initial();

    
LCM_PutROMString((const unsigned char*) "CAN202C Ex5 x200");
    
LCM_SetCursor(0, 1);
    
LCM_PutROMString((const unsigned char*) "T=              ");
    
LCM_PutASCII('x');
    
LCM_PutHex((MY_CANID >> 8) & 0xff);
    
LCM_PutHex(MY_CANID & 0xff);

    
Ecan1Init();
    
DMA0Init();
    
DMA2Init();

    
UART1_Initial();
    
IEC2bits.C1IE = 1;
    
C1INTEbits.TBIE = 1;
    
C1INTEbits.RBIE = 1;

    
StateMachine = 0;

    
CLOCK1.Hour = 0;
    
CLOCK1.Minute = 0;
    
CLOCK1.Second = 0;
    
CANTX_FLAG.ALL_Busy = 0;

    
LINE_12_Initial();
    
LINE_12_Write_Default();

    while (
1) {
        if (
FRAME_Buffer0.DATA_IN && (FRAME_Buffer0.SID == EXT_CLOCK_ID)) {
            
EXT_CLOCK1.Second = FRAME_Buffer0.Byte1;
            
EXT_CLOCK1.Minute = FRAME_Buffer0.Byte3;
            
EXT_CLOCK1.Hour = FRAME_Buffer0.Byte5;
            
EXT_CLOCK_to_LINE1();
            
FRAME_Buffer0.DATA_IN = 0;
        }

        
Check_CAN_FIFO();
        if (
FRAME_Buffer1.DATA_IN) {
            
EXT_VR1_Value = FRAME_Buffer1.DataWord0;
            
EXT_ADC_Buf_to_LINE2();
            
Nop();
            
Nop();
            
Nop();
            
FRAME_Buffer1.DATA_IN = 0;

        }

        if (
Flag_Timer1 == 1) // 2 ms interval
        
{
            
Flag_Timer1 = 0;
            
SEVEN_SEGMENT_SCAN(); // 7 Segment Display scan routine
        
}

        if (
Flag_Timer1_200MS == 1) {
            
AD1CON1bits.SAMP = 0;
            while (!
AD1CON1bits.DONE);
            
VR1_Value = ADC1BUF0;
            
ADC_Buf_to_LCD();
            if (!
PIN_DSW5)
                
CAN_Send_VR1_Message();
            
Flag_Timer1_200MS = 0;
        }


        if (
Flag_Timer1_1S == 1) //  1 Second interval
        
{
            
Flag_Timer1_1S = 0;

            
Update_CLOCK1();

            
VT100_puthex(0x35);
            
//printf("%02d:%02d:%02dn", CLOCK1.Hour, CLOCK1.Minute, CLOCK1.Second);
            
CAN_Send_CLOCK_Remote();
            
Time_Buf_to_LCD();

            if (!
PIN_DSW6)
                
CAN_Send_CLOCK_Message();


            switch (
StateMachine) {
                case
0x00:
                    break;
                case
0x01:
                    break;
                case
0x02:
                    break;
                default:
                    break;
            }
        }

        
Nop();
    }
}

char Busy1USART(void) {
    if (!
U1STAbits.TRMT) // Is the transmit shift register empty
        
return 1; // No, return FALSE
    
return 0; // Return TRUE
}

void Write1USART(char data) {

    
U1TXREG = data; // Write the data byte to the USART2
}

void VT100_puthex(unsigned char HEX_Val) {
    
unsigned char Temp_HEX;

    
Temp_HEX = (HEX_Val >> 4) & 0x0f;

    if (
Temp_HEX > 9)Temp_HEX += 0x37;
    else
Temp_HEX += 0x30;
    while (
Busy1USART());
    
Write1USART(Temp_HEX);

    
Temp_HEX = HEX_Val & 0x0f;
    if (
Temp_HEX > 9)Temp_HEX += 0x37;
    else
Temp_HEX += 0x30;
    while (
Busy1USART());
    
Write1USART(Temp_HEX);
}

void Check_CAN_FIFO(void) {
    if (
C1FIFObits.FBP != C1FIFObits.FNRB) {
        
RXFUL_Var = C1FIFObits.FNRB;

        
FRAME_Buffer1.SID = (ecan1msgBuf[RXFUL_Var][0] >> 2) & 0x07ff;
        if (
FRAME_Buffer1.SID == EXT_VR1_ID) {
            
FRAME_Buffer1.DataWord0 = ecan1msgBuf[RXFUL_Var][3]; //  Read ADC Value -- It at 1st word
            
FRAME_Buffer1.DATA_IN = 1;
            
Nop();
            
Nop();
        }

        
RXFUL_Var = 0xfffe;
        for (
RXFUL_Loop = 0; RXFUL_Loop < C1FIFObits.FNRB; RXFUL_Loop++)
            
RXFUL_Var <<= 1;
        
C1RXFUL1 &= RXFUL_Var; // Clear RXFUL Flag ...

    
}

}

void Update_CLOCK1(void) {
    
CLOCK1.Second++;
    if (
CLOCK1.Second >= 60) {
        
CLOCK1.Second = 0;
        
CLOCK1.Minute++;
        if (
CLOCK1.Minute >= 60) {
            
CLOCK1.Minute = 0;
            
CLOCK1.Hour++;
            if (
CLOCK1.Hour >= 12)
                
CLOCK1.Hour = 0;
        }
    }
}

// Configure UART1 to work as 19200 bps
//  U1TX = RP99
//  U1RX = RP98

void UART1_Initial(void) {
    
U1MODE = 0x2800;
    
U1STA = 0x0000;
    
U1BRG = 194;

    
RPINR18bits.U1RXR = 98; // set U1RX to RP98  (52)
    
RPOR8bits.RP99R = 1; // RPOR12bits.RP109R = 1 ; set U1TX to RP99        (51)       Function Code for U1TX is 000001       

    
U1STAbits.UTXEN = 1;
    
U1MODEbits.UARTEN = 1;
    
IEC0bits.U1RXIE = 1;

}

void LINE_12_Write_Default(void) {
    
FIRST_LINE_Data[0] = SEVEN_SEG_PATTERN[10];
    
FIRST_LINE_Data[1] = SEVEN_SEG_PATTERN[10];
    
FIRST_LINE_Data[2] = SEVEN_SEG_PATTERN[10];
    
FIRST_LINE_Data[3] = SEVEN_SEG_PATTERN[10];
    
FIRST_LINE_Data[4] = SEVEN_SEG_PATTERN[10];
    
FIRST_LINE_Data[5] = SEVEN_SEG_PATTERN[10];

    
SECOND_LINE_Data[0] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[1] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[2] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[3] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[4] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[5] = SEVEN_SEG_PATTERN[10];
}

void Time_Buf_to_LINE1(void) {
    
FIRST_LINE_Data[0] = SEVEN_SEG_PATTERN[CLOCK1.Hour / 10 ];
    
FIRST_LINE_Data[1] = SEVEN_SEG_PATTERN[CLOCK1.Hour % 10];
    
FIRST_LINE_Data[2] = SEVEN_SEG_PATTERN[CLOCK1.Minute / 10];
    
FIRST_LINE_Data[3] = SEVEN_SEG_PATTERN[CLOCK1.Minute % 10];
    
FIRST_LINE_Data[4] = SEVEN_SEG_PATTERN[CLOCK1.Second / 10];
    
FIRST_LINE_Data[5] = SEVEN_SEG_PATTERN[CLOCK1.Second % 10];
}

void EXT_CLOCK_to_LINE1(void) {
    
FIRST_LINE_Data[0] = SEVEN_SEG_PATTERN[EXT_CLOCK1.Hour / 10 ];
    
FIRST_LINE_Data[1] = SEVEN_SEG_PATTERN[EXT_CLOCK1.Hour % 10];
    
FIRST_LINE_Data[2] = SEVEN_SEG_PATTERN[EXT_CLOCK1.Minute / 10];
    
FIRST_LINE_Data[3] = SEVEN_SEG_PATTERN[EXT_CLOCK1.Minute % 10];
    
FIRST_LINE_Data[4] = SEVEN_SEG_PATTERN[EXT_CLOCK1.Second / 10];
    
FIRST_LINE_Data[5] = SEVEN_SEG_PATTERN[EXT_CLOCK1.Second % 10];
}

void Time_Buf_to_LCD(void) {
    
LCM_SetCursor(2, 1);
    
LCM_PutNumber(CLOCK1.Hour, 2);
    
LCM_PutASCII(':');
    
LCM_PutNumber(CLOCK1.Minute, 2);
    
LCM_PutASCII(':');
    
LCM_PutNumber(CLOCK1.Second, 2);

}

void ADC_Buf_to_LINE2(void) {
    
unsigned int Temp_Value;

    
Temp_Value = VR1_Value;
    
SECOND_LINE_Data[0] = SEVEN_SEG_PATTERN[Temp_Value / 1000 ];
    
Temp_Value = Temp_Value - (1000 * (Temp_Value / 1000));
    
SECOND_LINE_Data[1] = SEVEN_SEG_PATTERN[Temp_Value / 100];
    
Temp_Value = Temp_Value - (100 * (Temp_Value / 100));
    
SECOND_LINE_Data[2] = SEVEN_SEG_PATTERN[Temp_Value / 10];
    
SECOND_LINE_Data[3] = SEVEN_SEG_PATTERN[Temp_Value % 10];
    
SECOND_LINE_Data[4] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[5] = SEVEN_SEG_PATTERN[10];
}

void EXT_ADC_Buf_to_LINE2(void) {
    
unsigned int Temp_Value;

    
Temp_Value = EXT_VR1_Value;
    
SECOND_LINE_Data[0] = SEVEN_SEG_PATTERN[Temp_Value / 1000 ];
    
Temp_Value = Temp_Value - (1000 * (Temp_Value / 1000));
    
SECOND_LINE_Data[1] = SEVEN_SEG_PATTERN[Temp_Value / 100];
    
Temp_Value = Temp_Value - (100 * (Temp_Value / 100));
    
SECOND_LINE_Data[2] = SEVEN_SEG_PATTERN[Temp_Value / 10];
    
SECOND_LINE_Data[3] = SEVEN_SEG_PATTERN[Temp_Value % 10];
    
SECOND_LINE_Data[4] = SEVEN_SEG_PATTERN[10];
    
SECOND_LINE_Data[5] = SEVEN_SEG_PATTERN[10];
}

void ADC_Buf_to_LCD(void) {
    
LCM_SetCursor(12, 1);
    
LCM_PutNumber(VR1_Value, 4);
}

void BOARD_ID_Get(void) {
    if (
PIN_DSW1) BOARD_ID.DSW1 = 0;
    else
BOARD_ID.DSW1 = 1;
    if (
PIN_DSW2) BOARD_ID.DSW2 = 0;
    else
BOARD_ID.DSW2 = 1;

    if (
PIN_DSW3) BOARD_ID.DSW3 = 0;
    else
BOARD_ID.DSW3 = 1;
    if (
PIN_DSW4) BOARD_ID.DSW4 = 0;
    else
BOARD_ID.DSW4 = 1;

    if (
PIN_DSW5) BOARD_ID.DSW5 = 0;
    else
BOARD_ID.DSW5 = 1;
    if (
PIN_DSW6) BOARD_ID.DSW6 = 0;
    else
BOARD_ID.DSW6 = 1;

}


//
//
//void    Delay_for_Time(void)
//{
//    volatile unsigned long    Loop1 ;
//    volatile unsigned long    Loop2 ;
//    
//    for (Loop2 = 0 ; Loop2 < 10; Loop2 ++)
//        for (Loop1 = 0 ; Loop1 < 100 ; Loop1 ++ ) ;
//}

void IO_ADC_Config(void) {
    
ANSELA = 0x0000;
    
ANSELB = 0x0000;
    
ANSELC = 0x0000;
    
ANSELE = 0x0000;
    
ANSELD = 0x0000;
    
ANSELG = 0x0000;

    
DIR_LED1 = 0;
    
DIR_LED2 = 0;
    
DIR_LED3 = 0;
    
DIR_LED4 = 0;

    
AD1CON1 = 0b0001010000000110; // Clearing sample bit (SAMP) ends sampling and start conversion, ASAM = 1
    
AD1CON2 = 0b0000000000000000;
    
AD1CON3 = 0b1000001000000010;
    
AD1CON4 = 0b0000000000000000;
    
AD1CHS123 = 0x00;
    
AD1CHS0 = 0x00; // Select AN0 as Positive INPUT of ADC
    
ANSELBbits.ANSB0 = 1; // Set RB0 as Analog input !
    
AD1CON1bits.ADON = 1;

}

void CAN_Send_CLOCK_Message(void) {
    if (!
C1TR01CONbits.TXREQ0) {
        
Ecan1WriteTxMsgBufId(0, CLOCK_ID, 0, 0);
        
Ecan1WriteTxMsgBufData(0, 8, (unsigned int) CLOCK1.Second,
                (
unsigned int) CLOCK1.Minute,
                (
unsigned int) CLOCK1.Hour,
                (
unsigned int) BOARD_ID.ID_Byte);
        
C1TR01CONbits.TXREQ0 = 1;
        
CANTX_FLAG.TX0_Busy = 1;
    }

}

void CAN_Send_CLOCK_Remote(void) {
    if (!
C1TR23CONbits.TXREQ2) {
        
Ecan1WriteTxMsgBufId(2, CLOCK_ID, 0, 0);
        
Ecan1WriteTxMsgBufData(2, 8, (unsigned int) CLOCK1.Second,
                (
unsigned int) CLOCK1.Minute,
                (
unsigned int) CLOCK1.Hour,
                (
unsigned int) BOARD_ID.ID_Byte);
    }

}

void CAN_Send_VR1_Message(void) {
    if (!
C1TR01CONbits.TXREQ1) {
        
Ecan1WriteTxMsgBufId(1, VR1_ID, 0, 0);
        
Ecan1WriteTxMsgBufData(1, 8, VR1_Value,
                (
unsigned int) BOARD_ID.ID_Byte,
                
0x00,
                
0x00);
        
C1TR01CONbits.TXREQ1 = 1;
        
CANTX_FLAG.TX1_Busy = 1;
    }
}

/******************************************************************************
* Function:        void Ecan1WriteMessage(void)
*
* PreCondition:    None
*
* Input:           None
*
* Output:          None
*
* Side Effects:    None
*
* Overview:        This function is used to write the ECAN1 buffer with the
*                  identifiers and the data. Writes the message to be
*                  transmitted.
*****************************************************************************/
void Ecan1WriteMessage(void) {
    
/* Writing the message for Transmission
Ecan1WriteTxMsgBufId(unsigned int buf, long txIdentifier, unsigned int ide, unsigned int remoteTransmit);
Ecan1WriteTxMsgBufData(unsigned int buf, unsigned int dataLength, unsigned int data1, unsigned int data2, unsigned int data3, unsigned int data4);

buf -> Transmit Buffer number

txIdentifier -> SID<10:0> : EID<17:0>

ide = 0 -> Message will transmit standard identifier
ide = 1 -> Message will transmit extended identifier

remoteTransmit = 0 -> Normal message
remoteTransmit = 1 -> Message will request remote transmission

dataLength -> Data length can be from 0 to 8 bytes

data1, data2, data3, data4 -> Data words (2 bytes) each

     */
    
Ecan1WriteTxMsgBufId(0, 0x1FFEFFFF, 0, 0);
    
Ecan1WriteTxMsgBufData(0, 8, 0x1111, 0x1111, 0x1111, 0x1111);

    
Ecan1WriteTxMsgBufId(1, 0x1FFEFFFF, 0, 0);
    
Ecan1WriteTxMsgBufData(1, 8, 0x2222, 0x2222, 0x2222, 0x2222);

    
Ecan1WriteTxMsgBufId(2, 0x1FFEFFFF, 0, 0);
    
Ecan1WriteTxMsgBufData(2, 8, 0x3333, 0x3333, 0x3333, 0x3333);

    
Ecan1WriteTxMsgBufId(3, 0x1FFEFFFF, 0, 0);
    
Ecan1WriteTxMsgBufData(3, 8, 0x4444, 0x4444, 0x4444, 0x4444);

    
Ecan1WriteTxMsgBufId(4, 0x1FFEFFFF, 0, 0);
    
Ecan1WriteTxMsgBufData(4, 8, 0x5555, 0x5555, 0x5555, 0x5555);

    
Ecan1WriteTxMsgBufId(5, 0x1FFEFFFF, 0, 0);
    
Ecan1WriteTxMsgBufData(5, 8, 0x6666, 0x6666, 0x6666, 0x6666);
}

/******************************************************************************
* Function:        void ClearIntrflags(void)
*
* PreCondition:    None
*
* Input:           None
*
* Output:          None
*
* Side Effects:    None
*
* Overview:         Clears all the interrupt flag registers.
*****************************************************************************/
void ClearIntrflags(void) {
    
/* Clear Interrupt Flags */
    
IFS0 = 0;
    
IFS1 = 0;
    
IFS2 = 0;
    
IFS3 = 0;
    
IFS4 = 0;
}

/******************************************************************************
* Function:        void OscConfig(void)
*
* PreCondition:    None
*
* Input:           None
*
* Output:          None
*
* Side Effects:    None
*
* Overview:       This function configures the Oscillator to work at 60MHz.
*****************************************************************************/
void OscConfig(void) {
    
/*  Configure Oscillator to operate the device at 60Mhz
     Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
     Fosc= 8M*60/(2*2)=120Mhz for 8M input clock */
    
PLLFBD = 58; /* M=60 */
    
CLKDIVbits.PLLPOST = 0; /* N1=2 */
    
CLKDIVbits.PLLPRE = 0; /* N2=2 */
    
OSCTUN = 0; /* Tune FRC oscillator, if FRC is used */

    /* Disable Watch Dog Timer */
    
RCONbits.SWDTEN = 0;

    
/* Clock switch to incorporate PLL*/
    
__builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary

    // Oscillator with PLL (NOSC=0b011)
    
__builtin_write_OSCCONL(OSCCON || 0x01); // Start clock switching
    
while (OSCCONbits.COSC != 0b011);

    
// Wait for Clock switch to occur
    /* Wait for PLL to lock */
    
while (OSCCONbits.LOCK != 1) {
    };
}

/******************************************************************************
* Function:      void __attribute__((interrupt, no_auto_psv))_C1Interrupt(void)
*
* PreCondition:  None
*
* Input:         None
*
* Output:        None
*
* Side Effects:  None
*
* Overview:      Interrupt service routine to handle CAN1 Transmit and
*                recieve interrupt.
*****************************************************************************/
void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void) {
    
IFS2bits.C1IF = 0; // clear interrupt flag
    
if (C1INTFbits.TBIF) {
        if (
CANTX_FLAG.TX0_Busy) {
            
CANTX_FLAG.TX0_Busy = 0;
        }
        if (
CANTX_FLAG.TX1_Busy) {
            
CANTX_FLAG.TX1_Busy = 0;
        }

        
C1INTFbits.TBIF = 0;
    }

    if (
C1INTFbits.RBIF) {
        
C1INTFbits.RBIF = 0;

        if (
C1RXFUL1bits.RXFUL4) {
            
FRAME_Buffer0.SID = (ecan1msgBuf[4][0] >> 2) & 0x07ff;
            
FRAME_Buffer0.DLC = ecan1msgBuf[4][2] & 0x001f;
            
FRAME_Buffer0.DataWord0 = ecan1msgBuf[4][3];
            
FRAME_Buffer0.DataWord1 = ecan1msgBuf[4][4];
            
FRAME_Buffer0.DataWord2 = ecan1msgBuf[4][5];
            
FRAME_Buffer0.DataWord3 = ecan1msgBuf[4][6];
            
FRAME_Buffer0.DATA_IN = 1; // Set DATA_IN to indicate data valid !!
            
C1RXFUL1bits.RXFUL4 = 0;
        }
        
Nop();
        
Nop();
        
Nop();

    }
}



//------------------------------------------------------------------------------
//    DMA interrupt handlers
//------------------------------------------------------------------------------

/******************************************************************************
* Function:   void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void)
*
* PreCondition:  None
*
* Input:         None
*
* Output:        None
*
* Side Effects:  None
*
* Overview:      Interrupt service routine to handle DMA0interrupt
*****************************************************************************/
void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) {
    
IFS0bits.DMA0IF = 0; // Clear the DMA0 Interrupt Flag;
}

/******************************************************************************
* Function:   void __attribute__((interrupt, no_auto_psv)) _DMA1Interrupt(void)
*
* PreCondition:  None
*
* Input:         None
*
* Output:        None
*
* Side Effects:  None
*
* Overview:      Interrupt service routine to handle DMA1interrupt
*****************************************************************************/
void __attribute__((interrupt, no_auto_psv)) _DMA1Interrupt(void) {
    
IFS0bits.DMA1IF = 0; // Clear the DMA1 Interrupt Flag;
}
/*******************************************************************************
End of File
*/

附加檔案:



jpg  Annotation 2020-01-13 130917.jpg (362.59 KB)
74412_5e1bf8d693a62.jpg 1920X1080 px

1月13日 12:54:40
轉換PDF檔 列印


Re: 關於 printf 的用法請教
資深會員
註冊日期:
2009/02/26 09:42
所屬群組:
註冊會員
文章: 341
等級: 17; EXP: 24
HP : 0 / 406
MP : 113 / 13195
離線
您的問題如下:

我想問的是在Simulator上的UART IO Windows顯示部分是否僅能透過printf()才能實現。


以下是Microchip Developer Help的說明, 您可以參考一下.

https://microchipdeveloper.com/xc8:console-printing

1月13日 14:00:17
轉換PDF檔 列印



(1) 2 »



無法在此發表文章
可以在此觀看文章
無法回覆文章
無法編輯自己的文章
無法刪除自己的文章
無法發起投票調查
無法在此投票
無法上傳附加檔案
無法不經審核直接發表文章

[進階搜尋]


搜尋
Microchip連結
產品技術問題產品技術支援專線:0800-717718 台北02-25088600 新竹03-5778366 Ext. 8600 高雄07-2137830 MicrochipDIRECT 專線: 07-2137830
Powered by XOOPS © 2001-2012 The XOOPS Project