Very Noob - string stuck

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
andy_8a
Posts: 1
Joined: Thu Nov 10, 2011 8:54 am
Location: Mexico

Very Noob - string stuck

Post by andy_8a » Thu Nov 10, 2011 9:27 am

Hi, im a noob student and im tryin to make a simple usart pic to pic. I want just to turn leds with buttons, but the any led atached to the button just turn on one time, and when i press any button again it does nothing, thanks for some who want to help and sorry if a make a mistake in my english...

Mater

Code: Select all

Device = 18F4550
Clock = 20

Include "USART.bas"

Dim boton1 As PORTD.0
Dim boton2 As PORTD.1
Dim boton3 As PORTD.2
Dim boton4 As PORTD.3

SetBaudrate(br19200)
TRISD=%11111111

While true 
If boton1=1 Then      
USART.Write("A", 13, 10)
EndIf

If boton2=1 Then      
USART.Write("B", 13, 10)
EndIf

If boton3=1 Then      
USART.Write("C", 13, 10)
EndIf

If boton4=1 Then      
USART.Write("D", 13, 10)
EndIf

Wend

Slave

Code: Select all

Device = 18F4550
Clock = 20

Include "USART.bas"
   
Dim dato As String
Dim LED1 As PORTD.0
Dim LED2 As PORTD.1
Dim LED3 As PORTD.2
Dim LED4 As PORTD.3

SetBaudrate(br19200) 
ReadTerminator = 13 

While true
If DataAvailable = true Then 
    USART.Read(dato)
         
    If dato = "A" Then 
        High (LED1)
        DelayMS (1000)
        Low (LED1) 
    EndIf  
    
    If dato = "B" Then 
        High (LED2)
        DelayMS (1000)
        Low (LED2) 
    EndIf  
    
    If dato = "C" Then 
        High (LED3)
        DelayMS (1000)
        Low (LED3) 
    EndIf 
    
    If dato = "D" Then 
        High (LED4)
        DelayMS (1000)
        Low (LED4) 
    EndIf  
     
EndIf 
Wend  
Image

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Thu Nov 10, 2011 4:25 pm

You might have better luck just sending a single character instead of the three byte string that you're currently sending.

Try changing the master code to just send "A", "B", or "C" (leaving off the 13, 10), and then in the slave code change 'dato' to be a single byte.

You are probably getting into problems synchronizing the master and slave because the slave is set to terminate the read on the CR character, and the master is sending a LF after it. Since the USART module is polled, this will tend to generate overrun errors.

Post Reply