Buffer on UART port for 150 characters

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
nikhil
Posts: 35
Joined: 12 Jul 2009 06:24

Buffer on UART port for 150 characters

#1 Post by nikhil » 20 Jul 2009 09:02

Hi !

I am trying to create a buffer for UART 2 of P24f. The sample application on uart2 works fine..i.e. receive data and transfer data back to TX port.

I use a simple code as under

program Uart2_example

dim X as word[150]
dim y as byte

sub procedure rxb org $0050
dim rx2 as word
dim cnt as byte
cnt=0
if Uart2_Data_Ready = 1 then
rx2=Uart2_Read_Char
uart2_Write_Char(rx2)
X[cnt]=rx2
inc (cnt)
end if
IFS1.14=0
for y=1 to 150
uart2_write_char(x[y])
next y
end sub

main:
IEC1.14=1
Uart2_Init(9600)
while true
delay_ms(1000)
wend
end.

but it doesnt work. I would appreciate if someone could help me out. Thanks in advance.

regards,

NIkhil

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

#2 Post by p.erasmus » 20 Jul 2009 15:58

From you code it looks like you want an interrupt to happen
if so then you have now where in your code enabled the IEC interrupt control register bits for the UART and the IP registers

if you do not want an interrupt tp happen then you need to call the function in your main code but then you need not to specify the ORG $0050 ,

Also if you want to transmit 150 bytes one after another you should check in your code whether the UART is Busy before you try to load the next byte you Transmit loop does not do this


Regards
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

nikhil
Posts: 35
Joined: 12 Jul 2009 06:24

#3 Post by nikhil » 20 Jul 2009 16:16

Hi P.Erasmus,

Thanks for the reply to my post.

Further to the same, I've put IEC1.14=1 just under the main program to enable interrupt. Also, I really dont want to transmit, I want to capture (create a buffer) for any data which is being received in RX register upto 150 bytes / characters since the MCU only has a word fifo and we want to basically create a buffer for the uart port.

I look forward to your advise. Thanks in advance.

regards,

Nikhil

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#4 Post by jpc » 20 Jul 2009 18:00

seems this is still not easy to find : http://www.mikroe.com/forum/viewtopic.php?t=20232 , you may need to adapt buffersize and interrupt vector addresses perhaps
Au royaume des aveugles, les borgnes sont rois.

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

#5 Post by p.erasmus » 20 Jul 2009 18:37

nikhil

To receive a buffer of chars your code should look something like this

Code: Select all

sub procedure rxb org $0050
if (Uart2_Data_Ready = 1) then  'if data in Uart read
rx2 = Uart2_Read_Char     'read and put in Buffer
X[cnt] = rx2
inc (cnt)                 'Increment buffer pointer
end if
IFS1.14=0                 'clear INT flag U2
if (cnt = 149)Then        'check if 150 bytes in buffer
  cnt = 0
end if
end sub
To initialize the controller a better way is using a procedure

Code: Select all

sub procedure Init()
    INTCON1.15 = 1 'nested interupts disabled
    IEC1.14 = 1    'Enable U2RXIE
    IFS1.14 = 0    'Clear Interupt Flag U2RX
    Uart2_Init(2400)
    cnt = 0      ' Clear Rx Buffer pointer
end sub
you program should look something like this

Code: Select all

'********************************************************
program Uart2_example
'********************************************************

'*******************************************************
'Global variables
'*******************************************************
dim X as word[150]
dim rx2 as word
dim y as byte
dim cnt as byte
'*******************************************************

'*******************************************************
'Interrupt Handler U2Rx
'*******************************************************
sub procedure rxb org $0050
if (Uart2_Data_Ready = 1) then  'if data in Uart read
rx2 = Uart2_Read_Char     'read and put in Buffer
X[cnt] = rx2
inc (cnt)                 'Increment buffer pointer
end if
IFS1.14=0                 'clear INT flag U2
if (cnt = 149)Then        'check if 150 bytes in buffer
  cnt = 0
end sub
'**********************************************************
'This procedure initializes the micro
'**********************************************************
sub procedure Init()
    INTCON1.15 = 1 'nested interupts disabled
    IEC1.14 = 1    'Enable U2RXIE
    IFS1.14 = 0    'Clear Interupt Flag U2RX
    Uart2_Init(2400)
    cnt = 0      ' Clear Rx Buffer pointer
end sub

'**********************************************************
'Main Code Loop
'**********************************************************
main:

     Init()
     
while True
     nop
     
wend
end.
'***********************************************************
my code is not sending any Char you have to send a Char from the main loop to start the Intterrupt and you have to send 150 chars
if you place a break point inside the interrupt and then send the char your debugger should then stop at the break point in the interrupt indicating the interrupt is fired ,
Hope this helps you
regards
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

nikhil
Posts: 35
Joined: 12 Jul 2009 06:24

#6 Post by nikhil » 21 Jul 2009 02:53

Hi P.Erasmus,

Thanks for the code. I will try it today and report on functioning.

regards,

Nikhil

nikhil
Posts: 35
Joined: 12 Jul 2009 06:24

#7 Post by nikhil » 21 Jul 2009 03:03

Thx jpc ..I am actually migrating from Cubloc to Pic. Cubloc handles buffers and interrupts very well as it support ladder logic which runs parallel to basic code. Here it seems I need to stop other interrupts to process one of them

nikhil
Posts: 35
Joined: 12 Jul 2009 06:24

#8 Post by nikhil » 21 Jul 2009 04:00

Hi P.Erasmus,

I tried the code but somehow no luck. So I tried to use the ICD and when it comes to the Uart2_init(9600), I get a Warrning Message stating source file not found: _lib_uart2_p24_p33.dpas would you like to perform a step over instead?

I checked with support also on this error and they say simply ignore. Also, I am using a LV23-33A dev. board but all samples are for LV23-33, I think there is some compatibility issues since LCD / GLCD samples also dont run.

I modfied the code slightly by adding the line

uart2_write_text("o")

sub procedure rxb org $0050
if (Uart2_Data_Ready = 1) then 'if data in Uart read
rx2 = Uart2_Read_Char 'read and put in Buffer
X[cnt] = rx2
inc (cnt) 'Increment buffer pointer
end if
if (cnt = 149)Then 'check if 150 bytes in buffer
cnt = 0
end if
uart2_write_text("0")
IFS1.14=0
end sub

to enable me confirm the interrupt is working, but I get no receive data on terminal.

regards,

Nikhil

nikhil
Posts: 35
Joined: 12 Jul 2009 06:24

#9 Post by nikhil » 21 Jul 2009 04:23

Hi P.Erasmus,

My mistake. The codes works fine..Thanks.

I had the SW4 on wrong position.

Thanks once again.

regards,

Nikhil

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

#10 Post by p.erasmus » 21 Jul 2009 13:57

Great Happy that you solved your problem


nikhil wrote
I am actually migrating from Cubloc to Pic.
Now you are starting to work with real embedded controller
and at a high level
a dsPIC is no toy you will realize this as you start to understand them better ,you can run thins in parallel use a RTOS system

regards
P.E
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”