Setting up UART and some coding issues


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Default Setting up UART and some coding issues

    hi, i'm new to MCU and currently setting up a circuit to monitor temperature with PC. I'm using LM35 (10mV/degC) , PIC16F877A and Max232 with USB-to-RS232 converter, using hyperterminal to acquire data from RS232.
    The pin2(RA0) used as analog input, while RC6 for TX and RC7 for RX.
    i google for a while and found out a similar coding but unfortunately is not complete solution, does it missing some header?? and how to setup UART with SERIN and SEROUT?? thanks

    DEFINE ADC_BITS 8
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50

    x var byte

    TRISA=255
    TRISB=0
    ADCON1=%10000010
    pause 500

    loop1:

    ADCIN 0,x
    PortB = x
    output PortB
    pause 500

    goto loop1
    end

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Welcome to the forum.

    Serial..
    UART on those pins are done in hardware. Sometimes the settings are difficult to setup so we cheat and use this.
    PicMultiCalc V_1.3.1
    http://www.picbasic.co.uk/forum/atta...achmentid=2957

    If you want to use any pin ( almost any pin) for serial then the software (SERIN, SERIN2) is the way to go. SERIN2 uses the same syntax as HSERIN .

    ADC.
    Take a look here
    http://www.rentron.com/PICX2.htm
    Uses the "old" way to read the ADC, the way I prefer
    And this may also give you some insight
    http://www.rentron.com/serial.htm

    Your code.
    Looks like it is not all there. If that is all of it the links above should help with the missing parts. If there is more, post it all.
    It is a good idea to post all of your code and not snippets, sometimes the problem is not where we think..
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Apr 2010
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    This is the modified code by implemented the resource of link provided, but somehow there is no output to the screen... anything wrong??


    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically

    DEFINE ADC_BITS 10 ' ADCIN resolution (Bits)
    DEFINE ADC_CLOCK 2 ' ADC clock source (Fosc/32)
    DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)

    x var byte
    y var byte

    TRISA = %00111111 'Set PORTA to inputs
    TRISC = 0 'Set PORTC to outputs
    ADCON1 = %10000000

    loop1:

    Pauseus 50
    ADCON0.2 = 1 'start conversion
    Pauseus 50

    ree:
    if ADCON0.2 = 1 then ree
    ADCIN 0,x
    y = x * 100 'multiply 100 to change to degree celcius
    HSEROUT [y]
    pause 500

    goto loop1
    end

  4. #4


    Did you find this post helpful? Yes | No

    Default This may help

    Hi nodCarlos

    Do you have anything working ?

    Can you blink an LED ?

    Have you got the correct defines for registers and clock speeds ?

    What crystal/clock source are you using are you using ? It seems like 20 MHz external ocscillator/crystal according to your HSER defines , not so ?

    In order for many here to help you, you may consider posting your whole code examples showing things like clock speeds and so on.

    Try something simple first.....NO ADC or anything apart from serial data sending here!
    Here is some code you can try for a start.

    Code:
    'Port IO directions and presets for port pins begin here
    'TRISX = %76543210   << tris bit order numbering
    'TRISA = %11111111       'All pins are outputs
    '// Define port pins as inputs and outputs ...
            TRISA  = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs, 
            TRISB = %11111111  'for 4x4 keypad all input
            TRISC = %10010000  'NOTICE port 7 here for HSERIN !! And port 6 for HSEROUT
            TRISD = %00000000
            TRISE.0 = 0
            TRISE.1 = 0
            TRISE.2 = 0
    'End of Port IO directions and presets for port pins begin here
    
    'Variables being here
    	Y var BYTE 'decalre Y as byte
    	Y=123 'set Y=123
    
    'HSEROUT defines
    	DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    	DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    	DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0.16%
    	DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    '   Check port settings PORTC.6 = TX and PORTC.7 = RX
    '   For PIC to PIC TX to RX C.6 to C.7 and visa versa
    '   Don't forget TRISC=%10000000
    'HSEROUT USART defines and register settings end here
    
    main:
    
    
    "HSEROUT line begins here	
    	
    	HSEROUT ["here is the total ",dec Y,$0d,$0a]  'send result to pc com port
    	'this line sends the DECimal for of Y to MAX
    'HSEROUT line ends here
    
    'UNCOMMENT if you want to use SEROUT to do the same thing
    '*******************************************           
         
         	'this lines works perfectly to send data
         	'*******************************************
    	 'SEND data to MAX to PC
    
    	'SEROUT PORTC.3,T9600,["the received value is ", Y,10,13]
    'SEROUT line ends here 
    
    'UNCOMMENT these lines if you're using an LCD 
    '	pause 1000
    '	LCDOUT $fe,1
    '	LCDOUT "The total is",dec Y
    'End of LCDOUT line
    
    goto main
    
    END
    Please note the code above assumes you are using a 20 MHz clock,you will need to add the necessary lines at the beginning of your code to suit the PIC you are using. I have commented the code to help you out as much as possible.
    You will also notice the variable Y is set to the value 123..only change this after you are successfully seeing data in your serial tool window on the PC.

    Hope this helps

    Dennis

  5. #5
    Join Date
    Apr 2010
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    hi dennis,
    ya, im using 20MHz crystal, and i can blink LED with the C code UART example...
    i use MPlab v8.xx ,melab PicBasic Pro as compiler, and PICKIT 2 as programmer...
    does the difference of programmer will affect the result?? sorry for noob question

    best regards

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    does the difference of programmer will affect the result?? sorry for noob question
    No, the programmer will not make a difference.

    Try this without the MAX232. Connect PORTC.6 to pin #2 of the serial port as shown in the manual. Have the terminal program set for 9600 baud, 8,N,1.
    Code:
    DEFINE OSC 20
    X  VAR BYTE
    START:
    X = X + 1
    SEROUT2 PORTC.6, 16468, ["HELLO WORLD",$d,$a]
    SEROUT2 PORTC.6, 16468, [DEC X,$d,$a]
    PAUSE 1000
    GOTO START
    Dave
    Always wear safety glasses while programming.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts