• 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: C30 變數型別轉換問題
#10
高級會員
高級會員


查看用戶資訊
謝謝以上先進回覆
如 biko 所言
我好像也看過別的文件上有說明,
只是想試試看有沒有比較簡潔的寫法
我看也只好用一個暫存變數來處理了

我是在寫 Modbus RTU Slave 端程式
再30f4013內部開一塊 unsigned char 記憶體
可是我的資料也可能是 int or char or long
如果能用指標直接存取,或需是比較方便的多

發表於: 2009/10/3 2:20
Austin
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#9
資深會員
資深會員


查看用戶資訊
您好,您這個問題我在去年就問過了,不過也沒有得到答案。

給您網址參考一下。
http://www.microchip.com.tw/modules/n ... t_id=22068#forumpost22068

我的總結是因為您用的是16BITS的CPU,所以才會有這個問題,如果您是在8BITS的CPU的話,就沒有這個問題了。

我在寫電腦端程式時也是遇到相同的問題,這時候我的解決方式是

unsigned int uiData1 ;
unsigned int uiData2 ;

unsigned char HR[64] ;
unsigned char tmp[2];

tmp[0] = HR[1];
tmp[1] = HR[2];


uiData1 = *( (unsigned int *)&HR[0] ) ;
//uiData2 = *( (unsigned int *)&HR[1] ) ;
uiData2 = *( (unsigned int *)tmp ) ;

因為是16bits的MCU,所以陣列[]裡對單數的INDEX是會把它當成是雙數的。

例如宣告一個陣列
unsigned char a[3];//看起來會用到記憶體3bytes,但是實際上卻是4bytes

又定義一個struct

typedef struct{
unsigned char a;
unsigned int i;
}struct_a;

sizeof(a)會= 4bytes而不是3bytes

因為是16 bits MCU的關系,所以Compiler會在unsigned char a之後再放入1 byte的不用空間。

所以如果您定義了以上的struct
然後用指標的方式存取它:

struct_a a;
unsigned char *p = (unsigned char *)&a;

a.a=0x12;
a.i=0x1234;


p[2] = 會等於少?(Ans 會=0x34, 而不是0x12)

所以為了防止這種錯誤,在16bits的 MCU在定義struct時要這樣做
typedef struct{
unsigned char a;
unsigned char b;//雖然這個用不到
unsigned int i;
}a;

這樣您在用指標的方式存取這個struct時,才不會出錯。

反正,只要是在16 bits上的MCU您就要小心記憶體都要應該成雙成對的,不可以折散它們,就算您硬要折散,Compiler還是會保留一個空間,讓記憶體都是成雙成對的。







參照:

austin1211 寫道:
請教線上先進,
我的程式碼在使用取得 uiData2 會發生位址錯誤中斷

unsigned int uiData1 ;
unsigned int uiData2 ;

unsigned char HR[64] ;


uiData1 = *( (unsigned int *)&HR[0] ) ;
uiData2 = *( (unsigned int *)&HR[1] ) ;

請教要如何修改

發表於: 2009/10/2 18:17
我相信解決問題的方法不只一種,所以我在回答同好的問題時或者提出與主題不同的方案,
請不要以此做為攻擊的目標,畢竟我也只是想和大家討論如何解決問題而已…
解決問題最重要,.....
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#8
資深會員
資深會員


查看用戶資訊
順便一提.....
當你在要同時用一個int (16bits), 及二個 char (8bits, hi byte ,lo byte)

一般正規的做法是用union來做
union {
  unsigned int uiData;
  unsigned char HR[2];
  } combin;

bombin.uiData 與 bombin.HR[0], bombin.HR[1]
使用同一塊記億體....

只是不知你的應用是什麼...

發表於: 2009/10/2 9:56
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#7
資深會員
資深會員


查看用戶資訊
MPLAB裡面有個好用的工具 SIM

自己試試吧 !!!

發表於: 2009/10/2 8:40
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#6
高級會員
高級會員


查看用戶資訊
謝謝以上前輩的解答

p=(unsigned int*)&a[1];
c=*p;

會有問題嗎
我希望得到 c = 0x5634

發表於: 2009/10/2 2:58
Austin
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#5
資深會員
資深會員


查看用戶資訊
unsigned char a[4];
unsigned int c,*p;


void main (void)
{

a[0]=0x12;
a[1]=0x34;
a[2]=0x56;
a[3]=0x78;

p=(unsigned int*)&a[0];
c=*p; //c=0x3412

p=(unsigned int*)&a[2];
c=*p; //c=0x7856

}

發表於: 2009/10/1 18:11
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#4
資深會員
資深會員


查看用戶資訊
不介意的話,條條道路通羅馬....
uiData1 = (HR[1] <<8) + HR[0];
uiData2 = (HR[2] <<8) + HR[1];

發表於: 2009/10/1 17:54
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#3
高級會員
高級會員


查看用戶資訊
我不是要直接型別轉換
假如
HR[0] = 0x01 ;
HR[1] = 0x02 ;
HR[2] = 0x03 ;
HR[3] = 0x04 ;
經過
uiData1 = *( (unsigned int *)&HR[0] ) ;
uiData2 = *( (unsigned int *)&HR[1] ) ;

我希望
uiData1 = 0x0201
uiData2 = 0x0302

有辦法嗎??

發表於: 2009/10/1 16:02
Austin
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C30 變數型別轉換問題
#2
資深會員
資深會員


查看用戶資訊
如果是"變數型別"轉換

應該這樣就好了吧!!!
uiData1 = (unsigned int)HR[0];
uiData2 = (unsigned int)HR[1];

好像不轉也可以
uiData1 = HR[0];

發表於: 2009/9/30 19:03
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


C30 變數型別轉換問題
#1
高級會員
高級會員


查看用戶資訊
請教線上先進,
我的程式碼在使用取得 uiData2 會發生位址錯誤中斷

unsigned int uiData1 ;
unsigned int uiData2 ;

unsigned char HR[64] ;


uiData1 = *( (unsigned int *)&HR[0] ) ;
uiData2 = *( (unsigned int *)&HR[1] ) ;

請教要如何修改

發表於: 2009/9/30 17:55
Austin
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... ]

教育訓練中心

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