Участник:Vooon/crumb128/usb

Материал из roboforum.ru Wiki
Перейти к: навигация, поиск

Project tree

{{#tree:openlevels=2|

  • ./
    • doc
      • html
        • doxygen-html
      • latex
        • doxygen-latex
    • includes
      • portpc.h
      • portpc.cpp
    • main.h
    • main.cpp
    • Makefile
    • Doxyfile

}}

includes

portpc.h

<source lang="cpp"> /**

* \file portpc.h
* \version PortPC.h, v 0.0.1 01/10/2007 21:20 vovan
* \defgroup port_pc <portpc.h>: Соединение с ПК
* \code #include "portpc.h" \endcode
*/

/**

*/
  1. ifndef PORTPC_H
  2. define PORTPC_H
  1. include <avr/io.h>
  2. include <inttypes.h>

/**

* \ingroup port_pc
* Скорость обмена с ПК
*/

/*[[[cog import cog pc_baud = 115200 f_cpu = int(f_cpu) cog.outl( "#define PC_BAUD %s" % pc_baud ) ]]]*/

  1. define PC_BAUD 115200

//[[[end]]] (checksum: 515e903dc784c9393118884a663d767e)

/**

* \ingroup port_pc
* Формула для расчета значения делителя
*/

/*[[[cog pc_ubrr = int((f_cpu/(pc_baud*16)) - 1) pc_real_baud = int(f_cpu / (16 * (pc_ubrr + 1))) pc_baud_error = ((float(pc_real_baud)/float(pc_baud) - 1.0) * 100.0)

if pc_baud_error != 0 and pc_baud_error < 2: cog.msg( "WARNING: pc baudrate error: %s%%" % pc_baud_error ) if pc_baud_error > 2: cog.error( "pc baudrate error: %s%%" % pc_baud_error )

cog.outl( "#define PC_UBRR %s" % pc_ubrr ) cog.outl( "#define PC_UBRRH %s" % (pc_ubrr>>8) ) cog.outl( "#define PC_UBRRL %s" % (0xff & pc_ubrr) ) cog.outl( "#define PC_REAL_BAUD %s" % pc_real_baud ) cog.outl( "#define PC_BAUD_ERR %s" % pc_baud_error ) ]]]*/

  1. define PC_UBRR 7
  2. define PC_UBRRH 0
  3. define PC_UBRRL 7
  4. define PC_REAL_BAUD 115200
  5. define PC_BAUD_ERR 0.0

//[[[end]]] (checksum: 357c9b7125d3f4a2fca94deb9d6e94ea)

/**

* \ingroup port_pc
*/

class port_pc { public: void init(void); void proc(void); char is_char_available(void); char receive_char(void); void send_char(char data); };

  1. endif // PORTPC_H

</source>

portpc.cpp

<source lang="cpp"> /**

* \file portpc.c
* \version PortPC.c, v 0.0.1 01/10/2007 21:21 vovan
*/
  1. include <avr/io.h>
  2. include <inttypes.h>
  1. include "portpc.h"

/*

* Настройка USART
*/

void port_pc::init(void) { UBRR1L = PC_UBRRL; UBRR1H = PC_UBRRH; UCSR1C = (1<<UCSZ11) | (1<<UCSZ10); // 8N1 UCSR1B = (1<<RXEN1) | (1<<TXEN1); };

void port_pc::proc(void) { while(!is_char_available()); send_char(receive_char()); };

// //- stdio foo ----------------------------------------------------------------- //

char port_pc::is_char_available(void) {

       // Does the RX0 bit of the USART Status and Control Register
       // indicate a char has been received?

if ( (UCSR1A & _BV(RXC1)) ) return 1; else return 0; };

char port_pc::receive_char(void) {

       // Return the char in the UDR0 register

return UDR1; };

void port_pc::send_char(char data) { int i = 0;

   // To send data with the USART put the data in the USART data register

UDR1 = data;

   // Check to see if the global interrupts are enabled

if(SREG & 0x80) {

       // Wait until the byte is sent or we count out

while ( !(UCSR1A & _BV(TXC1)) && (i < 10000) ) { i++; } } else // Wait until the byte is sent while( !(UCSR1A & _BV(TXC1)) );

       // Clear the TXCflag

UCSR1A = UCSR1A | _BV(TXC1); }; </source>