2024年2月27日火曜日

EEPROM-UNO/328p ー8mhzーでUARTsend成功/16mhzでrecv成功

 外付けの中華モジュールはI2C EEPROMモジュールを使う (7ujm.net)が完全にうごいた

EEPROM (ht-deko.com) UNO/Nanoの内臓EEPROMで通用するコード群

--------------------------uart----------------------

AVR Atmega328P UART (rjhcoding.com)

GitHub - xanthium-enterprises/atmega328p-serial-uart-to-pc-communication: Atmega328p sample codes for USART (transmission and reception) communication with a Linux/Windows PC

ーーーーーー送信ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

atmega328p-serial-uart-to-pc-communication/_1_Serial_Transmit/main.c at master · xanthium-enterprises/atmega328p-serial-uart-to-pc-communication · GitHub 

を改変して以下のコードでteratermしたら成功した

#include <stdint.h>

#include <avr/io.h>

#include <util/delay.h>

#define F_CPU 8000000UL

// +-----------------------------------------------------------------------+ //

// | ATmega328p Baudrate は以下のサイトを参考に51とした

//  https://qiita.com/k_match/items/c99d25adf6b1a984061f

//---------------------------------------------------------------------+ //

#define BAUD_RATE_9600_BPS  51 // 9600bps

// +-----------------------------------------------------------------------+ //

int main()

{

  int i = 0;

  unsigned int ubrr = BAUD_RATE_9600_BPS;

  unsigned char data[] = "Hello from ATmega328p  ";

  /* Set Baudrate  */

  UBRR0H = (ubrr>>8); 

// Shift the 16bit value ubrr 8 times to the right and

// transfer the upper 8 bits to UBBR0H register.

  UBRR0L = (ubrr);   

// Copy the 16 bit value ubrr to the 8 bit UBBR0L register,

// Upper 8 bits are truncated while lower 8 bits are copied

  UCSR0C = 0x06;       /* Set frame format: 8data, 1stop bit  */

  UCSR0B = (1<<TXEN0); /* Enable  transmitter                 */

  while(1) /* Loop the messsage continously */

  {

      i = 0;

      while(data[i] != 0) /* print the String  "Hello from ATmega328p" */

      {

         while (!( UCSR0A & (1<<UDRE0))); /* Wait for empty transmit buffer       */

      /* When UDRE0 = 0,data transmisson ongoing.                         */

      /* So NOT{[UCSR0A & (1<<UDRE0)] = 0} = 1 ,While(1) loop stays there */ 

      /* When UDRE0 = 1,data transmisson completed.                       */

      /* So NOT{[UCSR0A & (1<<UDRE0)] = 1} = 0 ,While(0) loop fails       */

      UDR0 = data[i];          /* Put data into buffer, sends the data */

      i++;                             /* increment counter                    */

      }

    

    /* Sending '\n'  '\r' Character pair helps to format the output properly on console putty Screen */

    /* Send "\n" Character */

    while (!( UCSR0A & (1<<UDRE0)));   /* Wait for empty transmit buffer       */

    UDR0 = '\n';              /* Put data into buffer, sends the data */    

    /* Send "\r" Character */

    while (!( UCSR0A & (1<<UDRE0)));   /* Wait for empty transmit buffer       */

    UDR0 = '\r';              /* Put data into buffer, sends the data */

    _delay_ms(100);

  }

  ーーーー受信ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

これはArduinoのシリアルモニタからAを入力して成功!

// AVR Serial Reception @ 230.4k bps

// AVR Receives character 'A' Blinks the LED  on PC4

//                        'B' Beeps  the Buzzer on PC5

// External Oscillator = 11.0592MHz

// Controller ATmega328p



//+------------------------------------------------------------------------------------------------+

//| Compiler           : AVR GCC (WinAVR)                                                          |

//| Microcontroller    : ATmega328p                                                                |

//| Programmer         : Rahul.Sreedharan                                                          |

//| Date               : 16-July-2019                                                              |

//+------------------------------------------------------------------------------------------------+


//(C) www.xanthium.in 

// Visit to Learn More 



#include <stdint.h>

#include <avr/io.h>

#include <util/delay.h>


// +-----------------------------------------------------------------------+ //

// | ATmega328p Baudrate values for UBRRn for external crystal 11.0592MHz  | //

// +-----------------------------------------------------------------------+ //


#define BAUD_RATE_4800_BPS  143 // 4800bps

//#define BAUD_RATE_9600_BPS  71  // 9600bps

#define BAUD_RATE_9600_BPS 103 // 9600bps for 16mhz

// http://rjhcoding.com/avrc-uart.php

#define BAUD_RATE_14400_BPS  47 // 14.4k bps

#define BAUD_RATE_19200_BPS  35 // 19.2k bps  

#define BAUD_RATE_28800_BPS  23 // 28.8k bps

#define BAUD_RATE_38400_BPS  17 // 38.4k bps

#define BAUD_RATE_57600_BPS  11 // 57.6k bps

#define BAUD_RATE_76800_BPS   8 // 76.8k bps


#define BAUD_RATE_115200_BPS  5 // 115.2k bps

#define BAUD_RATE_230400_BPS  2 // 230.4k bps


// +-----------------------------------------------------------------------+ //



int main()

{

unsigned int ubrr = BAUD_RATE_9600_BPS; 


//PORTC = 0x00; //All LED's OFF

PORTD = 0x00;

/* Set Baudrate @ 230.4k bps */

UBRR0H = (ubrr>>8);

UBRR0L = (ubrr);

/*Enable receiver  */

UCSR0B = (1<<RXEN0);/* Set frame format: 8data, 1stop bit */

//pinMode(PD4,OUTPUT);

//digitalWrite(PD4,HIGH);

  _delay_ms(500);

 // digitalWrite(PD4,LOW);

while(1)

{

while ( !(UCSR0A & (1<<RXC0)) ); /* Wait for data to be received */

switch(UDR0) // using the switch() statement to  control the LED and Buzzer

{

case 'A' : DDRD  |= (1<<PD4); // Blinks LED

PORTD |= (1<<PD4);

_delay_ms(500);

PORTD &= ~(1<<PD4);

break;

            case 'B' : DDRD  |= (1<<PD5); // Beeps Buzzer

PORTD |= (1<<PD5);

_delay_ms(500);

PORTD &= ~(1<<PD5);

break;

default  :

break;

}//end of Switch()

}//end of While(1)

}//end of main


0 件のコメント:

コメントを投稿