USART Data Format

General discussion on mikroPascal.
Author
Message
Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

USART Data Format

#1 Post by Snake4 » 29 Apr 2009 15:02

Hi,

I have a little trouble getting USART to work.

Code: Select all

while true do
begin

if (USART_Data_Ready()) then
    begin

         i := USART_Read();
         PORTC.2 := true; // LED2

         if (i = 0x30) then
            PORTC.3 := true; // LED3
         if (i = 10) then
            PORTC.3 := true; // LED3
    end;

end;
This is the code I am using. If I am sending some Data, LED2 is turned on but I can't get LED3 to flash.
I tried this with PICKit2 USART Tool. With it you can send Hex or ASCII.
Can someone tell me what I would have to send to get LED3 to flash?

What exactly is stored in my i? How much?

A small problem might be that I havn't connected the TX and don't have a Debugger so I can't find out these things.

Snake

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#2 Post by Dany » 29 Apr 2009 16:36

Code: Select all

if (i = 0x30) then
            PORTC.3 := true; // LED3
         if (i = 10) then
            PORTC.3 := true; // LED3 <--- should be "false"?
Led3 won't "flash": it is turned on twice, never turned off.
Can someone tell me what I would have to send to get LED3 to flash?
Looking at your code, you should send hexadecimal values $30 and $0A (= 10 dec) to control Led3 (after repairing the above).
What exactly is stored in my i? How much?
I assume I is a byte or a character. It will hold the value that is received from the uart, So, if you send $30 or $0A, it will hold these values of course.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

#3 Post by Snake4 » 29 Apr 2009 18:18

Dany wrote: Led3 won't "flash": it is turned on twice, never turned off.
I am sorry. I didn't post all the code. It is turned off after a few seconds with LED2.
Ich chose the two examples because I wanted to know if both of them should work.?
Dany wrote:
hexadecimal values $30 and $0A (= 10 dec) to control Led3
That's what I tried. Obviously it didn't work. I thought I might have made a mistake or maybe the code won't work this way.
If you are sure it works like this, I will try it with my computer's serial port. Maybe there is a problem with the PICKit Tool.
Dany wrote:
I assume I is a byte or a character. It will hold the value that is received from the uart, So, if you send $30 or $0A, it will hold these values of course.
The help says a byte is returned but what if I send somethin like $AA00BB11EE? Will it hold all of them at once or something like: every time I read it 2 of them?

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#4 Post by Dany » 30 Apr 2009 08:44

Snake4 wrote:
Dany wrote: Led3 won't "flash": it is turned on twice, never turned off.
I am sorry. I didn't post all the code. It is turned off after a few seconds with LED2.
I chose the two examples because I wanted to know if both of them should work.?
Both should indeed work.
Snake4 wrote:
Dany wrote:hexadecimal values $30 and $0A (= 10 dec) to control Led3
That's what I tried. Obviously it didn't work. I thought I might have made a mistake or maybe the code won't work this way.
If you are sure it works like this, I will try it with my computer's serial port. Maybe there is a problem with the PICKit Tool.
Normally it should work. If you use the compiler's Uart terminal, set it to "send as number".You can enter then a decimal or hex number (like $30).
Snake4 wrote:
Dany wrote: I assume I is a byte or a character. It will hold the value that is received from the uart, So, if you send $30 or $0A, it will hold these values of course.
The help says a byte is returned but what if I send somethin like $AA00BB11EE? Will it hold all of them at once or something like: every time I read it 2 of them?
In the compiler's usart terminal you can send only one byte in number mode. $AA00BB11EE will return an error: invalid format.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

#5 Post by Snake4 » 30 Apr 2009 18:16

I tried it again but it didn't work.

So I built another board where I did connect RX and TX.

Code: Select all


TX:  11 

TX:  01 
RX:  80  

TX:  02 

TX:  03 
RX:  E0 

TX:  04 

TX:  05 
RX:  E0 
 
TX:  06 

TX:  07 
RX:  E0 

TX:  08
 
TX:  09 
RX:  86
 
TX:  30 

TX:  31 
RX:  98
 
TX:  0A
 
TX:  0B 
RX:  F8 

This is what I got. I tried every one of them a few times. I have no Idea why this happens. It seems like just every second is working. And as you can see 30 and 0A are not. Bad luck )-:
But like I said, the LED is flashing every time.

The code, if you want to see it:

Code: Select all


i := USART_Read();
USART_Write(i);

Any idea why this happens? Or RX TX discrepancy?( I did this test with PICKit2 Usart Tool)


Edit:
I changed the code to this:
if (i = 0x98) then
PORTC.3 := true;
From the list above I would assume I have to send 31 but it didn't work. Nor did 98.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#6 Post by Dany » 01 May 2009 08:19

Can you show the entire code? Perhaps there is a problem in the Led flashing part. Thanks in advance.

You can also do a test with only the code below, and nothing else:

Code: Select all

   while true do 
   begin
     if (USART_Data_Ready()) then
     begin
       i := USART_Read();
       USART_Write(i);
     end;  
   end;
to see if there are uart problems.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

FreeMan
Posts: 38
Joined: 25 Jul 2006 04:32

The USART is perfect, so where is the problem then?

#7 Post by FreeMan » 01 May 2009 15:15

peace be with you,

your only problem is the datasheet, everyone want the job to be done and no-one wants to read the bladysheet, go to the microchip.com and read the datasheet of your pic you are using and the discreptancy you are talking about is in your oscillator configuration part.

nearly sure you did not read the datasheet concerning the oscillation chaptor!!, did you?

how about the microcontroller you use? pic24? pic16, maybe pic18??

ok, let me guess, you using pic18 with PLL enabled and you dont have any clue about it. and just one morething, what frequency did you entered to mikropascal, is it the quartz frequency or the one after PLL frequency? or maybe an other one ??

go ahead read your datasheet before starting any complains.

have a nice reading and peace be with you,
freeman

FreeMan
Posts: 38
Joined: 25 Jul 2006 04:32

Forgotten info

#8 Post by FreeMan » 01 May 2009 15:23


Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

Re: The USART is perfect, so where is the problem then?

#9 Post by Snake4 » 01 May 2009 20:07

@Dany. I'll post the code if we still need it after that.
FreeMan wrote:everyone want the job to be done and no-one wants to read the bladysheet
Yes, thats right but I didn't think the answer would be in there and I don't think someone reads the whole datasheet.
FreeMan wrote: you using pic18 with PLL enabled and you dont have any clue about it
One thing is right. I don't have a clue about it. I am using a PIC16F688 and I don't think it even has PLL, whatever it is.

In mikroPascal I entered 8Mhz and INTRC_OSC_NOCLKOUT.
I won't need an external osc?

I read part of the datasheet now but I still have no idea whats the problem. Probably because I don't have a clue about anything concerning this.
FreeMan wrote:have a nice reading and peace be with you

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: The USART is perfect, so where is the problem then?

#10 Post by Dany » 01 May 2009 20:58

Snake4 wrote:@Dany. I'll post the code if we still need it after that.
Ok. :D Could you please include the project file (*.ppp or *.mpppi) also? Thanks.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

#11 Post by Snake4 » 01 May 2009 22:02

I assume this is what you want to see:

Code: Select all


[DeviceName]
Value=P16F688
[DeviceClock]
Value=4
[DeviceFlags]
Count=3
Value0=_CP_OFF = $3FFF
Value1=_MCLRE_OFF = $3FDF
Value2=_INTRC_OSC_NOCLKOUT = $3FFC


Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#12 Post by Dany » 02 May 2009 08:23

Snake4 wrote:I assume this is what you want to see:

Code: Select all

[DeviceName]
Value=P16F688
[DeviceClock]
Value=4                     <---------- here
[DeviceFlags]
Count=3
Value0=_CP_OFF = $3FFF
Value1=_MCLRE_OFF = $3FDF
Value2=_INTRC_OSC_NOCLKOUT = $3FFC

Hi, a few posts back you said
In mikroPascal I entered 8Mhz and INTRC_OSC_NOCLKOUT.
but the project file shows a deviceclock of 4 Mhz? Which freq did you actually select (see below)?

I assume you take this into account in your code:
The output of the 8 MHz HFINTOSC and 31 kHz
LFINTOSC connects to a postscaler and multiplexer
(see Figure 3-1). The Internal Oscillator Frequency
Select bits IRCF<2:0> of the OSCCON register select
the frequency output of the internal oscillators. One of
eight frequencies can be selected via software:
• 8 MHz
• 4 MHz (Default after Reset)
• 2 MHz
• 1 MHz
• 500 kHz
• 250 kHz
• 125 kHz
• 31 kHz (LFINTOSC)
?
I see that the clock configurations of the 16F688 are not so simple... I think also you should have a good look into the datasheet.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

#13 Post by Snake4 » 02 May 2009 16:47

Hi,

the 4Mhz confused me a little too. I think I have to put them on the left side, right under the PIC selection box? But in there is: Clock: 008.000000 Mhz??

I still have no clue how to solve my problem. I looked in the datasheet, again. I think I am missing the bigger picture where my problem is. Could you please help me with that, as you seem to know what the problem is?

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#14 Post by Dany » 02 May 2009 19:37

Snake4 wrote:the 4Mhz confused me a little too. I think I have to put them on the left side, right under the PIC selection box? But in there is: Clock: 008.000000 Mhz??
That is very odd: a difference between clock speed setting in the IDE and clock speed setting in the project file?
Snake4 wrote:I still have no clue how to solve my problem. I looked in the datasheet, again. I think I am missing the bigger picture where my problem is. Could you please help me with that, as you seem to know what the problem is?
I do not have any experience with the 16F688, so I think you better ask someone who has. :oops:
The only thing that I can see is (out of the datasheet) that one should select for the "INTOSC" (which you did in the config word), and "HFINTOSC" (to do with setting of IRCF<2:0> in the OSCCON regsiter). As far as I can see, if you do not fill OSCCON in the initialisation of your program the clock speed will be 4 Mhz. This is the clock speed to set in the IDE (I think), so I suggest you try first to change this.
But again, I can be wrong, perhaps you should ask someone with 16F688 experience...
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Snake4
Posts: 14
Joined: 16 Oct 2008 19:24

#15 Post by Snake4 » 04 May 2009 08:59

Ok, thanks.

I'll try to find someone who has experience.

Post Reply

Return to “mikroPascal General”