+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Member level 2
    Points: 598, Level: 5

    Join Date
    Oct 2010
    Posts
    47
    Helped
    2 / 2
    Points
    598
    Level
    5

    Send a sequence of binary number of 8 bits

    How could i send a sequence of binary number of 8 bits to the serial port(Tx Pin). I m using CCS C Compiler and PIC16f877a and operating frequency is 20Mhz ? I m planning to use two push button for count up and count down respectively.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 81,430, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,349
    Helped
    4456 / 4456
    Points
    81,430
    Level
    69

    Re: Send a sequence of binary number of 8 bits

    8 bits = 1 byte. Make sure you have set up the speed and pin directions then just write the byte to the USART TX register and it will magically appear on the TX pin.

    Brian.



    •   AltAdvertisement

        
       

  3. #3
    Member level 2
    Points: 598, Level: 5

    Join Date
    Oct 2010
    Posts
    47
    Helped
    2 / 2
    Points
    598
    Level
    5

    Re: Send a sequence of binary number of 8 bits

    Quote Originally Posted by betwixt View Post
    8 bits = 1 byte. Make sure you have set up the speed and pin directions then just write the byte to the USART TX register and it will magically appear on the TX pin.

    Brian.
    hi...i did some programming . But the output not of TX not showing any result when i tried in proteus. below is programming i could not find where it goes wrong.

    #include <16F877a.h>
    #include
    #include
    #use delay(clock=20000000)
    #fuses hs, noprotect, nowdt, nolvp
    #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,st ream=RS232,bits=8)

    #byte portb=6
    #byte portc=7
    #byte portd=8


    void main()
    {
    unsigned int n;

    n=0;

    if(!input(pin_B1))
    {
    n=n+1;
    putc(n);
    delay_ms(50);
    }
    else if(!input(pin_B2))
    {
    n=n-1;
    putc(n);
    }
    }



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 81,430, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,349
    Helped
    4456 / 4456
    Points
    81,430
    Level
    69

    Re: Send a sequence of binary number of 8 bits

    I do not have a CCS compiler but the code looks OK. The only things I would question are:

    1. Do you have to set the TRIS bits for the RX and TX pins or does the "#use RS232" directive do that for you?
    2. Does putc() automatically use the USART or do you need to tell it where the character stream is sent?

    I assume the compiler adds code to check if the TX register is free to be loaded. If it doesn't you need to check it is empty before using putc() to put new data into it. As the program is not in a loop, I'm also assuming you are not looking for a continuous output, at the moment it will execute with no output unless you 'trigger' it with an input.

    Brian.



  5. #5
    Member level 2
    Points: 598, Level: 5

    Join Date
    Oct 2010
    Posts
    47
    Helped
    2 / 2
    Points
    598
    Level
    5

    Re: Send a sequence of binary number of 8 bits

    Quote Originally Posted by betwixt View Post
    I do not have a CCS compiler but the code looks OK. The only things I would question are:

    1. Do you have to set the TRIS bits for the RX and TX pins or does the "#use RS232" directive do that for you?
    2. Does putc() automatically use the USART or do you need to tell it where the character stream is sent?

    I assume the compiler adds code to check if the TX register is free to be loaded. If it doesn't you need to check it is empty before using putc() to put new data into it. As the program is not in a loop, I'm also assuming you are not looking for a continuous output, at the moment it will execute with no output unless you 'trigger' it with an input.

    Brian.
    hi.
    for the first question, since the programming not working if i didnt set the TRIS Bit. how i can set the TRIS bits?

    for the second question, from the manual i read it will send the TX pin directly since there is one pin for serial transmitting.

    i m using a push button to trigger it. So that why i used the code if(!input_pinB1). However, i did simulation using proteus, but no result came out when i pressed the push button. So i m kind of confuse right now...



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 81,430, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,349
    Helped
    4456 / 4456
    Points
    81,430
    Level
    69

    Re: Send a sequence of binary number of 8 bits

    If it needs to be done, the data sheets says:
    bits TRISC<7:6> have to be
    set in order to configure pins RC6/TX/CK and RC7/RX/DT
    as the Universal Synchronous Asynchronous Receiver
    Transmitter.
    so adding TRISC |= 0xC0; should set them both.

    I have feeling your problem is in the way you are simulating though. The program will run and stop in a tiny fraction of a second, it will not wait for you to press the push button. If you want it to wait, either put it in a loop until the button is pressed or wrap everything from the first 'if' statement inside a while() loop so the whole code constantly repeats.

    Brian.



--[[ ]]--