ATmega328P TXEN0 and RXEN0, pins PD1 and PD0?

Thread Starter

kadawn

Joined Oct 3, 2018
7
Hi,

I've read the microcontroller's datasheet and programmed a sender and receiver of ADC data in the same ATmega328P.
As I don't have two of them, my testing of both sending and receiving sides consists of connecting the PD0 and PD1 pins, since those are supposed to be the UART pins once TXEN0 and RXEN0 are set in the corresponding register.
So far I've got nothing in the receiving side and I know the UART is transmitting. Am I missing a flag or do I have to set other pins?
Code:
// includes
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <util/delay.h>

int CLEAR = 0;
int TRANS = 0;
uint8_t RECV_TEMP = 0;
uint8_t SENT_TEMP = 0;

struct USART_configuration
{
    uint16_t baudrate;
    uint8_t databits;
    uint8_t parity;
    uint8_t stopbits;
};


// Call once to initialise USART communication
uint8_t USART_Init(struct USART_configuration config);

struct node_rx {
    volatile uint8_t val;
    struct node_rx * next;
};

struct node_tx {
    volatile uint8_t val;
    struct node_tx * next;
};


volatile struct node_tx * head;
volatile struct node_rx * headd;

void deleteNode(struct node_tx **head_ref, int position) ;
void deleteNoderx(struct node_rx **head_ref, int position);
void USART_Transmit_char(uint8_t data);
uint8_t adcread(void);


// This function prints contents of linked list starting from
// the given node
void printList(volatile struct node_tx *node)
{
    while (node != NULL)
    {
        USART_Transmit_char(node->val);
        node = node->next;
    }
}

/* Counts no. of nodes in linked list */
int getCount(volatile struct node_rx * head)
{
    int count = 0;  // Initialize count
    volatile struct node_rx * current = head;  // Initialize current
    while (current != NULL)
    {
        count++;
        current = current->next;
    }
    return count;
}
/* Counts no. of nodes in linked list */
int getCounttx(volatile struct node_tx * head)
{
    int count = 0;  // Initialize count
    volatile struct node_tx * current = head;  // Initialize current
    while (current != NULL)
    {
        count++;
        current = current->next;
    }
    return count;
}

void pushtx(volatile struct node_tx ** head, int val) {
    /* now we can add a new variable */
    struct node_tx * new_node = (struct node_tx*)malloc(sizeof(struct node_tx));
    volatile struct node_tx *last = *head;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->val  = val;

    /* 3. This new node is going to be the last node, so make next
          of it as NULL*/
    new_node->next = NULL;

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head == NULL)
    {
       *head = new_node;
       return;
    }
   
    /* 5. Else traverse till the last node */
    while (last->next != NULL) {
        last = last->next;
    }
    /* 6. Change the next of last node */
    last->next = new_node;
    return;
}

void pushrx(volatile struct node_rx ** head, int val) {
    /* now we can add a new variable */
    struct node_rx * new_node = (struct node_rx*)malloc(sizeof(struct node_rx));
    volatile struct node_rx *last = *head;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->val  = val;

    /* 3. This new node is going to be the last node, so make next
          of it as NULL*/
    new_node->next = NULL;

    //USART_Transmit_char(new_node->val);

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head == NULL)
    {
       *head = new_node;
       //USART_Transmit_char('e');
       return;
    }
   
    /* 5. Else traverse till the last node */
    while (last->next != NULL) {
        last = last->next;
    }
    /* 6. Change the next of last node */
    last->next = new_node;
    return;
}


void mainee(void){
    SENT_TEMP = adcread();
    //RECV_TEMP = SENT_TEMP;
    pushtx(&head, SENT_TEMP);
    //UCSR0A |= (0 << TXC0); //set flag for tx interrupt
    USART_Transmit_char('\0');
    //char character = getCounttx(head) + '0';
    //USART_Transmit_char(character);
    //printList(head);
    //USART_Transmit_char(SENT_TEMP);

    OCR0A = RECV_TEMP;

    if (CLEAR == 1){
        //for( a = 0; a <= i; a = a + 1 ){
        deleteNode(&head, 0);
        //}
        CLEAR = 0;
    }
    //char character = getCount(headd) + '0';
    //USART_Transmit_char(character);
    int i = 0;
    if ((TRANS == 1)&&(CLEAR == 0)){
        //printList(headd);
        //volatile node_tx * current = head;
        volatile struct node_rx * curr = headd;
        while ((curr) != NULL) { // set curr to head, stop if list empty.
            //USART_Transmit_char('o');
            RECV_TEMP = curr->val;
            USART_Transmit_char('\r');
            USART_Transmit_char(RECV_TEMP);
            curr = curr->next;          // advance head to next element.
            i++;
            //free(curr->val);
        }
        for(int a = 0; a <= i; a++ ){
            //char character = getCount(headd) + '0';
            //USART_Transmit_char(character);
            deleteNoderx(&headd, 0);
        }
        TRANS = 0;
        //USART_Transmit_char(' ');
        //UDR0 = ' ';
    }
}
 
Top