Jump to content
Sign in to follow this  
Fandu

Help With Array Indexing

Recommended Posts

Hey, I'm trying to learn to use Basic on the PIC and seem to be having trouble with array indexing and/or passing arrays to functions. I'm trying to write a simple program that will accept up to 20 characters from a PC over RS232, then simply echo those back when Enter is pressed. I'm getting compiler several compiler errors with this code:

 

Sub prompt()
call usart_tx(126)								' Print "-"
call usart_tx(62)								' Print ">"
End Sub

Sub print_str( msg(20) As Char, j As Integer)
Dim k As Integer								' k - string position counter
k = 0
Do while k < j
	call usart_tx( msg(k) )						' Print out each character.  *** ERROR HERE ***
k = k + 1
Loop
End Sub


sub main()
Dim msg_input(20) As Char						' Initialize 20 character input buffer
Dim msg_i2caddr As String (40)
Dim msg_readmem As String (40)
Dim i As Integer								' i = # of characters in buffer
i = 0
msg_i2caddr = "Set I2C Device Address = "		' Random strings
msg_readmem = "Read IC2 Memory"


call usart_init()       						'Initialize usart

Do while 1
	msg_input(i) = call usart_rx()
	If ( msg_input(i) = 13 ) Then				' If carrage return
		call print_str( msg_input, i)			' Print out the string  *** ERROR HERE ***
		i = 0									' Reset string length
		call usart_tx(10)						' Line Feed
		call prompt()
	End If
	i = i + 1

Loop

end sub

 

I'm using Punerjot Mangat's USART library. Target device is 16F877A @ 20Mhz

Share this post


Link to post
Share on other sites
Hey, I'm trying to learn to use Basic on the PIC and seem to be having trouble with array indexing and/or passing arrays to functions.  I'm trying to write a simple program that will accept up to 20 characters from a PC over RS232, then simply echo those back when Enter is pressed.  I'm getting compiler several compiler errors with this code:

I've just checked this out and there are a few problems with strings and character arrays :(

These problem need fixing.

 

The best option for you little application is to create a global char array and load this with characters as they come and, then call another function that displays that string once all the characters are received.

 

Regards

Dave

Share this post


Link to post
Share on other sites

OK, I modified the code to just use a global array and everything compiles fine. Unfortunately it doesn't work :( I modified the USART routine to set it up for a 20MHz processor and 9600 baud ( spbrg = 32). All I'm getting out of the serial port when I run the program is 0x01. Is there any obvious reason for that?

 

Also, I am using the Bloater boot loader, which needs the code origin at 0x0004. I noticed that SourceBasic is ignoring the pragma directive... How to I control code locations in SourceBasic?

 

Thanks

 

'	I2C - Serial Test Controller
'	Uses PIC16F877A @ 20Mhz

#pragma BOOTLOADER 0004


Dim msg_input(20) As Char						' Initialize 20 global character input buffer

Sub prompt()
call usart_tx(126)								' Print "-"
call usart_tx(62)								' Print ">"
End Sub

Sub print_str(j As Integer)
Dim k As Integer								' k - string position counter
k = 0
Do while k < j
	call usart_tx( msg_input(k) )						' Print out each character
	k = k + 1
Loop
End Sub




sub main()
call prompt()
Dim msg_i2caddr As String (40)
Dim msg_readmem As String (40)
Dim i As Integer								' i = # of characters in buffer
i = 0
msg_i2caddr = "Set I2C Device Address = "		' Random strings
msg_readmem = "Read IC2 Memory"


call usart_init()       						'Initialize software rs232

Do while 1
	msg_input(i) = call usart_rx()
	If ( msg_input(i) = 13 ) Then				' If carrage return
		call print_str( i)						' Print out the string
		i = 0									' Reset string length
		call usart_tx(10)						' Line Feed
		call prompt()
	End If

	If ( i = 19 ) Then							' If we have 20 characters
		call print_str( i)						' Print out the string
		i = 0									' Reset string length
		call usart_tx(10)						' Line Feed
		call prompt()
	End If


	i = i + 1

Loop

end sub

Share this post


Link to post
Share on other sites

Fandu,

Also, I am using the Bloater boot loader, which needs the code origin at 0x0004.  I noticed that SourceBasic is ignoring the pragma directive...  How to I control code locations in SourceBasic?

Use linker option -rb 0x004 (compile options -> extra linker options).

 

Regards

Dave

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...