bar_
Newbie level 6
- Joined
- Jun 22, 2016
- Messages
- 11
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 98
#include "16F877a.h"
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit = pin_c0, rcv = pin_c1, force_sw)
void main()
{
set_tris_c(0b10000010); // UART RX pin direction register
while(1)
{
printf("e\n\r");
delay_ms(100);
}
}
Then i tried to write my own code to just transmit the character "e" at a interval of 100ms continously but it is also not working. i mean not displaying any junk data also. below is the code
#include "16f877a.h"
#use delay(clock=40000000)
#define Baudrate 9600 //bps
#define one_bit_delay (1000000/Baudrate) //one bit delay
#define DataBitCount 8 // no parity, no flow control
#define set_rx_tx_direc set_tris_c(0b10000010) // UART RX pin direction register
#byte port_c = 0x07
#bit UART_RX = port_c.1
#bit UART_TX = port_c.0
void main()
{
set_rx_tx_direc;
UART_TX = 1;
delay_ms(3);
while(1)
{
UART_TX = 0; //start bit to transmit "e" whose ascii value = 0d101 or 0b01100101
delay_us(one_bit_delay);
UART_TX = 1; //0th bit
delay_us(one_bit_delay);
UART_TX = 0; //1st bit
delay_us(one_bit_delay);
UART_TX = 1; //2nd bit
delay_us(one_bit_delay);
UART_TX = 0; //3rd
delay_us(one_bit_delay);
UART_TX = 0; //4th
delay_us(one_bit_delay);
UART_TX = 1; //5th
delay_us(one_bit_delay);
UART_TX = 1; //6th
delay_us(one_bit_delay);
UART_TX = 0; //7th
delay_us(one_bit_delay);
UART_TX = 1; //stop bit
delay_us(one_bit_delay);
delay_ms(100); // delay_to_next_character
}
}
#include "16f877a.h"
#use delay(clock=40000000)
#define Baudrate 1200 //bps
#define OneBitDelay (1000000/Baudrate)
#define DataBitCount 8 // no parity, no flow control
#define set_rx_tx_direc set_tris_c(0b10000010) // UART RX pin direction register
#byte port_a = 0x05
#byte port_b = 0x06
#byte port_c = 0x07
#byte port_d = 0x08
#bit UART_RX = port_c.1
#bit UART_TX = port_c.0
void InitSoftUART(void);
char UART_Receive(void);
void UART_Transmit(char charc);
void InitSoftUART(void) // Initialize UART pins to proper values
{
output_high(pin_c0); // TX pin is high in idle state
set_rx_tx_direc;
}
char UART_Receive(void)
{
char DataValue = 0;
char i = 0;
//wait for start bit
while(UART_RX==1);
delay_us(833);
delay_us(416); // Take sample value in the mid of bit duration
for ( i = 0; i < DataBitCount; i++ )
{
if ( UART_RX == 1 ) //if received bit is high
{
//DataValue += (1<<i);
DataValue = (DataValue | (DataValue << i));
}
delay_us(833);
}
// Check for stop bit
if ( UART_RX == 1 ) //Stop bit should be high
{
//delay_us(OneBitDelay/2);
delay_us(416);
return DataValue;
}
else //some error occurred !
{
//delay_us(OneBitDelay/2);
delay_us(416);
return 0x00;
}
}
void UART_Transmit(char DataValue)
{
char i = 0;
UART_TX = 0; // Send Start Bit
delay_us(833); // Send Start Bit
for(i = 0; i < DataBitCount; i++)
{
//Set Data pin according to the DataValue
//DataValue = (DataValue | (DataValue << i));
if(((DataValue >> i) & 0x01) == 0x01)
//if( ((DataValue>>i)&0x1) == 0x1 ) //if Bit is high
{
UART_TX = 1;
}
else //if Bit is low
{
UART_TX = 0;
}
delay_us(833);
}
UART_TX = 1; //Send Stop Bit
delay_us(833); //Send Stop Bit
}
void main()
{
char ch = 0;
InitSoftUART(); // Intialize Soft UART
delay_ms(10);
while(1)
{
ch = UART_Receive(); // Receive a character from UART
if(ch != 0)
{
UART_Transmit(ch); // Echo back that character
}
}
}
How do you have it interfaced? Bear in mind the data stream will be positve logic but if the other devices are expecting RS232 data, it will be inverted (including the start and stop bits so it won't sync properly) and at the wrong voltage levels.
Brian.
KlausST, it can be done using interrupt, but i want to make the code simple.
Hi,
It´s a point of view. I like using interrupts BECAUSE it makes the code SIMPLE.
Additionally it generates a fixed UART timing. And in your case I assume it safes 99% processing power. (In other words: it needs only 1% processing power)
In main loop I just write the bytes to be transmitted in a software buffer. The rest of transmitting is made by the interrupt routine. But I don´t have to take care in main loop.
It´s as simple as writing bytes into SRAM. (That´s why I call it simple)
In the remaining 99% of processing power there is plenty of time to react to other events. Like incoming data. So you can run both UARTs full duplex in the same time without missing a byte.
Klaus
#include "16f877a.h"
#use delay(clock=20000000)
#define Baudrate 9600 //bps
#define set_rx_tx_direc set_tris_c(0b10000010) // UART RX pin direction register
#byte port_c = 0x07
#bit UART_RX = port_c.1
#bit UART_TX = port_c.0
short stop_bit = 0;
char bit_transmitted = 0;
char DataValue = 0;
#int_timer2
void tmr2_isr() // timer 2 ISR
{
if((DataValue != 0) && (bit_transmitted < 8))
{
if(((DataValue >> bit_transmitted) & 0x01) == 0x01) //if bit is high
{
UART_TX = 1;
}
else //if Bit is low
{
UART_TX = 0;
}
bit_transmitted++;
}
if(stop_bit == 1){UART_TX = 1; stop_bit = 0;}
if(bit_transmitted == 8)
{
DataValue = 0;
bit_transmitted = 0;
stop_bit = 1;
}
set_timer2(0); // zero the timer 2 register to minimize error
}
void main()
{
set_rx_tx_direc; //set direction of C0 as tx, C1 as rx
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
setup_timer_2(T2_DIV_BY_4, 130, 1); // timer 2 will overflow at period register value 130, i.e at 104uS
UART_TX = 1; // tx always ON
delay_ms(3);
while(1)
{
DataValue = 'k'; //chr to be sent
set_timer2(0); // set timer 2 register to zero
UART_TX = 0; // set start pulse
delay_ms(100);
}
}
#include "16f877a.h"
#use delay(clock=20000000)
#define Baudrate 9600 //bps
#define one_bit_delay (1000000/Baudrate) //one bit delay
#define DataBitCount 8 // no parity, no flow control
#define set_rx_tx_direc set_tris_c(0b10000010) // UART RX pin direction register
#byte port_c = 0x07
#bit UART_RX = port_c.1
#bit UART_TX = port_c.0
short stop_bit = 0;
char bit_transmitted = 0;
char DataValue = 0;
char rcv_byte = 0;
short byte_sent = 0;
char rcv_data = 0;
char i = 0;
#int_timer2
void tmr2_isr()
{
if((DataValue != 0) && (bit_transmitted < 8))
{
if(((DataValue >> bit_transmitted) & 0x01) == 0x01) //if bit is high h = 0b01101000
{
UART_TX = 1;
}
else //if Bit is low
{
UART_TX = 0;
}
bit_transmitted++;
}
if(stop_bit == 1){UART_TX = 1; stop_bit = 0; byte_sent = 1;}
if(bit_transmitted == 8)
{
DataValue = 0;
bit_transmitted = 0;
stop_bit = 1;
}
//set_timer2(0); //this line makes the code emits junk data on the serial port -
//timer functions used inside the timer ISR causes junk data and code crash
}
char rx_data(void)
{
while(UART_RX==1); //wait for start bit
delay_us(one_bit_delay); //start bit
delay_us(one_bit_delay/2); // Take sample value in the mid of bit duration
for(i = 0; i < DataBitCount; i++)
{
if(UART_RX == 1) //if received bit is high
{
rcv_data = ((rcv_data >> 1) | 0x80);
}
if(UART_RX == 0) //if received bit is low
{
rcv_data = ((rcv_data >> 1) | 0x00);
}
delay_us(one_bit_delay);
}
if(UART_RX == 1) // Check for stop bit //Stop bit should be high
{
delay_us(one_bit_delay/2);
return rcv_data;
}
else //some error occurred !
{
delay_us(one_bit_delay/2);
return 'r';
}
}
void tx_data(char ch)
{
DataValue = ch;
set_timer2(0);
UART_TX = 0;
while(!byte_sent);
byte_sent = 0;
}
void main()
{
set_rx_tx_direc;
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
setup_timer_2(T2_DIV_BY_4, 130, 1);
UART_TX = 1;
delay_ms(3);
while(1)
{
//tx_data("hello_world\n\r");
rcv_byte = rx_data();
delay_ms(100);
tx_data(rcv_byte);
}
}
#include "16f877a.h"
#use delay(clock=20000000)
#define Baudrate 9600 //bps
#define one_bit_delay (1000000/Baudrate) //one bit delay
#define DataBitCount 8 // no parity, no flow control
#define set_rx_tx_direc set_tris_c(0b10000010) // UART RX pin direction register
#byte port_c = 0x07
#bit UART_RX = port_c.1
#bit UART_TX = port_c.0
short stop_bit = 0;
char bit_transmitted = 0;
char DataValue = 0;
char rcv_byte = 0;
short byte_sent = 0;
char rcv_data = 0;
char i = 0;
#int_timer2
void tmr2_isr()
{
if((DataValue != 0) && (bit_transmitted < 8))
{
if(((DataValue >> bit_transmitted) & 0x01) == 0x01) //if bit is high h = 0b01101000
{
UART_TX = 1;
}
else //if Bit is low
{
UART_TX = 0;
}
bit_transmitted++;
}
if(stop_bit == 1){UART_TX = 1; stop_bit = 0; byte_sent = 1;}
if(bit_transmitted == 8)
{
DataValue = 0;
bit_transmitted = 0;
stop_bit = 1;
}
//set_timer2(0); //this line makes the code emits junk data on the serial port -
//timer functions used inside the timer ISR causes junk data and code crash
}
char rx_data(void)
{
while(UART_RX==1); //wait for start bit
delay_us(one_bit_delay); //start bit
delay_us(one_bit_delay/2); // Take sample value in the mid of bit duration
for(i = 0; i < DataBitCount; i++)
{
if(UART_RX == 1) //if received bit is high
{
rcv_data = ((rcv_data >> 1) | 0x80);
}
if(UART_RX == 0) //if received bit is low
{
rcv_data = ((rcv_data >> 1) | 0x00);
}
delay_us(one_bit_delay);
}
if(UART_RX == 1) // Check for stop bit //Stop bit should be high
{
delay_us(one_bit_delay/2);
return rcv_data;
}
else //some error occurred !
{
delay_us(one_bit_delay/2);
return 'r';
}
}
void tx_data(char ch)
{
DataValue = ch;
set_timer2(0);
UART_TX = 0;
while(!byte_sent);
byte_sent = 0;
}
void main()
{
set_rx_tx_direc;
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
setup_timer_2(T2_DIV_BY_4, 130, 1);
UART_TX = 1;
delay_ms(3);
while(1)
{
//tx_data("hello_world\n\r");
rcv_byte = rx_data();
delay_ms(100);
tx_data(rcv_byte);
}
}
i'm sending the character 'k' but i receive '.'
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?