using SUART and UART together

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

using SUART and UART together

Post by CharlieM » Sat Nov 06, 2010 12:03 pm

Hello Everyone,

OK I have the software Uart working. I also have the hardware USArt working. I just Can't get both to work together. Here is my code.
The problem is the var pressure does not update. IF I reset the pic then it does. Thanks for any advice.

Code: Select all


   DEVICE = 18F1220
   CLOCK = 16

  
 
 
  ' include modules

   INCLUDE "suart.bas"
   INCLUDE "ADC.bas"
   INCLUDE "Convert.bas"
   INCLUDE "usart.bas"
  ' create vars
   DIM ADVal AS WORD
   DIM pressure AS WORD
      
 
   SUB Read_pressure () 
    
    ADVal = ADC.Read(0) ' this code reads pressure on analog pin 0
    ADVal = (ADVal +1) * 500 / 1024 ' this code is math to convert the 10 bit value to voltage
    pressure = ADVal * 13.6  ' this math converts voltage to pressure
    
   END SUB 
   
   
    SUB SGLCD_Connect ()
    
        UART.Write (85) '  'connect to serial GLCD
      DELAYMS(50)
      UART.Write(170,128)  ' adjust SGLCD backlight
      UART.Write (186) ' clear the display 
      DELAYMS(50)
      UART.Write (188,17,1," Well Monitor v1.0",0)
      DELAYMS (5000)
      UART.Write(186)' clear the display
      DELAYMS(50)
      UART.Write(188,2,1," Powered by SwordFish",0)
      DELAYMS(5000)
      UART.Write(186)' clear the display
      DELAYMS(50)
      UART.Write(188,16,5,"PSI:",0)
      DELAYMS(50)
      UART.Write(255,4,16,16,32,16,255)  ' underline PSI
      DELAYMS(50)
      UART.Write (255,2,0,0,127,63,255) ' draw a rectangle around the outside the glcd
      DELAYMS(50)
     END SUB
    
    
     
     SUB SGLCD_Display()
     UART.Write(183) ' use big font to display numbers
     DELAYMS(20)
     UART.Write (188,12,32,DecToStr(pressure/100),".",DecToStr  (pressure,1),"  ",0)    ' write to SGLCD
     DELAYMS (100)
     END SUB
       
      SUB PC_Display ()
      USART.Write(pressure/100)  ' write to PC serial Port
      END SUB
      SetTX(PORTA.1)
      SetMode(umTrue) 
      SetBaudrate(sbr9600)
      SetBaudrate(br9600)  
      TRISA.1 = 0
      TRISA.0 = 1        // configure AN0 as an input 
      ADCON1 = %11111110       // set analogue input on PORTA.0 
 
      
 
  
      SGLCD_Connect  
  
        
      Main_program:
      
       
      Read_pressure
      DELAYMS(50)
      SGLCD_Display
      DELAYMS (50)
      PC_Display
      DELAYMS(50)
       
      GOTO Main_program    
       
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

Post by Jon Chandler » Sat Nov 06, 2010 5:05 pm

Code: Select all

      SGLCD_Connect 
 
       
     ' Main_program:
     while 1 = 1 'sets up an endless loop
       
      Read_pressure
      DELAYMS(50)
      SGLCD_Display
      DELAYMS (50)
      PC_Display
      DELAYMS(50)
       
      'GOTO Main_program 

      wend  
       

The usual program structure is to end up an endless loop for the main program. What you have looks like it should work, but try the more conventional way I've shown above.

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Sat Nov 06, 2010 6:36 pm

Hi John,

I tried using While True wend and while 1 =1 wend and it still won't work. If I comment out the Usart include and everything that goes with USart it will work.
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Sat Nov 06, 2010 6:57 pm

Don't forget

Code: Select all

UART.SetBaudrate(sbr9600)
USART.SetBaudrate(br9600)  

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Mon Nov 08, 2010 11:07 am

OK I figured out why I was not getting a update from the code. It seems that the program gets stuck in the PC_display sub. If I comment it out in the main loop then all works. I even deleted the PC_Display sub and put the usart.write(pressure/100) in the main loop and it still hangs on that line of code. if I comment it out then all works. does anyone have any ideas on how to resolve this?
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Mon Nov 08, 2010 1:40 pm

Charlie, what version of MPLAB are you running. I have updated to 8.53, but they are on 8.60 now. Anyways, upgrading the MPLAB files helped out a lot, since SF had been using 7.63 or something really old like that. This update fixed some of the anomalies I had been having.
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Mon Nov 08, 2010 10:28 pm

Hi Ryan,

I have 8.56. I opened MPLAB let MPLAB check for updates. It says that I am up todate.
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Mon Nov 08, 2010 10:56 pm

OK I updated to 8.60. and still the same thing. I really need to get this resolved.
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

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

Post by Jerry Messina » Tue Nov 09, 2010 10:23 am

If you've added the two changes David pointed out, have you tried checking to make sure you can print anything in the PC_Display() sub? I'd try replacing the 'usart.write(pressure/100)' with something simple like sending a text message or a single character, and work from there.

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Tue Nov 09, 2010 10:28 am

Hi Jerry,

The two things that david pointed out are already in the code I posted.
The program hangs at the USART.write sub and if I delete the sub and just put USART.write(pressure/100) in the main loop it hangs there. I will try to send a string.

Code: Select all

DEVICE = 18F1220 
   CLOCK = 16 

  
  
  
  ' include modules 

   INCLUDE "suart.bas" 
   INCLUDE "ADC.bas" 
   INCLUDE "Convert.bas" 
   INCLUDE "usart.bas" 
  ' create vars 
   DIM ADVal AS WORD 
   DIM pressure AS WORD 
      
  
   SUB Read_pressure () 
    
    ADVal = ADC.Read(0) ' this code reads pressure on analog pin 0 
    ADVal = (ADVal +1) * 500 / 1024 ' this code is math to convert the 10 bit value to voltage 
    pressure = ADVal * 13.6  ' this math converts voltage to pressure 
    
   END SUB 
    
    
    SUB SGLCD_Connect () 
    
        UART.Write (85) '  'connect to serial GLCD 
      DELAYMS(50) 
      UART.Write(170,128)  ' adjust SGLCD backlight 
      UART.Write (186) ' clear the display 
      DELAYMS(50) 
      UART.Write (188,17,1," Well Monitor v1.0",0) 
      DELAYMS (5000) 
      UART.Write(186)' clear the display 
      DELAYMS(50) 
      UART.Write(188,2,1," Powered by SwordFish",0) 
      DELAYMS(5000) 
      UART.Write(186)' clear the display 
      DELAYMS(50) 
      UART.Write(188,16,5,"PSI:",0) 
      DELAYMS(50) 
      UART.Write(255,4,16,16,32,16,255)  ' underline PSI 
      DELAYMS(50) 
      UART.Write (255,2,0,0,127,63,255) ' draw a rectangle around the outside the glcd 
      DELAYMS(50) 
     END SUB 
    
    
      
     SUB SGLCD_Display() 
     UART.Write(183) ' use big font to display numbers 
     DELAYMS(20) 
     UART.Write (188,12,32,DecToStr(pressure/100),".",DecToStr  (pressure,1),"  ",0)    ' write to SGLCD 
     DELAYMS (100) 
     END SUB 
        
      SUB PC_Display () 
      USART.Write(pressure/100)  ' write to PC serial Port 
      END SUB 
      SetTX(PORTA.1) 
      SetMode(umTrue) 
      SetBaudrate(sbr9600) <<<<<<<<<<Here
      SetBaudrate(br9600)  <<<<<<<<<<here
      TRISA.1 = 0 
      TRISA.0 = 1        // configure AN0 as an input 
      ADCON1 = %11111110       // set analogue input on PORTA.0 
  
      
  
  
      SGLCD_Connect  
  
        
      Main_program: 
      
        
      Read_pressure 
      DELAYMS(50) 
      SGLCD_Display 
      DELAYMS (50) 
      PC_Display 
      DELAYMS(50) 
        
      GOTO Main_program    
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

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

Post by Jerry Messina » Tue Nov 09, 2010 11:02 am

The two things that david pointed out are already in the code I posted.
If you look closely at what David posted compared to what you have, it's easy to miss, but they're not the same.

He qualified the calls to 'SetBaudrate' with the module names so the compiler would call the routines in each module. Without it, both of those calls end up using the SUART version, so the hardware never gets setup.

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Tue Nov 09, 2010 11:12 am

Thanks Jerry. I could not see the forest through the trees. :?
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Tue Nov 09, 2010 11:17 am

Yep that did it. I should have looked more closely. now I can get on with finishing the project. Thanks again.
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

Post Reply