Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Send a sequence of binary number of 8 bits

Status
Not open for further replies.

Rooney_04

Member level 2
Joined
Oct 2, 2010
Messages
47
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,604
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.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,184
Helped
4,685
Reputation
9,385
Reaction score
4,439
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
121,137
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.
 

Rooney_04

Member level 2
Joined
Oct 2, 2010
Messages
47
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,604
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 <stdio.h>
#include <stdlib.h>
#use delay(clock=20000000)
#fuses hs, noprotect, nowdt, nolvp
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=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);
}
}
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,184
Helped
4,685
Reputation
9,385
Reaction score
4,439
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
121,137
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.
 

Rooney_04

Member level 2
Joined
Oct 2, 2010
Messages
47
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,604
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...:-?
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,184
Helped
4,685
Reputation
9,385
Reaction score
4,439
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
121,137
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.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top