Albert 0 Report post Posted March 29, 2017 Hi, I found that the index of a far array is only calculated with an 8-bit addition without considering the overflow. You will find a code example and the resulting assembler file in the attachment. I think this is an error, at least in the documentation this is not mentioned. Best regards, PS attaching a .asm or .casm don't work, so you will find it below: #include "PIC16F1847.h" unsigned char uc_arr[192] @0x2270; void main(void) { unsigned char cnt; for(cnt = 0; cnt < 192; cnt++) { 0004 0020 MOVLB 0x00 0005 01A0 CLRF main_1_cnt 0006 label1 0006 30C0 MOVLW 0xC0 0007 0220 SUBWF main_1_cnt, W 0008 1803 BTFSC STATUS,C 0012 0AA0 INCF main_1_cnt, F 0013 2806 GOTO label1 uc_arr[cnt] = cnt; 000A 3022 MOVLW HIGH(gbl_uc_arr) 000B 0085 MOVWF FSR0H 000C 3070 MOVLW LOW(gbl_uc_arr+D'0') 000D 0084 MOVWF FSR0L 000E 0820 MOVF main_1_cnt, W 000F 0784 ADDWF FSR0L, F 0010 0820 MOVF main_1_cnt, W 0011 0080 MOVWF INDF0 } } 0009 0008 RETURN //////////////////////////////////////// // Code with no source :-) //////////////////////////////////////// 0000 2814 GOTO _startup 0014 _startup 0014 3180 MOVLP 0x00 0015 2804 GOTO main TestFarArray.c Quote Share this post Link to post Share on other sites
JorgeF 0 Report post Posted April 26, 2017 Hi you can use a pointer as a workaround, it generates correct code. #include <system.h> unsigned char uc_arr[192]@0x2270; void main(void) { unsigned char cnt; unsigned char *ptr = uc_arr; for(cnt = 0; cnt < 192; cnt++) { *ptr++ = cnt; } } generated code (lines reordered in program flash address order) //////////////////////////////////////// // Code with no source :-) //////////////////////////////////////// 0000 2819 GOTO _startup for(cnt = 0; cnt < 192; cnt++) { 0009 01A0 CLRF main_1_cnt 000A label1 000A 30C0 MOVLW 0xC0 000B 0220 SUBWF main_1_cnt, W 000C 1803 BTFSC STATUS,C 000D 0008 RETURN *ptr++ = cnt; 000E 0822 MOVF main_1_ptr+D'1', W 000F 0085 MOVWF FSR0H 0010 0821 MOVF main_1_ptr, W 0011 0084 MOVWF FSR0L 0012 0AA1 INCF main_1_ptr, F 0013 1903 BTFSC STATUS,Z 0014 0AA2 INCF main_1_ptr+D'1', F 0015 0820 MOVF main_1_cnt, W 0016 0080 MOVWF INDF0 0017 0AA0 INCF main_1_cnt, F 0018 280A GOTO label1 } } 0019 _startup 0019 3180 MOVLP 0x00 001A 2804 GOTO main Quote Share this post Link to post Share on other sites
JorgeF 0 Report post Posted April 26, 2017 Hi The problem must be related to the index size (-idx) of the compiler and linker command lines. But the option for an index size of 2 bytes is only available on the PIC18 compiler (large memory model). Looks like this RAM access improvement on the PIC16F1xxx family as created a problem for the PIC16 compilers. The pointer workaround works because the maths are done on the pointer variable and not on the indexing SFRs. Just my 2 cents on guessing... Best regards Jorge Quote Share this post Link to post Share on other sites