Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
你好:
了解,Thanks.
發表於: 2008/11/7 13:44
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
大家好:
1.Hitec PICC用的是16F914,但我不知ram<256會怎樣(沒試過). 2.原本16 bits的指標,我想變成2個8 bits指標, 而且16 bits跟2個8 bits都要可以使用,所以我 想到用union 3.結果用C18卻發生8 bits不能使用的狀況! 而ptr0和ptr1應該顯示為8 bits但卻顯示16 bits, 所以我不知是何原因? 4.構想為ptr1為ram bank切換,而ptr0為offset. 感謝大家的回覆,煩請指正,Thanks.
發表於: 2008/11/7 12:43
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
很好奇
開版大用的是哪個PIC型號, 哪個C compilier ? 不知道對於ram小於256byte的 PIC Hitech PICC 的指標是用1byte還是2byte ?
發表於: 2008/11/7 11:09
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
如前所述,指標存放的是變數的位址,要提醒的是: 如果指標只有 8 bit, 只能定址到 256 個地址的能力。
PIC 是 Harvard 架構,RAM 與 Program 存在於不同的空間,word size 不同,定址範圍也不相同,要分別考慮。 除非是一些容量很小的 PIC,否則 8 bit 的指標實在不夠用。 以 Hitech PICC (for PIC16 以下)為例,該編譯器所實作的指標如下: 3.4.11 Pointer Types The format and use of pointers depend upon the range of processor selected. This is described in the following sections. 3.4.11.1 Baseline Pointers All pointers in the Baseline (12-bit PICs) range are 8-bits. • RAM pointers point to RAM using the FSR index register. • Const pointers point to ROM via a RETLW table. • Function Pointers reference functions. A function is jumped to rather than called. A jump table is used to return control to the calling function. 3.4.11.2 Midrange Pointers All pointers for Midrange processors (14-bit PICs) are the same as for Baseline processors with the following exceptions: 50 C Language Features Supported Data Types and Variables • RAM pointers are 8-bits and therefore can only access 256 bytes. A pointer which is unqualified or has a bank 1 qualifier can point to any object in bank 0 or bank 1. If the pointer is qualified as bank 2 or bank 3, then it can access any object in banks 2 or 3. At present, there is no general purpose pointer that can read and write to all four banks. • Const pointers are 16-bits wide. They can be used to access either ROM or RAM. If the upper bit of the const pointer is non-zero, it is a pointer into RAM. A const pointer may be used to read from any RAM location in any bank, but writing to such locations is not permitted. If upper bit is zero, it is a pointer able to access the entire ROM space. The ability of this pointer to access both ROM and RAM is very useful in string-related functions where a pointer passed to the function may point to a string in ROM or RAM. • Function pointers reference functions. A function is called using the address assigned to the pointer. 3.4.11.3 Highend Pointers Highend processors (17Cxxx processors) differ in many ways from baseline and midrange processors and this effects the way pointers have been implemented. • RAM banks in highend processors are 0xFF bytes long, so an 8-bit RAM pointer can only access objects in one bank. Thus a bank 0 RAM pointer can only access bank 0; a bank 1 RAM pointer can only access bank 1, etc... • Const pointers for highend processors are 16-bits wide. Const pointers on highend processors work in the same way as they do for midrange processors. • Far pointers are 16-bits wide and can be used to access objects in any available bank of RAM. These pointers are similar to const pointers; they differ in that they may be used to indirectly read from, or write to, RAM locations. Far pointers can also access ROM locations as per const pointers. The far qualifier is used to specify the larger pointer size. • Function pointers reference function. A function is called using the address assigned to the pointer 至於 PICC18, 可能也有一些類似情形,我手上沒裝,確實情形請到 C:\Program Files\HI-TECH Software 下找一下 manual 查查看。 另附網路上找到的一篇 Comparison of Hi-Tech PICC-18 and MPLAB C18: http://www.xargs.com/pic/picc18-vs-c18.html
發表於: 2008/11/7 10:35
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
版主
|
如果在 如果在 C18 下的 RAM 指摽會使用 FSR0 暫存器 (13-bit) 來當作 RAM 的指標。
uns8* A; 是宣告一個 A 的指標,是屬於指向 unsigned char 的指標。這點你好像誤會了。
發表於: 2008/11/7 8:11
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
謝謝指教,感恩.
我想我測試出來應該是: ram_addr=0x500; uns8* A; A=&ram_addr; 雖然A是8 bits,但編譯器會強制變為16 bits, 所以0x05為不可變位址,而只能更改0x00~0xff, 這是我覺得奇怪的地方!反正就是只能這樣用了, Thanks.
發表於: 2008/11/6 14:46
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
其實也不太明白你的需求
如果是要把 unsigned int* 指標 轉成 unsigned char* 的話 可以用強制型別轉換 但不知是否合你需求 我大概也只會這個了 用結構的話,偶不會~ ex. unsigned char *p; unsigned int *ip; unsigned int ap[5]; ip=ap; *ip=0x1234; p=(unsigned char*)ip; *p=0x78; p++; *p=0x56; 結果,原來的0x1234會被改成 0x5678
發表於: 2008/11/6 14:28
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
大家好:
設ram區塊在bank5(0x500), 而指標ptr為16 bits, 因此ptr++; //(0x502) 所以我想將指標ptr為16 bits, 能夠轉成2 bytes的指標, 我該如何讓ptr++; //(0x501) 煩請告知,Thanks.
發表於: 2008/11/6 14:06
|
|||
|
Re: 請問結構的指標!
|
||||
---|---|---|---|---|
資深會員
|
你好:
謝謝指教. 剛剛我用HTC V9.6顯示是正常的, ptr0和ptr1為byte. 不知為何C18有這問題呢? Thanks.
發表於: 2008/11/6 9:46
|
|||
|