Урок 5.2: Работаем с EEPROM-памятью

Материал из roboforum.ru Wiki
Версия от 14:50, 2 августа 2008; =DeaD= (обсуждение | вклад) (Новая: <source lang="C"> #include <stdint.h> #include <stdlib.h> #include <string.h> void usart0_init( uint32_t baudrate ); void usart0_putchar( uint8_t data ); uint8_t usart0_getchar( void );...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

<source lang="C">

  1. include <stdint.h>
  2. include <stdlib.h>
  3. include <string.h>

void usart0_init( uint32_t baudrate ); void usart0_putchar( uint8_t data ); uint8_t usart0_getchar( void ); void usart0_putstr( char *str ); void usart0_getstr( char *str, uint8_t length );

void eeprom_write( uint16_t address, uint8_t data ); uint8_t eeprom_read( uint16_t address );

void usart0_init( uint32_t baudrate ) {

  uint16_t bauddiv = ( ( F_CPU + ( baudrate * 8L ) ) / ( baudrate * 16L ) - 1 );
  UBRRL |= bauddiv;
  UBRRH |= ( bauddiv >> 8 ); 
  UCSRB |= ( 1 << TXEN ) | ( 1 << RXEN );

}

void usart0_putchar( uint8_t data ) {

  while( !( UCSRA & ( 1 << UDRE ) ) );
  UDR = data;

}

uint8_t usart0_getchar( void ) {

  while( !( UCSRA & ( 1 << RXC ) ) );
  
  return UDR;

}

void usart0_putstr( char *str ) {

  while( *str ) 
     usart0_putchar( *str++ );

}

void usart0_getstr( char *str, uint8_t length ) {

  while( length )
  {
     *str = usart0_getchar();
     
     if( *str == '\n' || *str == '\r' )
        break;
     
     str++;
     length--;
  }

}

void eeprom_write( uint16_t address, uint8_t data ) {

  while( EECR & ( 1 << EEWE ) );
  EEAR = address;
  EEDR = data;
  EECR |= ( 1 << EEMWE );
  EECR |= ( 1 << EEWE );

}

uint8_t eeprom_read( uint16_t address ) {

  while( EECR & ( 1 << EEWE ) );
  EEAR = address;
  EECR |= ( 1 << EERE );
  return EEDR;   

}

int main( void ) {

  char str_enter_cmd[] = "Enter command ( 'r' to read, 'w' to write ): ";
  char str_enter_addr[] = "Enter address: ";
  char str_enter_value[] = "Enter value: ";
  char str_default[] = "Wrong command, try again!\n\r";
  char str_value[] = "Value is: ";
  
  usart0_init( 9600 );
  
  while( 1 )
  {
     char buffer[8] = { 0 };
     int addr = 0;
     uint8_t value = 0;
     
     usart0_putstr( str_enter_cmd );
     
     switch( usart0_getchar() )
     {
     case 'r':
        usart0_putchar( '\r' );
        
        usart0_putstr( str_enter_addr );
        
        usart0_getstr( buffer, sizeof( buffer ) );
        
        addr = atoi( buffer );
        
        memset( buffer, 0, sizeof( buffer ) );
        
        value = eeprom_read( addr );
        
        usart0_putstr( str_value );
        
        usart0_putstr( itoa( value, buffer, 10 ) );
        
        break;
        
     case 'w':
        usart0_putchar( '\r' );
                 
        usart0_putstr( str_enter_addr );
        
        usart0_getstr( buffer, sizeof( buffer ) );
        
        addr = atoi( buffer );
        
        memset( buffer, 0, sizeof( buffer ) );
        
        usart0_putchar( '\r' );
        
        usart0_putstr( str_enter_value  );
        
        usart0_getstr( buffer, sizeof( buffer ) );
        
        value = atoi( buffer );
        
        memset( buffer, 0, sizeof( buffer ) );
        
        eeprom_write( addr, value );
        
        break;
        
     default:
        usart0_putstr( str_default );
        break;
     }
     
     usart0_putchar( '\r' );
     
  }
  
  return 0;

}</source>