Menu

LCD and Serial Communication

Help
H2TECH
2013-07-20
2013-10-09
  • H2TECH

    H2TECH - 2013-07-20

    After making the leap to PC-PIC serial communication, I have encountered one problem. Using an adapted code from the Val() function example, this code runs well. It sends serial messages and receives them without a flaw. However, I have adapted the code to also print messages on an LCD display. Instead, the LCD screen does not display anything. I have confidence the LCD module fully works, but there must be a conflict between the USART and the LCD program. I believe I might have stumbled upon someone with the same problem, but that was months ago. The code is below (Be aware I have "'" before the #define lines):

    '#chip 16f628a, 4
    '#config osc = int

    'Set up hardware serial connection
    '#define USART_BAUD_RATE 9600
    '#define USART_BLOCKING
    '#define LCD_IO 4
    '#define LCD_DB7 PORTA.1
    '#define LCD_DB6 PORTA.0
    '#define LCD_DB5 PORTA.7
    '#define LCD_DB4 PORTA.6
    '#define LCD_RS PORTB.4
    '#define LCD_RW PORTB.5
    '#define LCD_Enable PORTB.6

    'Variable for output level
    Dim OutputLevel As Word

    'Variables for received bytes
    Dim DataIn As String
    DataInCount = 0

    cls
    print "Ready"
    hserprint "Serial Test"
    hsersend 13
    hsersend 10

    Do
    'Get serial byte
    Wait Until USARTHasData
    HSerReceive InByte

    'Process latest byte
    'Enter key?
    If InByte = 13 Then
        'Convert output level to numeric variable
        OutputLevel = Val(DataIn)
    
        cls
        print outputlevel
    
        'Clear output buffer for next command
        DataIn = ""
        DataInCount = 0
    End If
    
    'Number?
    If InByte >= 48 and InByte <= 57 Then
        'Add to end of DataIn string        
        DataInCount += 1
        DataIn(DataInCount) = InByte
        DataIn(0) = DataInCount
    End If
    

    Loop

    It is straight forward and very similar to the original example and copied from. Is there a conflict between the USART and LCD code?

     

    Last edit: H2TECH 2013-07-20
    • Saša

      Saša - 2013-10-09

      Hello, a post a code that works for me. I can display data i.e text (commands) and values (numeric). If I want to set pwm to 128 i simply send command and integer to PIC and then display on LCD what PIC receives from rs232 and then to execute a command received.
      Hope it helps. Sorry for bad english.

      Code used is simple and derived from help file.

      '#chip 16F887, 8
      '#config osc = Int

      '#define USART_BAUD_RATE 9600
      '#define LCD_IO 4
      '#define LCD_NO_RW
      '#define LCD_DB4 PORTB.0
      '#define LCD_DB5 PORTB.1
      '#define LCD_DB6 PORTB.2
      '#define LCD_DB7 PORTB.3
      '#define LCD_RS PORTB.4
      '#define LCD_Enable PORTB.5

      '#define USART_BAUD_RATE 9600
      '#define USART_BLOCKING 'This is important instruction

      'Set pin directions for USART and PWM
      ' I use RS232 and PWM to test it
      dir PortC.7 in
      Dir PortC.6 Out
      Dir PortC.2 Out

      'Variable for output level
      Dim OutLevel As Word 'This will be value
      Dim Rijec as string 'This will be command

      'Variables for received bytes
      Dim DataIn As String
      Dim ZnakIn as string
      ZnakCount=0
      DataInCount = 0

      Main:
      Do
      'Get serial byte
      'Wait Until USARTHasData
      HSerReceive InByte
      wait 10 ms
      HSerSend inbyte
      'Process latest byte
      'Enter key?
      If InByte = 13 Then
      cls
      'Convert output level to numeric variable
      Outlevel = Val(DataIn)
      Rijec=znakin
      lOCATE 0,0 : PRINT "Naredba: " ' This stands for command
      LOCATE 0,10 : PRINT RIJEC
      Locate 1,0 : Print "Iznos: " ' This stands for value
      Locate 1,10 : Print OutLevel
      'Output
      if rijec="PWM" then ' If you send from terminal "PWM"
      HPWM 1, 1, Outlevel 'PWM will be set to value given
      else
      Locate 3,0 :Print "Nedefinirano!" ' No command defined!"
      end if
      ' In terminal you may send in one line PWM255 to set PWM output to full
      ' or PWM128 or any other value you wish
      ' Also you may define your own commands, i.e. Left, right, On, Off and so on...

      'Clear output buffer for next command
      DataIn = ""
      DataInCount = 0
      znakin=""
      znakcount=0
      End If
      'Number?
      If InByte >= 48 and InByte <= 57 Then
      'Add to end of DataIn string
      DataInCount += 1
      DataIn(DataInCount) = InByte
      DataIn(0) = DataInCount
      End If
      'Znak
      if inbyte >=65 and inbyte <=90 then
      znakcount +=1
      znakin(znakcount) =inbyte
      znakin(0)=znakcount
      end if
      Loop
      end

       

      Last edit: Saša 2013-10-09
  • Saša

    Saša - 2013-10-09

    Oh one more thing, you mast set direction out from port that is dedicated to PWM.
    If is hardware you mast declare it. Or else it will not work. For PIC16F887 HPWM port is PORTC.2 (CCP1 - HPWM), so you must set DIR PortC.2 OUT

    Good luck!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.