Recent

Author Topic: What values to change for the ATtiny2313A ?  (Read 1614 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 270
What values to change for the ATtiny2313A ?
« on: October 26, 2023, 03:27:54 pm »
Code: Pascal  [Select][+][-]
  1. unit ufp_uartserial;

Code: Pascal  [Select][+][-]
  1.   {$elseif defined(fpc_mcu_atmega16a) or defined(fpc_mcu_attiny2313)}
  2.       {$DEFINE UART_UCSRA := UCSRA }
  3.       {$DEFINE UART_UCSRB := UCSRB }
  4.       {$DEFINE UART_UCSRC := UCSRC }
  5.       {$DEFINE UART_UBRRL := UBRRL }
  6.       {$DEFINE UART_UBRRH := UBRRH }
  7.       {$DEFINE UART_UDR   := UDR   }
  8.       {$DEFINE UART_UDRE  := UDRE  }
  9.       {$DEFINE UART_TXEN  := TXEN  }
  10.       {$DEFINE UART_RXEN  := RXEN  }
  11.       {$DEFINE UART_RXC   := RXC   }
  12.       {$DEFINE UART_U2X   := U2X   }
  13.     {$endif}
  14.      

Does anybody know what values to change for the ATtiny2313A ?

I am only receiving garbage

Friendly greets, Wouter, PascalByThree

PS: Running at 8 MHZ and 19200 BAUD

Code: Pascal  [Select][+][-]
  1.     unit ufp_uartserial;
  2.      
  3.     {
  4.      
  5.       UART AVR.
  6.      
  7.       Copyright (C) 2022 Dimitrios Chr. Ioannidis.
  8.         Nephelae - https://www.nephelae.eu
  9.      
  10.       https://www.nephelae.eu/
  11.      
  12.       Licensed under the MIT License (MIT).
  13.       See licence file in root directory.
  14.      
  15.       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  16.       ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  17.       TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  18.       PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  19.       SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  20.       ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21.       ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.       OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.       OTHER DEALINGS IN THE SOFTWARE.
  24.      
  25.     }
  26.      
  27.     {$mode objfpc}
  28.     {$modeswitch result-}
  29.     {$modeswitch advancedrecords}
  30.     {$longstrings off}
  31.     {$writeableconst off}
  32.     {$inline on}
  33.     {$macro on}
  34.      
  35.     {$if defined(fpc_mcu_atmega328p) }
  36.       {$DEFINE UART_UCSRA := UCSR0A }
  37.       {$DEFINE UART_UCSRB := UCSR0B }
  38.       {$DEFINE UART_UCSRC := UCSR0C }
  39.       {$DEFINE UART_UBRRL := UBRR0L }
  40.       {$DEFINE UART_UBRRH := UBRR0H }
  41.       {$DEFINE UART_UDR   := UDR0   }
  42.       {$DEFINE UART_UDRE  := UDRE0  }
  43.       {$DEFINE UART_TXEN  := TXEN0  }
  44.       {$DEFINE UART_RXEN  := RXEN0  }
  45.       {$DEFINE UART_RXC   := RXC0   }
  46.       {$DEFINE UART_U2X   := U2X0   }
  47.     {$elseif defined(fpc_mcu_atmega16a) or defined(fpc_mcu_attiny2313)}
  48.       {$DEFINE UART_UCSRA := UCSRA }
  49.       {$DEFINE UART_UCSRB := UCSRB }
  50.       {$DEFINE UART_UCSRC := UCSRC }
  51.       {$DEFINE UART_UBRRL := UBRRL }
  52.       {$DEFINE UART_UBRRH := UBRRH }
  53.       {$DEFINE UART_UDR   := UDR   }
  54.       {$DEFINE UART_UDRE  := UDRE  }
  55.       {$DEFINE UART_TXEN  := TXEN  }
  56.       {$DEFINE UART_RXEN  := RXEN  }
  57.       {$DEFINE UART_RXC   := RXC   }
  58.       {$DEFINE UART_U2X   := U2X   }
  59.     {$endif}
  60.      
  61.      
  62.     interface
  63.      
  64.     type
  65.      
  66.       { TUART }
  67.      
  68.       TUART = packed record
  69.       public
  70.         procedure Init(const ABaudRate: UInt32 = 57600);
  71.         function ReadChar: Char;
  72.         procedure WriteChar(const AChar: char);
  73.         procedure WriteStr(const s: ShortString = '');
  74.         procedure WriteStrLn(const s: ShortString = '');
  75.       end;
  76.      
  77.     var
  78.       UART: TUART;
  79.      
  80.     implementation
  81.      
  82.     { TUART }
  83.      
  84.     procedure TUART.Init(const ABaudRate: UInt32);
  85.     var
  86.       Prescaler: UInt16;
  87.     begin
  88.       //Prescaler := ((F_CPU div 4) div ABaudRate - 1) div 2;
  89.       Prescaler := F_CPU div 8 div ABaudRate - 1;
  90.      
  91.       UART_UBRRL  :=  Prescaler;
  92.       UART_UCSRA := (1 shl UART_U2X);
  93.       UART_UCSRB := (1 shl UART_TXEN) or (1 shl UART_RXEN);
  94.       UART_UCSRC := %10000110;
  95.     end;
  96.      
  97.     procedure TUART.WriteChar(const AChar: char); inline;
  98.     begin
  99.       while ((UART_UCSRA and (1 shl UART_UDRE)) = 0) do ;
  100.       UART_UDR := byte(AChar);
  101.     end;
  102.      
  103.     function TUART.ReadChar: Char;
  104.     begin
  105.       while ((UART_UCSRA and (1 shl UART_RXC)) = 0) do ;
  106.       ReadChar := Char(UART_UDR);
  107.     end;
  108.      
  109.     procedure TUART.WriteStr(const s: ShortString);
  110.     var
  111.       i: integer;
  112.     begin
  113.       if length(s) > 0 then
  114.       begin
  115.         for i := 1 to length(s) do
  116.            WriteChar(s[i]);
  117.       end;
  118.     end;
  119.      
  120.     procedure TUART.WriteStrLn(const s: ShortString);
  121.     begin
  122.       WriteStr(s);
  123.       WriteStr(#13#10);
  124.     end;
  125.      
  126.     end.
« Last Edit: October 26, 2023, 03:42:52 pm by pascalbythree »

Dzandaa

  • Sr. Member
  • ****
  • Posts: 407
  • From C# to Lazarus
Re: What values to change for the ATtiny2313A ?
« Reply #1 on: October 26, 2023, 04:58:56 pm »
Hi

Do you add:

-Wpattiny2313a

in Custom Options?

if you look at:

\fpc\3.2.2\source\rtl\embedded\avr

attiny2313a.pp exists.

But I never tried using avr board with Lazarus, so it is just a suggestion.

B->
Regards,
Dzandaa

ccrause

  • Hero Member
  • *****
  • Posts: 997
Re: What values to change for the ATtiny2313A ?
« Reply #2 on: October 26, 2023, 07:03:31 pm »
Code: Pascal  [Select][+][-]
  1.   {$elseif defined(fpc_mcu_atmega16a) or defined(fpc_mcu_attiny2313)}
  2.       {$DEFINE UART_UCSRA := UCSRA }

Does anybody know what values to change for the ATtiny2313A ?

Did the code compile when you specify -Wpattiny2313a as suggested by Dzandaa?  I would think the ifdef above needs to include the attiny2313a specifically:
Code: Pascal  [Select][+][-]
  1.   {$elseif defined(fpc_mcu_atmega16a) or defined(fpc_mcu_attiny2313) or defined(fpc_mcu_attiny2313a)}
  2.       {$DEFINE UART_UCSRA := UCSRA }

Quote
I am only receiving garbage

This often means (assuming the USART registers are configured correctly) that the clock frequency is not what you think it is.  Did you verify the clock frequency for example by measuring the timing of the USART pulses with an oscilloscope, or by configuring a timer to toggle a pin or LED at 1 second interval so that you can measure the pulses with a stopwatch?

Are you using the internal RC oscillator?  Note this is only accurate to 10%, so it may cause the bit rate to fall outside the allowable timing tolerances.  What are the fuse values? There is a CKDIV8 fuse option which is enabled by default, did you disable this?

 

TinyPortal © 2005-2018