PIC 32 baud rate - microcontroller

i have a PIC32MX460F512L. I am setting up a UART communication with raspberry Pi 3. And i made also some tests with mi PC Windows serial terminal (Putty, Teraterm etc.). I am having stucking problems, because the raspberry sends and receives only rubbish. Then i tested it with putty on Windows and i noticed strange things. For example the uart baud rate on the board is set as 9600, and to send and receive data on windows i have to set putty's baud rate to 130 !! Then i had a look to my PCLK settings on pic and it seems like everything is correct since there is an external clk oscillator of 8MHz. I post the code below .
PIC CODE:
#define GetSystemClock() (80000000ul) // Hz
#define GetInstructionClock() (GetSystemClock()/1)
#define GetPeripheralClock() (GetInstructionClock()/1) //
long Baud_Rate;
Baud_Rate = 9600;
TRISFbits.TRISF8 = 0; // Set UART1TX like output
TRISFbits.TRISF2 = 1; // Set UART1RX like input
U1MODE = 0x00008000;
U1STA = 0x00001400; // RXEN set, TXEN set
U1BRG = ((USART_Clock_Source)/(Baud_Rate/16))-1;
May it be a fault on the external oscillator or maybe is there an error on the baud rate calculation? I cannot understand.

Here is an error:
U1BRG = ((USART_Clock_Source)/(Baud_Rate/16))-1;
it should be:
U1BRG = ((USART_Clock_Source)/(Baud_Rate*16))-1;
And this:
#define GetSystemClock() (80000000ul) // Hz
should be
#define GetSystemClock() (8000000ul) // Hz
In this calculation is 8 MHz the instruction frequency not the clock frequency. The instruction frequency is half of the clock.

Related

How to read the reset vector on an ATMEGA328

I have several ATMEGA328 based custom boards: I recently found that the standard Arduino bootloader does not handle watchdog timeouts correctly (it keeps resetting, so the device is effectively bricked until the next power cycle) so I am switching to the Optiboot bootloader.
I want to add something to my app that tells me whether the bootloader is Arduino or Optiboot. As Optiboot is a lot smaller, the reset vector is different, so printing that out would be a good indication.
Serial.println (pgm_read_word_near(0), HEX);
The above line of code prints out 940C, and I was expecting something like 7E00. How can I print out the address from the reset vector?
The actual address of the start of the boot flash section is determined by the BOOTSZ fuses.
This reads the size of the bootloader for classic ATmega:
uint32_t bootloaderSize;
cli();
uint8_t highBits = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
sei();
if (!(highBits & bit(FUSE_BOOTRST))) {
uint8_t v = (highBits & ((~FUSE_BOOTSZ1 ) + (~FUSE_BOOTSZ0 )));
bootloaderSize = MIN_BOOTSZ << ((v >> 1) ^ 3);
}
use #include <avr/boot.h>

Serial.print() truncates data when reading with QSerialPort

I am trying to write a simple integer value from an Arduino Mega 2560 to a Qt Application. Baudrate is set to 9600 and Serial.read() works fine when I send data through an open port with serial->write(some_data); from Qt Application.
digitalWrite(SS, LOW);
if (Serial.available() == 2) {
response1 = SPI.transfer(Serial.read());
response2 = SPI.transfer(Serial.read());
}
digitalWrite(SS, HIGH);
The above code works fine. I read another by before and had to add delay(3) to make this work. Now I want to send back the response
Serial.print((response2 << 8 ) | (response1 & 0xFF));
But there are always truncated digits. I know from the logic analyzer that the response is e.g. 8193 so with QByteArray b = serial->readAll(); I get results like 8, 81, 819, and sometimes 8193. I.e.: always the last digits are truncated randomly. I assume that this is also a timing issue but I could not find a fix for this.
Just in case anyone facing the same issue: Changing the Baudrate from 9600 to 112500 fixed this.

DC motor speed is too low while interfacing with L293D morot Driver and Atmega8

I am trying to drive a 6v dc motor with L293D driver and Atmega8 without PWM. The problem is i am getting very low speed while connecting the motor with L293D driver. But, it rotates well when i provide direct 6V dc supply to the motor. I am using external 6v source at V2(motor supply) pin of L293D, but no improvement. The motor does not rotate until i turn the rotor with hand. Is the problem remaining for not using PWM? My code is here:
DDRB = 0xFF;
while(1)
{
PORTB = 0B00000010;
_delay_ms(20000);
}
i think may be you forgot to make Enable 1 pin high, if your connections is like this than
PB0 --> IN1
Penter code hereenter code here`B1 --> IN2
PB3 --> EN1
and pin 8 of L293d will connected with External Battery.
than code like this :
DDRB = 0xFF;
while(1) {
PORTB = 0B00000110;
_delay_ms(20000);
}

Failed to generate 1 second (BASCOM AVR)

I had a microcontroller trainer kit that includes ATMEGA8535 and 8MHZ xtal. I want to make simple program that switch off/on led every 1 sec.
But it wont work, when I run the trainer, the delay seems to be 7 sec.
here the program with BASCOM AVR.
$regfile = "8535def.dat"
$crystal = 8000000
Config Porta = Output
Config Timer1 = Timer , Prescale = 256
Enable Timer1
On Timer1 Isr_von_timer1
Enable Interrupts
Timer1 = 34285
Do
Loop
End
Isr_von_timer1:
Timer1 = 34285
Toggle Porta.0
Return
I've resolved the issue: I forgot to activate the external xtal :).
The correct value for timer1 using an 8 MHz oscillator with pre-scale=256 to turn on the output for one second and turn off for one second using the toggle command is 31249 (divisor - 1)

Arduino's analogRead() returns junk when I print via serial

I'm making a clock with the Arduino, and I want to make a button to set the time.
So, just to test, I pluged a wire in the Analog Input pin 0 and wrote two lines of code to read the pin and print it via Serial, but all I get is junk.
valm = analogRead(0);
Serial.println(valm);
And what I get from the serial monitor is this:
?j?d?±µ?Ê??jDd?±µ???ºjRd?±µ???ºj?d?±µ?Ê??j?d?±µ?É?ªjRd?±µ???ÊjRd?±µ???Âj?d?±µ????j?d?±µ?É??j
Rd?±µ????j?d?±µ???ºj?d?±µ?É??jRd?±µ????j?d?±µ?Ê?ÊjDd?±µ???Âj?d?±µ????j?d?±µ?É??jRd?±µ????j?d
?±µ?É?ÂjRd?±µ???Êj?d?±µ???ªj?d?±µ?Ê?Êj$d?±µ???ÂjRd?±µ????j?d?±µ????jdd?±µ????jRd?±µ???Âj?d?±
µ?É?ÊjRd?±µ????j?d?±µ?Ò?Êj?d?±µ?É?ÂjRd?±µ????j?d?±µ????j?d?±µ?É??jRd?±µ???Âj?d?±µ???Êj?d?±µ?
??ªj?d?±µ???Êj?d?±µ???ÂjRd?±µ????j?d?±µ????j?d?±µ?É??jRd?±µ???Âj?d?±µ?Ò?Êj?d?±µ?É?ªj?d?±µ?É?
?jTd?±µ???Âj?d?±µ????j?d?±µ?É??jRd?±µ????j?d?±µ?É?ÂjRd?±µ???Êj?d?±µ?É?ªjRd?±µ???Êj?d?±µ???ºj
?d?±µ?È??jTd?±µ????jRd?±µ???ªj?d?±µ?È?ÂjTd?±µ?É?ÊjRd?±µ???ªj?d?±µ???Êj?d?±µ???ºj?d?±µ?É??jRd
?±µ???Êj?d?±µ???ªj?d?±µ?É?ÂjPd?±µ???Êj?d?±µ???ªj?d?±µ?Â?Êj?d?±µ?É?ºjRd?±µ????j?d?±µ?É??jRd?±
µ???ªj?d?±µ?Ê?Âj?d?±µ???ÊjRd?±µ???ªj?d?±µ?Ò?Êj?d?±µ???ºjRd?±µ????j?d?±µ???Êj?d?±µ?Ê?ªjdd?±µ?
??ÂjRd?±µ???Âj?d?±µ?É?ªjPd?±µ???Êj?d?±µ????j?d?±µ?É??jRd?±µ???Êj?d?±µ?É?ªjdd?±µ???ÂjRd?±µ???
Âj?d?±µ?É?ªjDd?±µ???Êj?d?±µ?Ê??j?d?±µ?É??jRd?±µ???ÊjRd?±µ???ªj?d?±µ?Ê?ÂjPd?±µ???Âj?d?±µ???ªj
?d?±µ?É?Êjdd?±µ?É??jRd?±µ????j?d?±µ?É?Êj$d?±µ???ªjRd?±µ???Âj?d?±µ?Â?ÂjTd?±µ???ªj?d?±µ???Êj?d
?±µ????jRd?±µ????j?d?±µ?É?ÊjTd?±µ???ªjRd?±µ???Âj?d?±µ???Âj?d?±µ?É?ªjRd?±µ???ÊjRd?±µ?Ò??jTd?±
µ????j?d?±µ???Êj
Why?
Edit:
I had set the digital pin 0 to output, and I thought maybe that was the problem so I went to the Arduino website and found the page Analog Input Pins.
So I tried to read from pin 14 which they say is the analog input pin 0, but I got the same output.
By the way, I'm trying to read from the analog only pins, not the one wich are also digital.
Try running this sample code found on the Arduino website. You shouldn't need to convert the variable valm or anything.
/*
Analog input
Reads an analog input on analog in 0, and prints the value out.
Created 24 March 2006
by Tom Igoe
*/
int analogValue = 0; // Variable to hold the analog value.
void setup() {
// open the serial port at 9600 bit/s:
Serial.begin(9600);
}
void loop() {
// Read the analog input on pin 0:
analogValue = analogRead(0);
// Print it out in many formats:
Serial.println(analogValue); // print as an ASCII-encoded decimal
Serial.println(analogValue, DEC); // print as an ASCII-encoded decimal
Serial.println(analogValue, HEX); // print as an ASCII-encoded hexadecimal
Serial.println(analogValue, OCT); // print as an ASCII-encoded octal
Serial.println(analogValue, BIN); // print as an ASCII-encoded binary
Serial.println(analogValue, BYTE); // print as a raw byte value
// Delay 10 milliseconds before the next reading:
delay(10);
}
If your output is still gibberish, there's something wrong with your serial terminal.
The problem Here is analog device you might connected are not # same ground potentials.
Example: If your powering on Arduino board using PC and sensor is powered on using other source , But You trying to read value from Arduino port, for this instant it gives above error. Try this power on arduino and sensor from Same power source and try to read data using Serial Port through DOcklight you will get Problem solved.
You need to set the baud rate in the serial monitor window (bottom right corner) to the same value that has been set in your code (e.g. Serial.begin(9600);).
From what your output looks like, it seems like you need to convert valm into a string, so that you can print the value numerically.
Also, is the wire you plugged into analog in floating? Because if it is, it'll just act like an antenna and grab random noise (though, might have a strong 60Hz component).

Resources