This code should send letters L and H through serial port 0 and make bit 2 of port D high and low, unfortunately it does not give this result.
#define F_CPU 16000000UL
#define BAUD_RATE 57600
#define UBRR (F_CPU / (16UL * BAUD_RATE) - 1)
#include <xc.h>
int c = 1;
int d = 5;
int sum(int a, int b);
void initUsart();
void usartTransmitChar(unsigned char data);
int main(void) {
initUsart();
DDRD |= (1 << 2);
while (1) {
if (sum(c, d) == 6)
{
PORTD |= (1 << 2);
c = 2;
usartTransmitChar('H');
}
else
{
PORTD &= ~(1 << 2);
c = 1;
usartTransmitChar('L');
}
}
return 0;
}
int sum(int a, int b)
{
return a + b;
}
void initUsart()
{
unsigned int ubrr = UBRR;
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
void usartTransmitChar(unsigned char data)
{
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = data;
}
How can I do this correctly?
New contributor
Programmer 81 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4