• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






Re: 程式的寫法---位元組合成字元
#8
資深會員
資深會員


查看用戶資訊
用函數或巨集都可以作。

函數的方法前面已有。

巨集提供兩種寫法,針對特定情況,很可能還有更好的寫法:
#define Set_Select1(n) if (n & 4) LATBbits.LATB1=1; else LATBbits.LATB1=0; 
        
if (2LATCbits.LATC2=1; else LATCbits.LATC2=0
        if (
1LATDbits.LATD2=1; else LATDbits.LATD2=0;

#define Set_Select2(n) LATBbits.LATB1=(n & 4)?1:0; LATCbits.LATC2=(n & 2)?1:0; LATDbits.LATD2=(n & 1)?1:0;

調用:
3;
    
Set_Select1(x);
    
Set_Select2(x);

    
Set_Select1(5);  // 會產生 expression always true/false warning
    
Set_Select2(5);  // 不會有 warning

要注意這種拆開不同 port 的,不可能同時轉態,一定有先後,請依需要自行決定其 timing 次序

上面的巨集雖然長的樣子像函數,但編譯與執行均與函數不同:

函數是在《執行時》去 call 同一段的函數程式碼。
巨集是在《編譯前》就作展開,程式碼不共用,沒有(也不用)呼叫函式。(這對 PIC 可省掉使用一層堆疊)

發表於: 2008/3/7 11:43
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式的寫法---位元組合成字元
#7
初級會員
初級會員


查看用戶資訊
非常感謝版主和C_H_M的想法~

發表於: 2008/3/6 8:28
數位地球人
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式的寫法---位元組合成字元
#6
版主
版主


查看用戶資訊
不同的 PORT 很難做到。
同意 C_H_M 的作法,加一票。

發表於: 2008/3/5 16:25
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式的寫法---位元組合成字元
#5
中級會員
中級會員


查看用戶資訊
你好 !

我個人覺得, 要能完全符合你如此敘述的做法實在是難以實現.
你所期望的結果應是無法用單一個結構的方法來實現的,
建議你改使用函式的方法來達到你所希望的結果.

(因為硬體已經是成品呈現在那裡了.)
(而且其中的輸出並不包含可選擇輸出來源的選擇器線路在.)

以函式的方式來達成這樣的結果如下:

#define Sel0 LATBbits.LATB1
#define Sel1 LATCbits.LATC2
#define Sel2 LATDbits.LATD3
void SET_SELECT(unsigned int select)
{
 
Sel0 select 1;
 
Sel1 = (select >> 1) & 1;
 
Sel2 = (select >> 2) & 1;
}
         ...
         ...
         ...
         ...
   [
color=CC0000]Sel0 1;[/color]
   [
color=CC0000]Sel1 0;[/color]
   [
color=CC0000]Sel2 1;[/color]
   [
color=CC0000]SET_SELECT(5);[/color]


發表於: 2008/3/5 14:34
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式的寫法---位元組合成字元
#4
初級會員
初級會員


查看用戶資訊
經過一陣子的思考,了解了結構的用法,我還是想不通該有的寫法,容我再描述一次需求,請高手指點。

令三個不同或相同埠(port)PIO做Output,例如RB1、RC2、RD3。
我可以各別定義
#define Sel0 LATBbits.LATB1
#define Sel1 LATCbits.LATC2
#define Sel2 LATDbits.LATD3
然後針對任一output輸出Hi或Lo
Sel0=1; //RB1為Hi輸出
Sel1=0; //RC2為Lo輸出

但是如果我想要定義這三個Output是有相關的,
例如某個變數Var為2,則此時
RB1=0;RC2=1;RD3=0;該怎樣定義此變數Var呢?

結構我看過了,它可以針對結構變數的某一bit做設定
Var.bit0=0;這個我了解
但是跟怎樣定義,才能將結構變數裡的單一bit指派為RBx呢?
謝謝~

發表於: 2008/3/5 13:54
數位地球人
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式的寫法---位元組合成字元
#3
初級會員
初級會員


查看用戶資訊
好的,我會往這方面來思考。

發表於: 2008/2/23 9:54
數位地球人
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式的寫法---位元組合成字元
#2
版主
版主


查看用戶資訊
可以用位元結構的方式去定義,可以參考 元件的H檔案格式,有很多的周邊都是用幾個位元合並成一個結構變數的。
請參考 SR Register<IPL2:IPL0> 的宣告或IPC0 暫存器裡對三個位元的IP宣告。

p30f4011.h
:
:
/* IPC0: Interrupt Priority Control Register 0 */
extern volatile unsigned int IPC0 __attribute__((__sfr__));
typedef struct tagIPC0BITS {
        
unsigned INT0IP :3;
        
unsigned        :1;
        
unsigned IC1IP  :3;
        
unsigned        :1;
        
unsigned OC1IP  :3;
        
unsigned        :1;
        
unsigned T1IP   :3;
        
unsigned        :1;
IPC0BITS;
extern volatile IPC0BITS IPC0bits __attribute__((__sfr__));


寫法: IPCbits.T1IP=7; //將Timer1 的中斷優先權設為 7

發表於: 2008/2/22 16:59
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


程式的寫法---位元組合成字元
#1
初級會員
初級會員


查看用戶資訊
各位好
我目前在使用C30 Compiler,有一個語法或技巧不清楚該怎麼做會比較好,請有經驗的人指點,謝謝。

定義
#define Sel0 LATBbits.LATB0
#define Sel1 LATBbits.LATB3
#define Sel2 LATBbits.LATB8

其中PortB的第0、3、8是輸出,而Sel0、Sel1、Sel2是我給它定的名字。
Sel0是我要的第0個位元,Sel1是第1個位元,Sel2是第2個位元
如果要輸出“3”,則要寫成
Sel0 = 1;
Sel1 = 1;
Sel2 = 0;
這樣程式讀起來會較不順,而且要花3行。

可不可以有一種寫法,可以寫成
Select = 3;
就可以完成?謝謝

發表於: 2008/2/22 16:48
數位地球人
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------