Shop OBEX P1 Docs P2 Docs Learn Events
Does Timer1 Prescale affect the Serin baud rate? — Parallax Forums

Does Timer1 Prescale affect the Serin baud rate?

gtslabsgtslabs Posts: 40
edited 2007-08-20 17:33 in General Discussion
Does the prescale affect the serin baud rate?

I am using one chip to talk to another.
I just got in my SX48 Protoboard and put this code on it.
However I tried different baud rates for the sending serout and receiving serin with no luck.

I really wanted to send a word but I am still having problems getting a byte to tranfer.


here is an example of my sending code from another brand·chip.·This is on another board.
I have both boards powered by the same source so the ground and levels are the same.
If Key_Value=12 then
 high 1
 pause 200
 SerOut 0, T4800, (B1)
 pause 250
 low 1
 
endif


and my sx48 code.

DEVICE          SX48, OSCXT1
FREQ            4_000_000

IrLed           VAR     RB.6
frequency var Byte
'frequency var word
delay var word
half_delay var word

PROGRAM Start

Start:
  OUTPUT IrLed      
  Input RA.0
 TIMER1 PRESCALE, 6 ' 6 = 1:64 prescaler
Delay = 62500 / frequency                            ' enable LED output
Half_Delay = 31250 / frequency

TIMER1 PWM, half_Delay, Delay

DO
new:
 If RA.1 = 1 then
 pause 200
 SERIN RA.0, T4800, Frequency, 200, new
' SERIN RA.0, T4800, Frequency_MSB, 200, new
' SERIN RA.0, T4800, Frequency_LSB, 200, new
 watch frequency
 Goto start
 endif
    
  LOOP
  END

Comments

  • BeanBean Posts: 8,129
    edited 2007-08-13 22:51
    No the timer prescaler does not affect SERIN.
    You are probably missing the character. You should just loop to new: until the pin goes high.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 00:00
    I think the problem is with the division operator; when dividing words and the divisor is set to one, the result seems to be 2x what it should be -- I've seen this twice in the last week (this program, like another I worked with, has a work-around when the divisor is 1). The program attached does what you want: waits for a two-byte frequency value from an external host (I used the BS2 on my PDB to test) and sets the TIMER1 PWM mode to the desired frequency with a 50% duty cycle.

    Post Edited (JonnyMac) : 8/14/2007 2:09:45 AM GMT
  • gtslabsgtslabs Posts: 40
    edited 2007-08-14 01:42
    Thanks Jon and Bean,
    I got it to work ok except for the 1 hz where I would see it drop to 0.65 hz. I did not try Jon's correction yet.
    But my bigger problem is getting the hard reset to work. It runs fine when I rerun it from the Key and use the reset button.
    But when I remove the power from the 7.5V 800mA wall wart it will not restart.
    I searched these forums and tried some suggested directives but they did not help either. Do I need to set the Fusex directly?

    here is my code:
    DEVICE          SX48, OSCXT1,BOR42,wdrt006,sleepclk ' I tried different variations here. I am still on the key.
    FREQ            4_000_000
    IrLed           VAR     RB.6
    
    frequency var word
    delay var word
    half_delay var word
    PROGRAM Start
    
    Start:
    frequency=2
      OUTPUT IrLed      
      Input RA.0
     TIMER1 PRESCALE, 6 ' 6 = 1:64 prescaler
    newstart:
    Delay = 62500 / frequency         
    Half_Delay = 31250 / frequency
    TIMER1 PWM, half_Delay, Delay
    new:
    DO
    loop until RA.1 = 1
     SERIN RA.0, N4800, Frequency_LSB, 500, new
     SERIN RA.0, N4800, Frequency_MSB, 500, new
     Goto newstart
    END
    
  • PJMontyPJMonty Posts: 983
    edited 2007-08-14 01:50
    gtslabs,

    When you run code with the SX-Key, you program the chip using the Run->Debug (CTRL-D) menu option. To have the chip run without the SX-Key, you program using the Run->Program (CTRL-P) option. In addition, you need to have an external clock (TTL, resonator, or crystal) attached as you have specified OSCXT1 in your device directive.

    Thanks,
    PeterM
  • gtslabsgtslabs Posts: 40
    edited 2007-08-14 02:01
    Thanks Peter
    I have always used CTRL-R.
    But wouldn't OSCXT1 work because I am currently using the KEY as the osc source.
    Does powering down and up again disable the key osc?
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 02:01
    I'm running my version of the program on an SX52 Con Carne board with a 4 MHz resonator with no troubles -- why don't you just give it a try? I think you'll find the code is a little cleaner than what you're doing and that, in the long run, is important. As I stated I believe there is a problem with the division operator and you're using it twice -- you could have two errors and you're generating lots of code (division is code heavy). Also, subdividing your program into reusable chunks is always a good idea. And do you expect to have the serial stream fail midway? I think it's a little odd that you get a RTS (request to send) from your external device and then you assume it could timeout; is this really possible?

    I've updated my version of the program (see above) so that it uses the RTS.
  • BeanBean Posts: 8,129
    edited 2007-08-14 02:04
    JonnyMac said...
    I think the problem is with the division operator; when dividing words and the divisor is set to one, the result seems to be 2x what it should be -- I've seen this twice in the last week (this program, like another I worked with, has a work-around when the divisor is 1).
    Jon,
    · Can you post a small program that demonstrates the error. I will fix it in the next release.

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 02:07
    I'm running my version of the program on an SX52 Con Carne board with a 4 MHz resonator with no troubles -- why don't you just give it a try? It has some useful code bits (things are divided up) and I've already coded the work-around for the division issue (I also use >> instead of / when possible as this saves a lot of code). I didn't notice that you were using an RTS line in to the SX to signal new data is coming -- I've updated my program to include that.

    Post Edited (JonnyMac) : 8/14/2007 2:14:39 AM GMT
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 02:10
    Bean (Hitt Consulting) said...
    on,
    Can you post a small program that demonstrates the error. I will fix it in the next release.

    Bean.

    If you comment out the IF-THEN section of this subroutine so that just the ELSE statements are active you will see that sending 1 Hz results in an output of 2.

    SUB SET_FREQ
      tmpW1 = __WPARAM12                            ' capture freq
      IF tmpW1 = 1 THEN                             ' fix div by 1 problem
        tmpW1 = 31_250
        tmpW2 = 62_500
      ELSE
        tmpW2 = 62_500 / tmpW1                      ' calc units
        tmpW1 = tmpW2 >> 1                          ' set to 50% DC
      ENDIF
      TIMER1 PRESCALE, 6                            ' 1:64
      TIMER1 PWM, tmpW1, tmpW2                      ' activate PWM
      ENDSUB
    
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 02:15
    gtslabs said...
    Thanks Peter
    I have always used CTRL-R.
    But wouldn't OSCXT1 work because I am currently using the KEY as the osc source.
    Does powering down and up again disable the key osc?

    Yes, if you power down the key will stop clocking; you can reset by pullig MCLR low, but don't power down the key. Better yet, pop a 4 MHz resonator into your board and use Ctrl+P to program.
  • BeanBean Posts: 8,129
    edited 2007-08-14 02:43
    Jon,
    Thanks, I found the problem. I will get it fixed.

    The problem occurs when you divide a word value that is > 32767 by 1.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2007-08-14 09:27
    gtslabs said...
    Thanks Peter
    I have always used CTRL-R.
    But wouldn't OSCXT1 work because I am currently using the KEY as the osc source.
    Does powering down and up again disable the key osc?

    Yes, it does. When you power down and up again the key, it performs a reset, and stays in a loop, waiting for a connect string from the SX-Key IDE. As long as you maintain power after Ctrl-R, or Ctrl-K, the SX-Key generates the clock signal, even when you remove the serial cable, or shut down the SX-Key IDE.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • gtslabsgtslabs Posts: 40
    edited 2007-08-14 13:16
    JonnyMac said...
    ·And do you expect to have the serial stream fail midway? I think it's a little odd that you get a RTS (request to send) from your external device and then you assume it could timeout; is this really possible?
    Jon, I was not sure of the mechanism of how the serial out works. When·a·byte or word is sent out of a serout pin·does it continue to sit there on the sx serin pin until it read? Or is it a one shot signal and goes into electron cyerspace.·That is why I put the RTS condition in so it knew to look for it and then try to time it so I did not miss it. I probably should add some prequalifiers too as in your other example code.
    Steve
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 14:24
    Yes, SEROUT transmits a start bit and eight data bits (with a one bit stop period -- this gives the receiver time to do something with the value); and SERIN, after detecting a start bit, waits for eight data bits. Your hardware sync solution makes things very easy as shown in my example (the hardware replaces the sync string I used before). I don't think you need a timeout because your program is doing nothing except waiting on a new frequency value.
  • gtslabsgtslabs Posts: 40
    edited 2007-08-14 15:44
    Jon, should your I/O decleration of "NoPullUp" be a comment? It generates an error as is.
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-08-14 16:21
    Nope; you're probably using an old version of the compiler -- make sure you have 1.51.03. The easiest way to check your version is to look at the top line of a list file (Ctrl + L) of a program that does compile. I just downloaded the code from my post and retested; it compiles file (see attached).

    Post Edited (JonnyMac) : 8/14/2007 4:26:50 PM GMT
  • gtslabsgtslabs Posts: 40
    edited 2007-08-15 13:26
    IS there a way to count these pulses? I need to control the amount of steps.
    My current range of movement would need a 24 bit variable. However if I count every x steps I can probably get it to a 16bit value.
    Is there some internal counter?
  • BeanBean Posts: 8,129
    edited 2007-08-15 13:31
    You can use timer2.

    TIMER2 EXTERNAL

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com


    Post Edited (Bean (Hitt Consulting)) : 8/15/2007 1:37:05 PM GMT
  • gtslabsgtslabs Posts: 40
    edited 2007-08-20 17:33
    How do I turn off the Timer1 PWM feature?
    Just make the output an input?
    Then back to output when I want to turn it on again?
Sign In or Register to comment.