EUSART configuration issues...
Posted: Fri Dec 19, 2014 5:14 am
I'm attempting to learn the C programming language and micro-controller applications concurrently. Recently, I purchased an Olimex PIC-USB-STK board with the objective of learning the interfacing related to all of the functions on the board.
https://www.olimex.com/Products/PIC/Dev ... SB-STK.pdf
So far, I've written and successfully run a "Blinky.c" program. My next goal is to configure the EUSART module for communication with Tera Term so that I can create more advanced programs. Unfortunately, I'm having trouble configuring the EUSART module and writing a program that is useful as well as flexible enough to get me through the rest of the lessons that I have envisioned.
In the book I'm reading about the C programming language, there are numerous references to printf, scanf, and all sorts of other UNIX related library functions that don't necessarily equate to functions in the XC8 compiler, or maybe I'm wrong? I'm at a point now where learning more UNIX related C isn't going to help me with micro-controllers or their related compilers.
From what I've gathered in this forum and from the internetl, writing a program that echoes back the same character via RS232 communication is probably a good step in the right direction for me. I've attempted to write a program that echoes back a character and this is about as far as I've gotten. I haven't been able to get my program to compile correctly and churning through Microchip application notes, datasheets, web tutorials and the like just isn't cutting it. I've read the data sheet and feel that I understand the architecture, but can't figure out how to get around sending 8 bits at a time instead of printf("Hello World\n") or scanf.
**A gentleman named RIC on the MICROCHIP forums sent me here and suggested that I send "U" characters to the Tera Term terminal instead of trying to echo a character back and forth. The following code is my attempt at doing so.
Here is my code so far:
And:
https://www.olimex.com/Products/PIC/Dev ... SB-STK.pdf
So far, I've written and successfully run a "Blinky.c" program. My next goal is to configure the EUSART module for communication with Tera Term so that I can create more advanced programs. Unfortunately, I'm having trouble configuring the EUSART module and writing a program that is useful as well as flexible enough to get me through the rest of the lessons that I have envisioned.
In the book I'm reading about the C programming language, there are numerous references to printf, scanf, and all sorts of other UNIX related library functions that don't necessarily equate to functions in the XC8 compiler, or maybe I'm wrong? I'm at a point now where learning more UNIX related C isn't going to help me with micro-controllers or their related compilers.
From what I've gathered in this forum and from the internetl, writing a program that echoes back the same character via RS232 communication is probably a good step in the right direction for me. I've attempted to write a program that echoes back a character and this is about as far as I've gotten. I haven't been able to get my program to compile correctly and churning through Microchip application notes, datasheets, web tutorials and the like just isn't cutting it. I've read the data sheet and feel that I understand the architecture, but can't figure out how to get around sending 8 bits at a time instead of printf("Hello World\n") or scanf.
**A gentleman named RIC on the MICROCHIP forums sent me here and suggested that I send "U" characters to the Tera Term terminal instead of trying to echo a character back and forth. The following code is my attempt at doing so.
Here is my code so far:
- Code: Select all
// PIC18F4550 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
// CONFIG1H
#pragma config FOSC = INTOSC_HS // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
And:
- Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "USART.h"
void setup (void)
{
/*The pins of the Enhanced USART are multiplexed
*with PORTC. In order to configure RC6/TX/CK and
*RC7/RX/DT/SDO as an EUSART:
*/
RCSTAbits.SPEN = 1; //SPEN - Serial port enable bit
TRISC = 0b11000000;
/*The operation of the Enhanced USART module is
*controlled through three registers:
*Transmit Status and Control (TXSTA)
*Receive Status and Control (RCSTA)
*Baud Rate Control (BAUDCON)
*/
TXSTAbits.TXEN = 1; //Transmit Enable Bit
TXSTAbits.SYNC = 0; //EUSART Mode 1 = Sync 0 = Async
// See above for SPEN - Serial port enable bit
RCSTAbits.CREN = 1; //Continuous Receive Enable Bit 1 = enables receiver
BAUDCONbits.ABDEN = 1; //Enable baud rate measurement on the next character
}
void main()
{
void setup();
unsigned char data1;
while(PIR1bits.TXIF == 0);
data1 = 0x55; //U character
TXREG = data1;
}