PIC18F47K42 UART Transmission

Author
Magnus512
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/09/20 17:25:09
  • Location: 0
  • Status: offline
2018/10/11 14:26:48 (permalink)
0

PIC18F47K42 UART Transmission

Hello to everyone, I'm tryng to get working the UART module in the PIC18F47K42, I can recieve data, I send the letter T, D and A, and and interruption event ocur, so I guess I have the recieve part working good, but when I try to send something to my serial terminal with the printf command nothing happend I mean I don't see anything in my terminal, any idea about how to get this working ?, (Sorry for my bad english)

// PIC18F47K42 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config CONFIG1L = 0x72

// CONFIG1H
#pragma config CONFIG1H = 0x01

// CONFIG2L
#pragma config CONFIG2L = 0xFF

// CONFIG2H
#pragma config CONFIG2H = 0xAF

// CONFIG3L
#pragma config CONFIG3L = 0xA

// CONFIG3H
#pragma config CONFIG3H = 0x3F

// CONFIG4L
#pragma config CONFIG4L = 0x9F

// CONFIG4H
#pragma config CONFIG4H = 0xF

// CONFIG5L
#pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)

// CONFIG5H

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef INIT_MAIN_H
#define INIT_MAIN_H

#define _XTAL_FREQ 20000000UL
#define CHSEL LATBbits.LATB7
#define INCR LATBbits.LATB6
#define UD LATBbits.LATB5
#define SWITCH4 LATBbits.LATB4
#define SWITCH3 LATBbits.LATB3
#define SWITCH2 LATBbits.LATB2
#define SWITCH1 LATBbits.LATB1

void init_main(void){
    // Port A access
    ANSELA = 0x00; // Set digital I/O (not analog).
    TRISA = 0x00; // Set all bits in port A to be outputs.
    LATA = 0x00; // Initial configuration of Port A (All logic low state).
    WPUA = 0x00; // WPUx registers.
    
    // Port B access
    ANSELB = 0x00; // Set digital I/O (not analog).
    TRISB = 0x00; // Set all bits in port B to be outputs.
    LATB = 0xC0; // Initial configuration of Port B.
    WPUB = 0x00; // WPUx registers.
    
    // Port C access
    ANSELC = 0x00; // Set digital I/O (not analog).
    TRISC = 0x80; // Set all bits in port B to be outputs.
    LATC = 0x00; // Initial configuration of Port B.
    WPUC = 0x00; // WPUx registers.
    
    // Port D access
    ANSELD = 0x00; // Set digital I/O (not analog).
    TRISD = 0x00; // Set all bits in port D to be outputs.
    LATD = 0x00; // Initial configuration of Port D.
    WPUD = 0x00; // WPUx registers.
    
    // Port E access
    ANSELE = 0x00; // Set digital I/O (not analog).
    TRISE = 0x08; // Set all bits in port B to be outputs.
    LATE = 0x00; // Initial configuration of Port B.
    WPUE = 0x00; // WPUx registers.
    
    __delay_ms(2000);
}
void init_modules(void){
    // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC disabled; CRCMD CRC enabled; HLVDMD HLVD enabled; NVMMD NVM enabled PMD0 = 0x01;
    PMD0 = 0x7F;
    // NCO1MD DDS(NCO1) enabled; TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; TMR6MD TMR6 enabled;
    PMD1 = 0xFB;
    // ZCDMD ZCD enabled; DACMD DAC disabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
    PMD2 = 0x41;
    // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; PWM6MD PWM6 enabled; PWM5MD PWM5 enabled; PWM8MD PWM8 enabled; PWM7MD PWM7 enabled;
    PMD3 = 0x00;
    // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled;
    PMD4 = 0xE0;
    // U2MD UART2 disabled; U1MD UART1 enabled; SPI1MD SPI1 disabled; I2C2MD I2C2 disabled; I2C1MD I2C1 disabled;
    PMD5 = 0x27;
    // DSMMD DSM1 enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
    PMD6 = 0x3F;
    // DMA1MD DMA1 enabled; DMA2MD DMA2 enabled;
    PMD7 = 0x03;
}

void init_remapping(void){
    //Pines para UART
    RC6PPS = 0x13; //RC6->UART1:TX1;
    U1RXPPS = 0x17; //RC7->UART1:RX1;
    //Pines para PWM
    RA2PPS = 0x0D; //RA2->PWM5:PWM5;
    RA3PPS = 0x0E; //RA3->PWM6:PWM6;
    RA4PPS = 0x0F; //RA4->PWM7:PWM7;
    RA5PPS = 0x10; //RA5->PWM8:PWM8;
}
#endif /* INIT_MAIN_H */


 
#ifndef INTERRUPTS_H
#define INTERRUPTS_H

void init_interrupts(void){
    INTCON0bits.IPEN = 1; // Enable interrupt priority
    INTCON0bits.GIEH = 1; //Enable Global Interrupts High priority
    INTCON0bits.GIEL = 1;
    PIE0bits.SWIE = 1;
    PIE3bits.U1IE = 1; // Enable UART interrupt
    PIE3bits.U1RXIE = 1; //Enable Recieve UART interrupt;
    IPR3bits.U1RXIP = 1; // High priority
}

#endif /* INTERRUPTS_H */


 
// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef UART_H
#define UART_H

void UART_Init(void){
    ANSELC7 = 0; // RC7 como E/S Digital, RX.
    ANSELC6 = 0; // RC6 como E/S Digital, TX.
    TRISC7 = 1; // RC7 as input.
    TRISC6 = 0; // RC6 as output.
    // Disable interrupts
    PIE3bits.U1RXIE = 0;
    PIE3bits.U1IE = 0;
    // Setup
    U1CON0 = 0xB0; //High speed, transmitter enabled, reciever enabled, Asynchronous 8-bit UART mode.
    U1CON1 = 0x80; //Serial Port enabled, 0x80;
    U1CON2 = 0x00;
    U1BRGH = 0x02; //Baudrate 9600 -> 520 0x208
    U1BRGL = 0X08;
    //Error Registers
    U1ERRIR = 0x00;
    U1ERRIE = 0x00;
    U1UIR = 0x00;
    U1FIFO = 0x00;
    //Parameters
    U1P1H = 0x00; //Not used
    U1P1L = 0x00; //Not used
    U1P2H = 0x00; //Not used
    U1P2L = 0x00; //Not used
    U1P3H = 0x00; //Not used
    U1P3L = 0x00; //Not used
    // Checksum
    U1TXCHK = 0x00;
    U1RXCHK = 0x00;
    PIE3bits.U1RXIE = 1;
    PIE3bits.U1IE = 1;
    //U1CON1bits.ON = 1;
}

char UART_Read(void){
    return(U1RXB);
}

void UART_Write(char data){
    while(0 == PIR3bits.U1TXIF)
    {
    }
    U1TXB = data;
}

#endif /* XC_HEADER_TEMPLATE_H */


main code
/*
 * Author: magnus
 *
 * Created on 25 de septiembre de 2018, 03:45 PM
 */
#include "config.h"
#include <xc.h>
#include "init.h"
#include "interrupts.h"
#include "uart.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int A = 0;
char data;
unsigned int sensor, h, adc_data = 0;
float voltaje, vprom;

void __interrupt(irq(U1RX), base(0x0048)) rx_interrupt(void){
    data = UART_Read();
    if(data == 'T'){
        A = 1;
        SWITCH4 = 1;
    }
    else if(data == 'D'){
        A = 2;
        SWITCH4 = 0;
    }
    else if(data == 'A'){
        A = 2;
        SWITCH4 = 1;
    }
}

void main(void) {
    init_main();
    init_remapping();
    init_modules();
    UART_Init();
    init_interrupts();
    while(1){
        printf("Hola\n ");
    }
    return;
}

/*
 * File: oximetro.c
 * Author: magnus
 *
 * Created on 25 de septiembre de 2018, 03:45 PM
 */
#include "config.h"
#include <xc.h>
#include "init.h"
#include "interrupts.h"
#include "uart.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int A = 0;
char data;
unsigned int sensor, h, adc_data = 0;
float voltaje, vprom;

void __interrupt(irq(U1RX), base(0x0048)) rx_interrupt(void){
    data = UART_Read();
    if(data == 'T'){
        A = 1;
        SWITCH4 = 1;
    }
    else if(data == 'D'){
        A = 2;
        SWITCH4 = 0;
    }
    else if(data == 'A'){
        A = 2;
        SWITCH4 = 1;
    }
}

void main(void) {
    init_main();
    init_remapping();
    init_modules();
    UART_Init();
    init_interrupts();
    while(1){
        printf("Hola\n ");
    }
    return;
}

 
post edited by Magnus512 - 2018/10/11 14:28:24
#1

3 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 7537
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC18F47K42 UART Transmission 2018/10/11 14:35:55 (permalink)
    +1 (1)
    If you look in the XC8 User Guide, you will discover that printf() does not go anywhere until you define a function called putch() to handle outputting each character.
    Just rename your UART_Write() function to putch() and printf() will probably start working.
     
    #2
    Magnus512
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2018/09/20 17:25:09
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 UART Transmission 2018/10/11 15:06:11 (permalink)
    0
    qhb
    If you look in the XC8 User Guide, you will discover that printf() does not go anywhere until you define a function called putch() to handle outputting each character.
    Just rename your UART_Write() function to putch() and printf() will probably start working.
     


    I'm going to try to do this, I have a previous code that does this without the need of the putch() function.. It was for the PIC18F4550
    post edited by Magnus512 - 2018/10/11 15:50:17
    #3
    jtemples
    Super Member
    • Total Posts : 11007
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: PIC18F47K42 UART Transmission 2018/10/11 15:50:09 (permalink)
    +1 (1)
    I would recommend something simple like U1TXB = 'a'; rather than putting a huge blob of software between you and the UART.
    #4
    Jump to:
    © 2018 APG vNext Commercial Version 4.5