jfriend
New Member
- Total Posts : 8
- Reward points : 0
- Joined: 2020/10/03 11:46:16
- Location: 0
- Status: offline
Hard-code Table Data In PIC18F Flash During Programming?
I am writing a program in assembly. I've been reading the PIC18F67J60 datasheet about table reads and writes. No problems there. But suppose I want to hard-code some data into flash at program-time, not run-time. Is it possible to do this? For example, let's say I want to send "MICROCHIP PIC18F67J60" over EUSART. To get the string, I could use the typical ADDWF PCL, RETLW routine, but that gets messy if I have a long string to send. It would be much cleaner and more efficient if I could write that string to flash during programming then simply use a TBLRD pointed to that string's location in flash.
|
Equinoxin
New Users
- Total Posts : 61
- Reward points : 0
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 15:59:54
(permalink)
Hi have you looked into using the const type identifier for storing the table in flash? Please refer to the compiler manual for more information on using this.
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:21:03
(permalink)
rashmik Hi have you looked into using the const type identifier for storing the table in flash? Please refer to the compiler manual for more information on using this.
OP is using assembly, not C.
|
jfriend
New Member
- Total Posts : 8
- Reward points : 0
- Joined: 2020/10/03 11:46:16
- Location: 0
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:21:15
(permalink)
I have discovered how to do it, it is actually quite simple. First, indicate the origin point where you want it to be written in the ROM: ORG 0x???? ;example ORG 0x0800 Following that directive, add the hard-code using any number of methods: lbl_single DATA "M","C","U" ;creates three separate words of 004Dh, 0043h, and 0055h lbl_double DATA "MCU" ;creates two words of 434Dh and 0055h, the last one padded with "0"s lbl_hex DATA 0xffff,0xaa55; creates two words in that exact order For the PIC18, the first character is the least significant byte. Writing DATA "MC" will actually write "CM", which will be read that way when using a sequential table read. So when writing data by hand, it must be remembered to swap the two bytes.
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:21:30
(permalink)
jfriend I am writing a program in assembly. I've been reading the PIC18F67J60 datasheet about table reads and writes. No problems there. But suppose I want to hard-code some data into flash at program-time, not run-time. Is it possible to do this? For example, let's say I want to send "MICROCHIP PIC18F67J60" over EUSART. To get the string, I could use the typical ADDWF PCL, RETLW routine, but that gets messy if I have a long string to send. It would be much cleaner and more efficient if I could write that string to flash during programming then simply use a TBLRD pointed to that string's location in flash.
Which assembler? MPASM or PIC-AS?
|
jfriend
New Member
- Total Posts : 8
- Reward points : 0
- Joined: 2020/10/03 11:46:16
- Location: 0
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:23:35
(permalink)
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:29:00
(permalink)
jfriend MPASM.
jfriend For the PIC18, the first character is the least significant byte. Writing DATA "MC" will actually write "CM", which will be read that way when using a sequential table read. So when writing data by hand, it must be remembered to swap the two bytes.
These are the same: data "CHIP" db "CHIP" db 'C', 'H', 'I', 'P'
post edited by 1and0 - 2021/01/15 16:31:37
|
jfriend
New Member
- Total Posts : 8
- Reward points : 0
- Joined: 2020/10/03 11:46:16
- Location: 0
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:46:20
(permalink)
That is correct for PIC16 devices. PIC18 is not the same. I have successfully programmed hard data into ROM and can verify it must be written in 0x3412, 0x7856 format using DATA. Your first two examples will write 0x4843, 0x5049. That equates to "H", "C", "P", "I" if you read them sequentially. The last "db" will write 0x4300, 0x4800, 0x4900, 0x5000. From MPASM help file: "On all families except the PIC18 device family, the first character is in the most significant byte of the word. On the PIC18 device family, the first character is in the least significant byte of the word."
post edited by jfriend - 2021/01/15 16:51:15
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 16:59:43
(permalink)
jfriend That is correct for PIC16 devices. PIC18 is not the same. I have successfully programmed hard data into ROM and can verify it must be written in 0x3412, 0x7856 format using DATA. Your first two examples will write 0x4843, 0x5049. That equates to "H", "C", "P", "I" if you read them sequentially. The last "db" will write 0x4300, 0x4800, 0x4900, 0x5000. From MPASM help file: "On all families except the PIC18 device family, the first character is in the most significant byte of the word. On the PIC18 device family, the first character is in the least significant byte of the word."
Let me repeat again, the followings data "1234" db "1234" db '1', '2', '3', '4'
will generate the SAME opcodes of 0x3231 0x3433
on a PIC18 device.
|
jfriend
New Member
- Total Posts : 8
- Reward points : 0
- Joined: 2020/10/03 11:46:16
- Location: 0
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 17:22:20
(permalink)
I have not tried "db" specifically, I have only read the information from MPASM help file. According to that, your first two examples are correct but the last one will not produce the same result on a PIC18 device. Again, the help file: Example 2: PIC18 Devices db "t", "e", "s", "t", "\n" ASCII: 0x6574 0x7473 0x000a
Are you saying this example from the help file is wrong?
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 17:46:47
(permalink)
jfriend I have not tried "db" specifically, I have only read the information from MPASM help file. According to that, your first two examples are correct but the last one will not produce the same result on a PIC18 device.
Why don't you try it and see for yourself? jfriend Again, the help file: Example 2: PIC18 Devices db "t", "e", "s", "t", "\n" ASCII: 0x6574 0x7473 0x000a
Are you saying this example from the help file is wrong?
It is correct. 't' -> 0x74'e' -> 0x65's' -> 0x73't' -> 0x74'\n' -> 0x0Aso, the opcodes are 0x6574 0x7473 0x000A e t t s \n
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 17:55:48
(permalink)
In another view,
't' -> 0x74 <- lower byte 'e' -> 0x65 <- upper byte 's' -> 0x73 <- lower byte 't' -> 0x74 <- upper byte '\n' -> 0x0A <- lower byte 0x00 <- upper byte, odd number of bytes padded with 0x00
|
ric
Super Member
- Total Posts : 29861
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 20:22:00
(permalink)
jfriend I have not tried "db" specifically, I have only read the information from MPASM help file. According to that, your first two examples are correct but the last one will not produce the same result on a PIC18 device. Again, the help file: Example 2: PIC18 Devices db "t", "e", "s", "t", "\n" ASCII: 0x6574 0x7473 0x000a
Are you saying this example from the help file is wrong?
You are getting yourself confused by jumping between byte views and word views. Stop looking at what the word looks like in memory, and it all cancels out.
To get a useful answer, always state which PIC you are using!
|
teenix
Senior Member
- Total Posts : 87
- Reward points : 0
- Joined: 2017/12/21 13:47:21
- Location: Australia, Melbourne
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/15 23:29:54
(permalink)
Hi, This is pretty basic demo assembler for 18Fxxxx doStrXmit movlw UPPER str_Test movwf TBLPTRU movlw HIGH str_Test movwf TBLPTRH movlw LOW str_Test movwf TBLPTRL strLoop tblrd*+ movf TABLAT,W btfsc STATUS,Z ; is data 0? return ; yes call doXmit ; send Wreg to USART, assumes waits until data sent bra strLoop ; somewhere in ROM str_Test data "MICROCHIP PIC18F67J60", 0x000D ; + CR and 0 string terminator cheers Tony
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/16 10:09:46
(permalink)
teenix strLoop tblrd*+ movf TABLAT,W btfsc STATUS,Z ; is data 0? return ; yes ; snipped str_Test data "MICROCHIP PIC18F67J60", 0x000D ; + CR and 0 string terminator
That will not work as expected for string with an odd number of characters, which your string is. It is better to use escape characters which are also self documenting: data "MICROCHIP PIC18F67J60\r\0"
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/16 11:54:35
(permalink)
DB directive works too: db "MICROCHIP PIC18F67J60", 0x0D, 0 I'd prefer to use the DB directive like this: db "MICROCHIP PIC18F67J60\r\0"
post edited by 1and0 - 2021/01/16 12:25:05
|
teenix
Senior Member
- Total Posts : 87
- Reward points : 0
- Joined: 2017/12/21 13:47:21
- Location: Australia, Melbourne
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/16 21:27:38
(permalink)
Thanks for the heads up, I miss-counted the characters. Still, on the positive side, it is a example of errors that can occur. cheers Tony
|
1and0
Access is Denied
- Total Posts : 12080
- Reward points : 0
- Joined: 2007/05/06 12:03:20
- Location: Harry's Gray Matter
- Status: offline
Re: Hard-code Table Data In PIC18F Flash During Programming?
2021/01/16 21:52:23
(permalink)
teenix Thanks for the heads up, I miss-counted the characters. Still, on the positive side, it is a example of errors that can occur.
I rarely use the DATA directive. I prefer to use the DB directive for Bytes and DW directive for Words, meaning no need to count. ;)
|