I'm experimenting with standby mode on STM32F4O7VGT6. I know how to put MCU into standby mode(or I think to know that). I'm doing that with this init function:
void MX_RTC_Init(void) {
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/** Initialize RTC Only */ hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv =127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_WAKEUP;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK) {
Error_Handler();
}
/** Initialize RTC and set the Time and Date */
sTime.Hours = 0x13;
sTime.Minutes = 0x20;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 0x30;
sDate.Year = 0x20;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
}
This init function allows to weakup MCU from standby mode with wakeup pin(PA0) and with timer(currently period is ~20sec). Next function is called when I want to put MCU into standby mode:
void Enter_StandbyMode(void)
{
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 40960, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
HAL_PWR_EnterSTANDBYMode();
}
Again this works as I expect(I used diode to indicate when MCU wakeup from standby mode by timer) but my question is next one:
Why I still have voltage on pins when MCU is in standby mode? And when I use multimeter to measure current I got 10-20mA power consumption.
Related
I would like to enable EXTI interrupts in order to have information on whether the port has changed its state.
As you can see below, I've configured 2 pins, GPIOA5 and GPIOB3. Both have been configured with pullup resistors, so they should change state when they are connected to the ground.
The problem is that everything works for GPIOA5 but doesn't work for GPIOB3. The isr for GPIO5 is being called, but for GPIOB isn't.
I commented part of code regarding UART configuration as it's not relevant in this case - uart works fine, so I can see messages going out from MCU.
What could be a problem here?
I'm using STM32f411CEU6 and libopencm3.
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/flash.h>
#include <libopencm3/cm3/systick.h>
volatile bool sensor_isr_process { false };
volatile bool sensor_isr_process2 { false };
extern "C" {
void exti3_isr()
{
sensor_isr_process2 = true;
exti_reset_request(EXTI3);
}
void exti9_5_isr()
{
sensor_isr_process = true;
exti_reset_request(EXTI5);
}
}
void configure_exti(uint32_t nvicirq, uint32_t exti, uint32_t gpioport)
{
nvic_enable_irq(nvicirq);
exti_select_source(exti, gpioport);
exti_set_trigger(exti, EXTI_TRIGGER_FALLING);
exti_enable_request(exti);
}
int main()
{
//Setup Clock
auto clock = rcc_clock_scale{};
clock.pllm = 8;
clock.plln = 100; //336;
clock.pllp = 2; //4;
clock.pllq = 4; //7;
clock.pllr = 2; //0;
clock.pll_source = RCC_CFGR_PLLSRC_HSI_CLK;
clock.hpre = RCC_CFGR_HPRE_DIV_NONE; //?
clock.ppre1 = RCC_CFGR_PPRE_DIV_2; // ?
clock.ppre2 = RCC_CFGR_PPRE_DIV_NONE; // ?
clock.voltage_scale = PWR_SCALE1; //?
clock.flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN |
FLASH_ACR_LATENCY_2WS;
clock.ahb_frequency = 100000000;
clock.apb1_frequency = 50000000;
clock.apb2_frequency = 100000000;
rcc_clock_setup_pll(&clock);
//Enable GPIO
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
//Setup GPIO
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO5);
gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO3);
configure_exti(NVIC_EXTI9_5_IRQ, EXTI5, GPIOA);
configure_exti(NVIC_EXTI3_IRQ, EXTI3, GPIOB);
while(1)
{
if (sensor_isr_process) {
// trace(1, "Sensor 1\r\n"); //sends message by uart
sensor_isr_process = false;
}
else if (sensor_isr_process2) {
// trace(1, "Sensor 2\r\n"); //send message by uart
sensor_isr_process2 = false;
}
}
}
I'm not familiar with libopencm3, but normally EXTI mappings are configured in SYSCFG_EXTICR registers and in order to access them, SYSCFG clock needs to be enabled.
If you inspect its source file, you can see that exti_select_source() function doesn't enable SYSCFG clock itself. So probably you need to do it yourself by calling the relevant function:
rcc_periph_clock_enable(RCC_SYSCFG);
I have written a code with interrupts to control the servo but my servos are not working. One is meant to work with interrupt but the other one has to simply move but it is also not working. Even the serial window shows that the code is working properly ut the servos aren't moving I have checked both my servos with same connections and Sweep example of Arduino and both work fine.
#include <TimerOne.h> // Header file for TimerOne library
#include <Servo.h>
#define trigPin 12 // Pin 12 trigger output
#define echoPin 2 // Pin 2 Echo input
#define echo_int 0 // Interrupt id for echo pulse
#define TIMER_US 50 // 50 uS timer duration
#define TICK_COUNTS 4000 // 200 mS worth of timer ticks
volatile long echo_start = 0; // Records start of echo pulse
volatile long echo_end = 0; // Records end of echo pulse
volatile long echo_duration = 0; // Duration - difference between end and start
volatile int trigger_time_count = 0; // Count down counter to trigger pulse time
volatile long range_flasher_counter = 0; // Count down counter for flashing distance LED
int sound = 250;
Servo servo1; //Servos
Servo servo2;
const int button1 = 10; //Buttons
const int button2 = 8;
const int button3 = 13;
const byte interruptPin = 3;
int pos;
void setup() {
servo1.attach(9); // servo for arm
servo2.attach(5); // servo for base
pinMode(trigPin, OUTPUT); // Trigger pin set to output
pinMode(echoPin, INPUT); // Echo pin set to input
// Onboard LED pin set to output
Timer1.initialize(TIMER_US); // Initialise timer 1
Timer1.attachInterrupt( timerIsr ); // Attach interrupt to the timer service routine
attachInterrupt(echo_int, echo_interrupt, CHANGE);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin),Metal_detected, HIGH);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
servo1.write(0); // These will make the servos move to the mapped angles
servo2.write(90);
distance_checking();
if(digitalRead(button1) == HIGH)
{
while(digitalRead(button2) == LOW)
{
Serial.println("Entering Sweeping mode");
for (pos = 30; pos <= 150; pos += 1)
{ Serial.print("Angle is :");
Serial.println(pos);
servo2.write(pos);
distance_checking();
//delay(0.1); // waits 15ms for the servo to reach the position
if(digitalRead(button2) == HIGH)
{
Serial.print("Exiting Sweeping");
goto label;}
}
for (pos = 150; pos >= 30; pos -= 1) { // goes from 180 degrees to 0 degree
Serial.print("Angle is :");
Serial.println(pos);
servo2.write(pos); // tell servo to go to position in variable 'pos'
distance_checking();
//delay(0.1); // waits 15ms for the servo to reach the position
if(digitalRead(button2) == HIGH)
{
goto label;
Serial.print("Exiting Sweeping");}
}
}
}
//reset th
label:
if(digitalRead(button2) == HIGH){
servo1.write(0);
servo2.write(90);
Serial.println("press the sweeping button to enter sweeeping mode");
delay(300);
}
}
void distance_checking()
{
if (echo_duration/58 <= 20)
{
Serial.println("the servo angle is 30");
servo1.write(30);
delay(1500);
}
else {
servo1.write(0);
}
delay(500);
}
void Metal_detected()
{if(digitalRead(button2) == LOW)
{delay(5000);
Serial.print("Metal detected at servo angle:");
Serial.println(servo2.read());
servo1.write(servo1.read());
servo2.write(servo2.read());
Serial.println("Motion is stopped");
Serial.println("Press reset to go to the home position");
}
//while(digitalRead(button2) == HIGH)
// {
// Serial.print("Reseting");
// return 0;}
}
void timerIsr()
{
trigger_pulse(); // Schedule the trigger pulses
// Flash the onboard LED distance indicator
}
// --------------------------
// trigger_pulse() called every 50 uS to schedule trigger pulses.
// Generates a pulse one timer tick long.
// Minimum trigger pulse width for the HC-SR04 is 10 us. This system
// delivers a 50 uS pulse.
// --------------------------
void trigger_pulse()
{
static volatile int state = 0; // State machine variable
if (!(--trigger_time_count)) // Count to 200mS
{ // Time out - Initiate trigger pulse
trigger_time_count = TICK_COUNTS; // Reload
state = 1; // Changing to state 1 initiates a pulse
}
switch(state) // State machine handles delivery of trigger pulse
{
case 0: // Normal state does nothing
break;
case 1: // Initiate pulse
digitalWrite(trigPin, HIGH); // Set the trigger output high
state = 2; // and set state to 2
break;
case 2: // Complete the pulse
default:
digitalWrite(trigPin, LOW); // Set the trigger output low
state = 0; // and return state to normal 0
break;
}
}
// --------------------------
// echo_interrupt() External interrupt from HC-SR04 echo signal.
// Called every time the echo signal changes state.
//
// Note: this routine does not handle the case where the timer
// counter overflows which will result in the occassional error.
// --------------------------
void echo_interrupt()
{
switch (digitalRead(echoPin)) // Test to see if the signal is high or low
{
case HIGH: // High so must be the start of the echo pulse
echo_end = 0; // Clear the end time
echo_start = micros(); // Save the start time
break;
case LOW: // Low so must be the end of hte echo pulse
echo_end = micros(); // Save the end time
echo_duration = echo_end - echo_start; // Calculate the pulse duration
break;
}
}
I tested my motors with this code one by one and they worked absolutely fine:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
In MPLAB X IDE v5.10, I am using dspic33ep512mu810 microcontroller.
I have following piece of C code:
#include "xc.h"
_FOSCSEL(FNOSC_FRCPLL) //INT OSC with PLL (always keep this setting)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE) //disable external OSC
_FWDT(FWDTEN_OFF) //watchdog timer off
_FICD(JTAGEN_OFF & 0b11); //JTAG debugging off
void UART2TX(char c) {
if (U2STAbits.UTXEN == 0)
U2STAbits.UTXEN = 1; //enable UART TX
while (U2STAbits.UTXBF == 1); //if buffer is full, wait
U2TXREG = c;
}
int main(void) {
//setup internal clock for 80MHz/40MIPS
//7.37/2=3.685*43=158.455/2=79.2275
CLKDIVbits.PLLPRE = 0; // PLLPRE (N2) 0=/2
PLLFBD = 41; //pll multiplier (M) = +2
CLKDIVbits.PLLPOST = 0; // PLLPOST (N1) 0=/2
while (!OSCCONbits.LOCK); //wait for PLL ready
_U2TXIF = 0;
_U2TXIE = 0;
_U2RXIF = 0;
_U2RXIE = 0;
//setup UART
U2BRG = 85; //86#80mhz, 85#79.xxx=115200
U2MODE = 0; //clear mode register
U2MODEbits.BRGH = 1; //use high percison baud generator
U2STA = 0; //clear status register
//DSPIC33EP512MU810T-I/PT, RP96 as TX pin
RPOR7bits.RP96R = 3; //
while (1) {
UART2TX('H');
}
}
I am trying to send out 'H' through UART2 with baud rate 115200, but it is not working.
You had to switch RF0 (RP96) aus output:
TRISFbits.TRISF0 = 0; //make F0 an onput
And you had to switch RFO to digital:
ANSELFbits.ANSF0 = 0; //make F0 digital
I have created a project using cube mx and want to use the uart4 tx and rx to send and receive bytes in interrupt mode.
I have :
uint8_t buffer[] = "test\r\n";
if(HAL_UART_Transmit_IT(&huart4, (uint8_t*)buffer, strlen(buffer))!= HAL_OK)
{
}
The uart initialisation is
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 9600;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
The call to Transmit never returns and just sits there.
In the msp file I have
HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART4_IRQn);
and have in the it file
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */
/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */
/* USER CODE END UART4_IRQn 1 */
}
what am I missing?
the problem is related to uint8_t buffer[] = "test\r\n";
the right side is char and the left side is uint8. So, you must do as follows
char buffer[] = "test\r\n";
if(HAL_UART_Transmit_IT(&huart4, (char*)buffer, strlen(buffer))!= HAL_OK)
{
}
or you can do this (use sizeof for int8_t not strlen)
unit8_t buffer[] = "test\r\n";
if(HAL_UART_Transmit_IT(&huart4, (uint8_t*)buffer, sizeof(buffer))!= HAL_OK)
{
}
please add "string.h" and in the first line of you code
Adding a delay too allow the data to be sent fixed this problem. HAL_Delay(100)
I am using dsPic33EP512GM604. I have designed a test circuit to test UART Communication.
I have downloaded a sample code from Microchip website and modified accordingly for my device and circuit designed.
I am facing two issues while debugging.
PLL settings not working. Everytime it gets stuck at " while( OSCCONbits.COSC != 0b011 ); ". Hence I commented the clock configuration and Using simple Internal Oscillator FRC.
UART communication is not working. I m using RPI25 as an RX while RP20 as TX on my circuit.
Here is the final code I am using :
/*******************************************************************************/
#include <xc.h>
#include <stdint.h>
#if __XC16_VERSION < 1011
#warning "Please upgrade to XC16 v1.11 or newer."
#endif
//-----------------------------------------------------------------------------
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FPOR
#pragma config BOREN = ON // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25 // Watchdog Window Select bits (WDT Window is 25% of WDT period)
// 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 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)
// FOSC
#pragma config POSCMD = NONE // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)
// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config PWMLOCK = ON // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF // General Segment Code-Protect bit (General Segment Code protect is Disabled)
// *****************************************************************************
#define TRUE 1
#define FALSE 0
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop();// 105uS delay
// *****************************************************************************
#define FCY 60000000
#define BAUDRATE 9600
#define BRGVAL ( (FCY / BAUDRATE) / 16 ) - 1
uint8_t s3flag, s4flag, s5flag, S6Flag;
/*****************************************************************************/
void __attribute__ ( (interrupt, no_auto_psv) ) _U1RXInterrupt( void )
{
LATA = U1RXREG;
U1TXREG = LATA;
IFS0bits.U1RXIF = 0;
}
/******************************************************************************/
void __attribute__ ( (interrupt, no_auto_psv) ) _U1TXInterrupt( void )
{
IFS0bits.U1TXIF = 0;
}
/******************************************************************************/
void InitClock( void )
{
PLLFBD = 58; // M = 60
CLKDIVbits.PLLPOST = 0; // N1 = 2
CLKDIVbits.PLLPRE = 0; // N2 = 2
OSCTUN = 0;
RCONbits.SWDTEN = 0;
// Clock switch to incorporate PLL
__builtin_write_OSCCONH( 0x03 ); // Initiate Clock Switch to
// External oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 ); // Start clock switching
while( OSCCONbits.COSC != 0b011 );
// Wait for Clock switch to occur
while( OSCCONbits.LOCK != 1 )
{ };
}
/******************************************************************************/
void InitUART2( void )
{
// configure U1MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U1MODEbits.notimplemented;// Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U1MODEbits.notimplemented;// Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
U1BRG = BRGVAL; // 60Mhz osc, 9600 Baud
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
//U1STAbits.notimplemented = 0;//Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
IPC7 = 0x4400; // Mid Range Interrupt Priority level, no urgent reason
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
// RPOR1bits.RP36R = 1; //RB4 as U1TX
// RPINR18bits.U1RXR = 24; //RA8 as U1RX
RPOR0bits.RP20R = 1; // dsPic33EP512GM604 => RP20 as U1TX
_U1RXR = 19; // dsPic33EP512GM604 => RPI25 as U1RX
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
}
/******************************************************************************/
void InitPorts( void )
{
ANSELA = 0;
// TRISAbits.TRISA9 = 1;
// TRISAbits.TRISA4 = 0;
TRISAbits.TRISA10 = 0; //Output
}
/******************************************************************************
int main( void )
{
char recChar = 'a';
int i = 0;
// int count = 0;
// InitClock(); // This is the PLL settings
InitUART2(); // Initialize UART2 for 9600,8,N,1 TX/RX
InitPorts(); // LEDs outputs, Switches Inputs
/* Wait at least 105 microseconds (1/9600) before sending first char */
DELAY_105uS;
while( 1 )
{
PORTAbits.RA10 = 0;
for (i = 0; i < 1000; i++){
DELAY_105uS;
}
U1TXREG = recChar;
recChar++;
if (recChar == 122){
recChar = 48;
}
if (U1STAbits.OERR == 1){
U1STAbits.OERR = 0;
continue;
}
PORTAbits.RA10 = 0;
for (i = 0; i < 1000; i++){
DELAY_105uS;
}
}
}
/*******************************************************************************
I have tested the Circuit by adding LED at RA10 and its working. So, I guess there might be error in my code.
Got the Issues. Now its working
Edit the Test Code as follow :
void InitClock( void )
{
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 63; // M=65
CLKDIVbits.PLLPOST = 0; // N2=2
CLKDIVbits.PLLPRE = 0; // N1=2
// Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(OSCCON | 0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b001);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
}
void InitUART2( void )
{
// configure U1MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U1MODEbits.notimplemented;// Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
// Load a value into Baud Rate Generator.
U1BRG = BRGVAL; // 60Mhz osc, 9600 Baud
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
IPC7 = 0x4400; // Mid Range Interrupt Priority level, no urgent reason
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
RPOR0bits.RP20R = 1; // dsPic33EP512GM604 => RP20 as U1TX
_U1RXR = 0x19; // dsPic33EP512GM604 => RPI25 as U1RX
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
}
Applying these changes made my code working.