nairitb
Junior Member level 1
- Joined
- Mar 25, 2014
- Messages
- 18
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 154
I am using a prototype board designed and fabricated by me. Crystal is 24MHz.
Board is working. Can blink LEDs and enable PWM module.
However, I am currently stuck in UART module. I am connecting "U1Tx To Pin RP64" & "U1Rx To Pin RPI75". The program is to receive data from pc and if the received byte is "a" then transmit "b" back to pc. Its not working.
The debugger (ICD3) is not working for this program (dont know why). So I decided to use LEDs connected at RF0, RF1, RF2 & RF3 to see where the program gets stuck. Found that the program gets stuck at if(U1STAbits.FERR == 1) indicating "Framing error has been detected for the current character". But I did not send any data from the pc.
LEDs connected at RF0 & RF2 are ON but LEDs connected at RF1 & RF3 are OFF.
Can someone please look into the program and point out my mistake?
Thanks and regards,
Nairit
Board is working. Can blink LEDs and enable PWM module.
However, I am currently stuck in UART module. I am connecting "U1Tx To Pin RP64" & "U1Rx To Pin RPI75". The program is to receive data from pc and if the received byte is "a" then transmit "b" back to pc. Its not working.
The debugger (ICD3) is not working for this program (dont know why). So I decided to use LEDs connected at RF0, RF1, RF2 & RF3 to see where the program gets stuck. Found that the program gets stuck at if(U1STAbits.FERR == 1) indicating "Framing error has been detected for the current character". But I did not send any data from the pc.
LEDs connected at RF0 & RF2 are ON but LEDs connected at RF1 & RF3 are OFF.
Can someone please look into the program and point out my mistake?
Code:
// Crytal is 24MHz.
// Configure the oscillator to operate the device at 51 MIPS using PLL
#define _XTAL_FREQ 24000000
#define FOSC (102000000)
#define FCY (FOSC/2)
//#if defined(__dsPIC33E__)
//#include "p33exxxx.h"
//#endif
#include <stdio.h>
#include <stdlib.h>
#include <p33EP512MU810.h>
//#include <delay.h>
#include <libpic30.h>
// DSPIC33EP512MU810 Configuration Bit Settings
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)
// FOSCSEL
#pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection Bits (Internal Fast RC (FRC))
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Mode Select bits (HS Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow only one reconfiguration)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = OFF // Brown-out Reset (BOR) Detection Enable bit (BOR is disabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)
// FICD
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)
#define FP 51000000
#define BAUDRATE 9600
#define BRGVAL ((FP/BAUDRATE)/16) - 1
int main(void)
{
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
TRISF=0;
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD= 32; // M=34
CLKDIVbits.PLLPOST= 0; // N2=2
CLKDIVbits.PLLPRE= 2; // N1=4
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
//Oscillator select
__builtin_write_OSCCONH(0x03);
// Clock Switch
__builtin_write_OSCCONL(OSCCON | 0x01); //turn on OSWEN
while (OSCCONbits.OSWEN); //wait until OSWEN to low
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b011);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
PORTFbits.RF2 = 1;
// Configure Input Functions
//*************************************************************
//OSCCONL Unlock Sequence
__builtin_write_OSCCONL(0x46);
__builtin_write_OSCCONL(0x57);
// Assign U1Rx To Pin RPI75
//***************************
RPINR18bits.U1RXR = 0x4B;
//***************************
//*************************************************************
// Configure Output Functions
//*************************************************************
// Assign U1Tx To Pin RP64
//***************************
RPOR0bits.RP64R = 1;
//***************************
//Set IOLOCK as a single operation.
OSCCONbits.IOLOCK = 1;
PORTFbits.RF0 = 1;
// Configure oscillator as needed
U1MODEbits.STSEL = 0; // 1-stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
U1MODEbits.ABAUD = 0; // Auto-Baud disabled
U1MODEbits.BRGH = 0; // Standard-Speed mode
U1BRG = BRGVAL; // Baud Rate setting for 9600
U1STAbits.URXISEL = 0; // Interrupt after one RX character is received;
U1MODEbits.UARTEN = 1; // Enable UART
char ReceivedChar;
while(1)
{
while(U1STAbits.URXDA == 0);
/* Check for receive errors */
if(U1STAbits.FERR == 1)
{
continue;
PORTFbits.RF3 = 1;
}
/* Must clear the overrun error to keep UART receiving */
if(U1STAbits.OERR == 1)
{
U1STAbits.OERR = 0;
continue;
}
/* Get the data */
//if(U1STAbits.URXDA == 1)
//{
ReceivedChar = U1RXREG;
//}
if(ReceivedChar == 'a')
{
PORTFbits.RF1 = 1;
U1STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
U1STAbits.UTXISEL1 = 0;
IEC0bits.U1TXIE = 1; // Enable UART TX interrupt
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART TX
U1TXREG = 'b';
}
}
}
Thanks and regards,
Nairit